コード例 #1
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
        internal void OpenWrite()
        {
            lock (Sync)
            {
                if (this.writeOpened)
                {
                    Console.WriteLine("DataSeries::OpenWrite already write open");
                    return;
                }
                this.cache = this.cache ?? ReadCache();

                if (this.bufferCount != 0 && this.cache[this.bufferCount - 1] != null)
                {
                    this.writeKey = this.cache[this.bufferCount - 1];
                    this.writeKey.GetObjects();
                }
                else
                {
                    if (this.position2 != -1)
                    {
                        this.writeKey        = ReadKey(this.position2);
                        this.writeKey.number = this.bufferCount - 1;
                        this.writeKey.GetObjects();
                    }
                    else
                    {
                        this.writeKey = new DataKey(this.file)
                        {
                            number  = 0,
                            changed = true
                        };
                        this.bufferCount = 1;
                    }
                    this.cache[this.writeKey.number] = this.writeKey;
                }
                if (this.cacheKey.position != -1)
                {
                    this.file.DeleteKey(this.cacheKey, false, false);
                }

                this.cachePosition = -1;
                this.file.WriteKey(this.key);
                this.writeOpened = true;
            }
        }
コード例 #2
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
 private void InsertKey(DataKey key, DataKey keyAt)
 {
     for (int i = this.bufferCount; i > keyAt.number + 1; i--)
     {
         this.cache[i] = this.cache[i - 1];
         if (this.cache[i] != null)
         {
             this.cache[i].number = i;
         }
     }
     this.bufferCount++;
     key.number             = keyAt.number + 1;
     this.cache[key.number] = key;
     key.prev = keyAt.position;
     key.next = keyAt.next;
     WriteKey(key);
     this.file.WriteKey(this.key);
 }
コード例 #3
0
        public static object FromReader(BinaryReader reader, byte version)
        {
            int size = reader.ReadInt32();
            var keys = new IdArray <DataKey>(size);

            while (true)
            {
                int index = reader.ReadInt32();
                if (index == -1)
                {
                    break;
                }
                var key = new DataKey(null, null, -1, -1);
                key.Read(reader, true);
                keys.Add(index, key);
            }
            return(new DataKeyIdArray(keys));
        }
コード例 #4
0
ファイル: DataFile.cs プロジェクト: vcoda/fastquant.dll
        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);
            }
        }
コード例 #5
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
        public virtual long GetIndex(DateTime dateTime, SearchOption option = SearchOption.Prev)
        {
            lock (Sync)
            {
                if (!this.readOpened)
                {
                    OpenRead();
                }

                if (Count == 0)
                {
                    Console.WriteLine($"DataSeries::GetIndex Error, data series has no elements {Name}");
                    return(-1);
                }

                if (dateTime <= DateTime1)
                {
                    return(0);
                }

                if (dateTime >= DateTime2)
                {
                    return(Count - 1);
                }

                var key = GetKey(dateTime, this.readKey, IndexOption.Null);
                if (key == null)
                {
                    return(-1);
                }

                if (key != this.readKey)
                {
                    if (!CacheObjects && this.readKey != null && this.readKey != this.writeKey &&
                        this.readKey != this.insertKey && this.readKey != this.deleteKey)
                    {
                        this.readKey.objects = null;
                    }
                    this.readKey = key;
                }
                return(this.readKey.index1 + this.readKey.GetIndex(dateTime, option));
            }
        }
コード例 #6
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
        public virtual DataObject Get(DateTime dateTime)
        {
            lock (Sync)
            {
                if (!this.readOpened)
                {
                    OpenRead();
                }

                if (Count == 0 || dateTime > DateTime2)
                {
                    Console.WriteLine($"DataSeries::Get dateTime is out of range : {Name} {dateTime}");
                    return(null);
                }

                if (dateTime <= DateTime1)
                {
                    return(Get(0));
                }

                var key = GetKey(dateTime, this.readKey, IndexOption.Null);
                if (key == null)
                {
                    return(null);
                }

                if (key != this.readKey)
                {
                    if (!CacheObjects && this.readKey != null && this.readKey != this.writeKey && this.readKey != this.insertKey && this.readKey != this.deleteKey)
                    {
                        this.readKey.objects = null;
                    }
                    this.readKey = key;
                }
                return(this.readKey.GetObject(dateTime));
            }
        }
コード例 #7
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
 // 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);
     }
 }
コード例 #8
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
 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();
     }
 }
コード例 #9
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
 private void SetNext(DataKey key, long position)
 {
     key.next = position;
     SetNext(key.position, position);
 }
コード例 #10
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
 private void SetPrev(DataKey key, long position)
 {
     key.prev = position;
     SetPrev(key.position, position);
 }
コード例 #11
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
        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;
        }
コード例 #12
0
ファイル: DataSeries.cs プロジェクト: vcoda/fastquant.dll
        public void Remove(long index)
        {
            lock (Sync)
            {
                if (!this.writeOpened)
                {
                    OpenWrite();
                }

                var key = GetKey(index, this.deleteKey);
                if (key == null)
                {
                    return;
                }

                if (this.deleteKey == null)
                {
                    this.deleteKey = key;
                }
                else if (this.deleteKey != key)
                {
                    if (this.deleteKey.changed)
                    {
                        WriteKey(this.deleteKey);
                    }

                    if (!CacheObjects && this.deleteKey != this.readKey && this.deleteKey != this.writeKey && this.deleteKey != this.insertKey)
                    {
                        this.deleteKey.objects = null;
                    }
                    this.deleteKey = key;
                }
                key.RemoveObject(index - key.index1);
                key.index2 -= 1;
                if (this.readKey != null && this.readKey.number > key.number)
                {
                    this.readKey.index1 -= 1;
                    this.readKey.index2 -= 1;
                }
                if (this.writeKey != null && this.writeKey.number > key.number)
                {
                    this.writeKey.index1 -= 1;
                    this.writeKey.index2 -= 1;
                }
                if (this.insertKey != null && this.insertKey.number > key.number)
                {
                    this.insertKey.index1 -= 1;
                    this.insertKey.index2 -= 1;
                }
                if (key.count == 0)
                {
                    DeleteKey(key);
                    this.deleteKey = null;
                }

                Count--;
                if (Count != 0)
                {
                    if (index == 0)
                    {
                        DateTime1 = Get(0).DateTime;
                    }
                    if (index == Count)
                    {
                        DateTime2 = Get(Count - 1).DateTime;
                    }
                }
                this.changed         = true;
                this.file.isModified = true;
            }
        }