Example #1
0
        internal DataKey GetKey(DateTime dateTime, DataKey key = null, IndexOption option = IndexOption.Null)
        {
            lock (Sync)
            {
                if (Count == 0 || dateTime > DateTime2)
                {
                    Console.WriteLine($"DataSeries::GetKey dateTime is out of range : {dateTime}");
                    return(null);
                }
                if (key == null)
                {
                    key = this.readKey;
                }

                DataKey @class = null;
                DataKey class2 = null;
                if (dateTime <= DateTime1)
                {
                    return(GetFirstKey());
                }

                if (key != null)
                {
                    if (key.dateTime1 <= dateTime && dateTime <= key.dateTime2)
                    {
                        return(key);
                    }

                    if (dateTime > key.dateTime2)
                    {
                        class2 = key;
                        @class = GetNextKey(class2);
                    }
                }
                if (@class == null)
                {
                    @class = GetFirstKey();
                }

                while (option == IndexOption.Null || class2 == null || !(dateTime > class2.dateTime2) || !(dateTime < @class.dateTime1))
                {
                    if (@class.dateTime1 <= dateTime && dateTime <= @class.dateTime2)
                    {
                        return(@class);
                    }
                    class2 = @class;
                    @class = GetNextKey(class2);
                }
                if (option == IndexOption.Next)
                {
                    return(@class);
                }
                return(class2);
            }
        }
Example #2
0
        protected void ReadFree()
        {
            if (this.freeLength == 0)
            {
                return;
            }

            var key = ReadKey(this.freePosition, this.freeLength);

            this.free        = ((FreeKeyList)key.GetObject()).Keys;
            this.freeListKey = key;
        }
Example #3
0
 public void Dump()
 {
     lock (Sync)
     {
         Console.WriteLine("Data series " + Name);
         Console.WriteLine("Count = " + Count);
         Console.WriteLine("Position1 = " + this.position1);
         Console.WriteLine("Position2 = " + this.position2);
         Console.WriteLine("DateTime1 = " + DateTime1.Ticks);
         Console.WriteLine("DateTime2 = " + DateTime2.Ticks);
         Console.WriteLine("Buffer count = " + this.bufferCount);
         Console.WriteLine();
         Console.WriteLine("Keys in cache:");
         Console.WriteLine();
         if (this.cache != null)
         {
             for (int i = 0; i < this.bufferCount; i++)
             {
                 if (this.cache[i] != null)
                 {
                     Console.WriteLine(this.cache[i]);
                 }
             }
         }
         Console.WriteLine();
         Console.WriteLine("Keys on disk:");
         Console.WriteLine();
         if (this.position1 != -1)
         {
             var key = ReadKey(this.position1);
             while (true)
             {
                 Console.WriteLine(key);
                 if (key.next == -1)
                 {
                     break;
                 }
                 key = ReadKey(key.next);
             }
         }
         Console.WriteLine();
         if (this.writeKey != null)
         {
             Console.WriteLine("Write Key : " + this.changed);
         }
         else
         {
             Console.WriteLine("Write Key : null");
         }
         Console.WriteLine("\nEnd dump\n");
     }
 }
Example #4
0
        public static ObjectKeyList FromReader(BinaryReader reader, byte version)
        {
            var keys  = new Dictionary <string, ObjectKey>();
            int count = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                var key = new ObjectKey();
                key.Read(reader, true);
                keys.Add(key.Name, key);
            }
            return(new ObjectKeyList(keys));
        }
Example #5
0
        private void WriteFree()
        {
            if (this.freeListKey != null)
            {
                DeleteKey(this.freeListKey, false, true);
            }

            this.freeListKey = new ObjectKey(this, "FreeKeys", new FreeKeyList(this.free));
            this.freeListKey.CompressionLevel = 0;
            WriteKey(this.freeListKey);
            this.freePosition = this.freeListKey.position;
            this.freeLength   = this.freeListKey.keyLength + this.freeListKey.objLength;
        }
Example #6
0
        private void WriteKeys()
        {
            if (this.keysListKey != null)
            {
                DeleteKey(this.keysListKey, false, true);
            }

            this.keysListKey = new ObjectKey(this, "ObjectKeys", new ObjectKeyList(Keys));
            this.keysListKey.CompressionLevel = 0;
            WriteKey(this.keysListKey);
            this.keysPosition = this.keysListKey.position;
            this.keysLength   = this.keysListKey.keyLength + this.keysListKey.objLength;
        }
Example #7
0
 internal ObjectKey ReadKey(long position, int length)
 {
     lock (Sync)
     {
         var buffer = new byte[length];
         ReadBuffer(buffer, position, buffer.Length);
         var key = new ObjectKey(this, null, null);
         using (var ms = new MemoryStream(buffer))
             using (var rdr = new BinaryReader(ms))
                 key.Read(rdr, true);
         key.position = position;
         return(key);
     }
 }
Example #8
0
        protected void ReadKeys()
        {
            if (this.keysLength == 0)
            {
                return;
            }

            var key = ReadKey(this.keysPosition, this.keysLength);

            Keys = ((ObjectKeyList)key.GetObject()).Keys;
            foreach (var k in Keys.Values)
            {
                k.Init(this);
            }
            this.keysListKey = key;
        }
Example #9
0
        private IdArray <DataKey> ReadCache()
        {
            this.cacheKey = this.file.ReadKey(this.cachePosition, ObjectKey.EMPTYNAME_KEY_SIZE);
            var dataKeys = ((DataKeyIdArray)this.cacheKey.GetObject()).Keys;

            for (int i = 0; i < dataKeys.Size; i++)
            {
                var dk = dataKeys[i];
                if (dk != null)
                {
                    dk.file   = this.file;
                    dk.number = i;
                }
            }
            return(dataKeys);
        }
Example #10
0
        internal void Init(DataFile file, ObjectKey key)
        {
            lock (Sync)
            {
                this.file             = file;
                this.key              = key;
                key.CompressionLevel  = 0;
                key.CompressionMethod = 0;

                // Init dataKey list
                if (this.cachePosition == -1)
                {
                    this.cache    = new IdArray <DataKey>(Math.Max(4096, this.bufferCount));
                    this.cacheKey = new ObjectKey(file, "", new DataKeyIdArray(this.cache));
                }
            }
        }
Example #11
0
        internal ObjectKey ReadKey(long position)
        {
            lock (Sync)
            {
                var buffer = new byte[ObjectKey.HEADER_SIZE];
                ReadBuffer(buffer, position, buffer.Length);
                ObjectKey key;
                string    text;

                using (var ms = new MemoryStream(buffer))
                {
                    using (var rdr = new BinaryReader(ms))
                    {
                        text = rdr.ReadString();
                        key  = new ObjectKey(this, null, null)
                        {
                            Label = text
                        };
                        key.ReadHeader(rdr);
                    }
                }

                buffer = new byte[key.keyLength];
                ReadBuffer(buffer, position, buffer.Length);
                if (text.StartsWith("OK"))
                {
                    key = new ObjectKey(this, null, null);
                }
                else
                {
                    if (!text.StartsWith("DK"))
                    {
                        Console.WriteLine($"DataFile::ReadKey This is not object or data key : {text}");
                        return(null);
                    }
                    key = new DataKey(this, null, -1, -1);
                }

                using (var ms = new MemoryStream(buffer))
                    using (var rdr = new BinaryReader(ms))
                        key.Read(rdr, true);
                return(key);
            }
        }
Example #12
0
 internal void WriteKey(ObjectKey key)
 {
     lock (Sync)
     {
         this.mstream.SetLength(0);
         key.Write(this.writer);
         if (key.position != -1)
         {
             if (this.mstream.Length > key.recLength)
             {
                 DeleteKey(key, false, true);
                 key.recLength = (int)this.mstream.Length;
                 var fKey = key == this.freeListKey ? GetFree(key.keyLength + key.objLength - FreeKey.HEADER_SIZE) : GetFree(key.keyLength + key.objLength);
                 if (fKey != null)
                 {
                     key.position  = fKey.position;
                     key.recLength = fKey.length;
                     this.free.Remove(fKey);
                     this.freeNumber--;
                     if (key == this.freeListKey)
                     {
                         this.mstream.SetLength(0);
                         key.Write(this.writer);
                     }
                 }
                 else
                 {
                     key.position     = this.stream.Length;
                     this.endPosition = key.position;
                 }
             }
         }
         else
         {
             key.position     = this.stream.Length;
             this.endPosition = key.position;
         }
         var buf = this.mstream.ToArray();
         WriteBuffer(buf, key.position, buf.Length);
         key.changed     = false;
         this.isModified = true;
     }
 }
Example #13
0
 internal void DeleteKey(ObjectKey key, bool remove = true, bool recycle = true)
 {
     lock (Sync)
     {
         key.deleted = true;
         WriteBuffer(new byte[] { 1 }, key.position + ObjectKey.LABEL_SIZE, 1);
         if (remove)
         {
             Keys.Remove(key.Name);
             this.keysNumber--;
         }
         if (recycle)
         {
             this.free.Add(new FreeKey(key));
             this.free.Sort();
             this.freeNumber++;
         }
         this.isModified = true;
     }
 }
Example #14
0
 // TODO: rewrite it!
 internal DataKey GetKey(long index, DataKey key = null)
 {
     lock (Sync)
     {
         if (0 <= index && index < Count)
         {
             if (key == null)
             {
                 key = this.readKey;
             }
             DataKey @class = null;
             if (key != null)
             {
                 if (key.index1 <= index && index <= key.index2)
                 {
                     return(key);
                 }
                 if (index > key.index2)
                 {
                     @class = GetNextKey(key);
                 }
             }
             if (@class == null)
             {
                 @class = GetFirstKey();
             }
             while (index < @class.index1 || index > @class.index2)
             {
                 @class = GetNextKey(@class);
             }
             return(@class);
         }
         Console.WriteLine($"DataSeries::GetKey Error: index is out of range : {Name} {index}");
         return(null);
     }
 }
Example #15
0
 public void Clear()
 {
     lock (Sync)
     {
         this.cache = this.cache ?? ReadCache();
         if (this.position1 != -1)
         {
             var key = ReadKey(this.position1);
             while (true)
             {
                 this.file.DeleteKey(key, false, true);
                 if (key.next == -1)
                 {
                     break;
                 }
                 key = ReadKey(key.next);
             }
         }
         Count             = 0;
         this.bufferCount  = 0;
         DateTime1         = new DateTime(0);
         DateTime2         = new DateTime(0);
         this.position1    = -1;
         this.position2    = -1;
         this.readOpened   = false;
         this.writeOpened  = false;
         this.cache        = new IdArray <DataKey>(4096);
         this.cacheKey.obj = new DataKeyIdArray(this.cache);
         this.readKey      = null;
         this.writeKey     = null;
         this.deleteKey    = null;
         this.insertKey    = null;
         this.changed      = true;
         Flush();
     }
 }
Example #16
0
        private void Insert(DataObject obj)
        {
            Count++;
            if (this.writeKey.dateTime1 <= obj.DateTime && obj.DateTime <= this.writeKey.dateTime2)
            {
                this.writeKey.AddObject(obj);
                if (this.writeKey.count >= this.writeKey.size)
                {
                    WriteKey(this.writeKey);
                    this.writeKey = new DataKey(this.file, null, this.writeKey.position, -1L)
                    {
                        number  = this.bufferCount,
                        index1  = Count,
                        index2  = Count,
                        changed = true
                    };
                    this.bufferCount++;
                    this.cache[this.writeKey.number] = this.writeKey;
                }
                else
                {
                    this.changed = true;
                }
                this.file.isModified = true;
                return;
            }

            var key = GetKey(obj.DateTime, this.insertKey, IndexOption.Prev);

            if (this.insertKey == null)
            {
                this.insertKey = key;
            }
            else if (this.insertKey != key)
            {
                if (this.insertKey.changed)
                {
                    WriteKey(this.insertKey);
                }
                if (!CacheObjects && this.insertKey != this.readKey && this.insertKey != this.writeKey && this.insertKey != this.deleteKey)
                {
                    this.insertKey.objects = null;
                }
                this.insertKey = key;
            }
            this.insertKey.GetObjects();
            if (this.insertKey.count < this.insertKey.size)
            {
                this.insertKey.AddObject(obj);
                if (this.insertKey.count == this.insertKey.size)
                {
                    WriteKey(this.insertKey);
                }
            }
            else
            {
                key = new DataKey(this.file, null, -1L, -1L);
                int num = this.insertKey.GetIndex(obj.DateTime, SearchOption.Next);
                if (num == -1)
                {
                    key.AddObject(obj);
                }
                else
                {
                    for (int i = num; i < this.insertKey.count; i++)
                    {
                        key.AddObject(this.insertKey.objects[i]);
                        this.insertKey.objects[i] = null;
                    }
                    this.insertKey.count  = num;
                    this.insertKey.index2 = this.insertKey.index1 + this.insertKey.count - 1;
                    if (this.insertKey.count != 0)
                    {
                        this.insertKey.dateTime2 = this.insertKey.objects[this.insertKey.count - 1].DateTime;
                    }
                    this.insertKey.AddObject(obj);
                }
                InsertKey(key, this.insertKey);
            }
            if (this.readKey != null && this.readKey.number > this.insertKey.number)
            {
                this.readKey.index1 += 1;
                this.readKey.index2 += 1;
            }
            if (this.writeKey != null && this.writeKey.number > this.insertKey.number)
            {
                this.writeKey.index1 += 1;
                this.writeKey.index2 += 1;
            }
            if (this.deleteKey != null && this.deleteKey.number > this.insertKey.number)
            {
                this.deleteKey.index1 += 1;
                this.deleteKey.index2 += 1;
            }
            this.insertKey.changed = true;
            this.changed           = true;
            this.file.isModified   = true;
        }
Example #17
0
 public FreeKey(ObjectKey key) : this(key.file, key.position, key.recLength)
 {
 }
Example #18
0
        // TODO: rewrite it!
        public virtual void Recover()
        {
            Keys.Clear();
            this.free.Clear();
            long num = this.beginPosition;

            new BinaryReader(this.stream);
            ObjectKey objectKey = null;

            while (true)
            {
                try
                {
                    objectKey = ReadKey(num);
                    goto IL_F6;
                }
                catch (Exception arg)
                {
                    Console.WriteLine("DataFile::Recover exception " + arg);
                    break;
                }
                goto IL_4E;
IL_6A:
                if (objectKey.deleted)
                {
                    this.free.Add(new FreeKey(objectKey));
                }
                else if (!(objectKey is DataKey) && objectKey.TypeId != ObjectType.DataKeyIdArray)
                {
                    if (objectKey.TypeId != ObjectType.ObjectKeyList)
                    {
                        if (objectKey.TypeId != ObjectType.FreeKeyList)
                        {
                            Keys.Add(objectKey.Name, objectKey);
                            goto IL_CA;
                        }
                    }
                    this.DeleteKey(objectKey, false, true);
                }
IL_CA:
                Console.WriteLine(objectKey);
                num += (long)objectKey.recLength;
                if (objectKey.recLength <= 0)
                {
                    break;
                }
                if (num >= this.stream.Length)
                {
                    break;
                }
                continue;
IL_4E:
                Console.WriteLine("DataFile::Recover Key position is -1 , setting to " + num);
                objectKey.position = num;
                goto IL_6A;
IL_F6:
                if (objectKey.position == -1)
                {
                    goto IL_4E;
                }
                goto IL_6A;
            }
            this.isModified = true;
            Flush();
        }