MVstore openMap

本文介绍了一个名为MVMap的数据结构的开启与构建方法。通过给定的构建器打开或创建一个多版本并发映射(Map),并详细解释了如何通过检查store状态、读取元数据以及利用构建器来初始化或复用地图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /**
     * Open a map with the given builder. The map is automatically create if it
     * does not yet exist. If a map with this name is already open, this map is
     * returned.
     *
     * @param <K> the key type
     * @param <V> the value type
     * @param name the name of the map
     * @param builder the map builder
     * @return the map
     */
    public <M extends MVMap<K, V>, K, V> M openMap(String name, MVMap.MapBuilder<M, K, V> builder) {
//检查store是否已关闭,如果已关闭则抛出异常     
   checkOpen();
//meta是在创建store的时间新建的,一个默认的mvconcurrentmap,map的root是一个空的page,version为-1,所以这里首次获取x为null,get方法会从root page进行2分搜索,查出key对应的值。调用page的2分查找方法,因为当前root为空page,所以返回-1,导致get方法返回null
        String x = meta.get("name." + name);
        int id;
        long root;
        HashMap<String, String> c;
        M map;
        if (x != null) {
            id = Integer.parseInt(x);
            @SuppressWarnings("unchecked")
            M old = (M) maps.get(id);
            if (old != null) {
                return old;
            }
            map = builder.create();
            String config = meta.get("map." + x);
            c = DataUtils.parseMap(config);
            c.put("id", x);
            map.init(this, c);
            String r = meta.get("root." + id);
            root = r == null ? 0 : Long.parseLong(r);
        } else {
//因为返回null,创建一个空的hashmap
            c = New.hashMap();
            id = ++lastMapId;
            c.put("id", Integer.toString(id));
            c.put("createVersion", Long.toString(currentVersion));
//如果第一次,id为1,createVersion为0,只有metamap的id为0         
 map = builder.create();
            map.init(this, c);
//metamap里加入这个map的信息,行如key:map.1,value:name:test,因为map的createVersion为0和type为null,不进行拼接,按照page格式写入
            meta.put("map." + id, map.asString(name));
//加入形如key:name.test,value:1
            meta.put("name." + name, Integer.toString(id));
            markMetaChanged();
            root = 0;
        }
//设置一个空的root page
        map.setRootPos(root, -1);
        maps.put(id, map);
        return map;
    }

转载于:https://my.oschina.net/heatonn1/blog/188665

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值