コード例 #1
0
ファイル: PlaneDBTests.cs プロジェクト: nmaier/PlaneDB
        public void TestBasicIterators()
        {
            var di = new DirectoryInfo("testdb");

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

            using (var db = new PlaneDB(di, FileMode.Open, planeDBOptions.DisableJournal())) {
                db.Compact();
                // XXX Verify table count and sequence are correct
            }

            int read;

            using (var db = new PlaneDB(di, FileMode.Open, planeDBOptions.DisableJournal())) {
                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 _));
                read = db.Select((e, i) => new KeyValuePair <byte[], int>(e.Key, i)).Count();

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

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

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

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

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

            using (var db = new PlaneDB(di, FileMode.Open, planeDBOptions.DisableJournal())) {
                Assert.AreEqual(db.Count, read);
                db.Clear();
                Assert.AreEqual(db.Count, 0);
            }

            using (var db = new PlaneDB(di, FileMode.Open, planeDBOptions.DisableJournal())) {
                Assert.AreEqual(db.Count, 0);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: nmaier/PlaneDB
        private static void FillDB(DirectoryInfo di, PlaneDBOptions tableoptions)
        {
            using var db = new PlaneDB(di, FileMode.CreateNew, tableoptions);
            var value = Enumerable.Repeat(Encoding.UTF8.GetBytes("hello world"), 512).SelectMany(i => i).ToArray();

            Console.WriteLine($"Writing len={value.Length:N0}");

            for (var i = 0; i < 10000; ++i)
            {
                if (!db.TryAdd(BitConverter.GetBytes(i), value))
                {
                    throw new IOException("oops");
                }
            }
        }
コード例 #3
0
ファイル: PlaneDBTests.cs プロジェクト: nmaier/PlaneDB
        public void TestRemoveOrphans()
        {
            var di = new DirectoryInfo("testdb");

            using (var db = new PlaneDB(di, FileMode.CreateNew, planeDBOptions.DisableJournal())) {
                var value = new byte[100];
                for (var i = 0; i < 10; ++i)
                {
                    Assert.IsTrue(db.TryAdd(BitConverter.GetBytes(i), value));
                }
            }

            var junk = Path.Combine(di.FullName, "default-119191919191.planedb");

            File.WriteAllText(junk, "test");
            using (new PlaneDB(di, FileMode.OpenOrCreate, planeDBOptions)) {
                Assert.IsFalse(File.Exists(junk));
            }
        }
コード例 #4
0
ファイル: PlaneDBTests.cs プロジェクト: nmaier/PlaneDB
        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);
            }
        }