Example #1
0
 private static void VerifyDB(DirectoryInfo di, PlaneDBOptions tableoptions)
 {
     using var db = new PlaneDB(di, FileMode.Open, tableoptions);
     for (var i = 0; i < 10000; ++i)
     {
         if (!db.TryGetValue(BitConverter.GetBytes(i), out _))
         {
             throw new KeyNotFoundException();
         }
     }
 }
Example #2
0
        public void TestLargeish()
        {
            var di    = new DirectoryInfo("testdb");
            var value = new byte[4096];

            value[0]    = 1;
            value[4095] = 0xff;
            using (var db = new PlaneDB(di, FileMode.CreateNew, planeDBOptions)) {
                for (var i = 0; i < 10000; ++i)
                {
                    Assert.IsTrue(db.TryAdd(BitConverter.GetBytes(i), value));
                }
            }

            using (var db = new PlaneDB(di, FileMode.Open, planeDBOptions)) {
                for (var i = 0; i < 10000; ++i)
                {
                    Assert.IsTrue(db.TryGetValue(BitConverter.GetBytes(i), out var val));
                    Assert.IsTrue(value.AsSpan().SequenceEqual(val));
                }
            }

            using (var db = new PlaneDB(di, FileMode.Open, planeDBOptions)) {
                Assert.AreEqual(db.Count, 10000);
                Assert.IsTrue(db.TryRemove(BitConverter.GetBytes(1000), out _));
                Assert.AreEqual(db.Count, 9999);
                Assert.IsFalse(db.TryRemove(BitConverter.GetBytes(1000), out _));
                var read = db.Select((e, i) => new KeyValuePair <byte[], int>(e.Key, i)).Count();

                Assert.AreEqual(db.Count, read);
                db.Clear();
                Assert.AreEqual(db.Count, 0);
            }

            using (var db = new PlaneDB(di, FileMode.Open, planeDBOptions)) {
                Assert.AreEqual(db.Count, 0);
            }
        }
Example #3
0
        public void TestSetThreadUnsafe()
        {
            var opts = planeDBOptions.DisableThreadSafety();

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.CreateNew, opts)) {
                for (var i = 0; i < COUNT; ++i)
                {
                    var k = Encoding.UTF8.GetBytes(i.ToString());
                    var v = Encoding.UTF8.GetBytes(i.ToString() + i + i + i + i);
                    db[k] = v;
                }
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                for (var i = 0; i < COUNT; ++i)
                {
                    var k = Encoding.UTF8.GetBytes(i.ToString());
                    var v = Encoding.UTF8.GetString(db[k]);
                    Assert.AreEqual(v, i.ToString() + i + i + i + i);
                }

                Assert.IsTrue(db.TryRemove(Encoding.UTF8.GetBytes("0"), out var removed));
                Assert.AreEqual("00000", Encoding.UTF8.GetString(removed));
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                for (var i = 1; i < COUNT; ++i)
                {
                    var k = Encoding.UTF8.GetBytes(i.ToString());
                    var v = Encoding.UTF8.GetString(db[k]);
                    Assert.IsTrue(db.ContainsKey(k));
                    Assert.AreEqual(v, i.ToString() + i + i + i + i);
                }
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                for (var i = 0; i < COUNT; ++i)
                {
                    var k = Encoding.UTF8.GetBytes(i.ToString());
                    if (i == 0)
                    {
                        Assert.IsFalse(db.ContainsKey(k));
                        Assert.IsFalse(db.TryGetValue(k, out _));
                    }
                    else
                    {
                        Assert.IsTrue(db.ContainsKey(k));
                        Assert.IsTrue(db.TryGetValue(k, out var v));
                        Assert.AreEqual(Encoding.UTF8.GetString(v), i.ToString() + i + i + i + i);
                    }
                }
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                db.Add(Encoding.UTF8.GetBytes("test1"), Encoding.UTF8.GetBytes("test1"));
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                db.Add(Encoding.UTF8.GetBytes("test2"), Encoding.UTF8.GetBytes("test2"));
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                db.AddOrUpdate(Encoding.UTF8.GetBytes("test2"), Encoding.UTF8.GetBytes("test3"),
                               (_, __) => Encoding.UTF8.GetBytes("test3"));
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                db.Add(Encoding.UTF8.GetBytes("test3"), Encoding.UTF8.GetBytes("test4"));
            }

            using (var db = new PlaneDB(new DirectoryInfo("testdb"), FileMode.Open, opts)) {
                foreach (var i in new[] {
                    new[] { "test1", "test1" }, new[] { "test2", "test3" }, new[] { "test3", "test4" }
                })
                {
                    Assert.IsTrue(db.ContainsKey(Encoding.UTF8.GetBytes(i[0])));
                    Assert.IsTrue(db.TryGetValue(Encoding.UTF8.GetBytes(i[0]), out var v));
                    Assert.AreEqual(i[1], Encoding.UTF8.GetString(v));
                }
            }
        }