public void Given_a_filled_Trie_When_an_existing_item_is_removed_Then_true()
        {
            //when
            var result = _underTest.Remove("Gamma");

            Assert.True(result);
            _underTest.Persist();
            Assert.Equal(_underTest, new PersistentTrie(_stream));
        }
Beispiel #2
0
        public PersistentTrieSkipTests(ITestOutputHelper helper)
        {
            Helper  = helper;
            _stream = new MemoryStream();
            var temp = new PersistentTrie(_stream)
            {
                { "Alpha", "Alpha value" },
                { "Alphabet", "Alphabet value" },
                { "Bat", "Bat value" },
                { "Badminton", "Badminton value" },
                { "Charlie", "Charlie value" },
                { "Delta", "Delta value" },
                { "Epsilon", "Epsilon value" },
                { "Epsilon Delta Gamma", "Epsilon Delta Gamma value" },
                { "Gamma", "Gamma value" },
                { "Aarg", "Aarg value" },
                { "Beast", "Beast value" },
            };

            temp.Persist();

            _underTest = new PersistentTrie(_stream);

            Helper.WriteLine(_underTest.ToString());
        }
        public void Given_a_filled_Trie_When_an_existing_item_is_removed_Then_a_recreate_is_smaller()
        {
            //when
            _underTest.Remove("Gamma");
            var newStream = new MemoryStream();
            var recreate  = new PersistentTrie(newStream);

            foreach (var item in _underTest)
            {
                recreate.Add(item);
            }

            recreate.Persist();
            Assert.True(newStream.Length < _stream.Length);
        }
        public void Given_a_new_Trie_When_two_items_are_added_with_indexer_Then_it_has_two_item()
        {
            //Given
            var trie = new PersistentTrie(_stream);

            trie["Alpha"] = "first";
            trie["Beta"]  = "second";
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Beta", "second");
            trie.Persist();
            Assert.Equal(trie, new PersistentTrie(_stream));
        }
        public void Given_a_Trie_with_one_item_When_a_subitem_is_added_Then_it_has_two_item()
        {
            //Given
            var trie = new PersistentTrie(_stream);

            trie.Add("Alpha", "first");
            trie.Add("Aztec", "second");
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Aztec", "second");
            trie.Persist();
            Assert.Equal(trie, new PersistentTrie(_stream));
        }
        public void Given_a_new_Trie_with_two_items_are_added_When_toArray_Then_the_array_is_filled()
        {
            //Given
            var trie = new PersistentTrie(_stream);

            trie.Add("Beta", "second");
            trie.Add("Alpha", "first");
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Beta", "second");

            trie.Persist();
            Assert.Equal(trie.AsEnumerable(), new PersistentTrie(_stream).AsEnumerable());
        }
        public void Given_a_new_Trie_When_two_items_are_added_Then_it_has_two_item()
        {
            //Given
            var trie = new PersistentTrie(_stream);

            trie.Add("Alpha", "first");
            Helper.WriteLine(trie.ToString());
            trie.Add("Beta", "second");
            Helper.WriteLine(trie.ToString());
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Beta", "second");
            trie.Persist();
            Assert.Equal(trie, new PersistentTrie(_stream));
        }
        public void Given_a_new_Trie_When_two_items_are_added_in_reverse_order_Then_it_has_two_item()
        {
            //Given
            var trie = new PersistentTrie(_stream);

            trie.Add("Beta", "second");
            trie.Add("Alpha", "first");
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "Alpha", "first");
            AssertEqual(items[1], "Beta", "second");
            trie.Persist();
            var copy = new PersistentTrie(_stream);

            Assert.Equal(trie, copy);
            Assert.Equal(trie.Count, copy.Count);
        }
        public void Given_a_new_Trie_When_one_item_is_added_Then_it_has_one_item()
        {
            //Given
            var trie = new PersistentTrie(_stream);

            trie.Add("Alpha", "first");
            //Then
            Helper.WriteLine(trie.ToString());
            var item = trie.Single();

            AssertEqual(item, "Alpha", "first");

            trie.Persist();

            var copy = new PersistentTrie(_stream);

            Helper.WriteLine(copy.ToString());

            Assert.Equal(trie, copy);
        }
        public void Given_a_Trie_with_two_items_When_a_subitem_is_added_at_a_node_Then_it_has_three_items()
        {
            //Given
            var trie = new PersistentTrie(_stream);

            trie.Add("Bat", "second");
            trie.Add("Bison", "third");
            //When
            trie.Add("B", "first");

            Helper.WriteLine(trie.ToString());
            //Then
            var items = trie.ToArray();

            AssertEqual(items[0], "B", "first");
            AssertEqual(items[1], "Bat", "second");
            AssertEqual(items[2], "Bison", "third");

            trie.Persist();
            Assert.Equal(trie, new PersistentTrie(_stream));
        }
        public PersistentTrieRemoveTests(ITestOutputHelper helper)
        {
            Helper  = helper;
            _stream = new MemoryStream();
            var unsorted = new PersistentTrie(_stream)
            {
                { "Alpha", "Alpha value" },
                { "Alphabet", "Alphabet value" },
                { "Bat", "Bat value" },
                { "Badminton", "Badminton value" },
                { "Charlie", "Charlie value" },
                { "Delta", "Delta value" },
                { "Epsilon", "Epsilon value" },
                { "Epsilon Delta Gamma", "Epsilon Delta Gamma value" },
                { "Gamma", "Gamma value" },
                { "Aarg", "Aarg value" },
                { "Beast", "Beast value" },
            };

            unsorted.Persist();
            _underTest = new PersistentTrie(_stream);
        }
Beispiel #12
0
        private static void DoRebuildTree(ref PersistentTrie trie, ref FileStream stream)
        {
            _defragment_threshold = (int)(_defragment_threshold * 1.41);
            var newFile = new FileInfo(stream.Name + ".new");

            if (newFile.Exists)
            {
                newFile.Delete();
            }

            var tick          = trie.Count / 50;
            var i             = 0;
            var generateTimer = Stopwatch.StartNew();

            using (var newStream = newFile.Open(FileMode.CreateNew))
            {
                var newTrie = new PersistentTrie(newStream);
                foreach (var item in trie._root)
                {
                    newTrie._root.AddItem(new Bits(item.Key), item.Value);
                    if (i++ % tick == 0)
                    {
                        Console.WriteLine($"[{i,10}] {tick / generateTimer.Elapsed.TotalSeconds:0.0} inserts/sec");
                        generateTimer.Restart();
                    }
                }
                using (stream)
                {
                    /*dispose*/
                }
                newTrie.Persist();
            }

            newFile.Replace(stream.Name, null);
            stream = new FileStream(stream.Name, FileMode.Open);
            trie   = new PersistentTrie(stream);
        }