private static bool TryReadMap(ref DdbReader reader, ref DdbEntityReadStackFrame frame)
        {
            if (!TryReadMap(ref reader, out var document))
            {
                return(false);
            }

            frame.AttributesBuffer.Add(document);
            return(true);
        }
        private static bool TryReadNumberSet(ref DdbReader reader, ref DdbEntityReadStackFrame frame)
        {
            if (!TryReadNumberSet(ref reader, out var value))
            {
                return(false);
            }

            frame.AttributesBuffer.Add(new AttributeValue(new NumberSetAttributeValue(value)));
            return(true);
        }
        private static void ReadCore <T>(ref JsonReaderState readerState, bool isFinalBlock, ReadOnlySpan <byte> buffer, ref DdbEntityReadStack readStack) where T : class
        {
            var ddbReader = new DdbReader(buffer, isFinalBlock, ref readerState, ref readStack)
            {
                State = { ReadAhead = !isFinalBlock, BytesConsumed = 0 }
            };

            ReadCore <T>(ref ddbReader);

            readerState = ddbReader.JsonReaderValue.CurrentState;
            readStack   = ddbReader.State;
        }
Esempio n. 4
0
        /// <summary>
        /// A bridge from direct read (custom public converters) to resumable read (internal convreters that use state)
        /// Only used when custom converter calls internal resumable converter
        /// A bridge is required because internal resumable converters don't have direct <c>Read</c> implementation and we need to do some preparations before calling <c>TryRead</c>.
        /// </summary>
        public sealed override T Read(ref DdbReader reader)
        {
            // At this point current json value is guaranteed to fit in the buffer
            // Disable ReadAhead to not do unnecessary TrySkip calls
            // Enable UseFastPath to improve performance
            var oldReadAhead   = reader.State.ReadAhead;
            var oldUseFastPath = reader.State.UseFastPath;

            reader.State.ReadAhead   = false;
            reader.State.UseFastPath = true;

            ref var current = ref reader.State.GetCurrent();
Esempio n. 5
0
 public static bool TryReadValue(ref DdbReader reader, ref DdbEntityReadStackFrame frame)
 {
     return(reader.JsonReaderValue.TokenType switch
     {
         JsonTokenType.String => TryReadString(ref reader, ref frame),
         JsonTokenType.Number => TryReadNumber(ref reader, ref frame),
         JsonTokenType.True => TryReadTrue(ref reader, ref frame),
         JsonTokenType.False => TryReadFalse(ref reader, ref frame),
         JsonTokenType.StartObject => TryReadMap(ref reader, ref frame),
         JsonTokenType.StartArray => TryReadList(ref reader, ref frame),
         JsonTokenType.Null => TryReadNull(ref reader, ref frame),
         _ => throw new ArgumentOutOfRangeException()
     });
Esempio n. 6
0
        private static bool TryReadBinarySet(ref DdbReader reader, out List <byte[]> value)
        {
            var success = false;

            reader.State.PushDocument();

            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (reader.State.UseFastPath)
                {
                    reader.JsonReaderValue.ReadWithVerify();

                    while (reader.JsonReaderValue.TokenType != JsonTokenType.EndArray)
                    {
                        current.AttributesBuffer.Add(new AttributeValue(new BinaryAttributeValue(reader.JsonReaderValue.GetBytesFromBase64())));

                        reader.JsonReaderValue.ReadWithVerify();
                    }

                    value = CreateBinarySetFromBuffer(ref current.AttributesBuffer);

                    return(success = true);
                }
                else
                {
                    Unsafe.SkipInit(out value);

                    while (true)
                    {
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }

                        current.AttributesBuffer.Add(new AttributeValue(new BinaryAttributeValue(reader.JsonReaderValue.GetBytesFromBase64())));
                    }

                    value = CreateBinarySetFromBuffer(ref current.AttributesBuffer);

                    return(success = true);
                }
            }
        private static bool TryReadNumberSet(ref DdbReader reader, out HashSet <string> value)
        {
            var success = false;

            reader.State.PushDocument();

            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (reader.State.UseFastPath)
                {
                    reader.JsonReaderValue.ReadWithVerify();

                    while (reader.JsonReaderValue.TokenType != JsonTokenType.EndArray)
                    {
                        current.StringBuffer.Add(reader.JsonReaderValue.GetString() !);

                        reader.JsonReaderValue.ReadWithVerify();
                    }

                    value = CreateNumberArrayFromBuffer(ref current.StringBuffer);

                    return(success = true);
                }
                else
                {
                    Unsafe.SkipInit(out value);

                    while (true)
                    {
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }

                        current.StringBuffer.Add(reader.JsonReaderValue.GetString() !);
                    }

                    value = CreateNumberArrayFromBuffer(ref current.StringBuffer);

                    return(success = true);
                }
            }
 public static bool TryReadValue(ref DdbReader reader, ref DdbEntityReadStackFrame frame)
 {
     return(frame.AttributeType switch
     {
         AttributeType.String => TryReadString(ref reader, ref frame),
         AttributeType.Number => TryReadNumber(ref reader, ref frame),
         AttributeType.Bool => TryReadBool(ref reader, ref frame),
         AttributeType.Map => TryReadMap(ref reader, ref frame),
         AttributeType.List => TryReadList(ref reader, ref frame),
         AttributeType.StringSet => TryReadStringSet(ref reader, ref frame),
         AttributeType.NumberSet => TryReadNumberSet(ref reader, ref frame),
         AttributeType.Null => TryReadNull(ref reader, ref frame),
         AttributeType.Binary => TryReadBinary(ref reader, ref frame),
         AttributeType.BinarySet => TryReadBinarySet(ref reader, ref frame),
         _ => throw new ArgumentOutOfRangeException()
     });
        public bool TryReadRoot(ref DdbReader reader, out IReadOnlyList <Document> value)
        {
            var success = false;

            reader.State.PushDocument();

            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (DocumentJsonReader.TryReadBuffer(ref reader, ref current))
                {
                    value = CreateDocumentsArray(current.AttributesBuffer);
                    return(success = true);
                }

                Unsafe.SkipInit(out value);
                return(success = false);
            }
Esempio n. 10
0
        private static bool TryReadList(ref DdbReader reader, out List <AttributeValue> value)
        {
            var success = false;

            reader.State.PushDocument();

            try
            {
                ref var current = ref reader.State.GetCurrent();

                // ReSharper disable once AssignmentInConditionalExpression
                if (success = TryReadBuffer(ref reader, ref current))
                {
                    value = CreateListFromBuffer(ref current.AttributesBuffer);
                }
                else
                {
                    Unsafe.SkipInit(out value);
                }

                return(success);
            }
        private static bool TryReadList(ref DdbReader reader, out List <AttributeValue> value)
        {
            var success = false;

            reader.State.PushDocument();

            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (reader.State.UseFastPath)
                {
                    while (true)
                    {
                        // Start object or end array
                        reader.JsonReaderValue.ReadWithVerify();

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }

                        // Attribute type
                        reader.JsonReaderValue.ReadWithVerify();
                        current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);

                        // Attribute value
                        reader.JsonReaderValue.ReadWithVerify();

                        TryReadValue(ref reader, ref current);

                        // End object
                        reader.JsonReaderValue.ReadWithVerify();
                    }

                    value = CreateListFromBuffer(ref current.AttributesBuffer);

                    return(success = true);
                }
                else
                {
                    Unsafe.SkipInit(out value);

                    if (current.PropertyState != DdbStackFramePropertyState.None)
                    {
                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueType)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                            current.PropertyState = DdbStackFramePropertyState.ReadValueType;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValue)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValue;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.TryRead)
                        {
                            if (!TryReadValue(ref reader, ref current))
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.TryRead;
                        }

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    while (true)
                    {
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.ReadValueStart;

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndArray)
                        {
                            break;
                        }

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                        current.PropertyState = DdbStackFramePropertyState.ReadValueType;

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.ReadValue;

                        if (!TryReadValue(ref reader, ref current))
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.TryRead;

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    value = CreateListFromBuffer(ref current.AttributesBuffer);

                    return(success = true);
                }
            }
        public static bool TryReadMap(ref DdbReader reader, out Document value)
        {
            var success = false;

            reader.State.PushDocument();
            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (reader.State.UseFastPath)
                {
                    while (true)
                    {
                        // Property name
                        reader.JsonReaderValue.ReadWithVerify();

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                        {
                            break;
                        }

                        current.StringBuffer.Add(GetCachedString(ref reader.JsonReaderValue, ref reader.State));

                        // Attribute value
                        reader.JsonReaderValue.ReadWithVerify();

                        TryReadValue(ref reader, ref current);
                    }

                    value = DocumentJsonReader.CreateDocumentFromBuffer(ref current) !;

                    return(success = true);
                }
                else
                {
                    Unsafe.SkipInit(out value);

                    if (current.PropertyState != DdbStackFramePropertyState.None)
                    {
                        if (current.PropertyState < DdbStackFramePropertyState.Name)
                        {
                            current.StringBuffer.Add(GetCachedString(ref reader.JsonReaderValue, ref reader.State));
                            current.PropertyState = DdbStackFramePropertyState.Name;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValue)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValue;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.TryRead)
                        {
                            if (!TryReadValue(ref reader, ref current))
                            {
                                return(success = false);
                            }
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    while (true)
                    {
                        // Property name
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                        {
                            break;
                        }

                        current.StringBuffer.Add(GetCachedString(ref reader.JsonReaderValue, ref reader.State));
                        current.PropertyState = DdbStackFramePropertyState.Name;

                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.ReadValue;

                        if (!TryReadValue(ref reader, ref current))
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    value = DocumentJsonReader.CreateDocumentFromBuffer(ref current);
                    return(success = true);
                }
            }
Esempio n. 13
0
        internal sealed override bool TryRead(ref DdbReader reader, out TSet value)
        {
            var success = false;

            reader.State.Push();

            try
            {
                ref var current = ref reader.State.GetCurrent();

                if (reader.State.UseFastPath)
                {
                    value = CreateSet();

                    reader.JsonReaderValue.ReadWithVerify();

                    while (reader.JsonReaderValue.TokenType != JsonTokenType.EndArray)
                    {
                        value.Add(ElementConverter.Read(ref reader));

                        reader.JsonReaderValue.ReadWithVerify();
                    }

                    return(success = true);
                }
                else
                {
                    if (current.ObjectState < DdbStackFrameObjectState.CreatedObject)
                    {
                        current.ReturnValue = value = CreateSet();
                        current.ObjectState = DdbStackFrameObjectState.CreatedObject;
                    }
                    else
                    {
                        value = (TSet)current.ReturnValue !;
                    }

                    while (true)
                    {
                        if (current.PropertyState < DdbStackFramePropertyState.ReadValue)
                        {
                            if (!SingleValueReadWithReadAhead(ElementConverter.CanSeek, ref reader))
                            {
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValue;

                            if (reader.JsonReaderValue.TokenType == JsonTokenType.EndArray)
                            {
                                break;
                            }
                        }

                        value.Add(ElementConverter.Read(ref reader));

                        current.PropertyState = DdbStackFramePropertyState.None;
                    }

                    return(success = true);
                }
            }
Esempio n. 14
0
 public bool TryReadRoot(ref DdbReader reader, out Document value) => DocumentJsonReader.TryReadMap(ref reader, out value);
Esempio n. 15
0
        internal override bool TryRead(ref DdbReader reader, out TDictionary value)
        {
            Unsafe.SkipInit(out value);
            var success = false;

            reader.State.Push();
            try
            {
                ref var current = ref reader.State.GetCurrent();
                Dictionary <TKey, TValue> entity;

                if (reader.State.UseFastPath)
                {
                    entity = new Dictionary <TKey, TValue>();

                    if (ValueConverter.UseDirectRead)
                    {
                        while (true)
                        {
                            // Property name
                            reader.JsonReaderValue.ReadWithVerify();

                            if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                            {
                                break;
                            }

                            var pairKey = KeyConverter.Read(ref reader);

                            // Start object
                            reader.JsonReaderValue.ReadWithVerify();

                            // Attribute type
                            reader.JsonReaderValue.ReadWithVerify();

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);

                            // Attribute value
                            reader.JsonReaderValue.ReadWithVerify();

                            entity.Add(pairKey, ValueConverter.Read(ref reader));

                            // End object
                            reader.JsonReaderValue.ReadWithVerify();
                        }
                    }
                    else
                    {
                        while (true)
                        {
                            // Property name
                            reader.JsonReaderValue.ReadWithVerify();

                            if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                            {
                                break;
                            }

                            var pairKey = KeyConverter.Read(ref reader);

                            // Start object
                            reader.JsonReaderValue.ReadWithVerify();

                            // Attribute type
                            reader.JsonReaderValue.ReadWithVerify();

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);

                            // Attribute value
                            reader.JsonReaderValue.ReadWithVerify();

                            ValueConverter.TryRead(ref reader, out var pairValue);
                            entity.Add(pairKey, pairValue);

                            // End object
                            reader.JsonReaderValue.ReadWithVerify();
                        }
                    }

                    value = ToResult(entity);

                    return(success = true);
                }
                else
                {
                    if (current.ObjectState < DdbStackFrameObjectState.CreatedObject)
                    {
                        current.ReturnValue = entity = new Dictionary <TKey, TValue>();
                        current.ObjectState = DdbStackFrameObjectState.CreatedObject;
                    }
                    else
                    {
                        entity = (Dictionary <TKey, TValue>)current.ReturnValue !;
                    }

                    while (true)
                    {
                        if (current.PropertyState < DdbStackFramePropertyState.ReadName)
                        {
                            // Property name
                            if (!reader.JsonReaderValue.Read())
                            {
                                return(success = false);
                            }
                        }

                        TKey pairKey;
                        if (current.PropertyState < DdbStackFramePropertyState.Name)
                        {
                            if (reader.JsonReaderValue.TokenType == JsonTokenType.EndObject)
                            {
                                break;
                            }

                            pairKey = KeyConverter.Read(ref reader);
                            current.PropertyState = DdbStackFramePropertyState.Name;
                        }
                        else
                        {
                            pairKey = (TKey)current.DictionaryKey !;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueStart)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                current.DictionaryKey = pairKey;
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValueStart;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValueType)
                        {
                            if (!reader.JsonReaderValue.Read())
                            {
                                current.DictionaryKey = pairKey;
                                return(success = false);
                            }

                            current.AttributeType = DdbJsonReader.GetDdbAttributeType(ref reader.JsonReaderValue);
                            current.PropertyState = DdbStackFramePropertyState.ReadValueType;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.ReadValue)
                        {
                            if (!SingleValueReadWithReadAhead(ValueConverter.CanSeek, ref reader))
                            {
                                current.DictionaryKey = pairKey;
                                return(success = false);
                            }

                            current.PropertyState = DdbStackFramePropertyState.ReadValue;
                        }

                        if (current.PropertyState < DdbStackFramePropertyState.TryRead)
                        {
                            TValue pairValue;
                            if (ValueConverter.UseDirectRead)
                            {
                                pairValue = ValueConverter.Read(ref reader);
                            }
                            else
                            {
                                if (!ValueConverter.TryRead(ref reader, out pairValue))
                                {
                                    current.DictionaryKey = pairKey;
                                    return(success = false);
                                }
                            }

                            entity.Add(pairKey, pairValue);

                            current.PropertyState = DdbStackFramePropertyState.TryRead;
                        }

                        // End object
                        if (!reader.JsonReaderValue.Read())
                        {
                            return(success = false);
                        }

                        current.PropertyState = DdbStackFramePropertyState.None;
                        current.DictionaryKey = null;
                        current.PropertyInfo  = null;
                    }

                    value = ToResult(entity);
                    return(success = true);
                }
            }
Esempio n. 16
0
 internal override bool TryRead(ref DdbReader reader, out TCollection value)
 {
     if (reader.AttributeType == AttributeType.Null)
     {
         value = default !;
 private static void ReadCore <T>(ref DdbReader reader) where T : class
 {
     ref var current = ref reader.State.GetCurrent();