pub fn from_serializable_form(&mut self, stable_repr: RegistryStableStorage) {
        assert!(self.store.is_empty());
        assert!(self.changelog.is_empty());
        assert_eq!(self.version, 0);

        let repr_version = ReprVersion::from_i32(stable_repr.version).unwrap_or_else(|| {
            panic!(
                "Version {} of stable registry representation is not supported by this canister",
                stable_repr.version
            )
        });

        match repr_version {
            ReprVersion::Version1 => {
                let mut current_version = 0;
                for entry in stable_repr.changelog {
                    // Code to fix ICSUP-2589.
                    // This fills in missing versions with empty entries so that clients see an
                    // unbroken sequence.
                    // If the current version is different from the previous version + 1, we
                    // need to add empty records to fill out the missing versions, to keep
                    // the invariants that are present in the
                    // client side.
                    for i in current_version + 1..entry.version {
                        let mutations = vec![RegistryMutation {
                            mutation_type: Type::Upsert as i32,
                            key: "_".into(),
                            value: "".into(),
                        }];
                        self.apply_mutations_as_version(mutations, i);
                        self.version = i;
                    }
                    // End code to fix ICSUP-2589

                    let req = RegistryAtomicMutateRequest::decode(&entry.encoded_mutation[..])
                        .unwrap_or_else(|err| {
                            panic!("Failed to decode mutation@{}: {}", entry.version, err)
                        });
                    self.apply_mutations_as_version(req.mutations, entry.version);
                    self.version = entry.version;
                    current_version = self.version;
                }
            }
            ReprVersion::Unspecified => {
                let mut mutations_by_version = BTreeMap::<Version, Vec<RegistryMutation>>::new();
                for delta in stable_repr.deltas.into_iter() {
                    self.version = max(
                        self.version,
                        delta
                            .values
                            .last()
                            .map(|registry_value| registry_value.version)
                            .unwrap_or(0),
                    );

                    for v in delta.values.iter() {
                        mutations_by_version
                            .entry(v.version)
                            .or_default()
                            .push(RegistryMutation {
                                mutation_type: if v.deletion_marker {
                                    Type::Delete
                                } else {
                                    Type::Upsert
                                } as i32,
                                key: delta.key.clone(),
                                value: v.value.clone(),
                            })
                    }

                    self.store.insert(delta.key, VecDeque::from(delta.values));
                }
                // We iterated over keys in ascending order, so the mutations
                // must also be sorted by key, resulting in canonical encoding.
                self.changelog = mutations_by_version
                    .into_iter()
                    .map(|(v, mutations)| {
                        (
                            EncodedVersion::from(v),
                            pb_encode(&RegistryAtomicMutateRequest {
                                mutations,
                                preconditions: vec![],
                            }),
                        )
                    })
                    .collect()
            }
        }
    }
/// Sets the content of the registry from its serialized representation.
///
/// Panics if not currently empty: this is only meant to be used in
/// canister_post_upgrade.
///
/// In post_upgrade, one should do as much verification as possible, and
/// panic for anything unexpected. Indeed, panicking here keeps the
/// pre-upgrade state unchanged, and gives the developer an opportunity
/// to try upgrading to a different wasm binary. As a corollary, any
/// lossy way of handling unexpected data must be banned in
/// post_upgrade.
Sets the content of the registry from its serialized representation.
Panics if not currently empty: this is only meant to be used in
canister_post_upgrade.

In post_upgrade, one should do as much verification as possible, and
panic for anything unexpected. Indeed, panicking here keeps the
pre-upgrade state unchanged, and gives the developer an opportunity
to try upgrading to a different wasm binary. As a corollary, any
lossy way of handling unexpected data must be banned in
post_upgrade.
Sets the content of the registry from its serialized representation . Panics if not currently empty : this is only meant to be used in canister_post_upgrade . In post_upgrade one should do as much verification as possible and panic for anything unexpected . Indeed panicking here keeps the pre - upgrade state unchanged and gives the developer an opportunity to try upgrading to a different wasm binary . As a corollary any lossy way of handling unexpected data must be banned in post_upgrade .
pub fn from_serializable_form(&mut self, stable_repr: RegistryStableStorage) {
        assert!(self.store.is_empty());
        assert!(self.changelog.is_empty());
        assert_eq!(self.version, 0);
        let repr_version = ReprVersion::from_i32(stable_repr.version).unwrap_or_else(|| {
            panic!(
                "Version {} of stable registry representation is not supported by this canister",
                stable_repr.version
            )
        });
        match repr_version {
            ReprVersion::Version1 => {
                let mut current_version = 0;
                for entry in stable_repr.changelog {
                    // Code to fix ICSUP-2589.
                    // This fills in missing versions with empty entries so that clients see an
                    // unbroken sequence.
                    // If the current version is different from the previous version + 1, we
                    // need to add empty records to fill out the missing versions, to keep
                    // the invariants that are present in the
                    // client side.
                    for i in current_version + 1..entry.version {
                        let mutations = vec![RegistryMutation {
                            mutation_type: Type::Upsert as i32,
                            key: "_".into(),
                            value: "".into(),
                        }];
                        self.apply_mutations_as_version(mutations, i);
                        self.version = i;
                    }
                    // End code to fix ICSUP-2589
                    let req = RegistryAtomicMutateRequest::decode(&entry.encoded_mutation[..])
                        .unwrap_or_else(|err| {
                            panic!("Failed to decode mutation@{}: {}", entry.version, err)
                        });
                    self.apply_mutations_as_version(req.mutations, entry.version);
                    self.version = entry.version;
                    current_version = self.version;
                }
            }
            ReprVersion::Unspecified => {
                let mut mutations_by_version = BTreeMap::<Version, Vec<RegistryMutation>>::new();
                for delta in stable_repr.deltas.into_iter() {
                    self.version = max(
                        self.version,
                        delta
                            .values
                            .last()
                            .map(|registry_value| registry_value.version)
                            .unwrap_or(0),
                    );
                    for v in delta.values.iter() {
                        mutations_by_version
                            .entry(v.version)
                            .or_default()
                            .push(RegistryMutation {
                                mutation_type: if v.deletion_marker {
                                    Type::Delete
                                } else {
                                    Type::Upsert
                                } as i32,
                                key: delta.key.clone(),
                                value: v.value.clone(),
                            })
                    }
                    self.store.insert(delta.key, VecDeque::from(delta.values));
                }
                // We iterated over keys in ascending order, so the mutations
                // must also be sorted by key, resulting in canonical encoding.
                self.changelog = mutations_by_version
                    .into_iter()
                    .map(|(v, mutations)| {
                        (
                            EncodedVersion::from(v),
                            pb_encode(&RegistryAtomicMutateRequest {
                                mutations,
                                preconditions: vec![],
                            }),
                        )
                    })
                    .collect()
            }
        }
    }
pub fn from_serializable_form ( & mut self , stable_repr : RegistryStableStorage ) { assert ! ( self store is_empty ( ) ) ; assert ! ( self changelog is_empty ( ) ) ; assert_eq ! ( self version 0 ) ; let repr_version = ReprVersion :: from_i32 ( stable_repr . version ) . unwrap_or_else ( | | { panic ! ( " " stable_repr version ) } ) ; match repr_version { ReprVersion :: Version1 => { let mut current_version = 0 ; for entry in stable_repr . changelog { // Code to fix ICSUP-2589. // This fills in missing versions with empty entries so that clients see an // unbroken sequence. // If the current version is different from the previous version + 1, we // need to add empty records to fill out the missing versions, to keep // the invariants that are present in the // client side. for i in current_version + 1 .. entry . version { let mutations = vec ! [ RegistryMutation { mutation_type Type Upsert as i32 key " " into ( ) value " " into ( ) } ] ; self . apply_mutations_as_version ( mutations , i ) ; self . version = i ; } // End code to fix ICSUP-2589 let req = RegistryAtomicMutateRequest :: decode ( & entry . encoded_mutation [ .. ] ) . unwrap_or_else ( | err | { panic ! ( " " entry version err ) } ) ; self . apply_mutations_as_version ( req . mutations , entry . version ) ; self . version = entry . version ; current_version = self . version ; } } ReprVersion :: Unspecified => { let mut mutations_by_version = BTreeMap :: < Version , Vec < RegistryMutation > > :: new ( ) ; for delta in stable_repr . deltas . into_iter ( ) { self . version = max ( self . version , delta . values . last ( ) . map ( | registry_value | registry_value . version ) . unwrap_or ( 0 ) , ) ; for v in delta . values . iter ( ) { mutations_by_version . entry ( v . version ) . or_default ( ) . push ( RegistryMutation { mutation_type : if v . deletion_marker { Type :: Delete } else { Type :: Upsert } as i32 , key : delta . key . clone ( ) , value : v . value . clone ( ) , } ) } self . store . insert ( delta . key , VecDeque :: from ( delta . values ) ) ; } // We iterated over keys in ascending order, so the mutations // must also be sorted by key, resulting in canonical encoding. self . changelog = mutations_by_version . into_iter ( ) . map ( | ( v , mutations ) | { ( EncodedVersion :: from ( v ) , pb_encode ( & RegistryAtomicMutateRequest { mutations , preconditions : vec ! [ ] , } ) , ) } ) . collect ( ) } } }
Sets the content of the registry from its serialized representation.
Sets the content of the registry from its serialized representation .
&mut self
stable_repr
RegistryStableStorage
&mut self
stable_repr
RegistryStableStorage