public static BlittableJsonReaderObject SimulateSavingToFileAndLoading(JsonOperationContext context, BlittableJsonReaderObject toStream)
        {
            //Simulates saving to file and loading
            BlittableJsonReaderObject fromStream;

            using (Stream stream = new MemoryStream())
            {
                //Pass to stream
                using (var textWriter = new BlittableJsonTextWriter(context, stream))
                {
                    context.Write(textWriter, toStream);
                }

                //Get from stream
                stream.Position = 0;

                var state            = new JsonParserState();
                var parser           = new UnmanagedJsonParser(context, state, "some tag");
                var peepingTomStream = new PeepingTomStream(stream, context);

                using (context.GetManagedBuffer(out var buffer))
                    using (var builder =
                               new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "some tag", parser, state))
                    {
                        UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer);
                        UnmanagedJsonParserHelper.ReadObject(builder, peepingTomStream, parser, buffer);

                        fromStream = builder.CreateReader();
                    }
            }

            return(fromStream);
        }
            public bool MoveNext()
            {
                AssertInitialized();

                CheckIfContextOrCacheNeedToBeRenewed();

                _timeSeriesIt?.Dispose();

                if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                    UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);

                if (_state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);

                    if (_state.CurrentTokenType != JsonParserToken.EndObject)
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);

                    return false;
                }

                _builder.Renew("readArray/singleResult", BlittableJsonDocumentBuilder.UsageMode.ToDisk);

                if (_isTimeSeriesStream)
                    UnmanagedJsonParserHelper.ReadProperty(_builder, _peepingTomStream, _parser, _buffer);
                else
                    UnmanagedJsonParserHelper.ReadObject(_builder, _peepingTomStream, _parser, _buffer);

                Current = _builder.CreateReader();

                _builder.Reset();

                if (_isTimeSeriesStream)
                {
                    _timeSeriesIt = new TimeSeriesStreamEnumerator(_builderContext, _peepingTomStream, _parser, _state, _buffer);
                    _timeSeriesIt.Initialize();
                }

                return true;
            }
Exemple #3
0
            public bool MoveNext()
            {
                AssertInitialized();

                CheckIfContextNeedsToBeRenewed();

                if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                {
                    UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                }

                if (_state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    if (UnmanagedJsonParserHelper.Read(_peepingTomStream, _parser, _state, _buffer) == false)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    if (_state.CurrentTokenType != JsonParserToken.EndObject)
                    {
                        UnmanagedJsonParserHelper.ThrowInvalidJson(_peepingTomStream);
                    }

                    return(false);
                }

                using (var builder = new BlittableJsonDocumentBuilder(_session.Context, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "readArray/singleResult", _parser, _state))
                {
                    if (_cachedItemsRenew == false)
                    {
                        _cachedItemsRenew = builder.NeedResetPropertiesCache();
                    }

                    UnmanagedJsonParserHelper.ReadObject(builder, _peepingTomStream, _parser, _buffer);

                    Current = builder.CreateReader();
                    return(true);
                }
            }
        public void JsonDeserialize_WhenHasBlittableObjectPropertyAndWriteAndReadFromStream_ShouldResultInCommandWithTheProperty()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var jsonSerializer = new JsonSerializer
                {
                    ContractResolver = new DefaultRavenContractResolver(DocumentConventions.Default.Serialization),
                };
                jsonSerializer.Converters.Add(BlittableJsonConverter.Instance);

                var data     = new { SomeProperty = "SomeValue" };
                var expected = DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(data, context);
                var command  = new Command {
                    BlittableObject = expected
                };

                //Serialize
                BlittableJsonReaderObject toStream;
                using (var writer = new BlittableJsonWriter(context))
                {
                    jsonSerializer.Serialize(writer, command);
                    writer.FinalizeDocument();

                    toStream = writer.CreateReader();
                }

                //Simulates copying to file and loading
                BlittableJsonReaderObject fromStream;
                using (Stream stream = new MemoryStream())
                {
                    //Pass to stream
                    using (var textWriter = new BlittableJsonTextWriter(context, stream))
                    {
                        context.Write(textWriter, toStream);
                    }

                    //Get from stream
                    stream.Position = 0;

                    var state            = new JsonParserState();
                    var parser           = new UnmanagedJsonParser(context, state, "some tag");
                    var peepingTomStream = new PeepingTomStream(stream, context);

                    using (context.GetMemoryBuffer(out var buffer))
                        using (var builder =
                                   new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "some tag", parser, state))
                        {
                            UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer);
                            UnmanagedJsonParserHelper.ReadObject(builder, peepingTomStream, parser, buffer);

                            fromStream = builder.CreateReader();
                        }
                }

                //Deserialize
                BlittableJsonReaderObject actual;
                using (var reader = new BlittableJsonReader(context))
                {
                    reader.Initialize(fromStream);
                    var deserialized = jsonSerializer.Deserialize <Command>(reader);
                    actual = deserialized.BlittableObject;
                }

                Assert.Equal(expected, actual);
            }
        }
Exemple #5
0
        private static unsafe GetResponse ReadResponse(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.MemoryBuffer buffer)
        {
            if (state.CurrentTokenType != JsonParserToken.StartObject)
            {
                ThrowInvalidJsonResponse(peepingTomStream);
            }

            var getResponse = new GetResponse();

            while (true)
            {
                if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                {
                    ThrowInvalidJsonResponse(peepingTomStream);
                }

                if (state.CurrentTokenType == JsonParserToken.EndObject)
                {
                    break;
                }

                if (state.CurrentTokenType != JsonParserToken.String)
                {
                    ThrowInvalidJsonResponse(peepingTomStream);
                }

                var property = context.AllocateStringValue(null, state.StringBuffer, state.StringSize).ToString();
                switch (property)
                {
                case nameof(GetResponse.Result):
                    if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    if (state.CurrentTokenType == JsonParserToken.Null)
                    {
                        continue;
                    }

                    if (state.CurrentTokenType != JsonParserToken.StartObject)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "multi_get/result", parser, state))
                    {
                        UnmanagedJsonParserHelper.ReadObject(builder, peepingTomStream, parser, buffer);
                        getResponse.Result = builder.CreateReader();
                    }
                    continue;

                case nameof(GetResponse.Headers):
                    if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    if (state.CurrentTokenType == JsonParserToken.Null)
                    {
                        continue;
                    }

                    if (state.CurrentTokenType != JsonParserToken.StartObject)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "multi_get/result", parser, state))
                    {
                        UnmanagedJsonParserHelper.ReadObject(builder, peepingTomStream, parser, buffer);
                        using (var headersJson = builder.CreateReader())
                        {
                            foreach (var propertyName in headersJson.GetPropertyNames())
                            {
                                getResponse.Headers[propertyName] = headersJson[propertyName].ToString();
                            }
                        }
                    }
                    continue;

                case nameof(GetResponse.StatusCode):
                    if (UnmanagedJsonParserHelper.Read(peepingTomStream, parser, state, buffer) == false)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    if (state.CurrentTokenType != JsonParserToken.Integer)
                    {
                        ThrowInvalidJsonResponse(peepingTomStream);
                    }

                    getResponse.StatusCode = (HttpStatusCode)state.Long;
                    continue;

                default:
                    ThrowInvalidJsonResponse(peepingTomStream);
                    break;
                }
            }

            return(getResponse);
        }
Exemple #6
0
        private void ReadObject(BlittableJsonDocumentBuilder builder)
        {
            UnmanagedJsonParserHelper.ReadObject(builder, _peepingTomStream, _parser, _buffer);

            _totalObjectsRead.Add(builder.SizeInBytes, SizeUnit.Bytes);
        }