first of all, I tried to save short content data to my backend and it was fine. But when the content get bigger i can’t save anymore. Also, Could that happend because I migrate some field before like adding new field or removing field in the struct ? I think that because locally i ran dfx deploy backend --mode=reinstall
and the error gone. But on the IC i don’t wanna lose my data.
[Canister bkyz2-fmaaa-aaaaa-qaaaq-cai] Panicked at 'Attempting to allocate an already allocated chunk.', /Users/ahmed/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ic-stable-structures-0.6.5/src/btreemap/allocator.rs:166:9
my code
#[derive(Clone, Debug, Deserialize, CandidType)]
pub struct ContentNode {
pub id: ContentId,
pub parent: Option<ContentId>,
pub _type: String,
pub value: String,
pub text: String,
pub language: String,
pub indent: u64,
pub data: Option<ContentData>,
pub listStyleType: String,
pub listStart: u64,
#[serde(default)]
pub children: Vec<ContentId>,
}
pub type ContentTree = Vec<ContentNode>;
#[derive(Clone, Debug, Deserialize, CandidType)]
pub struct ContentNodeVec {
pub contents: HashMap<FileId, ContentTree>,
}
static FILE_CONTENTS: RefCell<StableBTreeMap<String, ContentNodeVec, Memory>> = RefCell::new(
StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(5))),
)
);
pub fn update_file_contents(file_id: FileId, content_nodes: ContentTree) {
FILE_CONTENTS.with(|file_contents| {
let mut contents = file_contents.borrow_mut();
let mut content_map = HashMap::new();
content_map.insert(file_id.clone(), content_nodes);
contents.insert(
file_id,
ContentNodeVec {
contents: content_map,
},
);
});
}
impl Storable for ContentNodeVec {
fn to_bytes(&self) -> Cow<[u8]> {
Cow::Owned(Encode!(self).unwrap())
}
fn from_bytes(bytes: Cow<[u8]>) -> Self {
Decode!(bytes.as_ref(), Self).unwrap_or_else(|_| ContentNodeVec {
contents: HashMap::new(),
})
}
const BOUND: Bound = Bound::Unbounded;
}