Exemplo n.º 1
0
        public void NTreeChecksAllWithLargeCount()
        {
            using (var tree = new NTree<int, MockClassA, long>("Id", true))
            {
                var objs = new List<MockClassA>();
                var seedIndex = new Seed32();
                var seedSegment = new Seed64();

                for (var i = 0; i < 5; i++)
                {
                    var additions = TestResourceFactory.GetMockClassAObjects(100000).ToList();

                    additions.ForEach(o => o.Id = seedIndex.Increment());

                    tree.AddOrUpdateRange(additions.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList());
                }

                Assert.AreEqual(500000, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                tree.AddOrUpdate(new Tuple<MockClassA, long>(TestResourceFactory.CreateRandom().WithId(seedIndex.Increment()), seedSegment.Increment()));

                Assert.AreEqual(500001, tree.Length);

                tree.AddOrUpdateRange(
                    tree.AsEnumerable().First().Take(100)
                    .Select(s => new NTreeItem<int, long>(s.Item2.Index, s.Item2.Segment))
                    .ToList());

                Assert.AreEqual(500001, tree.Length);
            }
        }
Exemplo n.º 2
0
        public void NTreeFetchesByIndexRange()
        {
            var objs = TestResourceFactory.GetMockClassAObjects(5000).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new NTree<int, MockClassA, long>("Id"))
            {
                var ids = tree.AddOrUpdateRange(toAdd);

                Assert.AreEqual(5000, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                tree.AddOrUpdate(new Tuple<MockClassA, long>(TestResourceFactory.CreateRandom().WithId(seedIndex.Increment()), seedSegment.Increment()));

                Assert.AreEqual(5001, tree.Length);

                long[] loc;
                var range = tree.GetByIndexRangeInclusive(51, 250, out loc);

                Assert.AreEqual(200, range.Length);
            }
        }
Exemplo n.º 3
0
        public void Seed64OpensUnused()
        {
            var seed = new Seed64(999);

            Assert.AreEqual(1000, seed.Increment());

            seed.Open(500);

            Assert.AreEqual(500, seed.Peek());

            Assert.AreEqual(500, seed.Increment());

            Assert.AreEqual(1000, seed.LastSeed);
        }
Exemplo n.º 4
0
        public void PTreeDeletesByIndexIndividualUnique()
        {
            _testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
            Cleanup();

            var objs = TestResourceFactory.GetMockClassAObjects(50).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());

            using (var tree = new PTree<int, MockClassA, long>("Id", _testName + ".index", true))
            {
                tree.Load();

                foreach (var o in objs)
                    tree.AddOrUpdate(new Tuple<MockClassA, long>(o, seedSegment.Increment()));

                Assert.AreEqual(50, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                Assert.AreEqual(1, tree.Delete(5).Length);

                Assert.AreEqual(50, tree.Length);

                Assert.AreEqual(0, tree.GetFirstByIndex(5));
            }
        }
Exemplo n.º 5
0
        public void NTreeDeletesByIndexIndividualUnique()
        {
            var objs = TestResourceFactory.GetMockClassAObjects(50).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());

            using (var tree = new NTree<int, MockClassA, long>("Id", true))
            {
                foreach (var o in objs)
                    tree.AddOrUpdate(new Tuple<MockClassA, long>(o, seedSegment.Increment()));

                Assert.AreEqual(50, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                Assert.AreEqual(1, tree.Delete(5).Length);

                Assert.AreEqual(50, tree.Length);

                Assert.AreEqual(0, tree.GetFirstByIndex(5));
            }
        }
Exemplo n.º 6
0
        public void PTreePushesEntities()
        {
            _testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
            Cleanup();

            var objs = TestResourceFactory.GetMockClassAObjects(50).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new PTree<int, MockClassA, long>("Id", _testName + ".index", true))
            {
                tree.Load();

                var ids = tree.AddOrUpdateRange(toAdd);
                tree.AddOrUpdate(toAdd.First());

                Assert.AreEqual(50, tree.Length);

                Assert.AreEqual(5L, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(1).Count());
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
            }
        }
Exemplo n.º 7
0
        public void Seed64IsThreadSafe()
        {
            var seed = new Seed64(999);

            var ids = new List<long>();

            Parallel.For(0, 50, delegate(int i)
            {
                ids.Add(seed.Increment());
            });

            Assert.AreEqual(50, ids.Distinct().Count());

            Assert.AreEqual(1050, seed.Increment());

            Assert.AreEqual(1050, seed.LastSeed);
        }
Exemplo n.º 8
0
        public void Seed64StartsWithParameter()
        {
            var seed = new Seed64(999);

            Assert.AreEqual(1000, seed.Peek());

            Assert.AreEqual(1000, seed.Increment());

            Assert.AreEqual(1000, seed.LastSeed);
        }
Exemplo n.º 9
0
        public void Seed64StartsAtOne()
        {
            var seed = new Seed64();

            Assert.AreEqual(1, seed.Peek());

            Assert.AreEqual(1, seed.Increment());

            Assert.AreEqual(1, seed.LastSeed);
        }
Exemplo n.º 10
0
        public void NTreePushesEntities()
        {
            var objs = TestResourceFactory.GetMockClassAObjects(50).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new NTree<int, MockClassA, long>("Id", true))
            {
                var ids = tree.AddOrUpdateRange(toAdd);
                tree.AddOrUpdate(toAdd.First());

                Assert.AreEqual(50, tree.Length);

                Assert.AreEqual(5L, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(1).Count());
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
            }
        }
Exemplo n.º 11
0
        public void NTreeReturnsEmptyPageWithWrongPageId()
        {
            var objs = TestResourceFactory.GetMockClassAObjects(50).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new NTree<int, MockClassA, long>("Id", true))
            {
                var ids = tree.AddOrUpdateRange(toAdd);
                tree.AddOrUpdate(toAdd.First());

                Assert.AreEqual(50, tree.Length);

                var p = tree.GetPage(8);

                Assert.AreEqual(0, p.Length);
            }
        }
Exemplo n.º 12
0
        public void NTreePushesLotsOfDuplicateEntities()
        {
            var objs = TestResourceFactory.GetMockClassAObjects(20480).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new NTree<int, MockClassA, long>("Id", true))
            {
                var ids = tree.AddOrUpdateRange(toAdd);

                Assert.AreEqual(20480, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                tree.AddOrUpdate(new Tuple<MockClassA, long>(TestResourceFactory.CreateRandom().WithId(seedIndex.Increment()), seedSegment.Increment()));

                Assert.AreEqual(20481, tree.Length);

                tree.AddOrUpdateRange(toAdd.Skip(100).Take(100).ToList());

                Assert.AreEqual(20481, tree.Length);
            }
        }
Exemplo n.º 13
0
        public void NTreeStreamsData()
        {
            var objs = TestResourceFactory.GetMockClassAObjects(5000).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new NTree<int, MockClassA, long>("Id"))
            {
                var ids = tree.AddOrUpdateRange(toAdd);

                Assert.AreEqual(5000, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                foreach (var p in tree.AsStreaming())
                {
                    if (p == null)
                        continue;

                    p.Dispose();
                }
            }
        }
Exemplo n.º 14
0
        public void NTreeReturnsEmptyStreamOnWrongPage()
        {
            var objs = TestResourceFactory.GetMockClassAObjects(5000).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new NTree<int, MockClassA, long>("Id"))
            {
                var ids = tree.AddOrUpdateRange(toAdd);

                Assert.AreEqual(5000, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                using (var p = tree.GetPageStream(8))
                {
                    Assert.AreEqual(0, p.Length);
                }
            }
        }
Exemplo n.º 15
0
        public void PTreeReverseEnumerates()
        {
            _testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
            Cleanup();

            var objs = TestResourceFactory.GetMockClassAObjects(5000).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new PTree<int, MockClassA, long>("Id", _testName + ".index"))
            {
                tree.Load();

                var ids = tree.AddOrUpdateRange(toAdd);

                Assert.AreEqual(5000, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                foreach (var p in tree.AsReverseEnumerable())
                {
                    if (p == null)
                        continue;
                }
            }
        }
Exemplo n.º 16
0
        public void PTreePushesLotsOfDuplicateEntities()
        {
            _testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
            Cleanup();

            var objs = TestResourceFactory.GetMockClassAObjects(5000).ToList();

            var seedIndex = new Seed32();
            var seedSegment = new Seed64();

            objs.ForEach(o => o.Id = seedIndex.Increment());
            var toAdd = objs.Select(o => new Tuple<MockClassA, long>(o, seedSegment.Increment())).ToList();

            using (var tree = new PTree<int, MockClassA, long>("Id", _testName + ".index"))
            {
                tree.Load();

                var ids = tree.AddOrUpdateRange(toAdd);

                Assert.AreEqual(5000, tree.Length);

                Assert.AreEqual(5, tree.GetFirstByIndex(5));
                Assert.AreEqual(5, tree.GetFirstBySegment(5));
                Assert.AreEqual(1, tree.GetByIndex(5).Count());

                tree.AddOrUpdate(new Tuple<MockClassA, long>(TestResourceFactory.CreateRandom().WithId(seedIndex.Increment()), seedSegment.Increment()));

                Assert.AreEqual(5001, tree.Length);

                tree.AddOrUpdateRange(toAdd.Skip(100).Take(100).ToList());

                Assert.AreEqual(5101, tree.Length);

                Assert.AreEqual(2, tree.GetBySegment(toAdd[101].Item2).Count());
            }
        }