void ReadHeader() { byte[] header = new byte[this.headersize]; this.fromfile.Seek(this.seekStart, System.IO.SeekOrigin.Begin); this.fromfile.Read(header, 0, this.headersize); int index = 0; // check prefix foreach (byte b in HEADERPREFIX) { if (header[index] != b) { throw new LinkedFileException("invalid header prefix"); } index++; } // skip version (for now) index++; // read buffersize this.buffersize = BufferFile.Retrieve(header, index); index += BufferFile.INTSTORAGE; this.FreeListHead = BufferFile.RetrieveLong(header, index); this.SanityCheck(); this.headerDirty = false; }
public void Load(byte[] serialization) { int index = 0; int byteCount = serialization.Length; if (this.values.Count != 0 || this.keys.Count != 0) { throw new BplusTreeException("load into nonempty xBucket not permitted"); } while (index < byteCount) { // get key prefix and key int keylength = BufferFile.Retrieve(serialization, index); index += BufferFile.INTSTORAGE; byte[] keybytes = new byte[keylength]; Array.Copy(serialization, index, keybytes, 0, keylength); string keystring = BplusTree.BytesToString(keybytes); index += keylength; // get value prefix and value int valuelength = BufferFile.Retrieve(serialization, index); index += BufferFile.INTSTORAGE; byte[] valuebytes = new byte[valuelength]; Array.Copy(serialization, index, valuebytes, 0, valuelength); // record new key and value this.keys.Add(keystring); this.values.Add(valuebytes); index += valuelength; } if (index != byteCount) { throw new BplusTreeException("bad byte count in serialization " + byteCount); } }
public byte[] GetChunk(long headBufferNumber) { // get the head, interpret the length byte buffertype; long nextBufferNumber; byte[] buffer = this.ParseBuffer(headBufferNumber, out buffertype, out nextBufferNumber); int length = BufferFile.Retrieve(buffer, 0); if (length < 0) { throw new LinkedFileException("negative length block? must be garbage: " + length); } if (buffertype != HEAD) { throw new LinkedFileException("first buffer not marked HEAD"); } byte[] result = new byte[length]; // read in the data from the first buffer int firstLength = this.buffersize - BufferFile.INTSTORAGE; if (firstLength > length) { firstLength = length; } Array.Copy(buffer, BufferFile.INTSTORAGE, result, 0, firstLength); int stored = firstLength; while (stored < length) { // get the next buffer long thisBufferNumber = nextBufferNumber; buffer = this.ParseBuffer(thisBufferNumber, out buffertype, out nextBufferNumber); int nextLength = this.buffersize; if (length - stored < nextLength) { nextLength = length - stored; } Array.Copy(buffer, 0, result, stored, nextLength); stored += nextLength; } return(result); }