private LevelDB.DB <string, string> GetTestDb([CallerMemberName] string name = null) { LevelDB.Options options = new LevelDB.Options(); options.CreateIfMissing = true; var db = new LevelDB.DB(options, $"/tmp/DBTests-{name}").Cast <string, string>(); db.Put("c", "3").Put("b", "2").Put("a", "1"); db.Put("l", "12").Put("m", "13").Put("n", "14"); db.Put("z", "26").Put("y", "25").Put("x", "24"); return(db); }
public void Integers() { using var log = Log(); LevelDB.Options options = new LevelDB.Options(); options.CreateIfMissing = true; using LevelDB.DB <int, string> db = new LevelDB.DB(options, $"/tmp/DBTests-Integers").Cast <int, string>(); db.Put(1, "aaa").Put(2, "bbb").Put(3, "ccc"); db.Put(7, "ggg").Put(8, "hhh").Put(9, "iii"); db.Put(6, "fff").Put(5, "eee").Put(4, "ddd"); Assert.Equal( "4=ddd;5=eee;6=fff;7=ggg", string.Join(";", db.GetIterable().Range(4, 8).Select(kv => $"{kv.Key}={kv.Value}"))); }
public override bool AddChests(IEnumerable <long> mapids, string playerid) { // acquire the file this player is stored in, and the tag that represents said player byte[] playeridbytes; if (playerid == LOCAL_IDENTIFIER) { playeridbytes = Encoding.Default.GetBytes("~local_player"); } else { playeridbytes = Encoding.Default.GetBytes(playerid); } byte[] playerdata = BedrockDB.Get(playeridbytes.ToArray()); var file = new NbtFile(); file.BigEndian = false; file.LoadFromBuffer(playerdata, 0, playerdata.Length, NbtCompression.None); var invtag = (NbtList)file.RootTag["Inventory"]; var success = PutChestsInInventory(invtag, mapids); byte[] bytes = file.SaveToBuffer(NbtCompression.None); BedrockDB.Put(playeridbytes, bytes); return(success); }
public override void Put(byte[] key, byte[] value) { if (key == null) { throw new ArgumentNullException("key"); } if (value == null) { throw new ArgumentNullException("value"); } db.Put(key, value as byte[]); }
public void PutToDB(LevelDB.DB db, byte[] key) { var snapshot = Helper.CreateSnapshot(db); if (this.Value == null) {//申请新的实例ID,然后初始化存储Map var key_instMax = Helper.tagKey_InstanceMax; var instid = db.Get(snapshot, 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); db.Put(key_instMax, instid); } //初始化字典数量 byte[] key_count = Helper.tagKey_MapCount.Concat(this.Value).ToArray(); db.Put(key_count, BitConverter.GetBytes((UInt64)0)); } else {//检查count是否存在, byte[] key_count = Helper.tagKey_MapCount.Concat(this.Value).ToArray(); var count = db.Get(snapshot, key_count); if (count == null || count.Length == 0) { throw new Exception("error map instance."); } } db.Put(key, Helper.tagValue_Map.Concat(this.Value).ToArray()); }
public void Prefix() { using var log = Log(); LevelDB.Options options = new LevelDB.Options(); options.CreateIfMissing = true; using var db = new LevelDB.DB(options, $"/tmp/DBTests-Prefix").Cast <string, int>(); db .Put("a", 1) .Put("aa", 2) .Put("ab", 3) .Put("b", 4) .Put("ba", 5) .Put("bb", 6) .Put("c", 7); Assert.Equal( "a=1;aa=2;ab=3", string.Join(";", db.GetIterable().Prefix("a").Select(kv => $"{kv.Key}={kv.Value}"))); Assert.Equal( "b=4;ab=3;aa=2", string.Join(";", db.GetIterable().Reverse().Prefix("a").Select(kv => $"{kv.Key}={kv.Value}"))); Assert.Equal( "b=4;ab=3;aa=2", string.Join(";", db.GetIterable().Prefix("a").Reverse().Select(kv => $"{kv.Key}={kv.Value}"))); }