private unsafe void ReadJsonValue()
        {
            int             start   = 1;
            JsonParserToken current = _state.CurrentTokenType;

            if (current == JsonParserToken.String)
            {
                BlittableJsonToken stringToken = BlittableJsonToken.Null;

                if (_state.EscapePositions.Count == 0 && _state.CompressedSize == null && _state.StringSize < 128)
                {
                    stringToken = BlittableJsonToken.String;
                }

                _state.CompressedSize = null;
                _writeToken           = new WriteToken(start, stringToken);
            }
            else if (current == JsonParserToken.Integer)
            {
                // start = _writer.WriteValue(_state.Long);
                _writeToken = new WriteToken(start, BlittableJsonToken.Integer);
            }
            else if (current == JsonParserToken.StartObject)
            {
                _continuationState.Push(new BuildingState(ContinuationState.ReadObject));
            }
            else if (current != JsonParserToken.EndObject)
            {
                ReadJsonValueUnlikely(current);
            }
        }
Ejemplo n.º 2
0
        private void requestResponseThread(WriteToken token, String str, StringListCallback callback)
        {
            writeToPipe(token, str);
            String        response          = waitForResponse();
            List <String> formattedResponse = formatResponse(response);

            callback(formattedResponse);
        }
Ejemplo n.º 3
0
 public void Reset()
 {
     _debugTag = null;
     _mode     = UsageMode.None;
     _continuationState.Clear();
     _writeToken = default(WriteToken);
     _writer.Reset();
 }
Ejemplo n.º 4
0
        public async Task WriteTokenLogicSignup()
        {
            var token = new WriteToken {
                UserId = UserData.FirstId, JwtKey = "xxx123456xxx123456xxx"
            };
            await token.Execute();

            token.Result.Should().BeTrue();
            token.Verification.Should().NotBeNullOrEmpty();
        }
        public void Reset()
        {
            _debugTag = null;
            _mode     = UsageMode.None;

            ClearState();

            _writeToken = default;
            _writer.Reset();
        }
Ejemplo n.º 6
0
 private void writeToPipe(WriteToken token, String str)
 {
     byte[] stringBytes = Encoding.Unicode.GetBytes(str);
     byte[] sendBytes   = new byte[stringBytes.Length + 3];
     sendBytes[0] = (byte)token;
     Array.Copy(stringBytes, 0, sendBytes, 1, stringBytes.Length);
     sendBytes[sendBytes.Length - 1] = 0;
     sendBytes[sendBytes.Length - 2] = 0; // end with a null terminator
     if (pipeServer != null)
     {
         pipeServer.Write(sendBytes, 0, sendBytes.Length);
     }
 }
        public void Renew(string debugTag, UsageMode mode)
        {
            _writeToken = default;
            _debugTag   = debugTag;
            _mode       = mode;

            ClearState();

            _writer.ResetAndRenew();
            _modifier?.Reset(_context);

            _fakeFieldName = _context.GetLazyStringForFieldWithCaching(UnderscoreSegment);
        }
Ejemplo n.º 8
0
        public unsafe void WriteEmbeddedBlittableDocument(byte* ptr, int size)
        {
            var currentState = _continuationState.Pop();
            var valuePos = _writer.WriteValue(ptr, size, out _, UsageMode.None, null);
            _writeToken = new WriteToken
            {
                ValuePos = valuePos,
                WrittenToken = BlittableJsonToken.EmbeddedBlittable
            };

            if (currentState.FirstWrite == -1)
                currentState.FirstWrite = valuePos;

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
Ejemplo n.º 9
0
        public void WriteValue(LazyNumberValue value)
        {
            var currentState = _continuationState.Pop();
            var valuePos = _writer.WriteValue(value);
            _writeToken = new WriteToken
            {
                ValuePos = valuePos,
                WrittenToken = BlittableJsonToken.LazyNumber
            };

            if (currentState.FirstWrite == -1)
                currentState.FirstWrite = valuePos;

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
        private unsafe void ReadJsonValue <TWriteStrategy>() where TWriteStrategy : IWriteStrategy
        {
            int             start;
            JsonParserToken current = _state.CurrentTokenType;

            if (current == JsonParserToken.String)
            {
                BlittableJsonToken stringToken;
                if (typeof(TWriteStrategy) == typeof(WriteNone))
                {
                    start = _writer.WriteValue(_state.StringBuffer, _state.StringSize, _state.EscapePositions, out stringToken, _mode, _state.CompressedSize);
                }
                else // WriteFull
                {
                    if (_state.EscapePositions.Count == 0 && _state.CompressedSize == null && (_mode & UsageMode.CompressSmallStrings) == 0 && _state.StringSize < 128)
                    {
                        start       = _writer.WriteValue(_state.StringBuffer, _state.StringSize);
                        stringToken = BlittableJsonToken.String;
                    }
                    else
                    {
                        start = _writer.WriteValue(_state.StringBuffer, _state.StringSize, _state.EscapePositions, out stringToken, _mode, _state.CompressedSize);
                    }
                }
                _state.CompressedSize = null;
                _writeToken           = new WriteToken(start, stringToken);
            }
            else if (current == JsonParserToken.Integer)
            {
                start       = _writer.WriteValue(_state.Long);
                _writeToken = new WriteToken(start, BlittableJsonToken.Integer);
            }
            else if (current == JsonParserToken.StartObject)
            {
                _modifier?.StartObject();
                _continuationState.Push(new BuildingState(ContinuationState.ReadObject));
            }
            else if (current == JsonParserToken.Blob)
            {
                start       = _writer.WriteValue(_state.StringBuffer, _state.StringSize);
                _writeToken = new WriteToken(start, BlittableJsonToken.RawBlob);
            }
            else if (current != JsonParserToken.EndObject)
            {
                ReadJsonValueUnlikely <TWriteStrategy>(current);
            }
        }
Ejemplo n.º 11
0
        public void WriteValue(string value)
        {
            var currentState = _continuationState.Pop();
            BlittableJsonToken stringToken;
            var valuePos = _writer.WriteValue(value, out stringToken, _mode);
            _writeToken = new WriteToken
            {
                ValuePos = valuePos,
                WrittenToken = stringToken
            };

            if (currentState.FirstWrite == -1)
                currentState.FirstWrite = valuePos;

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
        private unsafe void ReadJsonValueUnlikely <TWriteStrategy>(JsonParserToken current) where TWriteStrategy : IWriteStrategy
        {
            int start;

            switch (current)
            {
            case JsonParserToken.StartArray:
                _continuationState.Push(new BuildingState(ContinuationState.ReadArray));
                return;

            case JsonParserToken.Float:
                if ((_mode & UsageMode.ValidateDouble) == UsageMode.ValidateDouble)
                {
                    _reader.ValidateFloat();
                }

                if (typeof(TWriteStrategy) == typeof(WriteNone))
                {
                    start = _writer.WriteValue(_state.StringBuffer, _state.StringSize);
                }
                else
                {
                    start = _writer.WriteValue(_state.StringBuffer, _state.StringSize, out _, _mode, _state.CompressedSize);
                }

                _state.CompressedSize = null;
                _writeToken           = new WriteToken(start, BlittableJsonToken.LazyNumber);
                return;

            case JsonParserToken.True:
            case JsonParserToken.False:
                start       = _writer.WriteValue(current == JsonParserToken.True ? (byte)1 : (byte)0);
                _writeToken = new WriteToken(start, BlittableJsonToken.Boolean);
                return;

            case JsonParserToken.Null:
                // nothing to do here, we handle that with the token
                start       = _writer.WriteValue((byte)0);
                _writeToken = new WriteToken(start, BlittableJsonToken.Null);
                return;
            }

            ThrowExpectedValue(current);
        }
        public void WriteValue(LazyNumberValue value)
        {
            var currentState = _continuationState.Pop();
            var valuePos     = _writer.WriteValue(value);

            _writeToken = new WriteToken //todo: figure out if we really need those WriteTokens
            {
                ValuePos     = valuePos,
                WrittenToken = BlittableJsonToken.LazyNumber
            };

            if (currentState.FirstWrite == -1)
            {
                currentState.FirstWrite = valuePos;
            }

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
        public void WriteValueNull()
        {
            var currentState = _continuationState.Pop();
            var valuePos     = _writer.WriteNull();

            _writeToken = new WriteToken
            {
                ValuePos     = valuePos,
                WrittenToken = BlittableJsonToken.Null
            };

            if (currentState.FirstWrite == -1)
            {
                currentState.FirstWrite = valuePos;
            }

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
Ejemplo n.º 15
0
        public void WriteValue(LazyCompressedStringValue value)
        {
            var currentState = _continuationState.Pop();
            BlittableJsonToken stringToken;

            //public unsafe int WriteValue(byte* buffer, int size, out BlittableJsonToken token, UsageMode mode, int? initialCompressedSize)
            //var valuePos = _writer.WriteValue(value, out stringToken, UsageMode.None, null);
            var valuePos = _writer.WriteValue(value, out stringToken, UsageMode.None);
            _writeToken = new WriteToken
            {
                ValuePos = valuePos,
                WrittenToken = stringToken
            };

            if (currentState.FirstWrite == -1)
                currentState.FirstWrite = valuePos;

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
        public unsafe void WriteEmbeddedBlittableDocument(BlittableJsonReaderObject document)
        {
            var currentState = _continuationState.Pop();
            BlittableJsonToken token;
            var valuePos = _writer.WriteValue(document.BasePointer, document.Size, null, out token, UsageMode.None, null);

            _writeToken = new WriteToken
            {
                ValuePos     = valuePos,
                WrittenToken = BlittableJsonToken.EmbeddedBlittable
            };

            if (currentState.FirstWrite == -1)
            {
                currentState.FirstWrite = valuePos;
            }

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
Ejemplo n.º 17
0
        public void WriteValue(LazyStringValue value)
        {
            var currentState = _continuationState.Pop();
            BlittableJsonToken stringToken;

            var valuePos = _writer.WriteValue(value, out stringToken, UsageMode.None, null);

            _writeToken = new WriteToken
            {
                ValuePos     = valuePos,
                WrittenToken = stringToken
            };

            if (currentState.FirstWrite == -1)
            {
                currentState.FirstWrite = valuePos;
            }

            currentState = FinishWritingScalarValue(currentState);
            _continuationState.Push(currentState);
        }
Ejemplo n.º 18
0
        private bool ReadInternal <TWriteStrategy>() where TWriteStrategy : IWriteStrategy
        {
            var continuationState = _continuationState;
            var currentState      = continuationState.Pop();
            var reader            = _reader;
            var state             = _state;

            while (true)
            {
                switch (currentState.State)
                {
                case ContinuationState.ReadObjectDocument:
                    if (reader.Read() == false)
                    {
                        continuationState.Push(currentState);
                        goto ReturnFalse;
                    }
                    currentState.State = ContinuationState.ReadObject;
                    continue;

                case ContinuationState.ReadArrayDocument:
                    if (reader.Read() == false)
                    {
                        continuationState.Push(currentState);
                        goto ReturnFalse;
                    }

                    var fakeProperty = _context.CachedProperties.GetProperty(_fakeFieldName);
                    currentState.CurrentProperty = fakeProperty;
                    currentState.MaxPropertyId   = fakeProperty.PropertyId;
                    currentState.FirstWrite      = _writer.Position;
                    currentState.Properties      = _propertiesCache.Allocate();
                    currentState.Properties.Add(new PropertyTag {
                        Property = fakeProperty
                    });
                    currentState.State = ContinuationState.CompleteDocumentArray;
                    continuationState.Push(currentState);
                    currentState = new BuildingState(ContinuationState.ReadArray);
                    continue;

                case ContinuationState.CompleteDocumentArray:
                    currentState.Properties[0] = new PropertyTag(
                        type: (byte)_writeToken.WrittenToken,
                        property: currentState.Properties[0].Property,
                        position: _writeToken.ValuePos
                        );

                    // Register property position, name id (PropertyId) and type (object type and metadata)
                    _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite, currentState.MaxPropertyId);
                    goto ReturnTrue;

                case ContinuationState.ReadObject:
                    if (state.CurrentTokenType == JsonParserToken.StartObject)
                    {
                        currentState.State      = ContinuationState.ReadPropertyName;
                        currentState.Properties = _propertiesCache.Allocate();
                        currentState.FirstWrite = _writer.Position;
                        continue;
                    }

                    goto ErrorExpectedStartOfObject;

                case ContinuationState.ReadArray:
                    if (state.CurrentTokenType == JsonParserToken.StartArray)
                    {
                        currentState.Types     = _tokensCache.Allocate();
                        currentState.Positions = _positionsCache.Allocate();
                        currentState.State     = ContinuationState.ReadArrayValue;
                        continue;
                    }

                    goto ErrorExpectedStartOfArray;

                case ContinuationState.ReadArrayValue:
                    if (reader.Read() == false)
                    {
                        continuationState.Push(currentState);
                        goto ReturnFalse;
                    }

                    if (state.CurrentTokenType == JsonParserToken.EndArray)
                    {
                        currentState.State = ContinuationState.CompleteArray;
                        continue;
                    }

                    currentState.State = ContinuationState.CompleteArrayValue;
                    continuationState.Push(currentState);
                    currentState = new BuildingState(ContinuationState.ReadValue);
                    continue;

                case ContinuationState.CompleteArrayValue:
                    currentState.Types.Add(_writeToken.WrittenToken);
                    currentState.Positions.Add(_writeToken.ValuePos);
                    currentState.State = ContinuationState.ReadArrayValue;
                    continue;

                case ContinuationState.CompleteArray:
                    var arrayToken     = BlittableJsonToken.StartArray;
                    var arrayInfoStart = _writer.WriteArrayMetadata(currentState.Positions, currentState.Types, ref arrayToken);
                    _writeToken  = new WriteToken(arrayInfoStart, arrayToken);
                    currentState = continuationState.Pop();
                    continue;

                case ContinuationState.ReadPropertyName:
                    if (ReadMaybeModifiedPropertyName() == false)
                    {
                        continuationState.Push(currentState);
                        goto ReturnFalse;
                    }

                    if (state.CurrentTokenType == JsonParserToken.EndObject)
                    {
                        _modifier?.EndObject();
                        _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite, currentState.MaxPropertyId);
                        if (continuationState.Count == 0)
                        {
                            goto ReturnTrue;
                        }

                        currentState = continuationState.Pop();
                        continue;
                    }

                    if (state.CurrentTokenType != JsonParserToken.String)
                    {
                        goto ErrorExpectedProperty;
                    }

                    var property = CreateLazyStringValueFromParserState();
                    currentState.CurrentProperty = _context.CachedProperties.GetProperty(property);
                    currentState.MaxPropertyId   = Math.Max(currentState.MaxPropertyId, currentState.CurrentProperty.PropertyId);
                    currentState.State           = ContinuationState.ReadPropertyValue;
                    continue;

                case ContinuationState.ReadPropertyValue:
                    if (reader.Read() == false)
                    {
                        continuationState.Push(currentState);
                        goto ReturnFalse;
                    }
                    currentState.State = ContinuationState.CompleteReadingPropertyValue;
                    continuationState.Push(currentState);
                    currentState = new BuildingState(ContinuationState.ReadValue);
                    continue;

                case ContinuationState.CompleteReadingPropertyValue:
                    // Register property position, name id (PropertyId) and type (object type and metadata)
                    currentState.Properties.Add(new PropertyTag(
                                                    position: _writeToken.ValuePos,
                                                    type: (byte)_writeToken.WrittenToken,
                                                    property: currentState.CurrentProperty));

                    currentState.State = ContinuationState.ReadPropertyName;
                    continue;

                case ContinuationState.ReadValue:
                    ReadJsonValue <TWriteStrategy>();
                    currentState = _continuationState.Pop();
                    break;
                }
            }

            ReturnTrue : return(true);

            ReturnFalse : return(false);

            ErrorExpectedProperty      : ThrowExpectedProperty();
            ErrorExpectedStartOfObject : ThrowExpectedStartOfObject();
            ErrorExpectedStartOfArray  : ThrowExpectedStartOfArray();
            return(false); // Will never execute.
        }
Ejemplo n.º 19
0
        private unsafe void ReadJsonValue()
        {
            int start;

            switch (_state.CurrentTokenType)
            {
            case JsonParserToken.StartObject:
                _continuationState.Push(new BuildingState
                {
                    State = ContinuationState.ReadObject
                });
                return;

            case JsonParserToken.StartArray:
                _continuationState.Push(new BuildingState
                {
                    State = ContinuationState.ReadArray
                });
                return;

            case JsonParserToken.Integer:
                start       = _writer.WriteValue(_state.Long);
                _writeToken = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = BlittableJsonToken.Integer
                };
                return;

            case JsonParserToken.Float:
                if ((_mode & UsageMode.ValidateDouble) == UsageMode.ValidateDouble)
                {
                    _reader.ValidateFloat();
                }
                BlittableJsonToken ignored;

                start = _writer.WriteValue(_state.StringBuffer, _state.StringSize, out ignored, _mode, _state.CompressedSize);
                _state.CompressedSize = null;
                _writeToken           = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = BlittableJsonToken.Float
                };
                return;

            case JsonParserToken.String:
                BlittableJsonToken stringToken;
                start = _writer.WriteValue(_state.StringBuffer, _state.StringSize, _state.EscapePositions, out stringToken, _mode, _state.CompressedSize);
                _state.CompressedSize = null;
                _writeToken           = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = stringToken
                };
                return;

            case JsonParserToken.True:
            case JsonParserToken.False:
                start       = _writer.WriteValue(_state.CurrentTokenType == JsonParserToken.True ? (byte)1 : (byte)0);
                _writeToken = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = BlittableJsonToken.Boolean
                };
                return;

            case JsonParserToken.Null:
                start       = _writer.WriteValue((byte)0);
                _writeToken = new WriteToken     // nothing to do here, we handle that with the token
                {
                    WrittenToken = BlittableJsonToken.Null,
                    ValuePos     = start
                };
                return;

            default:
                throw new InvalidDataException("Expected a value, but got " + _state.CurrentTokenType);
            }
        }
Ejemplo n.º 20
0
        public virtual bool Read()
        {
            if (_continuationState.Count == 0)
            {
                return(false); //nothing to do
            }
            var currentState = _continuationState.Pop();

            while (true)
            {
                switch (currentState.State)
                {
                case ContinuationState.ReadObjectDocument:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }
                    currentState.State = ContinuationState.ReadObject;
                    continue;

                case ContinuationState.ReadArrayDocument:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }

                    var fakeFieldName = _context.GetLazyStringForFieldWithCaching("_");
                    var propIndex     = _context.CachedProperties.GetPropertyId(fakeFieldName);
                    currentState.CurrentPropertyId = propIndex;
                    currentState.MaxPropertyId     = propIndex;
                    currentState.FirstWrite        = _writer.Position;
                    currentState.Properties        = new List <PropertyTag>
                    {
                        new PropertyTag
                        {
                            PropertyId = propIndex
                        }
                    };
                    currentState.State = ContinuationState.CompleteDocumentArray;
                    _continuationState.Push(currentState);
                    currentState = new BuildingState
                    {
                        State = ContinuationState.ReadArray
                    };
                    continue;

                case ContinuationState.CompleteDocumentArray:
                    currentState.Properties[0].Type     = (byte)_writeToken.WrittenToken;
                    currentState.Properties[0].Position = _writeToken.ValuePos;

                    // Register property position, name id (PropertyId) and type (object type and metadata)
                    _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite, currentState.MaxPropertyId);

                    return(true);

                case ContinuationState.ReadObject:
                    if (_state.CurrentTokenType != JsonParserToken.StartObject)
                    {
                        throw new InvalidDataException("Expected start of object, but got " + _state.CurrentTokenType);
                    }
                    currentState.State      = ContinuationState.ReadPropertyName;
                    currentState.Properties = new List <PropertyTag>();
                    currentState.FirstWrite = _writer.Position;
                    continue;

                case ContinuationState.ReadArray:
                    if (_state.CurrentTokenType != JsonParserToken.StartArray)
                    {
                        throw new InvalidDataException("Expected start of array, but got " + _state.CurrentTokenType);
                    }
                    currentState.Types     = new List <BlittableJsonToken>();
                    currentState.Positions = new List <int>();
                    currentState.State     = ContinuationState.ReadArrayValue;
                    continue;

                case ContinuationState.ReadArrayValue:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }
                    if (_state.CurrentTokenType == JsonParserToken.EndArray)
                    {
                        currentState.State = ContinuationState.CompleteArray;
                        continue;
                    }
                    currentState.State = ContinuationState.CompleteArrayValue;
                    _continuationState.Push(currentState);
                    currentState = new BuildingState
                    {
                        State = ContinuationState.ReadValue
                    };
                    continue;

                case ContinuationState.CompleteArrayValue:
                    currentState.Types.Add(_writeToken.WrittenToken);
                    currentState.Positions.Add(_writeToken.ValuePos);
                    currentState.State = ContinuationState.ReadArrayValue;
                    continue;

                case ContinuationState.CompleteArray:

                    var arrayToken     = BlittableJsonToken.StartArray;
                    var arrayInfoStart = _writer.WriteArrayMetadata(currentState.Positions, currentState.Types,
                                                                    ref arrayToken);

                    _writeToken = new WriteToken
                    {
                        ValuePos     = arrayInfoStart,
                        WrittenToken = arrayToken
                    };

                    currentState = _continuationState.Pop();
                    continue;

                case ContinuationState.ReadPropertyName:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }

                    if (_state.CurrentTokenType == JsonParserToken.EndObject)
                    {
                        _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite,
                                                                  currentState.MaxPropertyId);
                        if (_continuationState.Count == 0)
                        {
                            return(true);
                        }
                        currentState = _continuationState.Pop();
                        continue;
                    }

                    if (_state.CurrentTokenType != JsonParserToken.String)
                    {
                        throw new InvalidDataException("Expected property, but got " + _state.CurrentTokenType);
                    }


                    var property = CreateLazyStringValueFromParserState();

                    currentState.CurrentPropertyId = _context.CachedProperties.GetPropertyId(property);
                    currentState.MaxPropertyId     = Math.Max(currentState.MaxPropertyId, currentState.CurrentPropertyId);
                    currentState.State             = ContinuationState.ReadPropertyValue;
                    continue;

                case ContinuationState.ReadPropertyValue:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }
                    currentState.State = ContinuationState.CompleteReadingPropertyValue;
                    _continuationState.Push(currentState);
                    currentState = new BuildingState
                    {
                        State = ContinuationState.ReadValue
                    };
                    continue;

                case ContinuationState.CompleteReadingPropertyValue:
                    // Register property position, name id (PropertyId) and type (object type and metadata)
                    currentState.Properties.Add(new PropertyTag
                    {
                        Position   = _writeToken.ValuePos,
                        Type       = (byte)_writeToken.WrittenToken,
                        PropertyId = currentState.CurrentPropertyId
                    });
                    currentState.State = ContinuationState.ReadPropertyName;
                    continue;

                case ContinuationState.ReadValue:
                    ReadJsonValue();
                    currentState = _continuationState.Pop();
                    break;
                }
            }
        }
        public bool Read()
        {
            if (_continuationState.Count == 0)
            {
                return(false); //nothing to do
            }
            var currentState = _continuationState.Pop();

            while (true)
            {
                switch (currentState.State)
                {
                case ContinuationState.ReadObjectDocument:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }
                    currentState.State = ContinuationState.ReadObject;
                    continue;

                case ContinuationState.ReadArrayDocument:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }

                    var fakeFieldName = _context.GetLazyStringForFieldWithCaching("_");
                    var propIndex     = _context.CachedProperties.GetPropertyId(fakeFieldName);
                    currentState.CurrentPropertyId = propIndex;
                    currentState.MaxPropertyId     = propIndex;
                    currentState.FirstWrite        = _position;
                    currentState.Properties        = new List <PropertyTag>
                    {
                        new PropertyTag
                        {
                            PropertyId = propIndex
                        }
                    };
                    currentState.State = ContinuationState.CompleteDocumentArray;
                    _continuationState.Push(currentState);
                    currentState = new BuildingState
                    {
                        State = ContinuationState.ReadArray
                    };
                    continue;

                case ContinuationState.CompleteDocumentArray:
                    currentState.Properties[0].Type     = (byte)_writeToken.WrittenToken;
                    currentState.Properties[0].Position = _writeToken.ValuePos;

                    // Register property position, name id (PropertyId) and type (object type and metadata)
                    _writeToken = FinalizeObjectWrite(currentState.Properties, currentState.FirstWrite, currentState.MaxPropertyId);

                    return(true);

                case ContinuationState.ReadObject:
                    if (_state.CurrentTokenType != JsonParserToken.StartObject)
                    {
                        throw new InvalidDataException("Expected start of object, but got " + _state.CurrentTokenType);
                    }
                    currentState.State      = ContinuationState.ReadPropertyName;
                    currentState.Properties = new List <PropertyTag>();
                    currentState.FirstWrite = _position;
                    continue;

                case ContinuationState.ReadArray:
                    if (_state.CurrentTokenType != JsonParserToken.StartArray)
                    {
                        throw new InvalidDataException("Expected start of array, but got " + _state.CurrentTokenType);
                    }
                    currentState.Types     = new List <BlittableJsonToken>();
                    currentState.Positions = new List <int>();
                    currentState.State     = ContinuationState.ReadArrayValue;
                    continue;

                case ContinuationState.ReadArrayValue:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }
                    if (_state.CurrentTokenType == JsonParserToken.EndArray)
                    {
                        currentState.State = ContinuationState.CompleteArray;
                        continue;
                    }
                    currentState.State = ContinuationState.CompleteArrayValue;
                    _continuationState.Push(currentState);
                    currentState = new BuildingState
                    {
                        State = ContinuationState.ReadValue
                    };
                    continue;

                case ContinuationState.CompleteArrayValue:
                    currentState.Types.Add(_writeToken.WrittenToken);
                    currentState.Positions.Add(_writeToken.ValuePos);
                    currentState.State = ContinuationState.ReadArrayValue;
                    continue;

                case ContinuationState.CompleteArray:
                    var arrayInfoStart = _position;
                    var arrayToken     = BlittableJsonToken.StartArray;

                    _position += WriteVariableSizeInt(currentState.Positions.Count);
                    if (currentState.Positions.Count == 0)
                    {
                        arrayToken |= BlittableJsonToken.OffsetSizeByte;
                        _writeToken = new WriteToken
                        {
                            ValuePos     = arrayInfoStart,
                            WrittenToken = arrayToken
                        };
                    }
                    else
                    {
                        var distanceFromFirstItem = arrayInfoStart - currentState.Positions[0];
                        var distanceTypeSize      = SetOffsetSizeFlag(ref arrayToken, distanceFromFirstItem);

                        for (var i = 0; i < currentState.Positions.Count; i++)
                        {
                            WriteNumber(arrayInfoStart - currentState.Positions[i], distanceTypeSize);
                            _position += distanceTypeSize;

                            _stream.WriteByte((byte)currentState.Types[i]);
                            _position++;
                        }

                        _writeToken = new WriteToken
                        {
                            ValuePos     = arrayInfoStart,
                            WrittenToken = arrayToken
                        };
                    }
                    currentState = _continuationState.Pop();
                    continue;

                case ContinuationState.ReadPropertyName:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }

                    if (_state.CurrentTokenType == JsonParserToken.EndObject)
                    {
                        _writeToken = FinalizeObjectWrite(currentState.Properties, currentState.FirstWrite,
                                                          currentState.MaxPropertyId);
                        if (_continuationState.Count == 0)
                        {
                            return(true);
                        }
                        currentState = _continuationState.Pop();
                        continue;
                    }

                    if (_state.CurrentTokenType != JsonParserToken.String)
                    {
                        throw new InvalidDataException("Expected property, but got " + _state.CurrentTokenType);
                    }


                    var property = CreateLazyStringValueFromParserState();
                    if (_state.EscapePositions.Count > 0)
                    {
                        property.EscapePositions = _state.EscapePositions.ToArray();
                    }

                    currentState.CurrentPropertyId = _context.CachedProperties.GetPropertyId(property);
                    currentState.MaxPropertyId     = Math.Max(currentState.MaxPropertyId, currentState.CurrentPropertyId);
                    currentState.State             = ContinuationState.ReadPropertyValue;
                    continue;

                case ContinuationState.ReadPropertyValue:
                    if (_reader.Read() == false)
                    {
                        _continuationState.Push(currentState);
                        return(false);
                    }
                    currentState.State = ContinuationState.CompleteReadingPropertyValue;
                    _continuationState.Push(currentState);
                    currentState = new BuildingState
                    {
                        State = ContinuationState.ReadValue
                    };
                    continue;

                case ContinuationState.CompleteReadingPropertyValue:
                    // Register property position, name id (PropertyId) and type (object type and metadata)
                    currentState.Properties.Add(new PropertyTag
                    {
                        Position   = _writeToken.ValuePos,
                        Type       = (byte)_writeToken.WrittenToken,
                        PropertyId = currentState.CurrentPropertyId
                    });
                    currentState.State = ContinuationState.ReadPropertyName;
                    continue;

                case ContinuationState.ReadValue:
                    ReadJsonValue();
                    currentState = _continuationState.Pop();
                    break;
                }
            }
        }
        private void ReadJsonValue()
        {
            var start = _position;

            switch (_state.CurrentTokenType)
            {
            case JsonParserToken.StartObject:
                _continuationState.Push(new BuildingState
                {
                    State = ContinuationState.ReadObject
                });
                return;

            case JsonParserToken.StartArray:
                _continuationState.Push(new BuildingState
                {
                    State = ContinuationState.ReadArray
                });
                return;

            case JsonParserToken.Integer:
                _position  += WriteVariableSizeLong(_state.Long);
                _writeToken = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = BlittableJsonToken.Integer
                };
                return;

            case JsonParserToken.Float:
                if ((_mode & UsageMode.ValidateDouble) == UsageMode.ValidateDouble)
                {
                    _reader.ValidateFloat();
                }
                BlittableJsonToken ignored;
                WriteStringFromReader(out ignored);
                _writeToken = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = BlittableJsonToken.Float
                };
                return;

            case JsonParserToken.String:
                BlittableJsonToken stringToken;
                WriteStringFromReader(out stringToken);
                _writeToken = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = stringToken
                };
                return;

            case JsonParserToken.True:
            case JsonParserToken.False:
                _stream.WriteByte(_state.CurrentTokenType == JsonParserToken.True ? (byte)1 : (byte)0);
                _position++;
                _writeToken = new WriteToken
                {
                    ValuePos     = start,
                    WrittenToken = BlittableJsonToken.Boolean
                };
                return;

            case JsonParserToken.Null:
                _stream.WriteByte(0);
                _position++;
                _writeToken = new WriteToken     // nothing to do here, we handle that with the token
                {
                    WrittenToken = BlittableJsonToken.Null,
                    ValuePos     = start
                };
                return;

            default:
                throw new InvalidDataException("Expected a value, but got " + _state.CurrentTokenType);
            }
        }
Ejemplo n.º 23
0
 private void ReusableMethod(WriteToken _)
 {
     // some complicated code
 }
Ejemplo n.º 24
0
        public Token GetToken()
        {
            State = LexicAnalyserState.Initial;
            Value = HasNext ? Character.ToString() : "";
            Token token = null;

            while (HasNext && token == null)
            {
                switch (State)
                {
                case LexicAnalyserState.Initial:
                    HandleInitial();
                    break;

                case LexicAnalyserState.NonTerminal:
                    token = new NonTerminalToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Space:
                    token = new SpaceToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Terminal:
                    token = new TerminalToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Empty:
                    token = new EmptyToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.NewLine:
                    token = new NewLineToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Identifier:
                    token = new IdentifierToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Number:
                    token = new NumberToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Var:
                    token = new VarToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Write:
                    token = new WriteToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Read:
                    token = new ReadToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.If:
                    token = new IfToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.End:
                    token = new EndToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Then:
                    token = new ThenToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Begin:
                    token = new BeginToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.While:
                    token = new WhileToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Do:
                    token = new DoToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.SemiColon:
                    token = new SemiColonToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.OpenParentheses:
                    token = new OpenParenthesesToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.CloseParentheses:
                    token = new CloseParenthesesToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Plus:
                    token = new PlusToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Sub:
                    token = new SubToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Great:
                    token = new GreatToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Less:
                    token = new LessToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Equal:
                    token = new EqualToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Repeat:
                    token = new RepeatToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Until:
                    token = new UntilToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Attribution:
                    token = new AttributionToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.NotEqual:
                    token = new NotEqualToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.GreatOrEqual:
                    token = new GreatOrEqualToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.LessOrEqual:
                    token = new LessOrEqualToken();
                    CurrentIndex++;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(token);
        }
        public void WriteArrayEnd()
        {
            var currentState = _continuationState.Pop();

            switch (currentState.State)
            {
            case ContinuationState.ReadArrayDocument:
                currentState.Properties[0] = new PropertyTag
                {
                    Property = currentState.Properties[0].Property,
                    Type     = (byte)_writeToken.WrittenToken,
                    Position = _writeToken.ValuePos
                };

                // Register property position, name id (PropertyId) and type (object type and metadata)
                _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite, currentState.MaxPropertyId);
                _continuationState.Push(currentState);
                break;

            case ContinuationState.ReadArray:
                var arrayToken     = BlittableJsonToken.StartArray;
                var arrayInfoStart = _writer.WriteArrayMetadata(currentState.Positions, currentState.Types, ref arrayToken);

                _writeToken = new WriteToken
                {
                    ValuePos     = arrayInfoStart,
                    WrittenToken = arrayToken
                };

                if (_continuationState.Count >= 1)
                {
                    var outerState = _continuationState.Pop();

                    if (outerState.FirstWrite == -1)
                    {
                        outerState.FirstWrite = arrayInfoStart;
                    }

                    if (outerState.State == ContinuationState.ReadPropertyName ||
                        outerState.State == ContinuationState.ReadPropertyValue)
                    {
                        outerState.Properties.Add(new PropertyTag(
                                                      type: (byte)_writeToken.WrittenToken,
                                                      property: outerState.CurrentProperty,
                                                      position: _writeToken.ValuePos
                                                      ));
                        outerState.State = ContinuationState.ReadPropertyName;
                    }
                    else if (outerState.State == ContinuationState.ReadArray)
                    {
                        outerState.Types.Add(_writeToken.WrittenToken);
                        outerState.Positions.Add(_writeToken.ValuePos);
                    }
                    else if (outerState.State == ContinuationState.ReadArrayDocument)
                    {
                        outerState.Properties[0] = new PropertyTag(
                            type: (byte)_writeToken.WrittenToken,
                            property: outerState.Properties[0].Property,
                            position: _writeToken.ValuePos
                            );

                        // Register property position, name id (PropertyId) and type (object type and metadata)
                        _writeToken = _writer.WriteObjectMetadata(outerState.Properties, outerState.FirstWrite, outerState.MaxPropertyId);
                    }
                    else
                    {
                        ThrowIllegalStateException(outerState.State, "ReadEndArray");
                    }

                    _continuationState.Push(outerState);
                }

                break;

            default:
                ThrowIllegalStateException(currentState.State, "ReadEndArray");
                break;
            }
        }
 public void Renew()
 {
     _writeToken = default(WriteToken);
     _continuationState.Clear();
 }
        public void WriteObjectEnd()
        {
            var  currentState = _continuationState.Pop();
            long start        = 0;

            start = _writer.Position;
            switch (currentState.State)
            {
            case ContinuationState.ReadPropertyName:
            case ContinuationState.ReadPropertyValue:
            {
                _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite,
                                                          currentState.MaxPropertyId);

                // here we know that the last itme in the stack is the keep the last ReadObjectDocument
                if (_continuationState.Count > 1)
                {
                    var outerState = _continuationState.Pop();
                    if (outerState.State == ContinuationState.ReadArray)
                    {
                        outerState.Types.Add(_writeToken.WrittenToken);
                        outerState.Positions.Add(_writeToken.ValuePos);
                    }
                    else
                    {
                        outerState.Properties.Add(new PropertyTag(
                                                      type: (byte)_writeToken.WrittenToken,
                                                      property: outerState.CurrentProperty,
                                                      position: _writeToken.ValuePos
                                                      ));
                    }

                    if (outerState.FirstWrite == -1)
                    {
                        outerState.FirstWrite = start;
                    }
                    _continuationState.Push(outerState);
                }
            }
            break;

            case ContinuationState.ReadArray:
            {
                _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite,
                                                          currentState.MaxPropertyId);

                if (_continuationState.Count > 1)
                {
                    var outerState = _continuationState.Count > 0 ? _continuationState.Pop() : currentState;
                    if (outerState.FirstWrite == -1)
                    {
                        outerState.FirstWrite = start;
                    }
                    _continuationState.Push(outerState);
                }

                currentState.Types.Add(_writeToken.WrittenToken);
                currentState.Positions.Add(_writeToken.ValuePos);
                _continuationState.Push(currentState);
            }
            break;

            case ContinuationState.ReadObjectDocument:
            {
                currentState.Properties.Add(new PropertyTag
                    {
                        Position = _writeToken.ValuePos,
                        Type     = (byte)_writeToken.WrittenToken,
                        Property = currentState.CurrentProperty
                    });
                if (currentState.FirstWrite == -1)
                {
                    currentState.FirstWrite = start;
                }

                _writeToken = _writer.WriteObjectMetadata(currentState.Properties, currentState.FirstWrite,
                                                          currentState.MaxPropertyId);
            }
            break;

            default:
                ThrowIllegalStateException(currentState.State, "ReadEndObject");
                break;
            }
        }