Beispiel #1
0
        private static KVType ConvertBinaryOnlyKVType(KVType type)
        {
            switch (type)
            {
            case KVType.BOOLEAN:
            case KVType.BOOLEAN_TRUE:
            case KVType.BOOLEAN_FALSE:
                return(KVType.BOOLEAN);

            case KVType.INT64:
            case KVType.INT32:
            case KVType.INT64_ZERO:
            case KVType.INT64_ONE:
                return(KVType.INT64);

            case KVType.UINT64:
            case KVType.UINT32:
                return(KVType.UINT64);

            case KVType.DOUBLE:
            case KVType.DOUBLE_ZERO:
            case KVType.DOUBLE_ONE:
                return(KVType.DOUBLE);

            case KVType.ARRAY_TYPED:
                return(KVType.ARRAY);
            }

            return(type);
        }
Beispiel #2
0
        private static KVValue MakeValue(KVType type, object data, KVFlag flag)
        {
            var realType = ConvertBinaryOnlyKVType(type);

            if (flag != KVFlag.None)
            {
                return(new KVFlaggedValue(realType, flag, data));
            }

            return(new KVValue(realType, data));
        }
        public EventDisplays GetTypeByEnum(KVType kvt)
        {
            switch (kvt)
            {
            case KVType.BOOLEAN:
                return(EventDisplays.StringValue);

            case KVType.OBJECT:
            case KVType.NULL:
            case KVType.STRING:
                return(EventDisplays.StringValue);

            case KVType.STRING_MULTI:
            case KVType.BINARY_BLOB:
            case KVType.ARRAY:
            case KVType.ARRAY_TYPED:
                return(EventDisplays.ArrayValue);

            case KVType.BOOLEAN_TRUE:
            case KVType.BOOLEAN_FALSE:
                return(EventDisplays.StringValue);

            case KVType.INT64:
            case KVType.UINT64:
            case KVType.DOUBLE:
            case KVType.INT32:
            case KVType.UINT32:
            case KVType.INT64_ZERO:
            case KVType.INT64_ONE:
            case KVType.DOUBLE_ZERO:
            case KVType.DOUBLE_ONE:
                return(EventDisplays.FloatValue);

            default:
                return(EventDisplays.StringValue);
            }
        }
Beispiel #4
0
 public KVValue(KVType type, object value)
 {
     Type  = type;
     Value = value;
 }
 public KVFlaggedValue(KVType type, KVFlag flag, object value)
     : base(type, value)
 {
     Flag = flag;
 }
 public KVFlaggedValue(KVType type, object value)
     : base(type, value)
 {
     Flag = KVFlag.None;
 }
Beispiel #7
0
        private KVObject ReadBinaryValue(string name, KVType datatype, KVFlag flagInfo, BinaryReader reader, KVObject parent)
        {
            var currentOffset = reader.BaseStream.Position;

            switch (datatype)
            {
            case KVType.NULL:
                parent.AddProperty(name, MakeValue(datatype, null, flagInfo));
                break;

            case KVType.BOOLEAN:
                if (currentBinaryBytesOffset > -1)
                {
                    reader.BaseStream.Position = currentBinaryBytesOffset;
                }

                parent.AddProperty(name, MakeValue(datatype, reader.ReadBoolean(), flagInfo));

                if (currentBinaryBytesOffset > -1)
                {
                    currentBinaryBytesOffset++;
                    reader.BaseStream.Position = currentOffset;
                }

                break;

            case KVType.BOOLEAN_TRUE:
                parent.AddProperty(name, MakeValue(datatype, true, flagInfo));
                break;

            case KVType.BOOLEAN_FALSE:
                parent.AddProperty(name, MakeValue(datatype, false, flagInfo));
                break;

            case KVType.INT64_ZERO:
                parent.AddProperty(name, MakeValue(datatype, 0L, flagInfo));
                break;

            case KVType.INT64_ONE:
                parent.AddProperty(name, MakeValue(datatype, 1L, flagInfo));
                break;

            case KVType.INT64:
                if (currentEightBytesOffset > 0)
                {
                    reader.BaseStream.Position = currentEightBytesOffset;
                }

                parent.AddProperty(name, MakeValue(datatype, reader.ReadInt64(), flagInfo));

                if (currentEightBytesOffset > 0)
                {
                    currentEightBytesOffset    = reader.BaseStream.Position;
                    reader.BaseStream.Position = currentOffset;
                }

                break;

            case KVType.UINT64:
                if (currentEightBytesOffset > 0)
                {
                    reader.BaseStream.Position = currentEightBytesOffset;
                }

                parent.AddProperty(name, MakeValue(datatype, reader.ReadUInt64(), flagInfo));

                if (currentEightBytesOffset > 0)
                {
                    currentEightBytesOffset    = reader.BaseStream.Position;
                    reader.BaseStream.Position = currentOffset;
                }

                break;

            case KVType.INT32:
                parent.AddProperty(name, MakeValue(datatype, reader.ReadInt32(), flagInfo));
                break;

            case KVType.UINT32:
                parent.AddProperty(name, MakeValue(datatype, reader.ReadUInt32(), flagInfo));
                break;

            case KVType.DOUBLE:
                if (currentEightBytesOffset > 0)
                {
                    reader.BaseStream.Position = currentEightBytesOffset;
                }

                parent.AddProperty(name, MakeValue(datatype, reader.ReadDouble(), flagInfo));

                if (currentEightBytesOffset > 0)
                {
                    currentEightBytesOffset    = reader.BaseStream.Position;
                    reader.BaseStream.Position = currentOffset;
                }

                break;

            case KVType.DOUBLE_ZERO:
                parent.AddProperty(name, MakeValue(datatype, 0.0D, flagInfo));
                break;

            case KVType.DOUBLE_ONE:
                parent.AddProperty(name, MakeValue(datatype, 1.0D, flagInfo));
                break;

            case KVType.STRING:
                var id = reader.ReadInt32();
                parent.AddProperty(name, MakeValue(datatype, id == -1 ? string.Empty : stringArray[id], flagInfo));
                break;

            case KVType.BINARY_BLOB:
                var length = reader.ReadInt32();

                if (currentBinaryBytesOffset > -1)
                {
                    reader.BaseStream.Position = currentBinaryBytesOffset;
                }

                parent.AddProperty(name, MakeValue(datatype, reader.ReadBytes(length), flagInfo));

                if (currentBinaryBytesOffset > -1)
                {
                    currentBinaryBytesOffset   = reader.BaseStream.Position;
                    reader.BaseStream.Position = currentOffset + 4;
                }

                break;

            case KVType.ARRAY:
                var arrayLength = reader.ReadInt32();
                var array       = new KVObject(name, true);
                for (var i = 0; i < arrayLength; i++)
                {
                    ParseBinaryKV3(reader, array, true);
                }

                parent.AddProperty(name, MakeValue(datatype, array, flagInfo));
                break;

            case KVType.ARRAY_TYPED:
                var typeArrayLength = reader.ReadInt32();
                var(subType, subFlagInfo) = ReadType(reader);
                var typedArray = new KVObject(name, true);

                for (var i = 0; i < typeArrayLength; i++)
                {
                    ReadBinaryValue(name, subType, subFlagInfo, reader, typedArray);
                }

                parent.AddProperty(name, MakeValue(datatype, typedArray, flagInfo));
                break;

            case KVType.OBJECT:
                var objectLength = reader.ReadInt32();
                var newObject    = new KVObject(name, false);
                for (var i = 0; i < objectLength; i++)
                {
                    ParseBinaryKV3(reader, newObject, false);
                }

                if (parent == null)
                {
                    parent = newObject;
                }
                else
                {
                    parent.AddProperty(name, MakeValue(datatype, newObject, flagInfo));
                }

                break;

            default:
                throw new InvalidDataException($"Unknown KVType {datatype} for field '{name}' on byte {reader.BaseStream.Position - 1}");
            }

            return(parent);
        }
 public KVFlaggedValue(KVType type, KVFlag flag, object value)
     : base(type, value)
 {
     Flag = flag;
 }
 public KVFlaggedValue(KVType type, object value)
     : base(type, value)
 {
     Flag = KVFlag.None;
 }
        //Print a value in the correct representation
        private void PrintValue(IndentedTextWriter writer, KVValue kvValue)
        {
            KVType type      = kvValue.Type;
            object value     = kvValue.Value;
            var    flagValue = kvValue as KVFlaggedValue;

            if (flagValue != null)
            {
                switch (flagValue.Flag)
                {
                case KVFlag.Resource:
                    writer.Write("resource:");
                    break;

                case KVFlag.DeferredResource:
                    writer.Write("deferred_resource:");
                    break;

                default:
                    throw new InvalidOperationException("Trying to print unknown flag");
                }
            }

            switch (type)
            {
            case KVType.OBJECT:
            case KVType.ARRAY:
                ((KVObject)value).Serialize(writer);
                break;

            case KVType.FLAGGED_STRING:
                writer.Write((string)value);
                break;

            case KVType.STRING:
                writer.Write("\"");
                writer.Write(EscapeUnescaped((string)value, '"'));
                writer.Write("\"");
                break;

            case KVType.STRING_MULTI:
                writer.Write("\"\"\"\n");
                writer.Write((string)value);
                writer.Write("\n\"\"\"");
                break;

            case KVType.BOOLEAN:
                writer.Write((bool)value ? "true" : "false");
                break;

            case KVType.DOUBLE:
                writer.Write(((double)value).ToString("#0.000000", CultureInfo.InvariantCulture));
                break;

            case KVType.INTEGER:
                writer.Write((long)value);
                break;

            case KVType.NULL:
                writer.Write("null");
                break;

            default:
                // Unknown type encountered
                throw new InvalidOperationException("Trying to print unknown type.");
            }
        }
Beispiel #11
0
 public KeyValueSpeedTest(KVType kvType)
 {
     _kvType       = kvType;
     _fastInMemory = true;
 }
Beispiel #12
0
 public KeyValueSpeedTest(KVType kvType, bool inMemory, bool memoryMapped)
 {
     _kvType       = kvType;
     _inMemory     = inMemory;
     _memoryMapped = memoryMapped;
 }
 public KVValue(KVType type, object value)
 {
     Type = type;
     Value = value;
 }