Loading...
Loading...
Compare original and translation side by side
core = "2.0.0"ic-stable-structures = "0.7"core = "2.0.0"ic-stable-structures = "0.7"thread_local! { RefCell<T> }icp deployStableBTreeMap#[post_upgrade]post_upgrade#[init]#[post_upgrade]stablepersistent actorletvarstable letstable varletvarpre_upgradepost_upgradeStableBTreeMapStableCellMemoryManagerNatIntpre_upgradeStableBTreeMapactor { }persistent actor { }actorstablepersistent actorpersistent actorthread_local! { RefCell<T> }icp deployStableBTreeMap#[post_upgrade]post_upgrade#[init]#[post_upgrade]stablepersistent actorletvarstable letstable varletvarpre_upgradepost_upgradeStableBTreeMapStableCellMemoryManagerNatIntpre_upgradeStableBTreeMapactor { }persistent actor { }actorstablepersistent actorpersistent actorpersistent actorletvarimport Map "mo:core/Map";
import List "mo:core/List";
import Nat "mo:core/Nat";
import Text "mo:core/Text";
import Time "mo:core/Time";
persistent actor {
// Types -- must be inside actor body
type User = {
id : Nat;
name : Text;
created : Int;
};
// These survive upgrades automatically -- no "stable" keyword needed
let users = Map.empty<Nat, User>();
var userCounter : Nat = 0;
let tags = List.empty<Text>();
// Transient data -- reset to initial value on every upgrade
transient var requestCount : Nat = 0;
public func addUser(name : Text) : async Nat {
let id = userCounter;
Map.add(users, Nat.compare, id, {
id;
name;
created = Time.now();
});
userCounter += 1;
requestCount += 1;
id
};
public query func getUser(id : Nat) : async ?User {
Map.get(users, Nat.compare, id)
};
public query func getUserCount() : async Nat {
Map.size(users)
};
// requestCount resets to 0 after every upgrade
public query func getRequestCount() : async Nat {
requestCount
};
}letvartransient varpre_upgradepost_upgradestablepersistent actorletvarimport Map "mo:core/Map";
import List "mo:core/List";
import Nat "mo:core/Nat";
import Text "mo:core/Text";
import Time "mo:core/Time";
persistent actor {
// Types -- must be inside actor body
type User = {
id : Nat;
name : Text;
created : Int;
};
// These survive upgrades automatically -- no "stable" keyword needed
let users = Map.empty<Nat, User>();
var userCounter : Nat = 0;
let tags = List.empty<Text>();
// Transient data -- reset to initial value on every upgrade
transient var requestCount : Nat = 0;
public func addUser(name : Text) : async Nat {
let id = userCounter;
Map.add(users, Nat.compare, id, {
id;
name;
created = Time.now();
});
userCounter += 1;
requestCount += 1;
id
};
public query func getUser(id : Nat) : async ?User {
Map.get(users, Nat.compare, id)
};
public query func getUserCount() : async Nat {
Map.size(users)
};
// requestCount resets to 0 after every upgrade
public query func getRequestCount() : async Nat {
requestCount
};
}letvartransient varpre_upgradepost_upgradestable[package]
name = "my-project"
version = "0.1.0"
[dependencies]
core = "2.0.0"[package]
name = "my-project"
version = "0.1.0"
[dependencies]
core = "2.0.0"ic-stable-structuresMemoryManageric-stable-structuresMemoryManager[package]
name = "stable_memory_backend"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
ic-cdk = "0.19"
ic-stable-structures = "0.7"
candid = "0.10"
serde = { version = "1", features = ["derive"] }
ciborium = "0.2"[package]
name = "stable_memory_backend"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
ic-cdk = "0.19"
ic-stable-structures = "0.7"
candid = "0.10"
serde = { version = "1", features = ["derive"] }
ciborium = "0.2"use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
storable::{Bound, Storable},
DefaultMemoryImpl, StableBTreeMap,
};
use ic_cdk::{init, post_upgrade, query, update};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// -- Implement Storable for custom types --
// StableBTreeMap keys need Storable + Ord, values need Storable.
// Storable defines how a type is serialized to/from bytes in stable memory.
// Use CBOR (via ciborium) for serialization -- compact binary format, faster than candid.
#[derive(CandidType, Serialize, Deserialize, Clone)]
struct User {
id: u64,
name: String,
created: u64,
}
impl Storable for User {
// Recommended: prefer Unbounded to avoid backwards compatibility issues when adding new fields.
// Bounded requires a fixed max_size -- adding a field that increases the size will break existing data.
const BOUND: Bound = Bound::Unbounded;
fn to_bytes(&self) -> Cow<'_, [u8]> {
let mut buf = vec![];
ciborium::into_writer(self, &mut buf).expect("Failed to encode User");
Cow::Owned(buf)
}
fn into_bytes(self) -> Vec<u8> {
let mut buf = vec![];
ciborium::into_writer(&self, &mut buf).expect("Failed to encode User");
buf
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
ciborium::from_reader(bytes.as_ref()).expect("Failed to decode User")
}
}
// Bound::Bounded { max_size, is_fixed_size: true } exists for fixed-size types but is NOT
// recommended -- adding a new field later will exceed max_size and break deserialization.
// Stable storage -- survives upgrades
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, User, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0)))
));
// Counter stored in stable memory via StableCell
static COUNTER: RefCell<ic_stable_structures::StableCell<u64, Memory>> =
RefCell::new(ic_stable_structures::StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(1))),
0u64,
));
}
#[init]
fn init() {
// Any one-time initialization
}
#[post_upgrade]
fn post_upgrade() {
// Stable structures auto-restore -- no deserialization needed
// Re-init timers or other transient state here
}
#[update]
fn add_user(name: String) -> u64 {
let id = COUNTER.with(|c| {
let mut cell = c.borrow_mut();
let current = *cell.get();
cell.set(current + 1);
current
});
let user = User {
id,
name,
created: ic_cdk::api::time(),
};
USERS.with(|users| {
users.borrow_mut().insert(id, user);
});
id
}
#[query]
fn get_user(id: u64) -> Option<User> {
USERS.with(|users| users.borrow().get(&id))
}
#[query]
fn get_user_count() -> u64 {
USERS.with(|users| users.borrow().len())
}
ic_cdk::export_candid!();use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
storable::{Bound, Storable},
DefaultMemoryImpl, StableBTreeMap,
};
use ic_cdk::{init, post_upgrade, query, update};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// -- Implement Storable for custom types --
// StableBTreeMap keys need Storable + Ord, values need Storable.
// Storable defines how a type is serialized to/from bytes in stable memory.
// Use CBOR (via ciborium) for serialization -- compact binary format, faster than candid.
#[derive(CandidType, Serialize, Deserialize, Clone)]
struct User {
id: u64,
name: String,
created: u64,
}
impl Storable for User {
// Recommended: prefer Unbounded to avoid backwards compatibility issues when adding new fields.
// Bounded requires a fixed max_size -- adding a field that increases the size will break existing data.
const BOUND: Bound = Bound::Unbounded;
fn to_bytes(&self) -> Cow<'_, [u8]> {
let mut buf = vec![];
ciborium::into_writer(self, &mut buf).expect("Failed to encode User");
Cow::Owned(buf)
}
fn into_bytes(self) -> Vec<u8> {
let mut buf = vec![];
ciborium::into_writer(&self, &mut buf).expect("Failed to encode User");
buf
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
ciborium::from_reader(bytes.as_ref()).expect("Failed to decode User");
}
}
// Bound::Bounded { max_size, is_fixed_size: true } exists for fixed-size types but is NOT
// recommended -- adding a new field later will exceed max_size and break deserialization.
// Stable storage -- survives upgrades
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, User, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0)))
));
// Counter stored in stable memory via StableCell
static COUNTER: RefCell<ic_stable_structures::StableCell<u64, Memory>> =
RefCell::new(ic_stable_structures::StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(1))),
0u64,
));
}
#[init]
fn init() {
// Any one-time initialization
}
#[post_upgrade]
fn post_upgrade() {
// Stable structures auto-restore -- no deserialization needed
// Re-init timers or other transient state here
}
#[update]
fn add_user(name: String) -> u64 {
let id = COUNTER.with(|c| {
let mut cell = c.borrow_mut();
let current = *cell.get();
cell.set(current + 1);
current
});
let user = User {
id,
name,
created: ic_cdk::api::time(),
};
USERS.with(|users| {
users.borrow_mut().insert(id, user);
});
id
}
#[query]
fn get_user(id: u64) -> Option<User> {
USERS.with(|users| users.borrow().get(&id))
}
#[query]
fn get_user_count() -> u64 {
USERS.with(|users| users.borrow().len())
}
ic_cdk::export_candid!();use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
DefaultMemoryImpl, StableBTreeMap, StableCell, StableLog,
};
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// Each structure gets its own MemoryId -- NEVER reuse IDs
const USERS_MEM_ID: MemoryId = MemoryId::new(0);
const POSTS_MEM_ID: MemoryId = MemoryId::new(1);
const COUNTER_MEM_ID: MemoryId = MemoryId::new(2);
const LOG_INDEX_MEM_ID: MemoryId = MemoryId::new(3);
const LOG_DATA_MEM_ID: MemoryId = MemoryId::new(4);
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(USERS_MEM_ID))
));
static POSTS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(POSTS_MEM_ID))
));
static COUNTER: RefCell<StableCell<u64, Memory>> =
RefCell::new(StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(COUNTER_MEM_ID)),
0u64,
));
static AUDIT_LOG: RefCell<StableLog<Vec<u8>, Memory, Memory>> =
RefCell::new(StableLog::init(
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_INDEX_MEM_ID)),
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_DATA_MEM_ID)),
));
}MemoryManagerMemoryIdMemoryIdStableBTreeMapStorableOrdStorableStorableBOUNDto_bytesinto_bytesfrom_bytesciborium::into_writerciborium::from_readerBound::UnboundedBound::Boundedmax_sizeu64boolf64StringVec<u8>PrincipalStorableStableCellStableLogthread_local! { RefCell<StableBTreeMap<...>> }pre_upgradepost_upgradeuse ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
DefaultMemoryImpl, StableBTreeMap, StableCell, StableLog,
};
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// Each structure gets its own MemoryId -- NEVER reuse IDs
const USERS_MEM_ID: MemoryId = MemoryId::new(0);
const POSTS_MEM_ID: MemoryId = MemoryId::new(1);
const COUNTER_MEM_ID: MemoryId = MemoryId::new(2);
const LOG_INDEX_MEM_ID: MemoryId = MemoryId::new(3);
const LOG_DATA_MEM_ID: MemoryId = MemoryId::new(4);
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(USERS_MEM_ID))
));
static POSTS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(POSTS_MEM_ID))
));
static COUNTER: RefCell<StableCell<u64, Memory>> =
RefCell::new(StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(COUNTER_MEM_ID)),
0u64,
));
static AUDIT_LOG: RefCell<StableLog<Vec<u8>, Memory, Memory>> =
RefCell::new(StableLog::init(
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_INDEX_MEM_ID)),
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_DATA_MEM_ID)),
));
}MemoryManagerMemoryIdMemoryIdStableBTreeMapStorableOrdStorableStorableBOUNDto_bytesinto_bytesfrom_bytesciborium::into_writerciborium::from_readerBound::UnboundedBound::Boundedmax_sizeu64boolf64StringVec<u8>PrincipalStorableStableCellStableLogthread_local! { RefCell<StableBTreeMap<...>> }pre_upgradepost_upgradeundefinedundefinedundefinedundefinedicp network start -d
icp deploy backend
icp canister call backend add_user '("Alice")'icp network start -d
icp deploy backend
icp canister call backend add_user '("Alice")'undefinedundefinedundefinedundefined
If the count drops to 0 after step 3, your data is NOT in stable memory. Review your storage declarations.
如果步骤3后计数变为0,说明你的数据未存储在稳定内存中。请检查你的存储声明。