Exemple #1
0
        private void VerifyHeader()
        {
            var buffer = new byte[UnicodeHeaderLength];

            ReadBytes(buffer, 0, 28);
            Validate.Match(buffer.Segment(0, 4), MagicBytes, "Magic value invalid.");
            Validate.Match(buffer.Segment(8, 2), MagicClientBytes, "Magic client value invalid.");
            _fileVersion = BitConverter.ToUInt16(buffer, 10);
            Validate.Any(_fileVersion, SupportedVersions, "Found unsupported version.");
            ReadBytes(buffer, 28, (IsAnsi ? AnsiHeaderLength : UnicodeHeaderLength) - 28);
            var crcPartial = BitConverter.ToUInt32(buffer, 4);

            Validate.Match(crcPartial, Crc32.Calculate(buffer.Segment(8, 471)), "Partial CRC invalid.");

            var nbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 36),
                BitConverter.ToUInt64(buffer, RootOffset + 44));
            var bbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 52),
                BitConverter.ToUInt64(buffer, RootOffset + 60));

            _nbtReader = new BTreeReader <NbtEntry>(Stream, (long)nbt.Ib, seg => new NbtEntry(seg));
            _bbtReader = new BTreeReader <BbtEntry>(Stream, (long)bbt.Ib, seg => new BbtEntry(seg));

            _cryptMethod = (CryptMethod)buffer[CryptMethodOffset];
        }
Exemple #2
0
        public static void NodeSelection(BTree <int> tree)
        {
            tree.ClearSelected();
            PrintSelectionInputFormat();
            string input = Console.ReadLine();

            try
            {
                Queue <int>           values  = BTreeReader.ReadParentFinderInput(input);
                BTreeOperations <int> treeOps = new BTreeOperations <int>();
                BTreeNode <int>       parent  = treeOps.ParentFinder(tree, new BTreeNode <int>(values.Dequeue()), new BTreeNode <int>(values.Dequeue()));
                if (parent != null)
                {
                    Console.WriteLine("Parent value of {0} has been found.", parent.Value);
                }
                else
                {
                    Console.WriteLine("Parent value was not found.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error reading input for parent finder, recheck input.");
                Console.WriteLine("Details: {0}", e.Message);
            }
        }
Exemple #3
0
        public void Nonexistent_BBT_Node_Returns_Null()
        {
            var stream = TestHelper.GetTestDataStream("bbt.bin");
            var reader = new BTreeReader <BbtEntry>(stream, 0, seg => new BbtEntry(seg));

            var node = reader.Find(6);

            Assert.IsNull(node);
        }
Exemple #4
0
        public void Nonexistent_NBT_Node_Returns_Null()
        {
            var stream = TestHelper.GetTestDataStream("nbt.bin");
            var reader = new BTreeReader<NbtEntry>(stream, 0);

            var node = reader.Find(6);

            Assert.IsNull(node);
        }
Exemple #5
0
        public void Finds_BBT_Node()
        {
            var stream = TestHelper.GetTestDataStream("bbt.bin");
            var reader = new BTreeReader <BbtEntry>(stream, 0, seg => new BbtEntry(seg));

            var node = reader.Find(3);

            Assert.IsNotNull(node);
            Assert.AreEqual(new Bref(3, 0xffee), node.Bref);
            Assert.AreEqual(0x100, node.Length);
            Assert.AreEqual(1, node.RefCount);
        }
Exemple #6
0
        public void Finds_BBT_Node()
        {
            var stream = TestHelper.GetTestDataStream("bbt.bin");
            var reader = new BTreeReader<BbtEntry>(stream, 0);

            var node = reader.Find(3);

            Assert.IsNotNull(node);
            Assert.AreEqual(new Bref(3, 0xffee), node.Bref);
            Assert.AreEqual(0x100, node.Length);
            Assert.AreEqual(1, node.RefCount);
        }
Exemple #7
0
        public void Finds_NBT_Node()
        {
            var stream = TestHelper.GetTestDataStream("nbt.bin");
            var reader = new BTreeReader <NbtEntry>(stream, 0, seg => new NbtEntry(seg));

            var node = reader.Find(3);

            Assert.IsNotNull(node);
            Assert.AreEqual(3u, (uint)node.Nid);
            Assert.AreEqual(0xffee, node.DataBid);
            Assert.AreEqual(0, node.SubnodeBid);
            Assert.AreEqual(0, (uint)node.ParentNid);
        }
Exemple #8
0
        public void Finds_NBT_Node()
        {
            var stream = TestHelper.GetTestDataStream("nbt.bin");
            var reader = new BTreeReader<NbtEntry>(stream, 0);

            var node = reader.Find(3);

            Assert.IsNotNull(node);
            Assert.AreEqual(3u, (uint)node.Nid);
            Assert.AreEqual(0xffee, node.DataBid);
            Assert.AreEqual(0, node.SubnodeBid);
            Assert.AreEqual(0, (uint)node.ParentNid);
        }
Exemple #9
0
        private static BTree <int> ReadFromConsole()
        {
            BTree <int> tree = new BTree <int>();

            PrintInputFormat();
            string input = Console.ReadLine();

            try
            {
                BTreeReader.ReadInputIntoTree(tree, input);
                Console.WriteLine("Successfully read into tree");
                return(tree);
            }
            catch (Exception)
            {
                Console.WriteLine("Error reading into tree, recheck input.");
                return(null);
            }
        }
Exemple #10
0
        public void ConstructorThrowsWhenArgIsNotReadOnly()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (MockStream stream = new MockStream(ms, true, true, true))
                {
                    long pageSize = BTreeNode <long, string> .GetRequiredPageSize(MockLongSerializer.CDataSize, MockStringSerializer.CDataSize, BTreeNode <long, string> .VeryMinKeyValuePairCapacity);

                    using (StreamingPageStorage storage = StreamingPageStorage.Create(stream, pageSize, 1, null, new CancellationToken(false), true))
                    {
                        MockBTree tree = new MockBTree(storage, new MockLongSerializer(), new MockStringSerializer());

                        Assert.ThrowsException <ArgumentException>(() => {
                            var reader = new BTreeReader <long, string>(tree);
                        });
                    }
                }
            }
        }
Exemple #11
0
        private static BTree <int> ReadFile()
        {
            BTree <int> tree      = new BTree <int>();
            string      inputPath = GetWorkingDirPath("input.txt");

            PrintInputFormat();
            Console.WriteLine("Reading from filepath:\n{0}\n...", inputPath);
            string input = ReadFileInput(inputPath);

            try
            {
                BTreeReader.ReadInputIntoTree(tree, input);
            }
            catch (Exception)
            {
                Console.WriteLine("Error reading into tree, recheck input. Make sure file is in the correct path.");
                return(null);
            }

            Console.WriteLine("Successfully read into tree");
            return(tree);
        }
Exemple #12
0
        public void ConstructorWorks(long count)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (MockStream stream = new MockStream(ms, true, true, true))
                {
                    long pageSize = BTreeNode <long, string> .GetRequiredPageSize(MockLongSerializer.CDataSize, MockStringSerializer.CDataSize, BTreeNode <long, string> .VeryMinKeyValuePairCapacity);

                    using (var storage = StreamingPageStorage.Create(stream, pageSize, 1, null, new CancellationToken(false), true))
                    {
                        var tree = new MockBTree(storage, new MockLongSerializer(), new MockStringSerializer());

                        //Initialize some data into the tree
                        for (long i = 0; i < count; i++)
                        {
                            Assert.IsTrue(tree.Insert(i, i.ToString(), false, out _));
                        }
                    }

                    //Load the tree as read-only
                    using (var storage = StreamingPageStorage.Load(stream, true, true, true))
                    {
                        var tree = new MockBTree(storage, new MockLongSerializer(), new MockStringSerializer());

                        BTreeReader <long, string> reader = new BTreeReader <long, string>(tree);
                        Assert.AreSame(tree, reader.BTree);

                        if (tree.Root != null)
                        {
                            Assert.AreEqual(tree.Root.PageIndex, reader.RootNode.PageIndex);
                        }
                        else
                        {
                            Assert.IsNull(reader.RootNode);
                        }
                    }
                }
            }
        }
Exemple #13
0
        private void VerifyHeader()
        {
            var buffer = new byte[UnicodeHeaderLength];
            ReadBytes(buffer, 0, 28);
            Validate.Match(buffer.Segment(0, 4), MagicBytes, "Magic value invalid.");
            Validate.Match(buffer.Segment(8, 2), MagicClientBytes, "Magic client value invalid.");
            _fileVersion = BitConverter.ToUInt16(buffer, 10);
            Validate.Any(_fileVersion, SupportedVersions, "Found unsupported version.");
            ReadBytes(buffer, 28, (IsAnsi ? AnsiHeaderLength : UnicodeHeaderLength) - 28);
            var crcPartial = BitConverter.ToUInt32(buffer, 4);
            Validate.Match(crcPartial, Crc32.Calculate(buffer.Segment(8, 471)), "Partial CRC invalid.");

            var nbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 36),
                BitConverter.ToUInt64(buffer, RootOffset + 44));
            var bbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 52),
                BitConverter.ToUInt64(buffer, RootOffset + 60));

            _nbtReader = new BTreeReader<NbtEntry>(_input, (long)nbt.Ib);
            _bbtReader = new BTreeReader<BbtEntry>(_input, (long)bbt.Ib);

            _cryptMethod = (CryptMethod)buffer[CryptMethodOffset];
        }
Exemple #14
0
 public void ConstructorThrowsWhenArgIsNull()
 {
     Assert.ThrowsException <ArgumentNullException>(() => {
         var reader = new BTreeReader <long, string>(null);
     });
 }