Exemple #1
0
        public void Batch_SetItem(WriteBatch wb, byte[] key, IValue item)
        {
            //var snapshot = Helper.CreateSnapshot(db);

            byte[] key_count = Helper.tagKey_MapCount.Concat(this.Value).ToArray();
            byte[] data      = wb.Get(key_count);
            if (data == null || data.Length == 0)
            {
                throw new Exception("error map in Count");
            }
            var count = BitConverter.ToUInt64(data, 0);

            //写入字典项
            var _key = Helper.tagKey_MapValues.Concat(this.Value).Concat(key).ToArray();


            var  value = wb.Get(_key);
            bool bAdd  = value == null;

            (item as IValueCreator).Batch_PutToDB(wb, db, _key);


            if (bAdd)
            {//更新count
                count++;
                data = BitConverter.GetBytes(count);
                wb.Put(key_count, data);
            }
        }
Exemple #2
0
        public void Batch_PutToDB(WriteBatch batch, LevelDB.DB db, byte[] key)
        {
            //var snapshot = Helper.CreateSnapshot(db);

            if (this.Value == null)
            {//申请新的实例ID,然后初始化存储Map
                var key_instMax = Helper.tagKey_InstanceMax;
                var instid      = batch.Get(key_instMax);
                if (instid == null || instid.Length == 0)
                {
                    instid = BitConverter.GetBytes((UInt64)1);
                }

                this.Value = instid;
                this.db    = db;
                //刷新max
                {
                    UInt64 v = BitConverter.ToUInt64(instid, 0);
                    v++;
                    instid = BitConverter.GetBytes((UInt64)v);
                    batch.Put(key_instMax, instid);
                }

                //初始化字典数量
                byte[] key_count = Helper.tagKey_MapCount.Concat(this.Value).ToArray();
                batch.Put(key_count, BitConverter.GetBytes((UInt64)0));
            }
            else
            {//检查count是否存在,
                byte[] key_count = Helper.tagKey_MapCount.Concat(this.Value).ToArray();
                var    count     = batch.Get(key_count);
                if (count == null || count.Length == 0)
                {
                    throw new Exception("error map instance.");
                }
            }

            batch.Put(key, Helper.tagValue_Map.Concat(this.Value).ToArray());
        }