Beispiel #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();
         }
     }
 }
Beispiel #2
0
        static void Main()
        {
            var di = new DirectoryInfo("testdb");

            if (di.Exists)
            {
                di.Delete(true);
            }

            var tableoptions = new PlaneDBOptions().EnableEncryption("this is just a test");

            FillDB(di, tableoptions);
            VerifyDB(di, tableoptions);
        }
Beispiel #3
0
        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");
                }
            }
        }
Beispiel #4
0
        private static void CompactOne(DirectoryInfo db, PlaneDBOptions popts)
        {
            var id = $"{db.FullName}:{popts.TableSpace}";

            try {
                Options.Write($"{id} - Opening");
                using var plane           = new PlaneDB(db, FileMode.Open, popts);
                plane.OnFlushMemoryTable += (_, __) => Options.Error($"{id} - Flushed memory table");
                plane.OnMergedTables     += (_, __) => Options.Error($"{id} - Merged tables");
                Options.Write($"{id} - Starting compaction");
                plane.Compact();
                Options.Write($"{id} - Finished compaction");
            }
            catch (BadMagicException ex) {
                Options.Error($"{id} - {ex.Message}");
            }
            catch (AlreadyLockedException ex) {
                Options.Error($"{id} - {ex.Message}");
            }
        }
Beispiel #5
0
        public override void Execute()
        {
            var popts = new PlaneDBOptions().DisableJournal();

            if (!string.IsNullOrEmpty(Owner.Passphrase))
            {
                popts = popts.EnableEncryption(Owner.Passphrase);
            }
            else if (Owner.Compressed)
            {
                popts = popts.EnableCompression();
            }

            if (!string.IsNullOrEmpty(Owner.Tablespace))
            {
                popts = popts.UsingTableSpace(Owner.Tablespace);
            }

            if (From == null)
            {
                throw new GetOptException("No from");
            }

            if (To == null)
            {
                throw new GetOptException("No to");
            }

            using var rocks           = RocksDb.OpenReadOnly(new DbOptions(), From.FullName, false);
            using var plane           = new PlaneDB(To, FileMode.OpenOrCreate, popts);
            plane.OnFlushMemoryTable += (_, __) => Console.WriteLine("Flushed memory table");
            plane.OnMergedTables     += (_, __) => Console.WriteLine("Merged tables");
            plane.Clear();

            var iter = rocks.NewIterator();

            plane.MassInsert(() => CopyFromRocks(plane, iter));

            Console.WriteLine($"{copyCount:N0} entries copied in total");
        }
Beispiel #6
0
        public override void Execute()
        {
            var popts = new PlaneDBOptions().DisableJournal().WithBlockCacheCapacity(2_048);

            if (!string.IsNullOrEmpty(Owner.Passphrase))
            {
                popts = popts.EnableEncryption(Owner.Passphrase);
            }
            else if (Owner.Compressed)
            {
                popts = popts.EnableCompression();
            }

            if (DB == null || DB.Length != 1)
            {
                throw new GetOptException("No database specified");
            }

            var db = DB[0];

            if (!string.IsNullOrEmpty(Owner.Tablespace))
            {
                InfoOne(db, popts.UsingTableSpace(Owner.Tablespace));
            }
            else if (AllTablespaces)
            {
                foreach (var tableSpace in Options.GetTableSpaces(db))
                {
                    InfoOne(db, popts.UsingTableSpace(tableSpace));
                }
            }
            else
            {
                InfoOne(db, popts);
            }
        }
Beispiel #7
0
        private void InfoOne(DirectoryInfo db, PlaneDBOptions popts)
        {
            infos.Clear();
            try {
                var stop = new Stopwatch();
                stop.Start();
                Options.Write($"{db.FullName}:{popts.TableSpace} - Querying");
                using var plane = new PlaneDB(db, FileMode.Open, popts);
                stop.Stop();
                Add("General Information");
                Add("Time To Open", stop.Elapsed.ToString());
                Add("Location", plane.Location.FullName);
                Add("Tablespace", plane.TableSpace);
                if (Verbose)
                {
                    Add("Table Files", plane.CurrentTableCount);
                    Add("Index Blocks", plane.CurrentIndexBlockCount);
                    foreach (var(key, value) in plane.AllLevels)
                    {
                        Add($"Level {key}", string.Join(", ", value));
                    }
                }

                var currentDiskSize = plane.CurrentDiskSize;
                var currentRealSize = plane.CurrentRealSize;

                Add("Sizes");
                AddByte("Disk Size", currentDiskSize);
                AddByte("Raw Size", currentRealSize);
                if (Verbose)
                {
                    Add("Bloom Bits", plane.CurrentBloomBits);
                    AddByte("Bloom Bytes (In Memory)", (long)Math.Ceiling(plane.CurrentBloomBits / 8.0));
                }

                stop.Reset();
                stop.Start();

                Add("Item Information");
                var  count   = 0;
                var  minK    = int.MaxValue;
                var  maxK    = 0;
                var  minV    = int.MaxValue;
                var  maxV    = 0;
                long allK    = 0;
                long allV    = 0;
                long inlined = 0;
                if (!Verbose)
                {
                    count = plane.Count;
                }
                else
                {
                    foreach (var(key, value) in plane)
                    {
                        ++count;
                        var kl = key.Length;
                        var vl = value.Length;
                        if (vl <= 9)
                        {
                            ++inlined;
                        }

                        minK  = Math.Min(kl, minK);
                        minV  = Math.Min(vl, minV);
                        maxK  = Math.Max(kl, maxK);
                        maxV  = Math.Max(vl, maxV);
                        allK += kl;
                        allV += vl;
                    }
                }

                stop.Stop();

                Add("Time To Gather Information", stop.Elapsed.ToString());
                Add("Item count", count);
                if (Verbose && count > 0)
                {
                    if (minK != maxK)
                    {
                        AddByte("Min Key Length", minK);
                        AddByte("Max Key Length", maxK);
                        Add("Average Key Length", allK / (double)count);
                    }
                    else
                    {
                        AddByte("Key Length", minK);
                    }

                    if (minV != maxV)
                    {
                        AddByte("Min Value Length", minV);
                        AddByte("Max Value Length", maxV);
                        Add("Average Value Length", allV / (double)count);
                    }
                    else
                    {
                        AddByte("Value Length", minV);
                    }

                    AddByte("Keys Length", allK);
                    AddByte("Values Length", allV);
                    Add("Inlined Values", inlined);
                }

                if (Verbose)
                {
                    Add("Overheads (bookkeeping, bloom, etc)");
                    var datalen = allV + allK;
                    AddByte("Raw Data Length", datalen);
                    AddByte("Disk Overhead", currentDiskSize - datalen);
                    AddByte("Raw Overhead", currentRealSize - datalen);
                    Add("Disk factor", (double)currentDiskSize / datalen);
                    Add("Raw factor", (double)currentRealSize / datalen);
                }


                var maxlen  = infos.Where(i => !string.IsNullOrEmpty(i.Value)).Max(i => i.Key.Length) + 4;
                var maxvlen = infos.Max(i => i.Value.Length) + 2;
                foreach (var(key, value) in infos)
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        var tlen = Math.Min(Console.WindowWidth - 2, maxlen + maxvlen);
                        var half = (int)Math.Ceiling((tlen - key.Length) / 2.0);
                        Console.WriteLine();
                        Console.WriteLine(
                            $"{key.PadLeft(key.Length + 1).PadRight(key.Length + 2).PadLeft(half + key.Length, '-').PadRight(tlen, '-')}");
                        Console.WriteLine();
                        continue;
                    }

                    Console.WriteLine($"{key.PadLeft(maxlen)}: {value}");
                }

                Console.WriteLine();
            }
            catch (BadMagicException ex) {
                Options.Error($"{db.FullName}:{popts.TableSpace} - {ex.Message}");
            }
            catch (AlreadyLockedException ex) {
                Options.Error($"{db.FullName}:{popts.TableSpace} - {ex.Message}");
            }
        }