Ejemplo n.º 1
0
        public void IndexReorganizes()
        {
            _testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
            Cleanup();

            var formatter = TestResourceFactory.CreateJsonFormatterWithoutArrayFormatting();
            var core = new FileCore<int, long>() { IdSeed = new Seed32(999), SegmentSeed = new Seed64(), Stride = 512 };

            var objs = TestResourceFactory.GetMockClassAObjects(100).ToDictionary(d => d.Id = core.IdSeed.Increment());

            IDictionary<int, long> segs = null;

            using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", core, formatter))
            {
                using (var index = new Index<int, MockClassA, long>(_testName + ".index", "Id", true, 8, new BinConverter32(), new BinConverter64(), new RowSynchronizer<long>(new BinConverter64()), new RowSynchronizer<int>(new BinConverter32())))
                {
                    afm.Load<int>();

                    index.Load();

                    segs = AtomicFileManagerHelper.SaveSegments(afm, objs);

                    //should trigger re-org.
                    index.Register(afm);

                    Assert.AreEqual(objs.Count, segs.Count);

                    afm.SaveCore();

                    long seg;
                    var f = index.FetchIndex(segs[objs.First().Key], out seg);
                    Assert.AreEqual(objs.First().Key, f);

                    long[] segList;
                    var all = index.FetchIndexes(segs.Values.ToArray(), out segList);
                    Assert.AreEqual(objs.Count, all.Count());

                    var many = index.FetchIndexes(segs[objs.First().Key], out segList);
                    Assert.AreEqual(1, many.Count());
                }
            }

            using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", core, formatter))
            {
                using (var index = new Index<int, MockClassA, long>(_testName + ".index", "Id", true))
                {
                    afm.Load<int>();

                    index.Load();
                    index.Register(afm);

                    var f = index.FetchSegment(objs.First().Key);
                    Assert.AreEqual(segs[objs.First().Key], f);

                    var all = index.FetchSegments(objs.First().Key, objs.Last().Key);
                    Assert.AreEqual(objs.Count, all.Count());

                    var many = index.FetchSegments(new int[] { objs.First().Key, objs.Last().Key });
                    Assert.AreEqual(2, many.Count());
                }
            }
        }