Example #1
0
        public void Can_find_exact()
        {
            var  tree = new LcrsTrie('\0', false);
            Word word;

            Assert.False(tree.HasWord("xxx", out word));

            tree.Add("xxx");

            Assert.True(tree.HasWord("xxx", out word));
            Assert.False(tree.HasWord("baby", out word));
            Assert.False(tree.HasWord("dad", out word));

            tree.Add("baby");

            Assert.True(tree.HasWord("xxx", out word));
            Assert.True(tree.HasWord("baby", out word));
            Assert.False(tree.HasWord("dad", out word));

            tree.Add("dad");

            Assert.True(tree.HasWord("xxx", out word));
            Assert.True(tree.HasWord("baby", out word));
            Assert.True(tree.HasWord("dad", out word));
        }
Example #2
0
        public void Can_append()
        {
            var root = new LcrsTrie('\0', false);

            root.Add("baby");
            root.Add("bad");

            Assert.That(root.LeftChild.Value, Is.EqualTo('b'));
            Assert.That(root.LeftChild.LeftChild.Value, Is.EqualTo('a'));
            Assert.That(root.LeftChild.LeftChild.LeftChild.Value, Is.EqualTo('d'));

            Assert.That(root.LeftChild.LeftChild.LeftChild.RightSibling.Value, Is.EqualTo('b'));
            Assert.That(root.LeftChild.LeftChild.LeftChild.RightSibling.LeftChild.Value, Is.EqualTo('y'));

            Assert.True(root.HasWord("baby"));
            Assert.True(root.HasWord("bad"));
        }
Example #3
0
        public void Can_append()
        {
            var root = new LcrsTrie('\0', false);

            root.Add("baby");
            root.Add("bad");
            Word word;

            Assert.AreEqual('b', root.LeftChild.Value);
            Assert.AreEqual('a', root.LeftChild.LeftChild.Value);
            Assert.AreEqual('d', root.LeftChild.LeftChild.LeftChild.RightSibling.Value);

            Assert.AreEqual('b', root.LeftChild.LeftChild.LeftChild.Value);
            Assert.AreEqual('y', root.LeftChild.LeftChild.LeftChild.LeftChild.Value);

            Assert.IsTrue(root.HasWord("baby", out word));
            Assert.IsTrue(root.HasWord("bad", out word));
        }
        public void Commit()
        {
            var deleteSet = new LcrsTrie();

            foreach (var value in _values)
            {
                var hashString = value.ToHash().ToString(CultureInfo.InvariantCulture);

                deleteSet.Add(hashString);
            }

            foreach (var ix in _ixs)
            {
                var docHashFileName    = Path.Combine(_directory, string.Format("{0}.{1}", ix.VersionId, "pk"));
                var tmpDocHashFileName = Path.Combine(_directory, string.Format("{0}.{1}", ix.VersionId, "pk.tmp"));

                var tmpIxFileName = Path.Combine(_directory, ix.VersionId + ".ix.tmp");
                var ixFileName    = Path.Combine(_directory, ix.VersionId + ".ix");

                var deleted = 0;

                using (var stream = new FileStream(tmpDocHashFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    foreach (var document in Serializer.DeserializeDocHashes(docHashFileName))
                    {
                        Word found;

                        var hash = document.Hash.ToString(CultureInfo.InvariantCulture);

                        if (deleteSet.HasWord(hash, out found))
                        {
                            if (!document.IsObsolete)
                            {
                                document.IsObsolete = true;
                                deleted++;
                            }
                        }

                        document.Serialize(stream);
                    }
                }

                if (deleted > 0)
                {
                    ix.DocumentCount -= deleted;
                    ix.Serialize(tmpIxFileName);

                    File.Copy(tmpIxFileName, ixFileName, overwrite: true);
                    File.Copy(tmpDocHashFileName, docHashFileName, overwrite: true);

                    File.Delete(tmpIxFileName);
                    File.Delete(tmpDocHashFileName);
                }
            }
        }
Example #5
0
        public void Can_build_two_legs()
        {
            var root = new LcrsTrie('\0', false);

            root.Add("baby");
            root.Add("dad");
            Word word;

            Assert.That(root.LeftChild.RightSibling.Value, Is.EqualTo('d'));
            Assert.That(root.LeftChild.LeftChild.Value, Is.EqualTo('a'));
            Assert.That(root.LeftChild.RightSibling.LeftChild.LeftChild.Value, Is.EqualTo('d'));

            Assert.That(root.LeftChild.Value, Is.EqualTo('b'));
            Assert.That(root.LeftChild.RightSibling.LeftChild.Value, Is.EqualTo('a'));
            Assert.That(root.LeftChild.LeftChild.LeftChild.Value, Is.EqualTo('b'));
            Assert.That(root.LeftChild.LeftChild.LeftChild.LeftChild.Value, Is.EqualTo('y'));

            Assert.True(root.HasWord("baby", out word));
            Assert.True(root.HasWord("dad", out word));
        }
Example #6
0
        public void Can_deserialize_whole_file()
        {
            var fileName = Path.Combine(Dir, "Can_serialize_whole_file.tri");

            var tree = new LcrsTrie('\0', false);

            tree.Add("baby");
            tree.Add("bad");
            tree.Add("bank");
            tree.Add("box");
            tree.Add("dad");
            tree.Add("dance");

            Word found;

            Assert.IsTrue(tree.HasWord("baby", out found));
            Assert.IsTrue(tree.HasWord("bad", out found));
            Assert.IsTrue(tree.HasWord("bank", out found));
            Assert.IsTrue(tree.HasWord("box", out found));
            Assert.IsTrue(tree.HasWord("dad", out found));
            Assert.IsTrue(tree.HasWord("dance", out found));

            tree.Serialize(fileName);

            var recreated = Serializer.DeserializeTrie(Dir, new FileInfo(fileName).Name);

            Assert.IsTrue(recreated.HasWord("baby", out found));
            Assert.IsTrue(recreated.HasWord("bad", out found));
            Assert.IsTrue(recreated.HasWord("bank", out found));
            Assert.IsTrue(recreated.HasWord("box", out found));
            Assert.IsTrue(recreated.HasWord("dad", out found));
            Assert.IsTrue(recreated.HasWord("dance", out found));
        }
Example #7
0
        public void Can_append_tries()
        {
            var one = new LcrsTrie('\0', false);

            one.Add("ape");
            one.Add("app");
            one.Add("banana");

            var two = new LcrsTrie('\0', false);

            two.Add("apple");
            two.Add("banana");

            one.Merge(two);

            Word found;

            Assert.IsTrue(one.HasWord("ape", out found));
            Assert.IsTrue(one.HasWord("app", out found));
            Assert.IsTrue(one.HasWord("apple", out found));
            Assert.IsTrue(one.HasWord("banana", out found));
        }
Example #8
0
        public void Can_build_one_leg()
        {
            var tree = new LcrsTrie('\0', false);

            tree.Add("baby");

            Assert.That(tree.LeftChild.Value, Is.EqualTo('b'));
            Assert.That(tree.LeftChild.LeftChild.Value, Is.EqualTo('a'));
            Assert.That(tree.LeftChild.LeftChild.LeftChild.Value, Is.EqualTo('b'));
            Assert.That(tree.LeftChild.LeftChild.LeftChild.LeftChild.Value, Is.EqualTo('y'));

            Assert.True(tree.HasWord("baby"));
        }
Example #9
0
        public void Can_build_one_leg()
        {
            var  tree = new LcrsTrie('\0', false);
            Word word;

            tree.Add("baby");

            Assert.AreEqual('b', tree.LeftChild.Value);
            Assert.AreEqual('a', tree.LeftChild.LeftChild.Value);
            Assert.AreEqual('b', tree.LeftChild.LeftChild.LeftChild.Value);
            Assert.AreEqual('y', tree.LeftChild.LeftChild.LeftChild.LeftChild.Value);

            Assert.IsTrue(tree.HasWord("baby", out word));
        }