public void InsertRecord(params object[] fields) { long insertLocation; //Find available spot to place record.... if (_fileHeader.BlocksInFile == 0) { var block = RecordBlock.CreateNew(_stream, _schema, 0); _fileHeader.BlocksInFile++; WriteFileHeader(); insertLocation = block.Insert(fields); } else { var lastBlock = RecordBlock.LoadFromStream(_stream, _schema, _fileHeader.BlocksInFile - 1); if (lastBlock.BlockHeader.FreeBytes > _schema.GetRecordSize()) { insertLocation = lastBlock.Insert(fields); } else { var newBlock = RecordBlock.CreateNew(_stream, _schema, _fileHeader.BlocksInFile); insertLocation = newBlock.Insert(fields); _fileHeader.BlocksInFile++; WriteFileHeader(); } } _indexManager.AddRecord(fields, insertLocation, _schema); }
public Record[] Query(Func <Record, bool> query) { List <Record> records = new List <Record>(); for (int i = 0; i < this._fileHeader.BlocksInFile; i++) { var block = RecordBlock.LoadFromStream(_stream, _schema, i); records.AddRange(block.Query(query)); } return(records.ToArray()); }
public Record[] ReadAllRecords() { List <Record> records = new List <Record>(); for (int i = 0; i < this._fileHeader.BlocksInFile; i++) { var block = RecordBlock.LoadFromStream(_stream, _schema, i); records.AddRange(block.ReadAll()); } return(records.ToArray()); }
public Record ReadRecordFromPointer(long pointer) { int totalBlockLength = Constants.Lengths.BlockLength + Constants.Lengths.BlockHeaderLength; long pointerInBody = pointer - Constants.Lengths.FileHeaderLength; int blockNumber = (int)Math.Floor((decimal)pointerInBody / totalBlockLength); long remainingBytes = pointerInBody - (blockNumber * totalBlockLength); long trueIndex = (int)(remainingBytes / _schema.GetRecordSize()); var block = RecordBlock.LoadFromStream(_stream, _schema, blockNumber - 1); return(block.Read(trueIndex)); }
public Record ReadRecord(long index) { long totalIndex = 0; for (int i = 0; i < this._fileHeader.BlocksInFile; i++) { var block = RecordBlock.LoadFromStream(_stream, _schema, i); //the index is less than the upper bound of this block if (index < totalIndex + block.BlockHeader.RecordCount) { return(block.Read(index - totalIndex)); } totalIndex += block.BlockHeader.RecordCount; } throw new Exception(); }