public void TestReadWriteWithOffset() { MemoryStream mainStream = new MemoryStream(); MemoryStream walStream = new MemoryStream(); using (AtomicStream stream = new AtomicStream(mainStream, walStream)) { stream.Write(new byte[] { 1, 2, 3, 4, 5 }, 2, 2); stream.Commit(); byte[] buffer = new byte[4]; stream.Position = 0; Assert.That(stream.Read(buffer, 1, 3), Is.EqualTo(2)); Assert.That(buffer, Is.EqualTo(new byte[] { 0, 3, 4, 0 })); } }
public void TestOverlappingWrite() { MemoryStream mainStream = new MemoryStream(); MemoryStream walStream = new MemoryStream(); using (AtomicStream stream = new AtomicStream(mainStream, walStream)) { stream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); stream.Position = 2; stream.Write(new byte[] { 5, 6, 7, 8 }, 0, 4); stream.Commit(); byte[] buffer = new byte[7]; stream.Position = 0; Assert.That(stream.Read(buffer, 0, 7), Is.EqualTo(6)); Assert.That(buffer, Is.EqualTo(new byte[] { 1, 2, 5, 6, 7, 8, 0 })); } }
public void TestSequentialRead() { MemoryStream mainStream = new MemoryStream(); MemoryStream walStream = new MemoryStream(); using (AtomicStream stream = new AtomicStream(mainStream, walStream)) { stream.Write(new byte[] {1, 2, 3, 4, 5}, 0, 5); stream.Commit(); byte[] buffer = new byte[3]; stream.Position = 0; Assert.That(stream.Read(buffer, 0, 3), Is.EqualTo(3)); Assert.That(buffer, Is.EqualTo(new byte[] {1, 2, 3})); Assert.That(stream.Read(buffer, 0, 3), Is.EqualTo(2)); Assert.That(buffer, Is.EqualTo(new byte[] {4, 5, 3})); Assert.That(stream.Read(buffer, 0, 3), Is.EqualTo(0)); Assert.That(buffer, Is.EqualTo(new byte[] {4, 5, 3})); } }
public VirtualDictionaryContainer(string filename, int keySize, int valueSize) { this.keySize = keySize; this.valueSize = valueSize; recordsPerBlock = (blockSize - 2)/(keySize + valueSize); nonIndexedRecordsPerBlock = (blockSize - 12)/8; treeNodesPerBlock = (blockSize - 12) / 8; FileStream mainStream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); FileStream walStream = new FileStream(filename + "-wal", FileMode.Create, FileAccess.ReadWrite, FileShare.Read); if (mainStream.Length != 0) { throw new Exception("Reopening of an existing database is not supported yet."); } stream = new AtomicStream(mainStream, walStream); writer = new BinaryWriter(stream, Encoding.UTF8, true); reader = new BinaryReader(stream, Encoding.UTF8, true); long firstTreeBlockOffset = AllocateBlock(); treeBlockOffsets.Add(firstTreeBlockOffset); dirtyTreeBlocks.Add(true); int nonIndexedBlockCount = (maxNonIndexedRecordsCount + nonIndexedRecordsPerBlock - 1)/nonIndexedRecordsPerBlock; for (int i = 0; i < nonIndexedBlockCount; i++) { nonIndexedBlockOffsets.Add(AllocateBlock()); dirtyNonIndexedBlocks.Add(true); } long firstDataBlockOffset = AllocateBlock(); tree = new CompactTree(CompactTreeNode.CreateDataNode(firstDataBlockOffset)); Commit(); }
public void TestSimple() { MemoryStream mainStream = new MemoryStream(); MemoryStream walStream = new MemoryStream(); using (AtomicStream stream = new AtomicStream(mainStream, walStream)) { stream.Write(new byte[] {1}, 0, 1); stream.Commit(); Assert.That(mainStream.ToArray(), Is.EqualTo(new byte[] {1})); stream.Write(new byte[] {2}, 0, 1); stream.Commit(); Assert.That(mainStream.ToArray(), Is.EqualTo(new byte[] {1, 2})); stream.Position = 0; stream.Write(new byte[] {3}, 0, 1); stream.Commit(); Assert.That(mainStream.ToArray(), Is.EqualTo(new byte[] {3, 2})); } }