Example #1
0
        private static MGRB DoBitOperation(MGRB bits, MGRB c, OPERATION op, int maxsize)
        {
            if (bits != null)
            {
                switch (op)
                {
                case OPERATION.AND:
                    bits = bits.And(c);
                    break;

                case OPERATION.OR:
                    bits = bits.Or(c);
                    break;

                case OPERATION.ANDNOT:
                    bits = bits.And(c.Not(maxsize));
                    break;
                }
            }
            else
            {
                bits = c;
            }
            return(bits);
        }
Example #2
0
        private MGRB doLessOp(RDBExpression exp, T key)
        {
            bool found  = false;
            int  pos    = FindPageOrLowerPosition(key, ref found);
            MGRB result = new MGRB();

            if (pos > 0)
            {
                // all the pages before
                for (int i = 0; i < pos - 1; i++)
                {
                    doPageOperation(ref result, i);
                }
            }
            // key page
            Page <T> page = LoadPage(_pageList.GetValue(pos).PageNumber);

            T[] keys = page.tree.Keys();
            Array.Sort(keys);
            // find better end position rather than last key
            pos = Array.BinarySearch <T>(keys, key);
            if (pos < 0)
            {
                pos = ~pos;
            }
            for (int i = 0; i < pos; i++)
            {
                T k = keys[i];
                if (k.CompareTo(key) > 0)
                {
                    break;
                }
                int bn = page.tree[k].DuplicateBitmapNumber;

                if (k.CompareTo(key) < 0)
                {
                    result = result.Or(_index.GetDuplicateBitmap(bn));
                }

                if (exp == RDBExpression.LessEqual && k.CompareTo(key) == 0)
                {
                    result = result.Or(_index.GetDuplicateBitmap(bn));
                }
            }
            return(result);
        }
Example #3
0
        private void doPageOperation(ref MGRB res, int pageidx)
        {
            Page <T> page = LoadPage(_pageList.GetValue(pageidx).PageNumber);

            T[] keys = page.tree.Keys(); // avoid sync issues
            foreach (var k in keys)
            {
                int bn = page.tree[k].DuplicateBitmapNumber;

                res = res.Or(_index.GetDuplicateBitmap(bn));
            }
        }
Example #4
0
        private MGRB ExecutionPlan(string filter, int maxsize)
        {
            //_log.Debug("query : " + filter);
            DateTime dt = FastDateTime.Now;

            // query indexes
            string[] words = filter.Split(' ');
            //bool defaulttoand = true;
            //if (filter.IndexOfAny(new char[] { '+', '-' }, 0) > 0)
            //    defaulttoand = false;

            MGRB found = null;// MGRB.Fill(maxsize);

            foreach (string s in words)
            {
                int    c;
                bool   not  = false;
                string word = s;
                if (s == "")
                {
                    continue;
                }

                OPERATION op = OPERATION.AND;
                //if (defaulttoand)
                //    op = OPERATION.AND;

                if (word.StartsWith("+"))
                {
                    op   = OPERATION.OR;
                    word = s.Replace("+", "");
                }

                if (word.StartsWith("-"))
                {
                    op   = OPERATION.ANDNOT;
                    word = s.Replace("-", "");
                    not  = true;
                    if (found == null) // leading with - -> "-oak hill"
                    {
                        found = MGRB.Fill(maxsize);
                    }
                }

                if (word.Contains("*") || word.Contains("?"))
                {
                    MGRB wildbits = new MGRB();

                    // do wildcard search
                    Regex reg = new Regex("^" + word.Replace("*", ".*").Replace("?", ".") + "$", RegexOptions.IgnoreCase);
                    foreach (string key in _words.Keys())
                    {
                        if (reg.IsMatch(key))
                        {
                            _words.TryGetValue(key, out c);
                            MGRB ba = _bitmaps.GetBitmap(c);

                            wildbits = DoBitOperation(wildbits, ba, OPERATION.OR, maxsize);
                        }
                    }
                    if (found == null)
                    {
                        found = wildbits;
                    }
                    else
                    {
                        if (not) // "-oak -*l"
                        {
                            found = found.AndNot(wildbits);
                        }
                        else if (op == OPERATION.AND)
                        {
                            found = found.And(wildbits);
                        }
                        else
                        {
                            found = found.Or(wildbits);
                        }
                    }
                }
                else if (_words.TryGetValue(word.ToLowerInvariant(), out c))
                {
                    // bits logic
                    MGRB ba = _bitmaps.GetBitmap(c);
                    found = DoBitOperation(found, ba, op, maxsize);
                }
                else if (op == OPERATION.AND)
                {
                    found = new MGRB();
                }
            }
            if (found == null)
            {
                return(new MGRB());
            }

            // remove deleted docs
            MGRB ret;

            if (_docMode)
            {
                ret = found.AndNot(_deleted.GetBits());
            }
            else
            {
                ret = found;
            }
            //_log.Debug("query time (ms) = " + FastDateTime.Now.Subtract(dt).TotalMilliseconds);
            return(ret);
        }
Example #5
0
        public MGRB Query(T from, T to, int maxsize)
        {
            MGRB bits = new MGRB();
            T    temp = default(T);

            if (from.CompareTo(to) > 0) // check values order
            {
                temp = from;
                from = to;
                to   = temp;
            }
            // find first page and do > than
            bool found    = false;
            int  startpos = FindPageOrLowerPosition(from, ref found);
            // find last page and do < than
            int  endpos   = FindPageOrLowerPosition(to, ref found);
            bool samepage = startpos == endpos;

            // from key page
            Page <T> page = LoadPage(_pageList.GetValue(startpos).PageNumber);

            T[] keys = page.tree.Keys();
            Array.Sort(keys);

            // find better start position rather than 0
            int pos = Array.BinarySearch <T>(keys, from); // FEATURE : rewrite??

            if (pos < 0)
            {
                pos = ~pos;
            }

            for (int i = pos; i < keys.Length; i++)
            {
                T   k  = keys[i];
                int bn = page.tree[k].DuplicateBitmapNumber;

                if (samepage)
                {
                    if (k.CompareTo(from) >= 0 && k.CompareTo(to) <= 0) // if from,to same page
                    {
                        bits = bits.Or(_index.GetDuplicateBitmap(bn));
                    }
                }
                else
                {
                    if (k.CompareTo(from) >= 0)
                    {
                        bits = bits.Or(_index.GetDuplicateBitmap(bn));
                    }
                }
            }
            if (!samepage)
            {
                // to key page
                page = LoadPage(_pageList.GetValue(endpos).PageNumber);
                keys = page.tree.Keys();
                Array.Sort(keys);
                // find better end position rather than last key
                pos = Array.BinarySearch <T>(keys, to);
                if (pos < 0)
                {
                    pos = ~pos;
                }

                for (int i = 0; i <= pos; i++)
                {
                    T   k  = keys[i];
                    int bn = page.tree[k].DuplicateBitmapNumber;

                    if (k.CompareTo(to) <= 0)
                    {
                        bits = bits.Or(_index.GetDuplicateBitmap(bn));
                    }
                }
                // do all pages in between
                for (int i = startpos + 1; i < endpos; i++)
                {
                    doPageOperation(ref bits, i);
                }
            }
            return(bits);
        }
Example #6
0
 public void InPlaceOR(MGRB left)
 {
     lock (_lock)
         _bits = _bits.Or(left);
 }