public int InsertKey(string P_key, String P_value, bool P_flag) { if (P_key.Length > KeySize) { return(-50); } if (P_value.Length > ValueSize) { return(-51); } try { LastPosition = (uint)RecordOffset + NumberOfKeys * RecordSize; if ((IndexError = CreateIndex(P_key, P_value)) < 0) { if (IndexError == -4) { return(1); } return(IndexError); } try { if ((FileStreamData.Length - LastPosition) < 256) { FileStreamData.SetLength(FileStreamData.Length + BlockSize); } FileStreamData.Seek(LastPosition, 0); BinaryWriterData.Write(P_key); BinaryWriterData.Write(P_flag); BinaryWriterData.Write(P_value); NumberOfKeys++; FileStreamData.Seek(0, 0); BinaryWriterData.Write(NumberOfKeys); BinaryWriterData.Flush(); } catch (Exception e) { StringLastError = e.Message; return(-1); } } catch (IOException e) { StringLastError = e.Message; return(-2); } return(1); }
public uint DeleteKey(string P_key) { uint RecordPointer = uint.MaxValue, IndexPointer = uint.MaxValue; KeyBytes = ASCIIEncoding.UTF8.GetBytes(P_key); ByteIndex = 0; IndexOffset = 0; for (ByteIndex = 0; ByteIndex < KeyBytes.Length; ByteIndex++) { #region count++; IndexPosition = (uint)(IndexOffset + (KeyBytes[ByteIndex] - 32) * 8); try { FileStreamIdx.Seek(IndexPosition, 0); RecordPointer = BinaryReaderIdx.ReadUInt32(); IndexPointer = BinaryReaderIdx.ReadUInt32(); if (IndexPointer == uint.MaxValue) { return(uint.MaxValue); } FileStreamData.Seek(RecordPointer, 0); Key = BinaryReaderData.ReadString(); if (Key.Equals(P_key)) { if (BinaryReaderData.PeekChar() == 0) { BinaryWriterData.Write(true); } return(RecordPointer); } else { IndexOffset = IndexPointer; } } catch (Exception e) { StringLastError = e.Message; return(uint.MaxValue);; } #endregion } return(uint.MaxValue); }
public int CreateIndex(string P_key, string P_value) { // Key Should contain ASCII 32-122 only KeyBytes = ASCIIEncoding.UTF8.GetBytes(P_key); uint RecordPointer = uint.MaxValue, NewRecordPointer = uint.MaxValue, IndexPointer = uint.MaxValue, NewIndexOffset = 0; String LastKey = ""; NewRecordPointer = (uint)LastPosition; /// Start of Next Key IndexOffset = 0; ByteIndex = 0; do { IndexPosition = (uint)(IndexOffset + (KeyBytes[ByteIndex] - 32) * 8); count++; try // Read next Branch (ASCII 32-122) { if (KeyBytes[ByteIndex] < 32 || KeyBytes[ByteIndex] > 122) { return(-52); // Validate Character Range } FileStreamIdx.Seek(IndexPosition, 0); RecordPointer = BinaryReaderIdx.ReadUInt32(); IndexPointer = BinaryReaderIdx.ReadUInt32(); } catch (Exception e) { StringLastError = e.Message; return(-1); } if (IndexPointer == uint.MaxValue) { try { FileStreamIdx.Seek(IndexPosition, 0); BinaryWriterIdx.Write(NewRecordPointer); BinaryWriterIdx.Write(IndexPosition); FileStreamIdx.Flush(); return(1); // Inserted new Branch successfully } catch (Exception e) { StringLastError = e.Message; return(-1); } } else { try { FileStreamData.Seek(RecordPointer, 0); LastKey = BinaryReaderData.ReadString(); LastKeyBytes = ASCIIEncoding.UTF8.GetBytes(LastKey); } catch (Exception e) { StringLastError = e.Message; return(-10); } Difference = GetDif(ref LastKeyBytes, ref KeyBytes); if (Difference == 0) { if (BinaryReaderData.PeekChar() == 0) { return(-3); } else { FileStreamData.Seek(RecordPointer, 0); BinaryWriterData.Write(P_key); BinaryWriterData.Write(false); BinaryWriterData.Write(P_value); BinaryWriterData.Flush(); return(-4); /// if the record flag is deleted, update record. } } else { if (Difference > 0) { if (IndexPointer == IndexPosition) { try { NewIndexOffset = (uint)FileStreamIdx.Length; FileStreamIdx.Seek(IndexPosition + 4, 0); BinaryWriterIdx.Write(NewIndexOffset); ADDIndexBlock(); IndexPosition = (uint)(NewIndexOffset + (KeyBytes[ByteIndex + 1] - 32) * 8); FileStreamIdx.Seek(IndexPosition, 0); BinaryWriterIdx.Write(NewRecordPointer); BinaryWriterIdx.Write(IndexPosition); FileStreamIdx.Flush(); return(1); // Inserted new key successfully } catch (Exception e) { StringLastError = e.Message; return(-16); } } else { IndexOffset = IndexPointer; } } else { if (IndexPointer == IndexPosition) { try { NewIndexOffset = (uint)FileStreamIdx.Length; FileStreamIdx.Seek(IndexPosition, 0); BinaryWriterIdx.Write(NewRecordPointer); BinaryWriterIdx.Write(NewIndexOffset); ADDIndexBlock(); NewRecordPointer = RecordPointer; KeyBytes = ASCIIEncoding.UTF8.GetBytes(LastKey); P_key = LastKey; IndexOffset = NewIndexOffset; IndexPosition = (uint)(NewIndexOffset + (KeyBytes[ByteIndex + 1] - 32) * 8); FileStreamIdx.Seek(IndexPosition, 0); BinaryWriterIdx.Write(NewRecordPointer); BinaryWriterIdx.Write(IndexPosition); FileStreamIdx.Flush(); return(1); // Inserted new key successfully } catch (Exception e) { StringLastError = e.Message; return(-17); } } else { try { FileStreamIdx.Seek(IndexPosition, 0); BinaryWriterIdx.Write(NewRecordPointer); NewRecordPointer = RecordPointer; KeyBytes = ASCIIEncoding.UTF8.GetBytes(LastKey); P_key = LastKey; FileStreamIdx.Flush(); IndexOffset = IndexPointer; } catch (Exception e) { StringLastError = e.Message; return(-18); } } } } } ByteIndex++; } while (ByteIndex < KeyBytes.Length); return(-20); }