예제 #1
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));
            }
        }
예제 #2
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));
            }
        }
예제 #3
0
        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(DocumentConventions.Default.Serialization),
                            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.Initialize(toDeseialize);
                            actual = jsonSerializer.Deserialize <Command>(reader);
                        }
                    }

                Assert.Equal(expected, actual.LazyString);
            }
        }
        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.Initialize(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);
            }
        }
예제 #5
0
        public void JsonDeserialize_WhenHasBlittableObjectProperty_ShouldResultInCopy()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext readContext))
            {
                Command actual;
                BlittableJsonReaderObject expected;
                using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext writeContext))
                    using (var writer = new BlittableJsonWriter(writeContext))
                    {
                        var data = new { Property = "Value" };
                        expected = DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(data, readContext);
                        var jsonSerializer = new JsonSerializer
                        {
                            ContractResolver = new DefaultRavenContractResolver(DocumentConventions.Default.Serialization),
                        };

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

                        var toDeseialize = writer.CreateReader();

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

                Assert.Equal(expected, actual.BlittableObject);
            }
        }
예제 #6
0
        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(DocumentConventions.Default.Serialization),
                        };
                        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.Initialize(toDeseialize);
                            actual = jsonSerializer.Deserialize <Command>(reader);
                        }
                    }

                Assert.Null(actual.BlittableArray);
                Assert.Null(actual.BlittableObject);
                Assert.Null(actual.LazyString);
            }
        }
예제 #7
0
        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);
                }
            }
        }
예제 #8
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);
            }
        }
예제 #9
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);
                }
        }
        internal override LazyStringValue ReadJson(BlittableJsonReader reader)
        {
            //Todo It will be better to change the reader to set the value as LazyStringValue
            if (!(reader.Value is string strValue))
            {
                throw new SerializationException($"Try to read {nameof(LazyStringValue)} from {reader.Value?.GetType()}. Should be string here");
            }

            return(reader.Context.GetLazyString(strValue));
        }
예제 #11
0
        internal override Stream ReadJson(BlittableJsonReader blittableReader)
        {
            if (!(blittableReader.Value is string strValue))
            {
                throw new SerializationException($"Try to read {nameof(Stream)} from {blittableReader.Value?.GetType()}. Should be string here");
            }

            var buffer = Convert.FromBase64String(strValue);

            return(new MemoryStream(buffer));
        }
        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));
            }
        }
예제 #13
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);
예제 #14
0
        public async Task 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
                        {
                            Type         = CommandType.PUT,
                            Id           = "Some Id",
                            ChangeVector = context.GetLazyString("Some Lazy String"),
                            Document     = DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(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 = await SerializeTestHelper.SimulateSavingToFileAndLoadingAsync(context, blitCommand);

                    BulkInsertHandler.MergedInsertBulkCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Initialize(fromStream);

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

                    //Assert
                    Assert.Equal(expected, actual, new CustomComparer <BulkInsertHandler.MergedInsertBulkCommand>(context));
                }
        }
예제 #15
0
        public async Task 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         = DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(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 = await SerializeTestHelper.SimulateSavingToFileAndLoadingAsync(context, blitCommand);

                    DatabaseDestination.MergedBatchPutCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Initialize(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);
                }
        }
예제 #16
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));
            }
예제 #17
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.)
                    }
                }
        }
예제 #18
0
        public async Task 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          = DocumentConventions.Default.Serialization.DefaultConverter.ToBlittable(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),
                        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 = await SerializeTestHelper.SimulateSavingToFileAndLoadingAsync(context, blitCommand);

                    PatchDocumentCommand actual;
                    using (var reader = new BlittableJsonReader(context))
                    {
                        reader.Initialize(fromStream);

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

                    //Assert
                    Assert.Equal(expected, actual, new CustomComparer <PatchDocumentCommand>(context));
                }
        }
        internal override BlittableJsonReaderArray ReadJson(BlittableJsonReader blittableReader)
        {
            if (!(blittableReader.Value is BlittableJsonReaderArray blittableArrayValue))
            {
                throw new SerializationException(
                          $"Can't convert {blittableReader.Value?.GetType()} type to {nameof(BlittableJsonReaderArray)}. The value must to be an array");
            }

            //Because the value that return is the blittable array as a whole
            //we skip the reading inside this blittable array
            blittableReader.SkipBlittableArrayInside();

            return
                (blittableArrayValue.BelongsToContext(blittableReader.Context)
                    ? blittableArrayValue
                    : blittableArrayValue.Clone(blittableReader.Context));
        }
예제 #20
0
        public void SerializeAndDeserialize_MergedBatchCommandCommandTest()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext 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 BatchHandler.MergedBatchCommand(null)
                {
                    ParsedCommands = 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);
                BatchHandler.MergedBatchCommand actual;
                using (var reader = new BlittableJsonReader(context))
                {
                    reader.Init(fromStream);

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

                //Assert
                Assert.Equal(expected, actual, new CustomComparer <BatchHandler.MergedBatchCommand>(context));
            }
        }
예제 #21
0
        internal override Slice ReadJson(BlittableJsonReader reader)
        {
            if (!(reader.Value is string strValue))
            {
                throw new SerializationException($"Try to read {nameof(Slice)} from {reader.Value?.GetType()}. Should be string here");
            }

            if (!(reader.Context is DocumentsOperationContext context))
            {
                throw new SerializationException($"{nameof(DocumentsOperationContext)} must to be used for reading {nameof(Slice)}");
            }

            var buffer = Convert.FromBase64String(strValue);

            Slice.From(context.Allocator, buffer, ByteStringType.Immutable, out var slice);

            return(slice);
        }
예제 #22
0
                static void FillStack(BlittableJsonReader reader, int numberOfItems)
                {
                    var stackField     = reader.GetType().BaseType.GetField("_stack", BindingFlags.Instance | BindingFlags.NonPublic);
                    var stackFieldItem = stackField.FieldType.GetGenericArguments()[0];

                    var stack = (IList)stackField.GetValue(reader);

                    while (stack.Count < numberOfItems)
                    {
                        var item      = Activator.CreateInstance(stackFieldItem);
                        var typeField = item.GetType().GetField("Type", BindingFlags.Instance | BindingFlags.NonPublic);
                        typeField.SetValue(item, 1); // :)

                        var propertyNameField = item.GetType().GetField("PropertyName", BindingFlags.Instance | BindingFlags.NonPublic);
                        propertyNameField.SetValue(item, $"Field_{stack.Count}");

                        stack.Add(item);
                    }
                }
예제 #23
0
        internal override CounterOperation ReadJson(BlittableJsonReader blittableReader)
        {
            var result = new CounterOperation();

            do
            {
                blittableReader.Read();
                if (blittableReader.TokenType == JsonToken.EndObject)
                {
                    return(result);
                }
                if (blittableReader.TokenType != JsonToken.PropertyName)
                {
                    throw new InvalidOperationException("Expected PropertyName, Got " + blittableReader.TokenType);
                }

                var property = (string)blittableReader.Value;
                switch (property)
                {
                case nameof(CounterOperation.CounterName):
                    result.CounterName = blittableReader.ReadAsString();
                    break;

                case nameof(CounterOperation.Delta):
                    blittableReader.Read();
                    result.Delta = (long)blittableReader.Value;
                    break;

                case nameof(CounterOperation.Type):
                    result.Type = Enum.Parse <CounterOperationType>(blittableReader.ReadAsString());
                    break;

                case nameof(CounterOperation.DocumentId):
                    result.DocumentId = blittableReader.ReadAsString();
                    break;

                case nameof(CounterOperation.ChangeVector):
                    result.ChangeVector = blittableReader.ReadAsString();
                    break;
                }
            } while (true);
        }
예제 #24
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));
            }
        }
예제 #25
0
        internal override BlittableJsonReaderObject ReadJson(BlittableJsonReader blittableReader)
        {
            if (!(blittableReader.Value is BlittableJsonReaderObject blittableValue))
            {
                throw new SerializationException(
                          $"Can't convert {blittableReader.Value?.GetType()} type to {nameof(BlittableJsonReaderObject)}. The value must to be a complex object");
            }

            if (blittableReader.TokenType == JsonToken.StartObject)
            {
                //Because the value that return is the blittable as a whole
                //we skip the reading inside this blittable
                blittableReader.SkipBlittableInside();
            }

            return
                (blittableValue.BelongsToContext(blittableReader.Context) &&
                 blittableValue.HasParent == false
                    ? blittableValue
                    : blittableValue.Clone(blittableReader.Context));
        }
예제 #26
0
        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));
            }
        }
예제 #27
0
        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);
            }
        }
예제 #28
0
        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);
            }
        }
 public static void CleanThreadStatics()
 {
     _reader     = null;
     _serializer = null;
 }
예제 #30
0
        private static TransactionOperationsMerger.IReplayableCommandDto <TransactionOperationsMerger.MergedTransactionCommand> DeserializeCommandDto(
            string type,
            JsonSerializer jsonSerializer,
            BlittableJsonReader reader,
            PeepingTomStream peepingTomStream)
        {
            switch (type)
            {
            case nameof(BatchHandler.MergedBatchCommand):
                return(jsonSerializer.Deserialize <MergedBatchCommandDto>(reader));

            case nameof(DeleteDocumentCommand):
                return(jsonSerializer.Deserialize <DeleteDocumentCommandDto>(reader));

            case nameof(PatchDocumentCommand):
                return(jsonSerializer.Deserialize <PatchDocumentCommandDto>(reader));

            case nameof(DatabaseDestination.MergedBatchPutCommand):
                return(jsonSerializer.Deserialize <DatabaseDestination.MergedBatchPutCommandDto>(reader));

            case nameof(MergedPutCommand):
                return(jsonSerializer.Deserialize <MergedPutCommand.MergedPutCommandDto>(reader));

            case nameof(BulkInsertHandler.MergedInsertBulkCommand):
                return(jsonSerializer.Deserialize <MergedInsertBulkCommandDto>(reader));

            case nameof(AttachmentHandler.MergedPutAttachmentCommand):
                return(jsonSerializer.Deserialize <MergedPutAttachmentCommandDto>(reader));

            case nameof(AttachmentHandler.MergedDeleteAttachmentCommand):
                return(jsonSerializer.Deserialize <MergedDeleteAttachmentCommandDto>(reader));

            case nameof(ResolveConflictOnReplicationConfigurationChange.PutResolvedConflictsCommand):
                return(jsonSerializer.Deserialize <PutResolvedConflictsCommandDto>(reader));

            case nameof(HiLoHandler.MergedNextHiLoCommand):
                return(jsonSerializer.Deserialize <MergedNextHiLoCommandDto>(reader));

            case nameof(HiLoHandler.MergedHiLoReturnCommand):
                return(jsonSerializer.Deserialize <MergedHiLoReturnCommandDto>(reader));

            case nameof(IncomingReplicationHandler.MergedDocumentReplicationCommand):
                return(jsonSerializer.Deserialize <MergedDocumentReplicationCommandDto>(reader));

            case nameof(ExpiredDocumentsCleaner.DeleteExpiredDocumentsCommand):
                return(jsonSerializer.Deserialize <DeleteExpiredDocumentsCommandDto>(reader));

            case nameof(OutgoingReplicationHandler.UpdateSiblingCurrentEtag):
                return(jsonSerializer.Deserialize <UpdateSiblingCurrentEtagDto>(reader));

            case nameof(IncomingReplicationHandler.MergedUpdateDatabaseChangeVectorCommand):
                return(jsonSerializer.Deserialize <MergedUpdateDatabaseChangeVectorCommandDto>(reader));

            case nameof(AdminRevisionsHandler.DeleteRevisionsCommand):
                return(jsonSerializer.Deserialize <DeleteRevisionsCommandDto>(reader));

            case nameof(RevisionsOperations.DeleteRevisionsBeforeCommand):
                throw new ReplayTransactionsException(
                          "Because this command is deleting according to revisions' date & the revisions that created by replaying have different date an in place decision needed to be made",
                          peepingTomStream);

            case nameof(TombstoneCleaner.DeleteTombstonesCommand):
                return(jsonSerializer.Deserialize <DeleteTombstonesCommandDto>(reader));

            case nameof(OutputReduceIndexWriteOperation.OutputReduceToCollectionCommand):
                return(jsonSerializer.Deserialize <OutputReduceToCollectionCommandDto>(reader));

            case nameof(BatchHandler.ClusterTransactionMergedCommand):
                return(jsonSerializer.Deserialize <ClusterTransactionMergedCommandDto>(reader));

            case nameof(CountersHandler.ExecuteCounterBatchCommand):
                return(jsonSerializer.Deserialize <ExecuteCounterBatchCommandDto>(reader));

            default:
                throw new ReplayTransactionsException($"Can't read {type} for replay", peepingTomStream);
            }
        }