微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

下面插入 Vec 到 hash map 的方法有效吗?

如何解决下面插入 Vec 到 hash map 的方法有效吗?

我在 decl_storage 中有以下变量:

Candidate get(fn candidate_name): map hasher(blake2_128_concat) T::AccountId => Vec<u8>;

这是我的 decl_module 函数

    #[weight = 10_000 + T::DbWeight::get().reads_writes(2,2)]
    pub fn add_peers_to_deparment(origin,departmentid: u128) -> dispatch::dispatchResult {
        let who = ensure_signed(origin)?;
        let mut approved_peer_dep = PeerDepartments::<T>::get(&who);

        match approved_peer_dep.binary_search(&departmentid) {

            Ok(_) => Err(Error::<T>::DepartmentExists.into()),Err(index) => {
                approved_peer_dep.insert(index,departmentid.clone());
                PeerDepartments::<T>::insert(&who,approved_peer_dep);
                Self::deposit_event(RawEvent::PeerDepartment(departmentid,who));
                Ok(())
             }

        }
     }

这里我写了两个语句,首先将 departmentid 插入到 approved_peer_dep,然后再次将 allowed_peer_dep 插入到区块链存储 PeerDepartments

approved_peer_dep.insert(index,departmentid.clone());
PeerDepartments::<T>::insert(&who,approved_peer_dep);

疑问是如果approved_peer_dep包含一个非常大的Vec,区块链(PeerDepartments)会插入包含所有部门使用更多资源的完整Vec,还是会使用添加一个部门?如果它将使用更多资源,那么编写代码的更好方法是什么。

如何拥有一个数据结构,以便我可以通过一个密钥访问 Vec 并更新它?

解决方法

首先,这里有一些资源可以帮助您决定前进的道路:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。