public void Test1() { File.Delete(@"C:\Temp\fileTemp.~d2i"); File.Delete(@"C:\Temp\fileTemp.d2i"); using (SimplifiedFileWriter writer = new SimplifiedFileWriter(@"C:\Temp\fileTemp.~d2i", @"C:\Temp\fileTemp.d2i", 4096, FileFlags.ManualRollover)) { using (ISupportsBinaryStream file = writer.CreateFile(SubFileName.CreateRandom())) using (BinaryStream bs = new BinaryStream(file)) { bs.Write(1); } writer.Commit(); } using (TransactionalFileStructure reader = TransactionalFileStructure.OpenFile(@"C:\Temp\fileTemp.d2i", true)) { using (SubFileStream file = reader.Snapshot.OpenFile(0)) using (BinaryStream bs = new BinaryStream(file)) { if (bs.ReadInt32() != 1) { throw new Exception(); } } } }
public void TestMultipleFiles() { Random r = new Random(1); File.Delete(@"C:\Temp\fileTemp.~d2i"); File.Delete(@"C:\Temp\fileTemp.d2i"); using (SimplifiedFileWriter writer = new SimplifiedFileWriter(@"C:\Temp\fileTemp.~d2i", @"C:\Temp\fileTemp.d2i", 4096, FileFlags.ManualRollover)) { for (int i = 0; i < 10; i++) { using (ISupportsBinaryStream file = writer.CreateFile(SubFileName.CreateRandom())) using (BinaryStream bs = new BinaryStream(file)) { bs.Write((byte)1); for (int x = 0; x < 100000; x++) { bs.Write(r.NextDouble()); } } } writer.Commit(); } r = new Random(1); using (TransactionalFileStructure reader = TransactionalFileStructure.OpenFile(@"C:\Temp\fileTemp.d2i", true)) { for (int i = 0; i < 10; i++) { using (SubFileStream file = reader.Snapshot.OpenFile(i)) using (BinaryStream bs = new BinaryStream(file)) { if (bs.ReadUInt8() != 1) { throw new Exception(); } for (int x = 0; x < 100000; x++) { if (bs.ReadDouble() != r.NextDouble()) { throw new Exception(); } } } } } }
/// <summary> /// Creates a new arhive file with the supplied data. /// </summary> /// <param name="pendingFileName"></param> /// <param name="completeFileName"></param> /// <param name="blockSize"></param> /// <param name="treeNodeType"></param> /// <param name="treeStream"></param> /// <param name="flags"></param> public static void Create(string pendingFileName, string completeFileName, int blockSize, Action <Guid> archiveIdCallback, EncodingDefinition treeNodeType, TreeStream <TKey, TValue> treeStream, params Guid[] flags) { using (SimplifiedFileWriter writer = new SimplifiedFileWriter(pendingFileName, completeFileName, blockSize, flags)) { if (archiveIdCallback != null) { archiveIdCallback(writer.ArchiveId); } using (ISupportsBinaryStream file = writer.CreateFile(GetFileName())) using (BinaryStream bs = new BinaryStream(file)) { SequentialSortedTreeWriter <TKey, TValue> .Create(bs, blockSize - 32, treeNodeType, treeStream); } writer.Commit(); } }
/// <summary> /// Creates a <see cref="BinaryStream"/> that is at position 0 of the provided stream. /// </summary> /// <param name="stream">The base stream to use.</param> /// <param name="leaveOpen">Determines if the underlying stream will automatically be /// disposed of when this class has it's dispose method called.</param> public BinaryStream(ISupportsBinaryStream stream, bool leaveOpen = true) { m_args = new BlockArguments(); m_leaveOpen = leaveOpen; m_stream = stream; FirstPosition = 0; Current = null; First = null; LastRead = null; LastWrite = null; if (stream.RemainingSupportedIoSessions < 1) { throw new Exception("Stream has run out of read sessions"); } m_mainIoSession = stream.CreateIoSession(); //if (stream.RemainingSupportedIoSessions >= 1) // m_secondaryIoSession = stream.CreateIoSession(); }
protected override void Dispose(bool disposing) { if (!m_disposed) { try { // This will be done regardless of whether the object is finalized or disposed. if (m_mainIoSession != null) { m_mainIoSession.Dispose(); } if (m_secondaryIoSession != null) { m_secondaryIoSession.Dispose(); } if (!m_leaveOpen && m_stream != null) { m_stream.Dispose(); } } finally { FirstPosition = 0; LastPosition = 0; Current = null; First = null; LastRead = null; LastWrite = null; m_mainIoSession = null; m_secondaryIoSession = null; m_stream = null; m_disposed = true; } } base.Dispose(disposing); }
public static unsafe void Test(ISupportsBinaryStream stream) { BinaryStream bs = new BinaryStream(stream); Random rand = new Random(); int seed = rand.Next(); rand = new Random(seed); byte[] data = new byte[16]; byte[] data2 = new byte[16]; fixed (byte* lp = data) { for (int x = 0; x < 10000; x++) { rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(byte*)lp); int skip = rand.Next(40) + 1; bs.Position += skip; bs.Position -= rand.Next(skip); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(sbyte*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(short*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(int*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(long*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(ushort*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(uint*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(ulong*)lp); for (int i = 0; i < 9; i++) { rand.NextBytes(data); while (rand.Next(4) < 2) bs.WriteUInt(*(ulong*)lp, i); } rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(decimal*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(*(Guid*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(NextDate(data, rand)); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(NextSingle(data, rand)); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write(NextDouble(data, rand)); rand.NextBytes(data); bool value = (*lp != 0); while (rand.Next(4) < 2) bs.Write(value); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp); data[3] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp); data[2] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp); data[1] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp); rand.NextBytes(data); while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); data[7] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); data[6] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); data[5] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); data[4] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); data[3] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); data[2] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); data[1] = 0; while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp); rand.NextBytes(data); bs.Write(data, 0, data.Length); while (rand.Next(4) < 2) { if (bs.Position > 100) { bs.Position -= 100; int insertCount = rand.Next(16) + 1; bs.InsertBytes(insertCount, 100); bs.Write(data, 0, insertCount); bs.Position -= insertCount; bs.ReadAll(data2, 0, insertCount); bs.Position -= insertCount; bs.RemoveBytes(insertCount, 100); bs.Position += 100; for (int y = 0; y < insertCount; y++) { if (data[y] != data2[y]) throw new Exception(); } } } } rand = new Random(seed); bs.Position = 0; for (int x = 0; x < 10000; x++) { rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadUInt8() != (*(byte*)lp)) throw new Exception(); int skip = rand.Next(40) + 1; bs.Position += skip; bs.Position -= rand.Next(skip); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadInt8() != (*(sbyte*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadInt16() != (*(short*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadInt32() != (*(int*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadInt64() != (*(long*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadUInt16() != (*(ushort*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadUInt32() != (*(uint*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadUInt64() != (*(ulong*)lp)) throw new Exception(); for (int i = 0; i < 9; i++) { rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadUInt(i) != (mask(i) & *(ulong*)lp)) throw new Exception(); } rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadDecimal() != (*(decimal*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadGuid() != (*(Guid*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadDateTime() != NextDate(data, rand)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadSingle() != NextSingle(data, rand)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.ReadDouble() != NextDouble(data, rand)) throw new Exception(); rand.NextBytes(data); bool b2 = (*lp != 0); while (rand.Next(4) < 2) if (bs.ReadBoolean() != b2) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception(); data[3] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception(); data[2] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception(); data[1] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception(); rand.NextBytes(data); while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); data[7] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); data[6] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); data[5] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); data[4] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); data[3] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); data[2] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); data[1] = 0; while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception(); rand.NextBytes(data); bs.ReadAll(data2, 0, 16); if (!data2.SequenceEqual<byte>(data)) throw new Exception(); while (rand.Next(4) < 2) { if (bs.Position > 100) { int insertCount = rand.Next(16); } } } } }
/// <summary> /// Creates a <see cref="BinaryStream"/> that is at position 0 of the provided stream. /// </summary> /// <param name="stream">The base stream to use.</param> /// <param name="leaveOpen">Determines if the underlying stream will automatically be /// disposed of when this class has it's dispose method called.</param> public BinaryStream(ISupportsBinaryStream stream, bool leaveOpen = true) { m_args = new BlockArguments(); m_leaveOpen = leaveOpen; m_stream = stream; FirstPosition = 0; Current = null; First = null; LastRead = null; LastWrite = null; if (stream.RemainingSupportedIoSessions < 1) throw new Exception("Stream has run out of read sessions"); m_mainIoSession = stream.CreateIoSession(); //if (stream.RemainingSupportedIoSessions >= 1) // m_secondaryIoSession = stream.CreateIoSession(); }
protected override void Dispose(bool disposing) { if (!m_disposed) { try { // This will be done regardless of whether the object is finalized or disposed. if (m_mainIoSession != null) m_mainIoSession.Dispose(); if (m_secondaryIoSession != null) m_secondaryIoSession.Dispose(); if (!m_leaveOpen && m_stream != null) m_stream.Dispose(); } finally { FirstPosition = 0; LastPosition = 0; Current = null; First = null; LastRead = null; LastWrite = null; m_mainIoSession = null; m_secondaryIoSession = null; m_stream = null; m_disposed = true; } } base.Dispose(disposing); }
public static unsafe void Test(ISupportsBinaryStream stream) { BinaryStream bs = new BinaryStream(stream); Random rand = new Random(); int seed = rand.Next(); rand = new Random(seed); byte[] data = new byte[16]; byte[] data2 = new byte[16]; fixed(byte *lp = data) { for (int x = 0; x < 10000; x++) { rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(byte *)lp); } int skip = rand.Next(40) + 1; bs.Position += skip; bs.Position -= rand.Next(skip); rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(sbyte *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(short *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(int *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(long *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(ushort *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(uint *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(ulong *)lp); } for (int i = 0; i < 9; i++) { rand.NextBytes(data); while (rand.Next(4) < 2) { bs.WriteUInt(*(ulong *)lp, i); } } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(decimal *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(*(Guid *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(NextDate(data, rand)); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(NextSingle(data, rand)); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write(NextDouble(data, rand)); } rand.NextBytes(data); bool value = (*lp != 0); while (rand.Next(4) < 2) { bs.Write(value); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write7Bit(*(uint *)lp); } data[3] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(uint *)lp); } data[2] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(uint *)lp); } data[1] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(uint *)lp); } rand.NextBytes(data); while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } data[7] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } data[6] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } data[5] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } data[4] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } data[3] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } data[2] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } data[1] = 0; while (rand.Next(4) < 2) { bs.Write7Bit(*(ulong *)lp); } rand.NextBytes(data); bs.Write(data, 0, data.Length); while (rand.Next(4) < 2) { if (bs.Position > 100) { bs.Position -= 100; int insertCount = rand.Next(16) + 1; bs.InsertBytes(insertCount, 100); bs.Write(data, 0, insertCount); bs.Position -= insertCount; bs.ReadAll(data2, 0, insertCount); bs.Position -= insertCount; bs.RemoveBytes(insertCount, 100); bs.Position += 100; for (int y = 0; y < insertCount; y++) { if (data[y] != data2[y]) { throw new Exception(); } } } } } rand = new Random(seed); bs.Position = 0; for (int x = 0; x < 10000; x++) { rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadUInt8() != (*(byte *)lp)) { throw new Exception(); } } int skip = rand.Next(40) + 1; bs.Position += skip; bs.Position -= rand.Next(skip); rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadInt8() != (*(sbyte *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadInt16() != (*(short *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadInt32() != (*(int *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadInt64() != (*(long *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadUInt16() != (*(ushort *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadUInt32() != (*(uint *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadUInt64() != (*(ulong *)lp)) { throw new Exception(); } } for (int i = 0; i < 9; i++) { rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadUInt(i) != (mask(i) & *(ulong *)lp)) { throw new Exception(); } } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadDecimal() != (*(decimal *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadGuid() != (*(Guid *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadDateTime() != NextDate(data, rand)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadSingle() != NextSingle(data, rand)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.ReadDouble() != NextDouble(data, rand)) { throw new Exception(); } } rand.NextBytes(data); bool b2 = (*lp != 0); while (rand.Next(4) < 2) { if (bs.ReadBoolean() != b2) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.Read7BitUInt32() != (*(uint *)lp)) { throw new Exception(); } } data[3] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt32() != (*(uint *)lp)) { throw new Exception(); } } data[2] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt32() != (*(uint *)lp)) { throw new Exception(); } } data[1] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt32() != (*(uint *)lp)) { throw new Exception(); } } rand.NextBytes(data); while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } data[7] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } data[6] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } data[5] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } data[4] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } data[3] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } data[2] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } data[1] = 0; while (rand.Next(4) < 2) { if (bs.Read7BitUInt64() != (*(ulong *)lp)) { throw new Exception(); } } rand.NextBytes(data); bs.ReadAll(data2, 0, 16); if (!data2.SequenceEqual <byte>(data)) { throw new Exception(); } while (rand.Next(4) < 2) { if (bs.Position > 100) { int insertCount = rand.Next(16); } } } } }