public void Dispose()
 {
     //epochManager?.Dispose();
     //reader?.Dispose();
     _writer?.Dispose();
     _db?.Close();
     _db?.Dispose();
 }
Beispiel #2
0
        public void try_read_returns_record_when_writerchecksum_equal()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var chaserchk = new InMemoryCheckpoint(Checkpoint.Chaser, 0);
            var db        = new TFChunkDb(new TFChunkDbConfig(PathName,
                                                              new VersionedPatternFileNamingStrategy(PathName, "chunk-"),
                                                              10000,
                                                              0,
                                                              writerchk,
                                                              chaserchk,
                                                              new InMemoryCheckpoint(-1),
                                                              new InMemoryCheckpoint(-1)));

            db.Open();

            var recordToWrite = new PrepareLogRecord(logPosition: 0,
                                                     correlationId: _correlationId,
                                                     eventId: _eventId,
                                                     transactionPosition: 0,
                                                     transactionOffset: 0,
                                                     eventStreamId: "WorldEnding",
                                                     expectedVersion: 1234,
                                                     timeStamp: new DateTime(2012, 12, 21),
                                                     flags: PrepareFlags.None,
                                                     eventType: "type",
                                                     data: new byte[] { 1, 2, 3, 4, 5 },
                                                     metadata: new byte[] { 7, 17 });
            var writer = new TFChunkWriter(db);

            writer.Open();
            long pos;

            Assert.IsTrue(writer.Write(recordToWrite, out pos));
            writer.Close();

            writerchk.Write(recordToWrite.GetSizeWithLengthPrefixAndSuffix());

            var chaser = new TFChunkChaser(db, writerchk, chaserchk);

            chaser.Open();

            LogRecord record;
            var       readRecord = chaser.TryReadNext(out record);

            chaser.Close();

            Assert.IsTrue(readRecord);
            Assert.AreEqual(record.GetSizeWithLengthPrefixAndSuffix(), chaserchk.Read());
            Assert.AreEqual(recordToWrite, record);

            db.Close();
        }
        public override Task TestFixtureTearDown()
        {
            _logFormat?.Dispose();
            ReadIndex.Close();
            ReadIndex.Dispose();

            _tableIndex.Close();

            _db.Close();
            _db.Dispose();

            return(base.TestFixtureTearDown());
        }
        public void try_read_returns_record_when_record_bigger_than_internal_buffer()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var chaserchk = new InMemoryCheckpoint(Checkpoint.Chaser, 0);

            var db = new TFChunkDb(TFChunkHelper.CreateDbConfig(PathName, writerchk, chaserchk));

            db.Open();

            var recordFactory = LogFormatHelper <TLogFormat, TStreamId> .RecordFactory;
            var streamId      = LogFormatHelper <TLogFormat, TStreamId> .StreamId;

            var recordToWrite = LogRecord.Prepare(
                factory: recordFactory,
                logPosition: 0,
                correlationId: _correlationId,
                eventId: _eventId,
                transactionPos: 0,
                transactionOffset: 0,
                eventStreamId: streamId,
                expectedVersion: 1234,
                timeStamp: new DateTime(2012, 12, 21),
                flags: PrepareFlags.None,
                eventType: "type",
                data: new byte[9000],
                metadata: new byte[] { 7, 17 });
            var writer = new TFChunkWriter(db);

            writer.Open();
            long pos;

            Assert.IsTrue(writer.Write(recordToWrite, out pos));
            writer.Close();

            writerchk.Write(recordToWrite.GetSizeWithLengthPrefixAndSuffix());

            var reader = new TFChunkChaser(db, writerchk, chaserchk, false);

            reader.Open();

            ILogRecord record;
            var        readRecord = reader.TryReadNext(out record);

            reader.Close();

            Assert.IsTrue(readRecord);
            Assert.AreEqual(record.GetSizeWithLengthPrefixAndSuffix(), chaserchk.Read());
            Assert.AreEqual(recordToWrite, record);

            db.Close();
        }
Beispiel #5
0
        public void try_read_returns_record_when_writerchecksum_ahead()
        {
            _logFormat.StreamNameIndex.GetOrAddId("WorldEnding", out var streamId, out _, out _);

            var recordToWrite = LogRecord.Prepare(
                factory: _logFormat.RecordFactory,
                logPosition: 0,
                correlationId: _correlationId,
                eventId: _eventId,
                transactionPos: 0,
                transactionOffset: 0,
                eventStreamId: streamId,
                expectedVersion: 1234,
                timeStamp: new DateTime(2012, 12, 21),
                flags: PrepareFlags.None,
                eventType: "type",
                data: new byte[] { 1, 2, 3, 4, 5 },
                metadata: new byte[] { 7, 17 });

            using (var fs = new FileStream(GetFilePathFor("chunk-000000.000000"), FileMode.CreateNew,
                                           FileAccess.Write)) {
                fs.SetLength(ChunkHeader.Size + ChunkFooter.Size + 10000);
                var chunkHeader = new ChunkHeader(TFChunk.CurrentChunkVersion, 10000, 0, 0, false, Guid.NewGuid())
                                  .AsByteArray();
                var writer = new BinaryWriter(fs);
                writer.Write(chunkHeader);
                recordToWrite.WriteWithLengthPrefixAndSuffixTo(writer);
                fs.Close();
            }

            var writerchk = new InMemoryCheckpoint(recordToWrite.GetSizeWithLengthPrefixAndSuffix() + 16);
            var chaserchk = new InMemoryCheckpoint(Checkpoint.Chaser, 0);
            var db        = new TFChunkDb(TFChunkHelper.CreateDbConfig(PathName, writerchk, chaserchk));

            db.Open();

            var chaser = new TFChunkChaser(db, writerchk, chaserchk, false);

            chaser.Open();

            ILogRecord record;
            var        recordRead = chaser.TryReadNext(out record);

            chaser.Close();

            Assert.AreEqual(record.GetSizeWithLengthPrefixAndSuffix(), chaserchk.Read());
            Assert.IsTrue(recordRead);
            Assert.AreEqual(recordToWrite, record);

            db.Close();
        }
Beispiel #6
0
        public void try_read_returns_record_when_writerchecksum_ahead()
        {
            var recordToWrite = new PrepareLogRecord(logPosition: 0,
                                                     correlationId: _correlationId,
                                                     eventId: _eventId,
                                                     transactionPosition: 0,
                                                     transactionOffset: 0,
                                                     eventStreamId: "WorldEnding",
                                                     expectedVersion: 1234,
                                                     timeStamp: new DateTime(2012, 12, 21),
                                                     flags: PrepareFlags.None,
                                                     eventType: "type",
                                                     data: new byte[] { 1, 2, 3, 4, 5 },
                                                     metadata: new byte[] { 7, 17 });

            using (var fs = new FileStream(Path.Combine(PathName, "prefix.tf0"), FileMode.CreateNew, FileAccess.Write))
            {
                fs.SetLength(ChunkHeader.Size + ChunkFooter.Size + 10000);
                var chunkHeader = new ChunkHeader(TFChunk.CurrentChunkVersion, 10000, 0, 0, 0).AsByteArray();
                var writer      = new BinaryWriter(fs);
                writer.Write(chunkHeader);
                recordToWrite.WriteWithLengthPrefixAndSuffixTo(writer);
                fs.Close();
            }

            var writerchk = new InMemoryCheckpoint(128);
            var chaserchk = new InMemoryCheckpoint(Checkpoint.Chaser, 0);
            var db        = new TFChunkDb(new TFChunkDbConfig(PathName,
                                                              new PrefixFileNamingStrategy(PathName, "prefix.tf"),
                                                              10000,
                                                              0,
                                                              writerchk,
                                                              new[] { chaserchk }));

            db.OpenVerifyAndClean();

            var chaser = new TFChunkChaser(db, writerchk, chaserchk);

            chaser.Open();

            LogRecord record;
            var       recordRead = chaser.TryReadNext(out record);

            chaser.Close();

            Assert.AreEqual(record.GetSizeWithLengthPrefixAndSuffix(), chaserchk.Read());
            Assert.IsTrue(recordRead);
            Assert.AreEqual(recordToWrite, record);

            db.Close();
        }
Beispiel #7
0
        public void try_read_returns_false_when_writer_checksum_is_zero()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var chaserchk = new InMemoryCheckpoint(0);
            var db        = new TFChunkDb(TFChunkHelper.CreateDbConfig(PathName, writerchk, chaserchk));

            db.Open();

            var reader = new TFChunkReader(db, writerchk, 0);

            Assert.IsFalse(reader.TryReadNext().Success);

            db.Close();
        }
Beispiel #8
0
        public override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();
            string dbPath = Path.Combine(PathName, string.Format("mini-node-db-{0}", Guid.NewGuid()));

            Bus          = new InMemoryBus("bus");
            IODispatcher = new IODispatcher(Bus, new PublishEnvelope(Bus));

            if (!Directory.Exists(dbPath))
            {
                Directory.CreateDirectory(dbPath);
            }

            var writerCheckFilename = Path.Combine(dbPath, Checkpoint.Writer + ".chk");
            var chaserCheckFilename = Path.Combine(dbPath, Checkpoint.Chaser + ".chk");

            if (Runtime.IsMono)
            {
                WriterCheckpoint = new FileCheckpoint(writerCheckFilename, Checkpoint.Writer, cached: true);
                ChaserCheckpoint = new FileCheckpoint(chaserCheckFilename, Checkpoint.Chaser, cached: true);
            }
            else
            {
                WriterCheckpoint = new MemoryMappedFileCheckpoint(writerCheckFilename, Checkpoint.Writer, cached: true);
                ChaserCheckpoint = new MemoryMappedFileCheckpoint(chaserCheckFilename, Checkpoint.Chaser, cached: true);
            }

            Db = new TFChunkDb(TFChunkHelper.CreateDbConfig(dbPath, WriterCheckpoint, ChaserCheckpoint,
                                                            TFConsts.ChunkSize));
            Db.Open();

            // create DB
            Writer = new TFChunkWriter(Db);
            Writer.Open();
            WriteTestScenario();

            Writer.Close();
            Writer = null;
            WriterCheckpoint.Flush();
            ChaserCheckpoint.Write(WriterCheckpoint.Read());
            ChaserCheckpoint.Flush();
            Db.Close();

            // start node with our created DB
            Node = new MiniNode(PathName, inMemDb: false, dbPath: dbPath);
            Node.Start();

            Given();
        }
Beispiel #9
0
        public void try_read_returns_record_when_writerchecksum_equal()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var chaserchk = new InMemoryCheckpoint(Checkpoint.Chaser, 0);
            var db        = new TFChunkDb(TFChunkHelper.CreateDbConfig(PathName, writerchk, chaserchk));

            db.Open();

            _logFormat.StreamNameIndex.GetOrAddId("WorldEnding", out var streamId, out _, out _);

            var recordToWrite = LogRecord.Prepare(
                factory: _logFormat.RecordFactory,
                logPosition: 0,
                correlationId: _correlationId,
                eventId: _eventId,
                transactionPos: 0,
                transactionOffset: 0,
                eventStreamId: streamId,
                expectedVersion: 1234,
                timeStamp: new DateTime(2012, 12, 21),
                flags: PrepareFlags.None,
                eventType: "type",
                data: new byte[] { 1, 2, 3, 4, 5 },
                metadata: new byte[] { 7, 17 });
            var writer = new TFChunkWriter(db);

            writer.Open();
            long pos;

            Assert.IsTrue(writer.Write(recordToWrite, out pos));
            writer.Close();

            writerchk.Write(recordToWrite.GetSizeWithLengthPrefixAndSuffix());

            var chaser = new TFChunkChaser(db, writerchk, chaserchk, false);

            chaser.Open();

            ILogRecord record;
            var        readRecord = chaser.TryReadNext(out record);

            chaser.Close();

            Assert.IsTrue(readRecord);
            Assert.AreEqual(record.GetSizeWithLengthPrefixAndSuffix(), chaserchk.Read());
            Assert.AreEqual(recordToWrite, record);

            db.Close();
        }
Beispiel #10
0
        public void try_read_returns_record_when_record_bigger_than_internal_buffer()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var chaserchk = new InMemoryCheckpoint(Checkpoint.Chaser, 0);
            var db        = new TFChunkDb(new TFChunkDbConfig(PathName,
                                                              new PrefixFileNamingStrategy(PathName, "prefix.tf"),
                                                              10000,
                                                              0,
                                                              writerchk,
                                                              new[] { chaserchk }));

            db.OpenVerifyAndClean();

            var recordToWrite = new PrepareLogRecord(logPosition: 0,
                                                     correlationId: _correlationId,
                                                     eventId: _eventId,
                                                     transactionPosition: 0,
                                                     eventStreamId: "WorldEnding",
                                                     expectedVersion: 1234,
                                                     timeStamp: new DateTime(2012, 12, 21),
                                                     flags: PrepareFlags.None,
                                                     eventType: "type",
                                                     data: new byte[9000],
                                                     metadata: new byte[] { 7, 17 });
            var writer = new TFChunkWriter(db);

            writer.Open();
            long pos;

            Assert.IsTrue(writer.Write(recordToWrite, out pos));
            writer.Close();

            writerchk.Write(recordToWrite.GetSizeWithLengthPrefix());

            var reader = new TFChunkChaser(db, writerchk, chaserchk);

            reader.Open();

            LogRecord record;
            var       readRecord = reader.TryReadNext(out record);

            reader.Close();

            Assert.IsTrue(readRecord);
            Assert.AreEqual(record.GetSizeWithLengthPrefix(), chaserchk.Read());
            Assert.AreEqual(recordToWrite, record);

            db.Close();
        }
        public override async Task TestFixtureSetUp()
        {
            await base.TestFixtureSetUp();

            string dbPath = Path.Combine(PathName, string.Format("mini-node-db-{0}", Guid.NewGuid()));

            Bus          = new InMemoryBus("bus");
            IODispatcher = new IODispatcher(Bus, new PublishEnvelope(Bus));

            if (!Directory.Exists(dbPath))
            {
                Directory.CreateDirectory(dbPath);
            }

            var writerCheckFilename = Path.Combine(dbPath, Checkpoint.Writer + ".chk");
            var chaserCheckFilename = Path.Combine(dbPath, Checkpoint.Chaser + ".chk");

            WriterCheckpoint = new MemoryMappedFileCheckpoint(writerCheckFilename, Checkpoint.Writer, cached: true);
            ChaserCheckpoint = new MemoryMappedFileCheckpoint(chaserCheckFilename, Checkpoint.Chaser, cached: true);

            Db = new TFChunkDb(TFChunkHelper.CreateDbConfig(dbPath, WriterCheckpoint, ChaserCheckpoint, TFConsts.ChunkSize));
            Db.Open();

            // create DB
            Writer = new TFChunkWriter(Db);
            Writer.Open();
            WriteTestScenario();

            Writer.Close();
            Writer = null;
            WriterCheckpoint.Flush();
            ChaserCheckpoint.Write(WriterCheckpoint.Read());
            ChaserCheckpoint.Flush();
            Db.Close();

            // start node with our created DB
            Node = new MiniNode(PathName, inMemDb: false, dbPath: dbPath);
            await Node.Start();

            try {
                await Given().WithTimeout();
            } catch (Exception ex) {
                throw new Exception("Given Failed", ex);
            }
        }
Beispiel #12
0
        public void try_read_returns_false_when_writer_checksum_is_zero()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var db        = new TFChunkDb(new TFChunkDbConfig(PathName,
                                                              new VersionedPatternFileNamingStrategy(PathName, "chunk-"),
                                                              10000,
                                                              0,
                                                              writerchk,
                                                              new InMemoryCheckpoint()));

            db.OpenVerifyAndClean();

            var       reader = new TFChunkSequentialReader(db, writerchk, 0);
            LogRecord record;

            Assert.IsFalse(reader.TryReadNext(out record));

            db.Close();
        }
Beispiel #13
0
        public void try_read_returns_false_when_writer_checksum_is_zero()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var db        = new TFChunkDb(new TFChunkDbConfig(PathName,
                                                              new VersionedPatternFileNamingStrategy(PathName, "chunk-"),
                                                              10000,
                                                              0,
                                                              writerchk,
                                                              new InMemoryCheckpoint(),
                                                              new InMemoryCheckpoint(-1),
                                                              new InMemoryCheckpoint(-1)));

            db.Open();

            var reader = new TFChunkReader(db, writerchk, 0);

            Assert.IsFalse(reader.TryReadNext().Success);

            db.Close();
        }
Beispiel #14
0
        private static string CreateExistingDatabase(Action <ICheckpoint, TFChunkWriter> runScenario)
        {
            var databasePath = $"{nameof(StreamRevisionGreaterThanIntMaxValueFixture)}-{Guid.NewGuid():n}";

            var bus          = new InMemoryBus("bus");
            var ioDispatcher = new IODispatcher(bus, new PublishEnvelope(bus));

            if (!Directory.Exists(databasePath))
            {
                Directory.CreateDirectory(databasePath);
            }

            var writerCheckFilename = Path.Combine(databasePath, Checkpoint.Writer + ".chk");
            var chaserCheckFilename = Path.Combine(databasePath, Checkpoint.Chaser + ".chk");
            var writerCheckpoint    = Runtime.IsMono
                                ? (ICheckpoint) new FileCheckpoint(writerCheckFilename, Checkpoint.Writer, cached: true)
                                : new MemoryMappedFileCheckpoint(writerCheckFilename, Checkpoint.Writer, cached: true);
            var chaserCheckpoint = Runtime.IsMono
                                ? (ICheckpoint) new FileCheckpoint(chaserCheckFilename, Checkpoint.Chaser, cached: true)
                                : new MemoryMappedFileCheckpoint(chaserCheckFilename, Checkpoint.Chaser, cached: true);

            var db = new TFChunkDb(CreateDbConfig(databasePath, writerCheckpoint, chaserCheckpoint,
                                                  TFConsts.ChunkSize));

            db.Open();

            // create DB
            var writer = new TFChunkWriter(db);

            writer.Open();
            runScenario(writerCheckpoint, writer);

            writer.Close();
            writerCheckpoint.Flush();
            chaserCheckpoint.Write(writerCheckpoint.Read());
            chaserCheckpoint.Flush();
            db.Close();

            return(databasePath);
        }
Beispiel #15
0
        public void try_read_does_not_cache_anything_and_returns_record_once_it_is_written_later()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var db        = new TFChunkDb(new TFChunkDbConfig(PathName,
                                                              new VersionedPatternFileNamingStrategy(PathName, "chunk-"),
                                                              10000,
                                                              0,
                                                              writerchk,
                                                              new InMemoryCheckpoint(),
                                                              new InMemoryCheckpoint(-1),
                                                              new InMemoryCheckpoint(-1)));

            db.Open();

            var writer = new TFChunkWriter(db);

            writer.Open();

            var reader = new TFChunkReader(db, writerchk, 0);

            Assert.IsFalse(reader.TryReadNext().Success);

            var  rec = LogRecord.SingleWrite(0, Guid.NewGuid(), Guid.NewGuid(), "ES", -1, "ET", new byte[] { 7 }, null);
            long tmp;

            Assert.IsTrue(writer.Write(rec, out tmp));
            writer.Flush();
            writer.Close();

            var res = reader.TryReadNext();

            Assert.IsTrue(res.Success);
            Assert.AreEqual(rec, res.LogRecord);

            db.Close();
        }
Beispiel #16
0
 public void Teardown()
 {
     _writer.Close();
     _db.Close();
 }