Beispiel #1
0
        public int GetPrevKey()
        {
            int RecordPointer = int.MaxValue;

            #region
            try
            {
                CurrentKey--;

                if (CurrentKey < 0)
                {
                    CurrentKey = 0;
                    return(-1);
                }

                RecordPointer = RecordOffset + CurrentKey * RecordSize;

                FileStreamData.Seek(RecordPointer, 0);

                Key   = BinaryReaderData.ReadString();
                Flag  = BinaryReaderData.ReadBoolean();
                Value = BinaryReaderData.ReadString();

                return(RecordPointer);
            }
            catch (Exception e)
            {
                StringLastError = e.Message;
                return(-3);
            }
            #endregion
        }
Beispiel #2
0
        public int GetNextKey()
        {
            int RecordPointer = int.MaxValue;

            #region
            try
            {
                CurrentKey++;

                if (CurrentKey == NumberOfKeys)
                {
                    CurrentKey = (int)NumberOfKeys - 1;
                    return(-2);
                }

                RecordPointer = RecordOffset + CurrentKey * RecordSize;
                FileStreamData.Seek(RecordPointer, 0);

                Key   = BinaryReaderData.ReadString();
                Flag  = BinaryReaderData.ReadBoolean();
                Value = BinaryReaderData.ReadString();

                return(RecordPointer);
            }
            catch (Exception e)
            {
                StringLastError = e.Message;
                return(-3);
            }
            #endregion
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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);
        }