Error storing in ic stable structure

@kristofer I am a bit lost here? so the bound has no issues, and the Vec I can’t use it. What is the solution ?

Sorry, I am not really sure what the question is now. Are you still having issues with Attempting to allocate an already allocated chunk.?

Yes. I that error when calling

let current_user = caller().to_string();
        FILE_CONTENTS.with(|file_contents| {
            let mut contents = file_contents.borrow_mut();
            let mut user_contents = contents.get(&current_user).unwrap_or_else(|| {
                ContentNodeVec {
                    files: HashMap::new(),
                }
            });
            user_contents.files.insert(file_id, content_nodes);
            contents.insert(current_user, user_contents);
        });

I changed imp storable

impl Storable for ContentNodeVec {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(Encode!(self).unwrap())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        // First try to decode as the new format
        match Decode!(bytes.as_ref(), Self) {
            Ok(content) => content,
            Err(_) => {
                // If that fails, try to decode as old format and migrate
                let old_content: HashMap<ContentId, OldContentNode> = 
                    Decode!(bytes.as_ref(), HashMap<ContentId, OldContentNode>)
                        .unwrap_or_default();
                
                // Migrate old format to new format
                let contents = old_content
                    .into_iter()
                    .map(|(id, old_node)| {
                        let new_node = ContentNode {
                            formats: Vec::new(), // Initialize with empty formats
                            id: old_node.id,
                            parent: old_node.parent,
                            _type: old_node._type,
                            value: old_node.value,
                            text: old_node.text,
                            language: old_node.language,
                            indent: old_node.indent,
                            data: old_node.data,
                            listStyleType: old_node.listStyleType,
                            listStart: old_node.listStart,
                            children: old_node.children,
                        };
                        (id, new_node)
                    })
                    .collect();

                ContentNodeVec { contents }
            }
        }
    }

    const BOUND: Bound = Bound::Unbounded;
}

I think it might be helpful if you shared here, the data structure that is currently live in the canister, along with the one you want to upgrade to. That way we can compare and perhaps see what is causing the issue.

I updated it :point_up: My equation

  • I just added one new field and I changed my from_bytes but to_bytes stay the same

I found it it is size issue, I just delete some data and tried to store something and it was success