Esempio n. 1
0
        public static MGRB Fill(long count)
        {
            if (count == 0)
            {
                return(new MGRB());
            }

            var  con = new SafeSortedList <int, Container>();
            int  i   = 0;
            long c   = count;

            while (count > 0)
            {
                if (count > Container.BSize)
                {
                    con.Add(i, new BitmapContainer(true));
                }
                else
                {
                    con.Add(i, new BitmapContainer((int)count));
                }
                count -= Container.BSize;
                i++;
            }

            return(new MGRB(con, c));
        }
Esempio n. 2
0
        public void Set(long position, bool val)
        {
            lock (_lock)
            {
                isDirty = true;
                if (_size < position && val == true)
                {
                    _size = position;
                }

                var       idx = (int)(position >> 16);
                Container c   = null;
                if (_containers.TryGetValue(idx, out c) == false)
                {
                    //if (Global.useSortedList)
                    //    c = new OffsetContainerSL();
                    //else
                    c = new OffsetContainer();
                    // add container
                    _containers.Add(idx, c);
                }
                c.Set(position & _MASK, val);

                //if (c.ChangeRequired())
                //    _containers[idx] = c.Change();
            }
        }
Esempio n. 3
0
 private void LoadWords()
 {
     lock (_lock)
     {
         if (_words == null)
         {
             _words = new SafeSortedList <string, int>();
         }
         if (File.Exists(_Path + _FileName + ".words") == false)
         {
             return;
         }
         // load words
         byte[] b = File.ReadAllBytes(_Path + _FileName + ".words");
         if (b.Length == 0)
         {
             return;
         }
         MemoryStream ms = new MemoryStream(b);
         BinaryReader br = new BinaryReader(ms, Encoding.UTF8);
         string       s  = br.ReadString();
         while (s != "")
         {
             int off = br.ReadInt32();
             _words.Add(s, off);
             try
             {
                 s = br.ReadString();
             }
             catch { s = ""; }
         }
         _log.Debug("Word Count = " + _words.Count);
         _loaded = true;
     }
 }
Esempio n. 4
0
        public MGIndex(string path, string filename, byte keysize, bool allowdups)
        {
            if (Global.UseLessMemoryStructures)
            {
                _cache = new SafeSortedList <int, Page <T> >();
            }
            else
            {
                _cache = new SafeDictionary <int, Page <T> >();
            }

            _AllowDuplicates = allowdups;
            if (path.EndsWith(Path.DirectorySeparatorChar.ToString()) == false)
            {
                path += Path.DirectorySeparatorChar;
            }

            _index = new IndexFile <T>(path + filename, keysize);
            // load page list
            _index.GetPageList(_pageListDiskPages, _pageList, out _LastIndexedRecordNumber);
            if (_pageList.Count() == 0)
            {
                Page <T> page = new Page <T>();
                page.FirstKey = (T)RDBDataType <T> .GetEmpty();

                page.DiskPageNumber = _index.GetNewPageNumber();
                page.isDirty        = true;
                _pageList.Add(page.FirstKey, new PageInfo(page.DiskPageNumber, 0, 0));
                _cache.Add(page.DiskPageNumber, page);
            }
        }
Esempio n. 5
0
        public int GetFreeRecordNumber()
        {
            using (new L(this))
            {
                int i = _lastRecordNumber++;

                _cache.Add(i, new WAHBitArray());
                return(i);
            }
        }
Esempio n. 6
0
        public MGRB And(MGRB B)
        {
            var v   = new SafeSortedList <int, Container>();
            var len = _size;

            if (B.Length < len)
            {
                len = B.Length;
            }
            var a   = LastContainerIdx();
            var b   = B.LastContainerIdx();
            var min = a;

            if (b < min)
            {
                min = b;
            }
            min++;

            for (int i = 0; i < min; i++)
            {
                Container ca = null;
                Container cb = null;

                _containers.TryGetValue(i, out ca);
                B._containers.TryGetValue(i, out cb);

                if (ca != null && cb != null)
                {
                    v.Add(i, containerAND(ca, cb));
                }
            }

            return(new MGRB(v, len));
        }
Esempio n. 7
0
        //private int _maxPageItems = 0;

        public MGIndex(string path, string filename, byte keysize, /*ushort maxcount,*/ bool allowdups)
        {
            _AllowDuplicates = allowdups;
            _index           = new IndexFile <T>(path + Path.DirectorySeparatorChar + filename, keysize);//, maxcount);
            //_maxPageItems = maxcount;
            // load page list
            _index.GetPageList(_pageListDiskPages, _pageList, out _LastIndexedRecordNumber);
            if (_pageList.Count == 0)
            {
                Page <T> page = new Page <T>();
                page.FirstKey = (T)RDBDataType <T> .GetEmpty();

                page.DiskPageNumber = _index.GetNewPageNumber();
                page.isDirty        = true;
                _pageList.Add(page.FirstKey, new PageInfo(page.DiskPageNumber, 0, 0));
                _cache.Add(page.DiskPageNumber, page);
            }
        }
Esempio n. 8
0
        public MGRB Or(MGRB B)
        {
            var v   = new SafeSortedList <int, Container>();
            var len = _size;

            if (B.Length > len)
            {
                len = B.Length;
            }
            var a   = LastContainerIdx();
            var b   = B.LastContainerIdx();
            var max = a;

            if (b > max)
            {
                max = b;
            }
            max++;

            for (int i = 0; i < max; i++)
            {
                Container ca = null;
                Container cb = null;

                _containers.TryGetValue(i, out ca);
                B._containers.TryGetValue(i, out cb);

                if (ca == null && cb != null)
                {
                    v.Add(i, cb.Copy());
                }
                else if (cb == null && ca != null)
                {
                    v.Add(i, ca.Copy());
                }
                else if (ca != null && cb != null)
                {
                    v.Add(i, containerOR(ca, cb));
                }
            }

            return(new MGRB(v, len));
        }
Esempio n. 9
0
        public MGRB Not()
        {
            var con = new SafeSortedList <int, Container>();

            foreach (var c in _containers)
            {
                con.Add(c.Key, c.Value.Not());
            }

            return(new MGRB(con, _size));
        }
Esempio n. 10
0
        public MGRB Not(long count)
        {
            var con = new SafeSortedList <int, Container>();
            var c   = count >> 16;

            for (int i = 0; i <= c; i++)
            {
                Container a = null;
                _containers.TryGetValue(i, out a);
                if (a == null)
                {
                    con.Add(i, new BitmapContainer(true));
                }
                else
                {
                    con.Add(i, a.Not());
                }
            }

            return(new MGRB(con, count));
        }
Esempio n. 11
0
        public static MGRB Fill(long count)
        {
            if (count == 0)
            {
                return(new MGRB());
            }

            var con = new SafeSortedList <int, Container>();
            var c   = count >> 16;

            for (int i = 0; i <= c; i++)
            {
                con.Add(i, new BitmapContainer(true));
            }

            return(new MGRB(con));
        }
Esempio n. 12
0
        private int LoadPageListData(int page, SafeSortedList <T, PageInfo> PageList)
        {
            lock (_fileLock)
            {
                // load page list data
                int nextpage = -1;
                SeekPage(page);
                byte[] b = new byte[_PageLength];
                _file.Read(b, 0, _PageLength);

                if (b[0] == _BlockHeader[0] && b[1] == _BlockHeader[1] && b[2] == _BlockHeader[2] && b[3] == _BlockHeader[3])
                {
                    short count = Helper.ToInt16(b, 5);
                    if (count > _PageNodeCount)
                    {
                        throw new Exception("Count > node size");
                    }
                    nextpage = Helper.ToInt32(b, 11);
                    int index = _BlockHeader.Length;

                    for (int i = 0; i < count; i++)
                    {
                        int  idx     = index + _rowSize * i;
                        byte ks      = b[idx];
                        T    key     = _T.GetObject(b, idx + 1, ks);
                        int  pagenum = Helper.ToInt32(b, idx + 1 + _maxKeySize);
                        // add counts
                        int unique = Helper.ToInt32(b, idx + 1 + _maxKeySize + 4);
                        // FEATURE : add dup count
                        PageList.Add(key, new PageInfo(pagenum, unique, 0));
                    }
                }
                else
                {
                    throw new Exception("Page List header is invalid");
                }

                return(nextpage);
            }
        }
Esempio n. 13
0
        private int LoadPageListData(int page, SafeSortedList <T, PageInfo> PageList)
        {
            lock (_fileLock)
            {
                // load page list data
                int nextpage = -1;
                SeekPage(page);
                byte[] b = new byte[_PageLength];
                _file.Read(b, 0, _PageLength);

                if (b[0] == _BlockHeader[0] && b[1] == _BlockHeader[1] && b[2] == _BlockHeader[2] && b[3] == _BlockHeader[3])
                {
                    short count = Helper.ToInt16(b, 5);
                    if (count > _PageNodeCount)
                    {
                        throw new Exception("Count > node size");
                    }
                    nextpage = Helper.ToInt32(b, 11);
                    int      index = _BlockHeader.Length;
                    object[] keys  = null;
                    // TODO : needed??
                    //if (File.Exists(_FileName + ".pagelist"))
                    //{
                    //    var bn = File.ReadAllBytes(_FileName + ".pagelist");
                    //    int blknum = Helper.ToInt32(bn, 0);
                    //    byte[] bb = _strings.GetData(blknum, out _pagelistalllocblock);
                    //    keys = (object[])BJSON.ToObject(bb);
                    //}
                    for (int i = 0; i < count; i++)
                    {
                        int  idx = index + _rowSize * i;
                        byte ks  = b[idx];
                        T    key;
                        if (_externalStrings == false)
                        {
                            key = _T.GetObject(b, idx + 1, ks);
                        }
                        else
                        {
                            if (keys == null)
                            {
                                key = _T.GetObject(b, idx + 1, ks); // do old way until better way
                            }
                            else
                            {
                                key = (T)keys[i];
                            }
                        }
                        int pagenum = Helper.ToInt32(b, idx + 1 + _maxKeySize);
                        // add counts
                        int unique = Helper.ToInt32(b, idx + 1 + _maxKeySize + 4);
                        // FEATURE : add dup count
                        PageList.Add(key, new PageInfo(pagenum, unique, 0));
                    }
                }
                else
                {
                    throw new Exception("Page List header is invalid");
                }

                return(nextpage);
            }
        }