Пример #1
0
            public int CompareTo(StringPiece other)
            {
                var a = Encoding.BigEndianUnicode.GetString(ToArray());
                var b = Encoding.BigEndianUnicode.GetString(other.ToArray());

                return(string.Compare(a, b, StringComparison.Ordinal));
            }
Пример #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());
                }
            }
        }
Пример #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));
            }
Пример #4
0
        private static bool ExtractEncodedIdbKey(StringPiece slice, out string result)
        {
            var start = slice.Fork();

            if (!ConsumeEncodedIdbKey(slice))
            {
                result = null;
                return(false);
            }

            result = start.Slice(start.Left - slice.Left).ToString();
            return(true);
        }
Пример #5
0
            public static bool Decode(StringPiece slice, out ObjectStoreNamesKey result)
            {
                KeyPrefix prefix;
                byte      typeByte;

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

                result = new ObjectStoreNamesKey();
                return(slice.DecodeStringWithLength(out result._objectStoreName));
            }
Пример #6
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));
            }
Пример #7
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));
            }
Пример #8
0
            public static bool Decode(StringPiece slice, out DatabaseNameKey result)
            {
                KeyPrefix prefix;
                byte      typeByte;

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

                result = new DatabaseNameKey();
                return(slice.DecodeStringWithLength(out result.Origin) && slice.DecodeStringWithLength(out result.DatabaseName));
            }
Пример #9
0
            public static bool Decode(StringPiece slice, out ObjectStoreDataKey result)
            {
                KeyPrefix prefix;

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

                result = new ObjectStoreDataKey();
                if (!ExtractEncodedIdbKey(slice, out result.EncodedUserKey))
                {
                    return(false);
                }

                return(true);
            }
Пример #10
0
    public void Speak(string id, float remain = 3f)
    {
        int randomFace = Random.Range(0, 3);

        ChangeFace((unitychanFace)randomFace, 2f);


        StringPiece temp = StringManager.instance.getStringByID(id);

        balloon.Call(temp.korean, remain, 0.3f, 0.3f);
        if (temp.voice == null)
        {
            Debug.LogError("로딩되지 않은 목소리파일 사용 : " + temp.voiceFile);
        }
        else
        {
            sound.clip = temp.voice;
        }
        sound.Play();
    }
Пример #11
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));
            }
Пример #12
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)));
        }
Пример #13
0
            public static bool Decode(StringPiece slice, out KeyPrefix prefix)
            {
                if (slice.Empty)
                {
                    prefix = default(KeyPrefix);
                    return(false);
                }

                var firstByte          = slice.Next();
                var databaseIdBytes    = ((firstByte >> 5) & 0x7) + 1;
                var objectStoreIdBytes = ((firstByte >> 2) & 0x7) + 1;
                var indexIdBytes       = (firstByte & 0x3) + 1;

                if (databaseIdBytes + objectStoreIdBytes + indexIdBytes > slice.Left)
                {
                    prefix = default(KeyPrefix);
                    return(false);
                }

                prefix = new KeyPrefix();
                return(slice.Slice(databaseIdBytes).DecodeInt(out prefix.DatabaseId) &&
                       slice.Slice(objectStoreIdBytes).DecodeInt(out prefix.ObjectStoreId) &&
                       slice.Slice(indexIdBytes).DecodeInt(out prefix.IndexId));
            }
Пример #14
0
        public void Split(IReadOnlyDictionary <string, StringPieces> strings)
        {
            var parts      = new Dictionary <string, StringPiece>();
            var maxFreq    = 0;
            var minPartLen = Options.MinPartLen;
            var maxPartLen = Options.MaxPartLen;

            for (var fs = minPartLen; fs <= maxPartLen; fs++)
            {
                foreach (var s in strings.Keys)
                {
                    for (var i = 0; i <= s.Length - fs; i++)
                    {
                        var part = s.Substring(i, fs);
                        if (!parts.TryGetValue(part, out var p))
                        {
                            parts.Add(part, p = new FrequencySplitterPiece(part));
                        }
                        var f = ((FrequencySplitterPiece)p).Frequency++;
                        if (f > maxFreq)
                        {
                            maxFreq = f;
                        }
                    }
                }
            }

            foreach (var kv in strings)
            {
                var i      = 0;
                var s      = kv.Key;
                var pieces = new List <StringPiece>();
                while (i < s.Length)
                {
                    StringPiece best       = null;
                    double      bestWeight = 0;
                    for (var j = Math.Min(s.Length - i, maxPartLen); j >= minPartLen; j--)
                    {
                        var part = s.Substring(i, j);
                        var freq = (FrequencySplitterPiece)parts[part];
                        if (freq.Frequency == 1)
                        {
                            continue;
                        }
                        var w = Options.FrequencyWeight * freq.Frequency / maxFreq +
                                Options.UsageWeight * freq.Usage / maxFreq +
                                Options.LengthWeight * part.Length / maxPartLen;
                        if (w > bestWeight)
                        {
                            best       = freq;
                            bestWeight = w;
                        }
                    }

                    if (best == null)
                    {
                        var part = s.Substring(i);
                        if (!parts.TryGetValue(part, out best))
                        {
                            parts.Add(part, best = new FrequencySplitterPiece(part)
                            {
                                Frequency = 1
                            });
                        }
                    }

                    ((FrequencySplitterPiece)best).Usage++;

                    pieces.Add(best);
                    i += best.Value.Length;
                }
                kv.Value.Set(pieces.ToArray()); // save memory by converting to array
            }
        }
Пример #15
0
            public static int CompareSuffix(StringPiece sliceA, StringPiece sliceB)
            {
                bool ok;

                return(CompareEncodedIdbKeys(sliceA, sliceB, out ok));
            }
Пример #16
0
            public static int Compare(StringPiece a, StringPiece b)
            {
                IndexNamesKey keyA, keyB;

                return(Decode(a, out keyA) && Decode(b, out keyB) ? keyA.CompareTo(keyB) : 0);
            }
Пример #17
0
            public static int Compare(StringPiece a, StringPiece b)
            {
                DatabaseFreeListKey keyA, keyB;

                return(Decode(a, out keyA) && Decode(b, out keyB) ? keyA.CompareTo(keyB) : 0);
            }
Пример #18
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);
        }
Пример #19
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);
        }
Пример #20
0
        // https://cs.chromium.org/chromium/src/content/browser/indexed_db/indexed_db_database_callbacks.cc
        private static int Compare(StringPiece a, StringPiece b, bool onlyCompareIndexKeys)
        {
            var sliceA = a.Fork();
            var sliceB = b.Fork();

            KeyPrefix prefixA, prefixB;

            if (!KeyPrefix.Decode(sliceA, out prefixA) || !KeyPrefix.Decode(sliceB, out prefixB))
            {
                return(0);
            }

            {
                var x = prefixA.CompareTo(prefixB);
                if (x != 0)
                {
                    return(x);
                }
            }

            switch (prefixA.Type)
            {
            case KeyType.GlobalMetadata: {
                if (sliceA.Empty || sliceB.Empty)
                {
                    return(0);
                }

                var typeByteA = sliceA.Next();
                var typeByteB = sliceB.Next();

                {
                    var x = typeByteA - typeByteB;
                    if (x != 0)
                    {
                        return(x);
                    }
                }

                if (typeByteA < KMaxSimpleGlobalMetaDataTypeByte)
                {
                    return(0);
                }

                // Compare<> is used (which re-decodes the prefix) rather than an
                // specialized CompareSuffix<> because metadata is relatively uncommon
                // in the database.
                switch (typeByteA)
                {
                case KDatabaseFreeListTypeByte:
                    return(DatabaseFreeListKey.Compare(a, b));

                case KDatabaseNameTypeByte:
                    return(DatabaseNameKey.Compare(a, b));
                }
                break;
            }

            case KeyType.DatabaseMetadata: {
                if (sliceA.Empty || sliceB.Empty)
                {
                    return(0);
                }

                var typeByteA = sliceA.Next();
                var typeByteB = sliceB.Next();

                {
                    var x = typeByteA - typeByteB;
                    if (x != 0)
                    {
                        return(x);
                    }
                }

                if (typeByteA < (int)MetaDataType.MaxSimpleMetadataType)
                {
                    return(0);
                }

                switch (typeByteA)
                {
                case KObjectStoreMetaDataTypeByte:
                    return(ObjectStoreMetaDataKey.Compare(a, b));

                case KIndexMetaDataTypeByte:
                    return(IndexMetaDataKey.Compare(a, b));

                case KObjectStoreNamesTypeByte:
                    return(ObjectStoreNamesKey.Compare(a, b));

                case KObjectStoreFreeListTypeByte:
                    return(ObjectStoreFreeListKey.Compare(a, b));

                case KIndexFreeListTypeByte:
                    return(IndexFreeListKey.Compare(a, b));

                case KIndexNamesKeyTypeByte:
                    return(IndexNamesKey.Compare(a, b));
                }

                break;
            }

            case KeyType.ObjectStoreData:
                return(sliceA.Empty || sliceB.Empty ? CompareSizes(sliceA.Left, sliceB.Left) :
                       ObjectStoreDataKey.CompareSuffix(sliceA, sliceB));

            case KeyType.ExistsEntry:
                return(sliceA.Empty || sliceB.Empty ? CompareSizes(sliceA.Left, sliceB.Left) :
                       ExistsEntryKey.CompareSuffix(sliceA, sliceB));

            case KeyType.BlobEntry:
                return(sliceA.Empty || sliceB.Empty ? CompareSizes(sliceA.Left, sliceB.Left) :
                       BlobEntryKey.CompareSuffix(sliceA, sliceB));

            case KeyType.IndexData:
                return(sliceA.Empty || sliceB.Empty ? CompareSizes(sliceA.Left, sliceB.Left) :
                       IndexDataKey.CompareSuffix(sliceA, sliceB, onlyCompareIndexKeys));

            case KeyType.InvalidType:
                break;
            }

            return(0);
        }
Пример #21
0
            public static int Compare(StringPiece a, StringPiece b)
            {
                ObjectStoreMetaDataKey keyA, keyB;

                return(Decode(a, out keyA) && Decode(b, out keyB) ? keyA.CompareTo(keyB) : 0);
            }