Beispiel #1
0
        public void CouldWriteAndReadFromStreamLogAsSeries()
        {
            var path = TestUtils.GetPath();

            var table = new StreamBlockIndex(path, 16, LMDBEnvironmentFlags.WriteMap | LMDBEnvironmentFlags.NoSync, null);

            var streamId = (StreamLogId)10000;

            var series = table.GetBlockRecordSeries(streamId);

            var count = 1000;

            for (int i = 0; i < count; i++)
            {
                var lr1 = new StreamBlockRecord(BufferRef.Create(0, i + 1))
                {
                    Version = (ulong)i + 1, Timestamp = new Timestamp(i + 1)
                };

                Assert.IsTrue(table.TryAddBlockRecord(streamId, lr1));
                Assert.AreEqual(i + 1, table.GetBlockCount(streamId));
                Assert.AreEqual(i + 1, series.Count());

                var first = series.First;
                Assert.IsTrue(first.IsPresent);

                Assert.IsTrue(table.TryGetLast(streamId, true, out var lst));
                Assert.AreEqual((ulong)i + 1, lst.Version);

                var last = series.Last;
                Assert.IsTrue(last.IsPresent);

                Assert.AreEqual(last.Present.Key, i + 1);
                Assert.AreEqual(last.Present.Value.BufferRef, lr1.BufferRef);
            }

            var j = 1;

            foreach (var kvp in series)
            {
                Assert.AreEqual(kvp.Key, j);
                Assert.AreEqual(kvp.Value.Version, j);
                Assert.AreEqual(kvp.Value.BufferRef, BufferRef.Create(0, j));
                j++;
            }

            Assert.IsTrue(series.First.IsPresent);
            Assert.IsTrue(series.First.Present.Key == 1);

            Assert.IsTrue(series.Last.IsPresent);
            Assert.IsTrue(series.Last.Present.Key == (ulong)count);

            var c = series.GetEnumerator();

            Assert.IsTrue(c.MoveFirst());
            Assert.IsTrue(c.CurrentKey == 1);
            Assert.IsFalse(c.MovePrevious());

            Assert.IsTrue(c.MoveNext());
            Assert.IsTrue(c.CurrentKey == 2);

            Assert.IsTrue(c.MoveLast());
            Assert.IsTrue(c.CurrentKey == (ulong)count);

            Assert.IsFalse(c.MoveNext());

            Assert.IsTrue(c.MovePrevious());
            Assert.IsTrue(c.CurrentKey == (ulong)count - 1);

            Assert.IsTrue(c.MoveAt(50, Lookup.EQ));
            Assert.IsTrue(c.CurrentKey == 50);

            Assert.IsTrue(c.MoveAt((ulong)count + 10, Lookup.LE));
            Assert.IsTrue(c.CurrentKey == (ulong)count);

            Assert.IsTrue(c.MoveAt(0, Lookup.GE));
            Assert.IsTrue(c.CurrentKey == 1);

            Assert.IsTrue(c.MoveAt(50, Lookup.LE));
            Assert.IsTrue(c.CurrentKey == 50);

            Assert.IsTrue(c.MoveAt(50, Lookup.LT));
            Assert.IsTrue(c.CurrentKey == 49);

            Assert.IsTrue(c.MoveAt(50, Lookup.GE));
            Assert.IsTrue(c.CurrentKey == 50);

            Assert.IsTrue(c.MoveAt(50, Lookup.GT));
            Assert.IsTrue(c.CurrentKey == 51);

            var chunkRecord = c.CurrentValue;

            chunkRecord.BufferRef = default;

            var _ = table.UpdateBlockRecord(streamId, chunkRecord);

            // add more after update
            for (int i = count; i < count + 100; i++)
            {
                var lr1 = new StreamBlockRecord(BufferRef.Create(0, i + 1))
                {
                    Version = (ulong)i + 1
                };

                Assert.IsTrue(table.TryAddBlockRecord(streamId, lr1));
                Assert.AreEqual(i + 1, table.GetBlockCount(streamId));
                Assert.AreEqual(i + 1, series.Count());

                var last = series.Last;

                Assert.IsTrue(last.IsPresent);

                Assert.AreEqual(last.Present.Key, i + 1);
                Assert.AreEqual(last.Present.Value.BufferRef, lr1.BufferRef);
            }

            Assert.IsTrue(c.MoveAt(chunkRecord.Version, Lookup.GE));
            Assert.IsTrue(c.CurrentValue.BufferRef == default);

            // ready chunk test

            var lastVersion = series.Last.Present.Key;

            var readyChunk = new StreamBlockRecord()
            {
                Version = StreamBlockIndex.ReadyBlockVersion
            };

            table.TryAddBlockRecord(streamId, readyChunk);

            // not visible before update
            Assert.AreEqual(lastVersion, series.Last.Present.Value.Version);

            table.TryGetLast(streamId, false, out var lastIsReady);

            Assert.AreEqual(StreamBlockIndex.ReadyBlockVersion, lastIsReady.Version);

            //var newVersion = lastVersion + 1;

            //Assert.IsTrue(table.UpdateReadyChunkVersion(streamId, newVersion));

            //Assert.AreEqual(newVersion, series.Last.Present.Value.Version);

            c.Dispose();
            table.Dispose();
        }
Beispiel #2
0
        public void CouldUpdateChunk()
        {
            var path = TestUtils.GetPath();

            var table = new StreamBlockIndex(path, 512, LMDBEnvironmentFlags.WriteMap | LMDBEnvironmentFlags.NoSync, null);

            var streamId = (StreamLogId)10000;

            var series = table.GetBlockRecordSeries(streamId);

            var count = 1000;

            using (Benchmark.Run("Writes", count))
            {
                for (int i = 0; i < count; i++)
                {
                    var version = (ulong)i + 1;

                    var chunk = new StreamBlockRecord(BufferRef.Create(0, i + 1))
                    {
                        Version = version
                    };

                    if (!table.TryAddBlockRecord(streamId, chunk))
                    {
                        Assert.Fail("!table.TryAddChunkRecord(streamId, chunk)");
                    }

                    if (!table.TryGetLast(streamId, false, out var lastReady))
                    {
                        Assert.Fail("!table.TryGetLast(streamId, false, out var lastReady)");
                    }

                    if (version != lastReady.Version)
                    {
                        Assert.Fail("version != lastReady.Version");
                    }

                    chunk.BufferRef = default;

                    if (!table.UpdateBlockRecord(streamId, chunk))
                    {
                        Assert.Fail("!table.UpdateChunk(streamId, chunk)");
                    }

                    if (i + 1 != table.GetBlockCount(streamId))
                    {
                        Assert.Fail("i + 1 != table.GetChunkCount(streamId)");
                    }

                    var last = series.Last;

                    if (!last.IsPresent)
                    {
                        Assert.Fail("!last.IsPresent");
                    }

                    if (last.Present.Key != (ulong)i + 1)
                    {
                        Assert.Fail("last.Present.Key != (ulong)i + 1");
                    }

                    if (last.Present.Value.BufferRef != chunk.BufferRef)
                    {
                        Assert.Fail("last.Present.Value.BufferRef != chunk.BufferRef");
                    }

                    if (last.Present.Value.BufferRef != default)
                    {
                        Assert.Fail("last.Present.Value.BufferRef != default");
                    }
                }
            }
            Benchmark.Dump();

            table.Dispose();
        }