Beispiel #1
0
        VPackSlice SearchObjectKeyBinary(
            string attribute,
            long ieBase,
            int offsetsize,
            long n)
        {
            bool       useTranslator = attributeTranslator != null;
            VPackSlice result;
            long       l = 0;
            long       r = n - 1;

            for (;;)
            {
                // midpoint
                long       index    = l + ((r - l) / 2);
                long       offset   = ieBase + index * offsetsize;
                long       keyIndex = NumberUtil.ToLong(vpack, (int)(start + offset), offsetsize);
                VPackSlice key      = new VPackSlice(vpack, (int)(start + keyIndex));
                int        res      = 0;
                if (key.IsType(SliceType.String))
                {
                    res = key.CompareString(attribute);
                }
                else if (key.IsInteger())
                {
                    // translate key
                    if (!useTranslator)
                    {
                        // no attribute translator
                        throw new VPackNeedAttributeTranslatorException();
                    }
                    res = key.TranslateUnchecked().CompareString(attribute);
                }
                else
                {
                    // invalid key
                    result = new VPackSlice();
                    break;
                }
                if (res == 0)
                {
                    // found
                    result = new VPackSlice(vpack, key.start + key.GetByteSize());
                    break;
                }
                if (res > 0)
                {
                    if (index == 0)
                    {
                        result = new VPackSlice();
                        break;
                    }
                    r = index - 1;
                }
                else
                {
                    l = index + 1;
                }
                if (r < l)
                {
                    result = new VPackSlice();
                    break;
                }
            }
            return(result);
        }