Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        private static byte[] MRowToByteArray(MRow <T> row)
        {
            var bf = new BinaryFormatter();

            using (var ms = new MemoryStream())
            {
                bf.Serialize(ms, row);
                return(ms.ToArray());
            }
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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);
            }
        }