Ejemplo n.º 1
0
            public static bool Decode(StringPiece slice, out IndexFreeListKey result)
            {
                KeyPrefix prefix;
                byte      typeByte;

                if (!KeyPrefix.Decode(slice, out prefix) || !slice.DecodeByte(out typeByte))
                {
                    result = default(IndexFreeListKey);
                    return(false);
                }

                result = new IndexFreeListKey();
                return(slice.DecodeVarInt(out result.ObjectStoreId) && slice.DecodeVarInt(out result.IndexId));
            }
Ejemplo n.º 2
0
        private IEnumerable <byte[]> GetBytesData()
        {
            using (var it = _levelDb.CreateIterator()) {
                for (it.SeekToFirst(); it.IsValid(); it.Next())
                {
                    var piece = new StringPiece(it.Key());

                    KeyPrefix          prefix;
                    ObjectStoreDataKey store;
                    if (!KeyPrefix.Decode(piece, out prefix) || prefix.Type != KeyType.ObjectStoreData ||
                        !ObjectStoreDataKey.Decode(piece.Reset(), out store))
                    {
                        continue;
                    }

                    var  dataSlice = new StringPiece(it.Value());
                    long version;
                    if (!dataSlice.DecodeVarInt(out version))
                    {
                        continue;
                    }
                    yield return(dataSlice.ToSwappedArray());
                }
            }
        }
Ejemplo n.º 3
0
            public static int CompareSuffix(StringPiece sliceA, StringPiece sliceB, bool onlyCompareIndexKeys)
            {
                bool ok;
                int  result = CompareEncodedIdbKeys(sliceA, sliceB, out ok);

                if (!ok || result != 0 || onlyCompareIndexKeys)
                {
                    return(result);
                }

                // sequence number [optional]
                long sequenceNumberA = -1;
                long sequenceNumberB = -1;

                if (!sliceA.Empty && !sliceA.DecodeVarInt(out sequenceNumberA) ||
                    !sliceB.Empty && !sliceB.DecodeVarInt(out sequenceNumberB))
                {
                    return(0);
                }

                if (sliceA.Empty || sliceB.Empty)
                {
                    return(CompareSizes(sliceA.Left, sliceB.Left));
                }

                // primary key [optional]
                result = CompareEncodedIdbKeys(sliceA, sliceB, out ok);
                if (!ok || result != 0)
                {
                    return(result);
                }

                return(CompareInts(sequenceNumberA, sequenceNumberB));
            }
Ejemplo n.º 4
0
            public static bool Decode(StringPiece slice, out DatabaseFreeListKey result)
            {
                KeyPrefix prefix;
                byte      typeByte;

                if (!KeyPrefix.Decode(slice, out prefix) || !slice.DecodeByte(out typeByte))
                {
                    result = default(DatabaseFreeListKey);
                    return(false);
                }

                result = new DatabaseFreeListKey();
                return(slice.DecodeVarInt(out result.DatabaseId));
            }
Ejemplo n.º 5
0
            public static bool Decode(StringPiece slice, out ObjectStoreMetaDataKey result)
            {
                KeyPrefix prefix;

                if (!KeyPrefix.Decode(slice, out prefix))
                {
                    result = default(ObjectStoreMetaDataKey);
                    return(false);
                }

                if (slice.Empty)
                {
                    result = default(ObjectStoreMetaDataKey);
                    return(false);
                }

                slice.Next();

                result = new ObjectStoreMetaDataKey();
                return(slice.DecodeVarInt(out result.ObjectStoreId) && slice.DecodeByte(out result.MetaDataTypeValue));
            }
Ejemplo n.º 6
0
        private static int CompareEncodedStringsWithLength(StringPiece slice1, StringPiece slice2, out bool ok)
        {
            long len1, len2;

            if (!slice1.DecodeVarInt(out len1) || !slice2.DecodeVarInt(out len2) || len1 < 0 || len2 < 0)
            {
                ok = false;
                return(0);
            }

            var size1 = (int)len1 * 2;
            var size2 = (int)len2 * 2;

            if (slice1.Left < size1 || slice2.Left < size2)
            {
                ok = false;
                return(0);
            }

            // Extract the binary data, and advance the passed slices.
            ok = true;
            return(slice1.Slice(size1).CompareTo(slice2.Slice(size2)));
        }
Ejemplo n.º 7
0
        private static int CompareEncodedIdbKeys(StringPiece sliceA, StringPiece sliceB, out bool ok)
        {
            ok = true;
            var typeA = sliceA.Next();
            var typeB = sliceB.Next();

            {
                var x = CompareTypes(KeyTypeByteToKeyType(typeA), KeyTypeByteToKeyType(typeB));
                if (x != 0)
                {
                    return(x);
                }
            }

            switch (typeA)
            {
            case KIndexedDbKeyNullTypeByte:
            case KIndexedDbKeyMinKeyTypeByte:
                // Null type or max type; no payload to compare.
                return(0);

            case KIndexedDbKeyArrayTypeByte: {
                long lengthA, lengthB;
                if (!sliceA.DecodeVarInt(out lengthA) || !sliceB.DecodeVarInt(out lengthB))
                {
                    ok = false;
                    return(0);
                }

                for (long i = 0; i < lengthA && i < lengthB; ++i)
                {
                    var result = CompareEncodedIdbKeys(sliceA, sliceB, out ok);
                    if (!ok || result != 0)
                    {
                        return(result);
                    }
                }

                return((int)(lengthA - lengthB));
            }

            case KIndexedDbKeyBinaryTypeByte:
                return(CompareEncodedBinary(sliceA, sliceB, out ok));

            case KIndexedDbKeyStringTypeByte:
                return(CompareEncodedStringsWithLength(sliceA, sliceB, out ok));

            case KIndexedDbKeyDateTypeByte:
            case KIndexedDbKeyNumberTypeByte: {
                double d, e;
                if (!sliceA.DecodeDouble(out d) || !sliceB.DecodeDouble(out e))
                {
                    ok = false;
                    return(0);
                }
                return(d < e ? -1 : (d > e ? 1 : 0));
            }
            }

            return(0);
        }
Ejemplo n.º 8
0
        private static bool ConsumeEncodedIdbKey(StringPiece slice)
        {
            var type = slice.Next();

            switch (type)
            {
            case KIndexedDbKeyNullTypeByte:
            case KIndexedDbKeyMinKeyTypeByte:
                return(true);

            case KIndexedDbKeyArrayTypeByte: {
                long length;
                if (!slice.DecodeVarInt(out length))
                {
                    return(false);
                }
                while (length-- != 0)
                {
                    if (!ConsumeEncodedIdbKey(slice))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case KIndexedDbKeyBinaryTypeByte: {
                long length;
                if (!slice.DecodeVarInt(out length) || length < 0)
                {
                    return(false);
                }
                if (slice.Left < length)
                {
                    return(false);
                }
                slice.Skip(length);
                return(true);
            }

            case KIndexedDbKeyStringTypeByte: {
                long length;
                if (!slice.DecodeVarInt(out length) || length < 0)
                {
                    return(false);
                }
                if (slice.Left < length * 2)
                {
                    return(false);
                }
                slice.Skip(length * 2);
                return(true);
            }

            case KIndexedDbKeyDateTypeByte:
            case KIndexedDbKeyNumberTypeByte:
                if (slice.Left < sizeof(double))
                {
                    return(false);
                }
                slice.Skip(sizeof(double));
                return(true);
            }

            return(false);
        }