示例#1
0
            public PutIndexesCommand(DocumentConventions conventions, JsonOperationContext context, IndexDefinition[] indexesToAdd)
            {
                if (conventions == null)
                {
                    throw new ArgumentNullException(nameof(conventions));
                }
                if (indexesToAdd == null)
                {
                    throw new ArgumentNullException(nameof(indexesToAdd));
                }
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                _indexToAdd = new BlittableJsonReaderObject[indexesToAdd.Length];
                for (var i = 0; i < indexesToAdd.Length; i++)
                {
                    if (indexesToAdd[i].Name == null)
                    {
                        throw new ArgumentNullException(nameof(IndexDefinition.Name));
                    }
                    _indexToAdd[i] = EntityToBlittable.ConvertCommandToBlittable(indexesToAdd[i], context);
                }
            }
示例#2
0
        public void ShouldAssignTheTypeOfDerivedProperties()
        {
            var entity5 = new IDocumentWithPropertyTypeOverride5();
            var type5   = EntityToBlittable.GetPropertyType(nameof(entity5.Reference), entity5.GetType());

            Assert.Equal(typeof(IRefDerived_Class), type5);


            var entity4 = new DocumentWithPropertyTypeOverride4();
            var type4   = EntityToBlittable.GetPropertyType(nameof(entity4.Reference), entity4.GetType());

            Assert.Equal(typeof(RefDerived3), type4);

            var entity3 = new DocumentWithPropertyTypeOverride3();
            var type3   = EntityToBlittable.GetPropertyType(nameof(entity3.Reference), entity3.GetType());

            Assert.Equal(typeof(RefDerived3), type3);

            var entity2 = new DocumentWithPropertyTypeOverride2();
            var type2   = EntityToBlittable.GetPropertyType(nameof(entity2.Reference), entity2.GetType());

            Assert.Equal(typeof(RefDerived2), type2);

            var entity1 = new DocumentWithPropertyTypeOverride1();
            var type1   = EntityToBlittable.GetPropertyType(nameof(entity1.Reference), entity1.GetType());

            Assert.Equal(typeof(RefDerived2), type1);

            var entity0 = new DocumentBase();
            var type0   = EntityToBlittable.GetPropertyType(nameof(entity0.Reference), entity0.GetType());

            Assert.Equal(typeof(RefDerived1), type0);
        }
示例#3
0
        public static T GetOptions <T>(string optionsAsStringOrParameterName, ValueTokenType optionsType, BlittableJsonReaderObject parameters, JsonOperationContext context)
        {
            BlittableJsonReaderObject optionsJson;

            if (optionsType == ValueTokenType.Parameter)
            {
                if (parameters == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }

                if (parameters.TryGetMember(optionsAsStringOrParameterName, out var optionsObject) == false)
                {
                    throw new InvalidOperationException($"Parameter '{optionsAsStringOrParameterName}' containing '{typeof(T).Name}' was not present in the list of parameters.");
                }

                optionsJson = optionsObject as BlittableJsonReaderObject;

                if (optionsJson == null)
                {
                    throw new InvalidOperationException($"Parameter '{optionsAsStringOrParameterName}' should contain JSON object.");
                }
            }
            else if (optionsType == ValueTokenType.String)
            {
                optionsJson = IndexReadOperation.ParseJsonStringIntoBlittable(optionsAsStringOrParameterName, context);
            }
            else
            {
                throw new InvalidOperationException($"Unknown options type '{optionsType}'.");
            }

            return((T)EntityToBlittable.ConvertToEntity(typeof(T), "options", optionsJson, DocumentConventions.DefaultForServer));
        }
示例#4
0
        public void JsonSerialize_WhenNestedBlittableObjectIsProperty_ShouldSerialize()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                //TODO To consider if should support direct couple of write on the same context
                using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context2))
                    using (var writer = new BlittableJsonWriter(context2))
                    {
                        var data            = new { ParentProperty = new { NestedProperty = "Some Value" } };
                        var parentBlittable = EntityToBlittable.ConvertCommandToBlittable(data, context);
                        parentBlittable.TryGet(nameof(data.ParentProperty), out BlittableJsonReaderObject expected);
                        var jsonSerializer = new JsonSerializer
                        {
                            ContractResolver    = new DefaultRavenContractResolver(),
                            ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                        };

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

                        //Assert
                        var reader = writer.CreateReader();
                        reader.TryGet(nameof(Command.BlittableObject), out BlittableJsonReaderObject actual);
                        Assert.Equal(expected, actual);
                    }
        }
示例#5
0
        //Todo To consider if should support direct serialize of BlittableObject
        public void JsonSerialize_WhenBlittableIsTheRoot_ShouldResultInCopy()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var writer = new BlittableJsonWriter(context))
                {
                    var data = new DynamicJsonValue
                    {
                        ["Property"] = "Value"
                    };
                    var blittableData = EntityToBlittable.ConvertCommandToBlittable(data, context);

                    var jsonSerializer = new JsonSerializer
                    {
                        ContractResolver    = new DefaultRavenContractResolver(),
                        ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                    };

                    jsonSerializer.Converters.Add(BlittableJsonConverter.Instance);
                    jsonSerializer.Serialize(writer, blittableData);
                    writer.FinalizeDocument();

                    //Assert
                    var result = writer.CreateReader();
                    Assert.True(result.TryGet(nameof(data), out object _));
                }
        }
示例#6
0
            public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
            {
                url = $"{node.Url}/databases/{node.Database}/smuggler/import/csv?operationId={_operationId}&collection={_collection}";
                var form = new MultipartFormDataContent();

                if (_csvConfig != null)
                {
                    var _csvConfigBlittable = EntityToBlittable.ConvertCommandToBlittable(_csvConfig, ctx);
                    form = new MultipartFormDataContent
                    {
                        { new BlittableJsonContent(stream => { ctx.Write(stream, _csvConfigBlittable); }), Constants.Smuggler.CsvImportOptions },
                        { new StreamContent(_stream), "file", "name" }
                    };
                }
                else
                {
                    form = new MultipartFormDataContent
                    {
                        { new StreamContent(_stream), "file", "name" }
                    };
                }

                return(new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    Content = form
                });
            }
示例#7
0
        public void JsonSerialize_WhenBlittableObjectIsProperty_ShouldResultInCopy()
        {
            using (Server.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var writer = new BlittableJsonWriter(context))
                {
                    var data           = new { Property = "Value" };
                    var expected       = EntityToBlittable.ConvertCommandToBlittable(data, context);
                    var jsonSerializer = new JsonSerializer
                    {
                        ContractResolver    = new DefaultRavenContractResolver(),
                        ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                    };

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

                    //Assert
                    var reader = writer.CreateReader();
                    reader.TryGet(nameof(Command.BlittableObject), out BlittableJsonReaderObject actual);
                    Assert.Equal(expected, actual);
                }
        }
        private PerformanceHint GetOrCreateSlowWrites(out SlowWritesDetails details)
        {
            const string source = "slow-writes";

            var id = PerformanceHint.GetKey(PerformanceHintType.SlowIO, source);

            using (_notificationsStorage.Read(id, out var ntv))
            {
                if (ntv == null || ntv.Json.TryGet(nameof(PerformanceHint.Details), out BlittableJsonReaderObject detailsJson) == false || detailsJson == null)
                {
                    details = new SlowWritesDetails();
                }
                else
                {
                    details = (SlowWritesDetails)EntityToBlittable.ConvertToEntity(
                        typeof(SlowWritesDetails),
                        null,
                        detailsJson,
                        DocumentConventions.Default);
                }

                return(PerformanceHint.Create(
                           _database,
                           "An extremely slow write to disk",
                           "We have detected very slow writes",
                           PerformanceHintType.SlowIO,
                           NotificationSeverity.Info,
                           source,
                           details
                           ));
            }
        }
示例#9
0
 public PatchCommand(DocumentConventions conventions, JsonOperationContext context, string id, string changeVector, PatchRequest patch, PatchRequest patchIfMissing, bool skipPatchIfChangeVectorMismatch, bool returnDebugInformation, bool test)
 {
     if (conventions == null)
     {
         throw new ArgumentNullException(nameof(conventions));
     }
     if (patch == null)
     {
         throw new ArgumentNullException(nameof(patch));
     }
     if (string.IsNullOrWhiteSpace(patch.Script))
     {
         throw new ArgumentNullException(nameof(patch.Script));
     }
     if (patchIfMissing != null && string.IsNullOrWhiteSpace(patchIfMissing.Script))
     {
         throw new ArgumentNullException(nameof(patchIfMissing.Script));
     }
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     _id           = id ?? throw new ArgumentNullException(nameof(id));
     _changeVector = changeVector;
     _patch        = EntityToBlittable.ConvertCommandToBlittable(new
     {
         Patch          = patch,
         PatchIfMissing = patchIfMissing
     }, context);
     _skipPatchIfChangeVectorMismatch = skipPatchIfChangeVectorMismatch;
     _returnDebugInformation          = returnDebugInformation;
     _test = test;
 }
示例#10
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));
            }
        }
示例#11
0
        public Task PopulateIps()
        {
            AssertOnlyInSetupMode();
            var rootDomain = GetQueryStringValueAndAssertIfSingleAndNotEmpty("rootDomain");

            using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var userDomainsWithIpsJson = context.ReadForMemory(RequestBodyStream(), "setup-secured"))
                {
                    var userDomainsWithIps = JsonDeserializationServer.UserDomainsWithIps(userDomainsWithIpsJson);

                    foreach (var domain in userDomainsWithIps.Domains)
                    {
                        foreach (var subDomain in domain.Value)
                        {
                            try
                            {
                                subDomain.Ips = Dns.GetHostAddresses(subDomain.SubDomain + "." + rootDomain).Select(ip => ip.ToString()).ToList();
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }

                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        var blittable = EntityToBlittable.ConvertCommandToBlittable(userDomainsWithIps, context);
                        context.Write(writer, blittable);
                    }
                }

            return(Task.CompletedTask);
        }
示例#12
0
        private PerformanceHint GetOrCreatePerformanceLatencies(out RequestLatencyDetail details)
        {
            //Read() is transactional, so this is thread-safe
            using (_notificationsStorage.Read(QueryRequestLatenciesId, out var ntv))
            {
                if (ntv == null || ntv.Json.TryGet(nameof(PerformanceHint.Details), out BlittableJsonReaderObject detailsJson) == false || detailsJson == null)
                {
                    details = new RequestLatencyDetail();
                }
                else
                {
                    details = (RequestLatencyDetail)EntityToBlittable.ConvertToEntity(
                        typeof(RequestLatencyDetail),
                        QueryRequestLatenciesId,
                        detailsJson,
                        DocumentConventions.Default);
                }

                return(PerformanceHint.Create(
                           _database,
                           "Request latency is too high",
                           "We have detected that some query duration has surpassed the configured threshold",
                           PerformanceHintType.RequestLatency,
                           NotificationSeverity.Warning,
                           "Query",
                           details
                           ));
            }
        }
示例#13
0
        public void FillFromBlittableJson(BlittableJsonReaderObject json)
        {
            var timings = (QueryTimings)EntityToBlittable.ConvertToEntity(typeof(QueryTimings), "query/timings", json, DocumentConventions.Default);

            DurationInMs = timings.DurationInMs;
            Timings      = timings.Timings;
        }
        public static Dictionary <string, CompareExchangeValue <T> > GetValues(JsonOperationContext context, BlittableJsonReaderObject response, DocumentConventions conventions)
        {
            if (response.TryGet("Results", out BlittableJsonReaderArray items) == false)
            {
                throw new InvalidDataException("Response is invalid. Results is missing.");
            }

            LazyStringValue lsv     = null;
            var             results = new Dictionary <string, CompareExchangeValue <T> >();

            foreach (BlittableJsonReaderObject item in items)
            {
                if (item == null)
                {
                    throw new InvalidDataException("Response is invalid. Item is null.");
                }

                if (item.TryGet("Key", out string key) == false)
                {
                    throw new InvalidDataException("Response is invalid. Key is missing.");
                }
                if (item.TryGet("Index", out long index) == false)
                {
                    throw new InvalidDataException("Response is invalid. Index is missing.");
                }
                if (item.TryGet("Value", out BlittableJsonReaderObject raw) == false)
                {
                    throw new InvalidDataException("Response is invalid. Value is missing.");
                }

                if (typeof(T).GetTypeInfo().IsPrimitive || typeof(T) == typeof(string))
                {
                    // simple
                    T value = default;
                    raw?.TryGet("Object", out value);
                    results[key] = new CompareExchangeValue <T>(key, index, value);
                }
                else
                {
                    if (lsv == null)
                    {
                        lsv = context.GetLazyString("Object");
                    }

                    if (raw == null || raw.Contains(lsv) == false)
                    {
                        results[key] = new CompareExchangeValue <T>(key, index, default);
                    }
                    else
                    {
                        var converted = (ResultHolder)EntityToBlittable.ConvertToEntity(typeof(ResultHolder), null, raw, conventions);
                        results[key] = new CompareExchangeValue <T>(key, index, converted.Object);
                    }
                }
            }

            return(results);
        }
示例#15
0
        private async Task SaveSourceReplicationInformation(LegacySourceReplicationInformation replicationSource, DocumentsOperationContext context, string documentId)
        {
            var blittable = EntityToBlittable.ConvertCommandToBlittable(replicationSource, context);

            using (var cmd = new MergedPutCommand(blittable, documentId, null, Database))
            {
                await Database.TxMerger.Enqueue(cmd);
            }
        }
        public static Dictionary <string, CompareExchangeValue <T> > GetValues(BlittableJsonReaderObject response, DocumentConventions conventions)
        {
            if (response.TryGet("Results", out BlittableJsonReaderArray items) == false)
            {
                throw new InvalidDataException("Response is invalid. Results is missing.");
            }

            var results = new Dictionary <string, CompareExchangeValue <T> >();

            foreach (BlittableJsonReaderObject item in items)
            {
                if (item == null)
                {
                    throw new InvalidDataException("Response is invalid. Item is null.");
                }

                if (item.TryGet("Key", out string key) == false)
                {
                    throw new InvalidDataException("Response is invalid. Key is missing.");
                }
                if (item.TryGet("Index", out long index) == false)
                {
                    throw new InvalidDataException("Response is invalid. Index is missing.");
                }
                if (item.TryGet("Value", out BlittableJsonReaderObject raw) == false)
                {
                    throw new InvalidDataException("Response is invalid. Value is missing.");
                }

                if (typeof(T).GetTypeInfo().IsPrimitive || typeof(T) == typeof(string))
                {
                    // simple
                    T value = default(T);
                    raw?.TryGet("Object", out value);
                    results[key] = new CompareExchangeValue <T>(key, index, value);
                }
                else
                {
                    BlittableJsonReaderObject val = null;
                    raw?.TryGet("Object", out val);
                    if (val == null)
                    {
                        results[key] = new CompareExchangeValue <T>(key, index, default(T));
                    }
                    else
                    {
                        var convereted = EntityToBlittable.ConvertToEntity(typeof(T), null, val, conventions);
                        results[key] = new CompareExchangeValue <T>(key, index, (T)convereted);
                    }
                }
            }

            return(results);
        }
示例#17
0
        public void FullBackupTo(string backupPath)
        {
            using (var file = new FileStream(backupPath, FileMode.Create))
                using (var package = new ZipArchive(file, ZipArchiveMode.Create, leaveOpen: true))
                    using (_serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                        using (context.OpenReadTransaction())
                        {
                            var databaseRecord = _serverStore.Cluster.ReadDatabase(context, Name);
                            Debug.Assert(databaseRecord != null);

                            var zipArchiveEntry = package.CreateEntry(RestoreSettings.FileName, CompressionLevel.Optimal);
                            using (var zipStream = zipArchiveEntry.Open())
                                using (var writer = new BlittableJsonTextWriter(context, zipStream))
                                {
                                    //TODO: encrypt this file using the MasterKey
                                    //http://issues.hibernatingrhinos.com/issue/RavenDB-7546

                                    writer.WriteStartObject();

                                    // save the database record
                                    writer.WritePropertyName(nameof(RestoreSettings.DatabaseRecord));
                                    var databaseRecordBlittable = EntityToBlittable.ConvertEntityToBlittable(databaseRecord, DocumentConventions.Default, context);
                                    context.Write(writer, databaseRecordBlittable);

                                    // save the database values (subscriptions, periodic backups statuses, etl states...)
                                    writer.WriteComma();
                                    writer.WritePropertyName(nameof(RestoreSettings.DatabaseValues));
                                    writer.WriteStartObject();

                                    var first = true;
                                    foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context,
                                                                                                        Helpers.ClusterStateMachineValuesPrefix(Name)))
                                    {
                                        if (first == false)
                                        {
                                            writer.WriteComma();
                                        }

                                        first = false;

                                        writer.WritePropertyName(keyValue.Key.ToString());
                                        context.Write(writer, keyValue.Value);
                                    }
                                    writer.WriteEndObject();
                                    // end of dictionary

                                    writer.WriteEndObject();
                                }

                            BackupMethods.Full.ToFile(GetAllStoragesEnvironmentInformation(), package);

                            file.Flush(true); // make sure that we fully flushed to disk
                        }
        }
示例#18
0
        public static FacetQuery Create(DocumentsOperationContext context, IndexQueryServerSide query)
        {
            long?facetsEtag         = null;
            DocumentsTransaction tx = null;

            try
            {
                var facets = new Dictionary <string, FacetSetup>(StringComparer.OrdinalIgnoreCase);
                foreach (var selectField in query.Metadata.SelectFields)
                {
                    if (selectField.IsFacet == false)
                    {
                        continue;
                    }

                    var facetField = (FacetField)selectField;
                    if (facetField.FacetSetupDocumentId == null || facets.ContainsKey(facetField.FacetSetupDocumentId))
                    {
                        continue;
                    }

                    if (tx == null)
                    {
                        tx = context.OpenReadTransaction();
                    }

                    var documentJson = context.DocumentDatabase.DocumentsStorage.Get(context, facetField.FacetSetupDocumentId);
                    if (documentJson == null)
                    {
                        throw new DocumentDoesNotExistException(facetField.FacetSetupDocumentId);
                    }

                    if (facetsEtag.HasValue == false)
                    {
                        facetsEtag = documentJson.Etag;
                    }
                    else
                    {
                        facetsEtag = facetsEtag.Value ^ documentJson.Etag;
                    }

                    var document = (FacetSetup)EntityToBlittable.ConvertToEntity(typeof(FacetSetup), facetField.FacetSetupDocumentId, documentJson.Data, DocumentConventions.Default);

                    facets[facetField.FacetSetupDocumentId] = document;
                }

                return(new FacetQuery(query, facets, facetsEtag ?? 0));
            }
            finally
            {
                tx?.Dispose();
            }
        }