public int Add(T value) { var row = new MRow <T>(value); long position = default(long); using (Stream stream = new FileStream(dbName, FileMode.Append)) { using (BinaryWriter bw = new BinaryWriter(stream)) { position = stream.Position; byte[] bytes = MRowToByteArray(row); bw.Write(bytes.Length); bw.Write(bytes); } } foreach (var item in _indexes) { var index = item.Value; index.AddProperty(value, position); } return(1); }
private static byte[] MRowToByteArray(MRow <T> row) { var bf = new BinaryFormatter(); using (var ms = new MemoryStream()) { bf.Serialize(ms, row); return(ms.ToArray()); } }
private MRow <T> GetRowByPosition(long position) { using (BinaryReader reader = new BinaryReader(File.Open(dbName, FileMode.Open, FileAccess.Read))) { reader.BaseStream.Position = position; int bytesToRead = reader.ReadInt32(); byte[] v = reader.ReadBytes(bytesToRead); MRow <T> value = FromByteArray(v); return(value); } }
private IEnumerable <MRow <T> > GetAllRows() { using (BinaryReader reader = new BinaryReader(File.Open(dbName, FileMode.Open, FileAccess.Read))) { int length = (int)reader.BaseStream.Length; var rows = new List <MRow <T> >(); while (reader.BaseStream.Position != length) { int bytesToRead = reader.ReadInt32(); byte[] v = reader.ReadBytes(bytesToRead); MRow <T> value = FromByteArray(v); rows.Add(value); } return(rows); } }