private static void ReadStartRecordingDetails(IEnumerator <BlittableJsonReaderObject> iterator, DocumentsOperationContext context, PeepingTomStream peepingTomStream)
        {
            if (false == iterator.MoveNext())
            {
                throw new ReplayTransactionsException("Replay stream is empty", peepingTomStream);
            }
            using (iterator.Current)
            {
                var jsonSerializer = GetJsonSerializer();
                StartRecordingDetails startDetail;
                using (var reader = new BlittableJsonReader(context))
                {
                    reader.Init(iterator.Current);
                    startDetail = jsonSerializer.Deserialize <StartRecordingDetails>(reader);
                }

                if (string.IsNullOrEmpty(startDetail.Type))
                {
                    throw new ReplayTransactionsException($"Can't read {nameof(RecordingDetails.Type)} of replay detail", peepingTomStream);
                }

                if (string.IsNullOrEmpty(startDetail.Type))
                {
                    throw new ReplayTransactionsException($"Can't read {nameof(StartRecordingDetails.Version)} of replay instructions", peepingTomStream);
                }

                if (startDetail.Version != RavenVersionAttribute.Instance.Build)
                {
                    throw new ReplayTransactionsException($"Can't replay transaction instructions of different server version - Current version({ServerVersion.FullVersion}), Record version({startDetail.Version})", peepingTomStream);
                }
            }
        }
Example #2
0
        internal void PopulateEntity(object entity, LazyStringValue id, BlittableJsonReaderObject document, JsonSerializer serializer)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            try
            {
                using (var reader = new BlittableJsonReader())
                {
                    reader.Init(document);

                    serializer.Populate(reader, entity);

                    _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Could not populate entity {id}", ex);
            }
        }
Example #3
0
        public void SerializeAndDeserialize_DeleteDocumentCommandTest()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                //Arrange
                var expected = new DeleteDocumentCommand("Some Id", "Some Change Vector", null);

                //Action
                var jsonSerializer = GetJsonSerializer();
                BlittableJsonReaderObject blitCommand;
                using (var writer = new BlittableJsonWriter(context))
                {
                    var dto = expected.ToDto(context);
                    jsonSerializer.Serialize(writer, dto);
                    writer.FinalizeDocument();

                    blitCommand = writer.CreateReader();
                }
                var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);
                DeleteDocumentCommand actual;
                using (var reader = new BlittableJsonReader(context))
                {
                    reader.Init(fromStream);

                    var dto = jsonSerializer.Deserialize <DeleteDocumentCommandDto>(reader);
                    actual = dto.ToCommand(null, null);
                }

                //Assert
                Assert.Equal(expected, actual, new CustomComparer <DeleteDocumentCommand>(context));
            }
        }
Example #4
0
        public void SerializeAndDeserialize_MergedPutCommandTest()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                //Arrange
                var data         = new { ParentProperty = new { NestedProperty = "Some Value" } };
                var document     = EntityToBlittable.ConvertCommandToBlittable(data, context);
                var changeVector = context.GetLazyString("Some Lazy String");
                var expected     = new MergedPutCommand(document, "user/", changeVector, null);

                //Action
                var jsonSerializer = GetJsonSerializer();
                BlittableJsonReaderObject blitCommand;
                using (var writer = new BlittableJsonWriter(context))
                {
                    var dto = expected.ToDto(context);
                    jsonSerializer.Serialize(writer, dto);
                    writer.FinalizeDocument();

                    blitCommand = writer.CreateReader();
                }
                var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);
                MergedPutCommand actual;
                using (var reader = new BlittableJsonReader(context))
                {
                    reader.Init(fromStream);

                    var dto = jsonSerializer.Deserialize <MergedPutCommand.MergedPutCommandDto>(reader);
                    actual = dto.ToCommand(null, null);
                }

                //Assert
                Assert.Equal(expected, actual, new CustomComparer <MergedPutCommand>(context));
            }
        }
Example #5
0
        public void Directly()
        {
            var json = "{'Id':'users/1', 'Name': 'Oren', 'Dogs':['Arava','Oscar','Phoebe'], 'Age': 34, 'Children':[{'Name':'Date'}]}";

            using (var ctx = JsonOperationContext.ShortTermSingleUse())
            {
                BlittableJsonReaderObject reader = ctx.Read(new MemoryStream(Encoding.UTF8.GetBytes(json)), "users/1");

                var serializer          = new JsonSerializer();
                var blittableJsonReader = new BlittableJsonReader();
                blittableJsonReader.Init(reader);
                var u = serializer.Deserialize <User>(blittableJsonReader);

                Assert.Equal("Oren", u.Name);
                Assert.Equal("users/1", u.Id);
                Assert.Equal(3, u.Dogs.Length);
                Assert.Equal("Arava", u.Dogs[0]);
                Assert.Equal("Oscar", u.Dogs[1]);
                Assert.Equal("Phoebe", u.Dogs[2]);
                Assert.Equal(34, u.Age);

                Assert.Equal(1, u.Children.Count);
                Assert.Equal("Date", u.Children[0].Name);
            }
        }
        public void JsonDeserialize_WhenBlittableArrayBlittableObjectAndLazyStringAreNull_ShouldResultInCopy()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext readContext))
            {
                Command actual;
                using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext writeContext))
                    using (var writer = new BlittableJsonWriter(writeContext))
                    {
                        var jsonSerializer = new JsonSerializer
                        {
                            ContractResolver = new DefaultRavenContractResolver(),
                        };
                        jsonSerializer.Converters.Add(BlittableJsonReaderArrayConverter.Instance);
                        jsonSerializer.Converters.Add(LazyStringValueJsonConverter.Instance);
                        jsonSerializer.Converters.Add(BlittableJsonConverter.Instance);

                        var command = new Command();
                        jsonSerializer.Serialize(writer, command);
                        writer.FinalizeDocument();

                        var toDeseialize = writer.CreateReader();

                        using (var reader = new BlittableJsonReader(readContext))
                        {
                            reader.Init(toDeseialize);
                            actual = jsonSerializer.Deserialize <Command>(reader);
                        }
                    }

                Assert.Null(actual.BlittableArray);
                Assert.Null(actual.BlittableObject);
                Assert.Null(actual.LazyString);
            }
        }
        public void JsonDeserialize_WhenHasLazyStringProperty_ShouldResultInCopy()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext readContext))
            {
                Command         actual;
                LazyStringValue expected;
                using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext writeContext))
                    using (var writer = new BlittableJsonWriter(writeContext))
                    {
                        expected = readContext.GetLazyString("Some Lazy String");
                        var jsonSerializer = new JsonSerializer
                        {
                            ContractResolver    = new DefaultRavenContractResolver(),
                            ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                        };

                        jsonSerializer.Converters.Add(LazyStringValueJsonConverter.Instance);
                        var command = new Command {
                            LazyString = expected
                        };
                        jsonSerializer.Serialize(writer, command);
                        writer.FinalizeDocument();

                        var toDeseialize = writer.CreateReader();

                        using (var reader = new BlittableJsonReader(readContext))
                        {
                            reader.Init(toDeseialize);
                            actual = jsonSerializer.Deserialize <Command>(reader);
                        }
                    }

                Assert.Equal(expected, actual.LazyString);
            }
        }
Example #8
0
        public void SerializeAndDeserialize_MergedPutAttachmentCommand()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var database = CreateDocumentDatabase())
                {
                    //Arrange
                    var recordFilePath = NewDataPath();

                    var          attachmentStream = new StreamsTempFile(recordFilePath, database);
                    var          stream           = attachmentStream.StartNewStream();
                    const string bufferContent    = "Menahem";
                    var          buffer           = Encoding.ASCII.GetBytes(bufferContent);
                    stream.Write(buffer);

                    var changeVector = context.GetLazyString("Some Lazy String");
                    var expected     = new AttachmentHandler.MergedPutAttachmentCommand
                    {
                        DocumentId           = "someId",
                        Name                 = "someName",
                        ExpectedChangeVector = changeVector,
                        ContentType          = "someContentType",
                        Stream               = stream,
                        Hash                 = "someHash",
                    };

                    //Serialize
                    var jsonSerializer = GetJsonSerializer();
                    BlittableJsonReaderObject blitCommand;
                    using (var writer = new BlittableJsonWriter(context))
                    {
                        var dto = expected.ToDto(context);
                        jsonSerializer.Serialize(writer, dto);
                        writer.FinalizeDocument();

                        blitCommand = writer.CreateReader();
                    }

                    var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);

                    //Deserialize
                    AttachmentHandler.MergedPutAttachmentCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Init(fromStream);

                        var dto = jsonSerializer.Deserialize <MergedPutAttachmentCommandDto>(reader);
                        actual = dto.ToCommand(null, null);
                    }

                    //Assert
                    Assert.Equal(expected, actual,
                                 new CustomComparer <AttachmentHandler.MergedPutAttachmentCommand>(context, new [] { typeof(Stream) }));

                    stream.Seek(0, SeekOrigin.Begin);
                    var expectedStream = expected.Stream.ReadData();
                    var actualStream   = actual.Stream.ReadData();
                    Assert.Equal(expectedStream, actualStream);
                }
        }
        public object EntityFromJsonStream(Type type, BlittableJsonReaderObject jsonObject)
        {
            if (_reader == null)
            {
                _reader = new BlittableJsonReader();
            }
            if (_serializer == null ||
                _conventions.CustomizeJsonSerializer != _customize)
            {
                // we need to keep track and see if the event has been changed,
                // if so, we'll need a new instance of the serializer
                _customize  = _conventions.CustomizeJsonSerializer;
                _serializer = _conventions.CreateSerializer();
            }

            _reader.Init(jsonObject);
            using (DefaultRavenContractResolver.RegisterExtensionDataSetter((o, key, value) =>
            {
                JToken id;
                if (key == Constants.Documents.Metadata.Key && value is JObject json)
                {
                    if (json.TryGetValue(Constants.Documents.Metadata.Id, out id))
                    {
                        if (_generateEntityIdOnTheClient.TryGetIdFromInstance(o, out var existing) &&
                            existing != null)
                        {
                            return;
                        }
                        _generateEntityIdOnTheClient.TrySetIdentity(o, id.Value <string>());
                    }
                }

                if (key == Constants.Documents.Metadata.Id)
                {
                    id = value as JToken;
                    if (id == null)
                    {
                        return;
                    }

                    if (_generateEntityIdOnTheClient.TryGetIdFromInstance(o, out var existing) &&
                        existing != null)
                    {
                        return;
                    }
                    _generateEntityIdOnTheClient.TrySetIdentity(o, id.Value <string>());
                }
            }))
            {
                return(_serializer.Deserialize(_reader, type));
            }
        }
Example #10
0
        public Task ImportSql()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                using (var sqlImportDoc = context.ReadForMemory(RequestBodyStream(), "sql-migration-request"))
                {
                    MigrationRequest migrationRequest;

                    // we can't use JsonDeserializationServer here as it doesn't support recursive processing
                    var serializer = DocumentConventions.Default.CreateSerializer();
                    using (var blittableJsonReader = new BlittableJsonReader())
                    {
                        blittableJsonReader.Init(sqlImportDoc);
                        migrationRequest = serializer.Deserialize <MigrationRequest>(blittableJsonReader);
                    }

                    var operationId = Database.Operations.GetNextOperationId();

                    var sourceSqlDatabase = migrationRequest.Source;

                    var dbDriver = DatabaseDriverDispatcher.CreateDriver(sourceSqlDatabase.Provider, sourceSqlDatabase.ConnectionString);
                    var schema   = dbDriver.FindSchema();
                    var token    = CreateOperationToken();

                    var result = new MigrationResult(migrationRequest.Settings);

                    var collectionsCount     = migrationRequest.Settings.Collections.Count;
                    var operationDescription = "Importing " + collectionsCount + " " + (collectionsCount == 1 ? "collection" : "collections") + " from SQL database: " + schema.CatalogName;

                    Database.Operations.AddOperation(Database, operationDescription, Documents.Operations.Operations.OperationType.MigrationFromSql, onProgress =>
                    {
                        return(Task.Run(async() =>
                        {
                            try
                            {
                                // allocate new context as we executed this async
                                using (ContextPool.AllocateOperationContext(out DocumentsOperationContext migrationContext))
                                {
                                    await dbDriver.Migrate(migrationRequest.Settings, schema, Database, migrationContext, result, onProgress, token.Token);
                                }
                            }
                            catch (Exception e)
                            {
                                result.AddError($"Error occurred during import. Exception: {e.Message}");
                                onProgress.Invoke(result.Progress);
                                throw;
                            }

                            return (IOperationResult)result;
                        }));
                    }, operationId, token: token);
Example #11
0
        public void SerializeAndDeserialize_MergedBatchPutCommandTest()
        {
            using (var database = CreateDocumentDatabase())
                using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                {
                    //Arrange
                    var data     = new { ParentProperty = new { NestedProperty = "Some Value" } };
                    var expected = new DatabaseDestination.MergedBatchPutCommand(database, BuildVersionType.V4, null);
                    var document = new DocumentItem
                    {
                        Document = new Document
                        {
                            ChangeVector = "Some Change Vector",
                            Data         = EntityToBlittable.ConvertCommandToBlittable(data, context),
                            Id           = context.GetLazyString("Some Id")
                        }
                    };
                    expected.Add(document);

                    //Action
                    var jsonSerializer = GetJsonSerializer();
                    BlittableJsonReaderObject blitCommand;
                    using (var writer = new BlittableJsonWriter(context))
                    {
                        var dto = expected.ToDto(context);
                        jsonSerializer.Serialize(writer, dto);
                        writer.FinalizeDocument();

                        blitCommand = writer.CreateReader();
                    }
                    var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);
                    DatabaseDestination.MergedBatchPutCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Init(fromStream);

                        var dto = jsonSerializer.Deserialize <DatabaseDestination.MergedBatchPutCommandDto>(reader);
                        actual = dto.ToCommand(context, database);
                    }

                    //Assert
                    var expectedDoc = expected.Documents[0].Document;
                    var actualDoc   = actual.Documents[0].Document;

                    Assert.Equal(expectedDoc.Id, actualDoc.Id);
                    Assert.Equal(expectedDoc.ChangeVector, actualDoc.ChangeVector);
                    Assert.Equal(expectedDoc.Data, actualDoc.Data);
                }
        }
Example #12
0
            public object EntityFromJsonStream(Type type, BlittableJsonReaderObject jsonObject)
            {
                if (_reader == null)
                {
                    _reader = new BlittableJsonReader();
                }
                if (_serializer == null)
                {
                    _serializer = _conventions.CreateSerializer();
                }

                _reader.Init(jsonObject);

                return(_serializer.Deserialize(_reader, type));
            }
Example #13
0
        public void SerializeAndDeserialize_MergedInsertBulkCommandTest()
        {
            using (var database = CreateDocumentDatabase())
                using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                {
                    //Arrange
                    var data     = new { ParentProperty = new { NestedProperty = "Some Value" } };
                    var commands = new[]
                    {
                        new BatchRequestParser.CommandData
                        {
                            Id           = "Some Id",
                            ChangeVector = context.GetLazyString("Some Lazy String"),
                            Document     = EntityToBlittable.ConvertCommandToBlittable(data, context),
                            Patch        = new PatchRequest("Some Script", PatchRequestType.None)
                        }
                    };
                    var expected = new BulkInsertHandler.MergedInsertBulkCommand
                    {
                        NumberOfCommands = commands.Length,
                        Commands         = commands
                    };

                    //Action
                    var jsonSerializer = GetJsonSerializer();
                    BlittableJsonReaderObject blitCommand;
                    using (var writer = new BlittableJsonWriter(context))
                    {
                        var dto = expected.ToDto(context);
                        jsonSerializer.Serialize(writer, dto);
                        writer.FinalizeDocument();

                        blitCommand = writer.CreateReader();
                    }
                    var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);
                    BulkInsertHandler.MergedInsertBulkCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Init(fromStream);

                        var dto = jsonSerializer.Deserialize <MergedInsertBulkCommandDto>(reader);
                        actual = dto.ToCommand(context, database);
                    }

                    //Assert
                    Assert.Equal(expected, actual, new CustomComparer <BulkInsertHandler.MergedInsertBulkCommand>(context));
                }
        }
Example #14
0
        public void SerializeAndDeserialize_PutResolvedConflictsCommand()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var database = CreateDocumentDatabase())
                {
                    //Arrange
                    var resolvedConflicts = new List <(DocumentConflict, long)>
                    {
                        (new DocumentConflict
                        {
                            Id = context.GetLazyString("Some id"),
                            LowerId = context.GetLazyString("Some lower id"),
                            Collection = context.GetLazyString("Some collection"),
                            Doc = EntityToBlittable.ConvertCommandToBlittable(new { SomeName = "Some Value" }, context)
                        }, 10)
                    };

                    var expected = new ResolveConflictOnReplicationConfigurationChange.PutResolvedConflictsCommand(
                        null, resolvedConflicts, null);
                    var expectedDto = expected.ToDto(context);

                    //Serialize
                    var jsonSerializer = GetJsonSerializer();
                    BlittableJsonReaderObject blitCommand;
                    using (var writer = new BlittableJsonWriter(context))
                    {
                        jsonSerializer.Serialize(writer, expectedDto);
                        writer.FinalizeDocument();

                        blitCommand = writer.CreateReader();
                    }

                    var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);

                    //Deserialize
                    ResolveConflictOnReplicationConfigurationChange.PutResolvedConflictsCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Init(fromStream);

                        var actualDto = jsonSerializer.Deserialize <PutResolvedConflictsCommandDto>(reader);

                        //Assert
//                    Assert.Equal(expectedDto.)
                    }
                }
        }
Example #15
0
        public void SerializeAndDeserialize_PatchDocumentCommandTest()
        {
            using (var database = CreateDocumentDatabase())
                using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                {
                    //Arrange
                    var data         = new { ParentProperty = new { NestedProperty = "Some Value" } };
                    var arg          = EntityToBlittable.ConvertCommandToBlittable(data, context);
                    var patchRequest = new PatchRequest("", PatchRequestType.None);

                    var expected = new PatchDocumentCommand(
                        context,
                        "Some Id",
                        context.GetLazyString("Some Lazy String"),
                        false,
                        (patchRequest, arg),
                        (null, null),
                        database,
                        false, false, false, false);

                    //Action
                    var jsonSerializer = GetJsonSerializer();
                    BlittableJsonReaderObject blitCommand;
                    using (var writer = new BlittableJsonWriter(context))
                    {
                        var dto = expected.ToDto(context);
                        jsonSerializer.Serialize(writer, dto);
                        writer.FinalizeDocument();

                        blitCommand = writer.CreateReader();
                    }
                    var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);
                    PatchDocumentCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Init(fromStream);

                        var dto = jsonSerializer.Deserialize <PatchDocumentCommandDto>(reader);
                        actual = dto.ToCommand(context, database);
                    }

                    //Assert
                    Assert.Equal(expected, actual, new CustomComparer <PatchDocumentCommand>(context));
                }
        }
Example #16
0
        public void SerializeAndDeserialize_MergedDeleteAttachmentCommand()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                //Arrange
                var changeVector = context.GetLazyString("Some Lazy String");
                var expected     = new AttachmentHandler.MergedDeleteAttachmentCommand
                {
                    DocumentId           = "someId",
                    Name                 = "someName",
                    ExpectedChangeVector = changeVector
                };

                //Serialize
                var jsonSerializer = GetJsonSerializer();
                BlittableJsonReaderObject blitCommand;
                using (var writer = new BlittableJsonWriter(context))
                {
                    var dto = expected.ToDto(context);
                    jsonSerializer.Serialize(writer, dto);
                    writer.FinalizeDocument();

                    blitCommand = writer.CreateReader();
                }

                var fromStream = SerializeTestHelper.SimulateSavingToFileAndLoading(context, blitCommand);

                //Deserialize
                AttachmentHandler.MergedDeleteAttachmentCommand actual;
                using (var reader = new BlittableJsonReader(context))
                {
                    reader.Init(fromStream);

                    var dto = jsonSerializer.Deserialize <MergedDeleteAttachmentCommandDto>(reader);
                    actual = dto.ToCommand(null, null);
                }

                //Assert
                Assert.Equal(expected, actual, new CustomComparer <AttachmentHandler.MergedDeleteAttachmentCommand>(context));
            }
        }
        private static TransactionOperationsMerger.MergedTransactionCommand DeserializeCommand(
            DocumentsOperationContext context,
            DocumentDatabase database,
            string type,
            BlittableJsonReaderObject wrapCmdReader,
            PeepingTomStream peepingTomStream)
        {
            if (!wrapCmdReader.TryGet(nameof(RecordingCommandDetails.Command), out BlittableJsonReaderObject commandReader))
            {
                throw new ReplayTransactionsException($"Can't read {type} for replay", peepingTomStream);
            }

            var jsonSerializer = GetJsonSerializer();

            using (var reader = new BlittableJsonReader(context))
            {
                reader.Init(commandReader);
                var dto = DeserializeCommandDto(type, jsonSerializer, reader, peepingTomStream);
                return(dto.ToCommand(context, database));
            }
        }
        public void JsonDeserialize_WhenHasBlittableJsonReaderArrayProperty_ShouldResultInCopy()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext readContext))
            {
                Command actual;
                BlittableJsonReaderArray expected;
                using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext writeContext))
                    using (var writer = new BlittableJsonWriter(writeContext))
                    {
                        var data         = new { Property = new[] { "Value1", "Value2" } };
                        var readerObject = EntityToBlittable.ConvertCommandToBlittable(data, readContext);
                        readerObject.TryGet(nameof(data.Property), out expected);

                        var jsonSerializer = new JsonSerializer
                        {
                            ContractResolver = new DefaultRavenContractResolver(),
                        };
                        jsonSerializer.Converters.Add(BlittableJsonReaderArrayConverter.Instance);
                        jsonSerializer.Converters.Add(LazyStringValueJsonConverter.Instance);
                        jsonSerializer.Converters.Add(BlittableJsonConverter.Instance);

                        var command = new Command {
                            BlittableArray = expected
                        };
                        jsonSerializer.Serialize(writer, command);
                        writer.FinalizeDocument();

                        var toDeseialize = writer.CreateReader();

                        using (var reader = new BlittableJsonReader(readContext))
                        {
                            reader.Init(toDeseialize);
                            actual = jsonSerializer.Deserialize <Command>(reader);
                        }
                    }

                Assert.Equal(expected, actual.BlittableArray);
            }
        }
Example #19
0
        public void Physical_store_test()
        {
            List <Car> cars;

            using (var documentStore = GetDocumentStore())
            {
                documentStore.Initialize();

                new CarIndex().Execute(documentStore);

                using (var session = documentStore.OpenSession())
                {
                    foreach (CarLot doc in Docs)
                    {
                        session.Store(doc);
                    }
                    session.SaveChanges();
                }

                string targetLeasee = CarLeasees[0].Id;

                using (var session = documentStore.OpenSession())
                {
                    //Synchronize indexes
                    session.Query <CarLot, CarIndex>().Customize(x => x.WaitForNonStaleResults(TimeSpan.FromMinutes(2))).FirstOrDefault();

                    var query = session.Query <CarLot, CarIndex>()
                                .Where(carLot => carLot.Cars.Any(car => car.LeaseHistory.Any(leasee => leasee.Id == targetLeasee)))
                                .Take(1024);

                    var deserializer = session.Advanced.DocumentStore.Conventions.CreateSerializer();
                    var indexQuery   = RavenTestHelper.GetIndexQuery(query);

                    using (var commands = documentStore.Commands())
                    {
                        var queryResult = commands.Query(indexQuery);

                        var carLots = queryResult
                                      .Results
                                      .Select(x =>
                        {
                            using (var reader = new BlittableJsonReader())
                            {
                                reader.Init((BlittableJsonReaderObject)x);
                                return(deserializer.Deserialize <CarLot>(reader));
                            }
                        })
                                      .ToArray();

                        foreach (var carLot in carLots)
                        {
                            Assert.NotNull(carLot.Cars);
                            Assert.NotEmpty(carLot.Cars);
                        }

                        cars = carLots
                               .SelectMany(x => x.Cars)
                               .Where(car => car.LeaseHistory.Any(leasee => leasee.Id == targetLeasee))
                               .ToList();
                    }
                }
            }

            Assert.NotNull(cars);
            Assert.NotEmpty(cars);

            foreach (Car car in cars)
            {
                Assert.NotNull(car.LeaseHistory);
                Assert.NotEmpty(car.LeaseHistory);
            }
        }
        public void JsonDeserialize_WhenHasBlittableObjectPropertyAndWriteAndReadFromStream_ShouldResultInCommandWithTheProperty()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var jsonSerializer = new JsonSerializer
                {
                    ContractResolver = new DefaultRavenContractResolver(),
                };
                jsonSerializer.Converters.Add(BlittableJsonConverter.Instance);

                var data     = new { SomeProperty = "SomeValue" };
                var expected = EntityToBlittable.ConvertCommandToBlittable(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.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();
                        }
                }

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

                Assert.Equal(expected, actual);
            }
        }