Esempio n. 1
0
            public void WriteDatabaseRecord(DatabaseRecord databaseRecord, SmugglerProgressBase.DatabaseRecordProgress progress, AuthorizationStatus authorizationStatus)
            {
                _writer.WritePropertyName(nameof(databaseRecord.DatabaseName));
                _writer.WriteString(databaseRecord.DatabaseName);
                _writer.WriteComma();

                _writer.WritePropertyName(nameof(databaseRecord.Encrypted));
                _writer.WriteBool(databaseRecord.Encrypted);
                _writer.WriteComma();

                _writer.WritePropertyName(nameof(databaseRecord.Revisions));
                WriteRevisions(databaseRecord.Revisions);
                _writer.WriteComma();

                _writer.WritePropertyName(nameof(databaseRecord.Expiration));
                WriteExpiration(databaseRecord.Expiration);
                _writer.WriteComma();

                if (authorizationStatus == AuthorizationStatus.DatabaseAdmin)
                {
                    _writer.WritePropertyName(nameof(databaseRecord.RavenConnectionStrings));
                    WriteRavenConnectionStrings(databaseRecord.RavenConnectionStrings);
                    _writer.WriteComma();

                    _writer.WritePropertyName(nameof(databaseRecord.SqlConnectionStrings));
                    WriteSqlConnectionStrings(databaseRecord.SqlConnectionStrings);
                    _writer.WriteComma();
                }

                _writer.WritePropertyName(nameof(databaseRecord.Client));
                WriteClientConfiguration(databaseRecord.Client);
            }
Esempio n. 2
0
        public async Task Put()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var id          = GetQueryStringValueAndAssertIfSingleAndNotEmpty("id");
                var name        = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name");
                var contentType = GetStringQueryString("contentType", false) ?? "";

                AttachmentDetails result;
                using (var streamsTempFile = Database.DocumentsStorage.AttachmentsStorage.GetTempFile("put"))
                    using (var stream = streamsTempFile.StartNewStream())
                    {
                        var hash = await AttachmentsStorageHelper.CopyStreamToFileAndCalculateHash(context, RequestBodyStream(), stream, Database.DatabaseShutdown);

                        var changeVector = context.GetLazyString(GetStringQueryString("If-Match", false));

                        var cmd = new MergedPutAttachmentCommand
                        {
                            Database             = Database,
                            ExpectedChangeVector = changeVector,
                            DocumentId           = id,
                            Name        = name,
                            Stream      = stream,
                            Hash        = hash,
                            ContentType = contentType
                        };
                        await Database.TxMerger.Enqueue(cmd);

                        cmd.ExceptionDispatchInfo?.Throw();
                        result = cmd.Result;
                    }

                HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    writer.WritePropertyName(nameof(AttachmentDetails.ChangeVector));
                    writer.WriteString(result.ChangeVector);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.Name));
                    writer.WriteString(result.Name);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.DocumentId));
                    writer.WriteString(result.DocumentId);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.ContentType));
                    writer.WriteString(result.ContentType);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.Hash));
                    writer.WriteString(result.Hash);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.Size));
                    writer.WriteInteger(result.Size);

                    writer.WriteEndObject();
                }
            }
        }
        public async Task Try()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                BlittableJsonReaderObject json = await context.ReadForMemoryAsync(RequestBodyStream(), null);

                SubscriptionTryout tryout = JsonDeserializationServer.SubscriptionTryout(json);

                SubscriptionConnection.ParsedSubscription sub = SubscriptionConnection.ParseSubscriptionQuery(tryout.Query);
                SubscriptionPatchDocument patch = null;
                if (string.IsNullOrEmpty(sub.Script) == false)
                {
                    patch = new SubscriptionPatchDocument(sub.Script, sub.Functions);
                }

                if (sub.Collection == null)
                {
                    throw new ArgumentException("Collection must be specified");
                }

                var pageSize = GetIntValueQueryString("pageSize") ?? 1;

                var state = new SubscriptionState
                {
                    ChangeVectorForNextBatchStartingPoint = tryout.ChangeVector,
                    Query = tryout.Query
                };

                var fetcher = new SubscriptionDocumentsFetcher(Database, pageSize, -0x42,
                                                               new IPEndPoint(HttpContext.Connection.RemoteIpAddress, HttpContext.Connection.RemotePort), sub.Collection, sub.Revisions, state, patch);

                var includeCmd = new IncludeDocumentsCommand(Database.DocumentsStorage, context, sub.Includes);

                if (Enum.TryParse(
                        tryout.ChangeVector,
                        out Constants.Documents.SubscriptionChangeVectorSpecialStates changeVectorSpecialValue))
                {
                    switch (changeVectorSpecialValue)
                    {
                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.BeginningOfTime:
                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.DoNotChange:
                        state.ChangeVectorForNextBatchStartingPoint = null;
                        break;

                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.LastDocument:
                        state.ChangeVectorForNextBatchStartingPoint = Database.DocumentsStorage.GetLastDocumentChangeVector(context, sub.Collection);
                        break;
                    }
                }

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName("Results");
                    writer.WriteStartArray();

                    using (context.OpenReadTransaction())
                    {
                        var first = true;

                        foreach (var itemDetails in fetcher.GetDataToSend(context, 0))
                        {
                            if (itemDetails.Doc.Data == null)
                            {
                                continue;
                            }

                            includeCmd.Gather(itemDetails.Doc);

                            if (first == false)
                            {
                                writer.WriteComma();
                            }

                            if (itemDetails.Exception == null)
                            {
                                writer.WriteDocument(context, itemDetails.Doc, metadataOnly: false);
                            }
                            else
                            {
                                var docWithExcepton = new DocumentWithException
                                {
                                    Exception    = itemDetails.Exception.ToString(),
                                    ChangeVector = itemDetails.Doc.ChangeVector,
                                    Id           = itemDetails.Doc.Id,
                                    DocumentData = itemDetails.Doc.Data
                                };
                                writer.WriteObject(context.ReadObject(docWithExcepton.ToJson(), ""));
                            }

                            first = false;
                        }

                        writer.WriteEndArray();
                        writer.WriteComma();
                        writer.WritePropertyName("Includes");
                        var includes = new List <Document>();
                        includeCmd.Fill(includes);
                        writer.WriteIncludes(context, includes);
                        writer.WriteEndObject();
                    }
                }
            }
        }
Esempio n. 4
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 values

                                    writer.WriteComma();
                                    writer.WritePropertyName(nameof(RestoreSettings.Identities));
                                    writer.WriteStartObject();

                                    first = true;
                                    foreach (var identity in ServerStore.Cluster.ReadIdentities(context, Name, 0, long.MaxValue))
                                    {
                                        if (first == false)
                                        {
                                            writer.WriteComma();
                                        }

                                        first = false;
                                        writer.WritePropertyName(identity.Prefix);
                                        writer.WriteInteger(identity.Value);
                                    }
                                    writer.WriteEndObject();
                                    // end of identities

                                    writer.WriteEndObject();
                                }

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

                            file.Flush(true); // make sure that we fully flushed to disk
                        }
        }
        private static void WriteIndexFieldOptions(this BlittableJsonTextWriter writer, JsonOperationContext context, IndexFieldOptions options, bool removeAnalyzers)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(options.Analyzer));
            if (string.IsNullOrWhiteSpace(options.Analyzer) == false && !removeAnalyzers)
            {
                writer.WriteString(options.Analyzer);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Indexing));
            if (options.Indexing.HasValue)
            {
                writer.WriteString(options.Indexing.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Storage));
            if (options.Storage.HasValue)
            {
                writer.WriteString(options.Storage.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Suggestions));
            if (options.Suggestions.HasValue)
            {
                writer.WriteBool(options.Suggestions.Value);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.TermVector));
            if (options.TermVector.HasValue)
            {
                writer.WriteString(options.TermVector.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Spatial));
            if (options.Spatial != null)
            {
                writer.WriteStartObject();

                writer.WritePropertyName(nameof(options.Spatial.Type));
                writer.WriteString(options.Spatial.Type.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxTreeLevel));
                writer.WriteInteger(options.Spatial.MaxTreeLevel);
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxX));
                LazyStringValue lazyStringValue;
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MaxX)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxY));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MaxY)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MinX));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MinX)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MinY));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MinY)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.Strategy));
                writer.WriteString(options.Spatial.Strategy.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.Units));
                writer.WriteString(options.Spatial.Units.ToString());

                writer.WriteEndObject();
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
Esempio n. 6
0
        public Task ExtractInfoFromZip()
        {
            AssertOnlyInSetupMode();

            using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var continueSetupInfoJson = context.ReadForMemory(RequestBodyStream(), "continue-setup-info"))
                {
                    var    continueSetupInfo = JsonDeserializationServer.ContinueSetupInfo(continueSetupInfoJson);
                    byte[] zipBytes;
                    try
                    {
                        zipBytes = Convert.FromBase64String(continueSetupInfo.Zip);
                    }
                    catch (Exception e)
                    {
                        throw new ArgumentException($"Unable to parse the {nameof(continueSetupInfo.Zip)} property, expected a Base64 value", e);
                    }

                    try
                    {
                        var urlByTag = new Dictionary <string, string>();

                        using (var ms = new MemoryStream(zipBytes))
                            using (var archive = new ZipArchive(ms, ZipArchiveMode.Read, false))
                            {
                                foreach (var entry in archive.Entries)
                                {
                                    if (entry.Name.Equals("settings.json") == false)
                                    {
                                        continue;
                                    }

                                    var tag = entry.FullName[0].ToString();
                                    using (var sr = new StreamReader(entry.Open()))
                                    {
                                        dynamic jsonObj = JsonConvert.DeserializeObject(sr.ReadToEnd());
                                        urlByTag[tag] = jsonObj[nameof(ConfigurationNodeInfo.PublicServerUrl)];
                                    }
                                }
                            }

                        using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                        {
                            writer.WriteStartArray();
                            var first = true;

                            foreach (var node in urlByTag)
                            {
                                if (first == false)
                                {
                                    writer.WriteComma();
                                }

                                writer.WriteStartObject();
                                writer.WritePropertyName(nameof(ConfigurationNodeInfo.Tag));
                                writer.WriteString(node.Key);
                                writer.WriteComma();
                                writer.WritePropertyName(nameof(ConfigurationNodeInfo.PublicServerUrl));
                                writer.WriteString(node.Value);
                                writer.WriteEndObject();

                                first = false;
                            }

                            writer.WriteEndArray();
                        }
                    }
                    catch (Exception e)
                    {
                        throw new InvalidOperationException("Unable to extract setup information from the zip file.", e);
                    }
                }

            return(Task.CompletedTask);
        }
Esempio n. 7
0
        public Task QueriesCacheList()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    var queryCache = Database.QueryMetadataCache.GetQueryCache();

                    var responseDjv = new DynamicJsonValue();
                    var queriesList = new List <DynamicJsonValue>();

                    writer.WritePropertyName("TotalCachedQueries");
                    writer.WriteInteger(queryCache.Length);
                    writer.WriteComma();

                    foreach (var item in queryCache)
                    {
                        if (item != null)
                        {
                            var curDjvItem = new DynamicJsonValue();
                            queriesList.Add(curDjvItem);

                            curDjvItem[nameof(Queries.QueryMetadata.CreatedAt)] = item.CreatedAt;

                            curDjvItem[nameof(Queries.QueryMetadata.LastQueriedAt)] = item.LastQueriedAt;
                            if (item.IsGroupBy)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.IsGroupBy)] = true;
                            }

                            if (item.IsDistinct)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.IsDistinct)] = true;
                            }

                            if (item.HasFacet)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.HasFacet)] = true;
                            }

                            if (item.HasMoreLikeThis)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.HasMoreLikeThis)] = true;
                            }

                            if (item.HasSuggest)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.HasSuggest)] = true;
                            }

                            if (item.OrderBy != null)
                            {
                                curDjvItem["Sorted"] = true;
                            }

                            if (item.IsOptimizedSortOnly)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.IsOptimizedSortOnly)] = true;
                            }

                            if (item.HasCmpXchg)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.HasCmpXchg)] = true;
                            }

                            if (item.HasExplanations)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.HasExplanations)] = true;
                            }

                            if (item.HasIntersect)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.HasIntersect)] = true;
                            }

                            if (item.IsDynamic)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.IsDynamic)] = true;
                            }

                            if (item.SelectFields.Any(x => x.Function != null))
                            {
                                curDjvItem["IsJSProjection"] = true;
                            }

                            if (string.IsNullOrEmpty(item.CollectionName) == false)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.CollectionName)] = item.CollectionName;
                            }

                            if (string.IsNullOrEmpty(item.AutoIndexName) == false)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.AutoIndexName)] = item.AutoIndexName;
                            }

                            if (string.IsNullOrEmpty(item.IndexName) == false)
                            {
                                curDjvItem[nameof(Queries.QueryMetadata.IndexName)] = item.IndexName;
                            }

                            curDjvItem[nameof(Queries.QueryMetadata.QueryText)] = item.QueryText;
                        }
                    }

                    writer.WriteArray("Results", queriesList, context);
                }

            return(Task.CompletedTask);
        }
Esempio n. 8
0
        public async Task PostMultiGet()
        {
            using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var input = await context.ReadForMemoryAsync(RequestBodyStream(), "multi_get");

                if (input.TryGet("Requests", out BlittableJsonReaderArray requests) == false)
                {
                    ThrowRequiredPropertyNameInRequest("Requests");
                }

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName("Results");
                    writer.WriteStartArray();
                    var resultProperty  = context.GetLazyStringForFieldWithCaching(nameof(GetResponse.Result));
                    var statusProperty  = context.GetLazyStringForFieldWithCaching(nameof(GetResponse.StatusCode));
                    var headersProperty = context.GetLazyStringForFieldWithCaching(nameof(GetResponse.Headers));

                    var features       = new FeatureCollection(HttpContext.Features);
                    var responseStream = new MultiGetHttpResponseStream(ResponseBodyStream());
                    features.Set <IHttpResponseFeature>(new MultiGetHttpResponseFeature(responseStream));
                    var httpContext = new DefaultHttpContext(features);
                    var host        = HttpContext.Request.Host;
                    var scheme      = HttpContext.Request.Scheme;

                    for (int i = 0; i < requests.Length; i++)
                    {
                        var request = (BlittableJsonReaderObject)requests[i];

                        if (i != 0)
                        {
                            writer.WriteComma();
                        }
                        writer.WriteStartObject();

                        if (request.TryGet("Url", out string url) == false || request.TryGet("Query", out string query) == false)
                        {
                            writer.WriteEndObject();
                            continue;
                        }

                        if (request.TryGet("Method", out string method) == false || string.IsNullOrEmpty(method))
                        {
                            method = HttpMethod.Get.Method;
                        }

                        httpContext.Request.Method = method;

                        var routeInformation = Server.Router.GetRoute(method, url, out RouteMatch localMatch);
                        if (routeInformation == null)
                        {
                            writer.WritePropertyName(statusProperty);
                            writer.WriteInteger((int)HttpStatusCode.BadRequest);
                            writer.WritePropertyName(resultProperty);
                            context.Write(writer, new DynamicJsonValue
                            {
                                ["Error"] = $"There is no handler for path: {method} {url}{query}"
                            });
                            writer.WriteEndObject();
                            continue;
                        }

                        var requestHandler = routeInformation.GetRequestHandler();
                        writer.WritePropertyName(resultProperty);
                        writer.Flush();

                        httpContext.Response.StatusCode = 0;
                        httpContext.Request.Headers.Clear();
                        httpContext.Response.Headers.Clear();
                        httpContext.Request.Host        = host;
                        httpContext.Request.Scheme      = scheme;
                        httpContext.Request.QueryString = new QueryString(query);
                        if (request.TryGet("Headers", out BlittableJsonReaderObject headers))
                        {
                            foreach (var header in headers.GetPropertyNames())
                            {
                                if (headers.TryGet(header, out string value) == false)
                                {
                                    continue;
                                }

                                if (string.IsNullOrWhiteSpace(value))
                                {
                                    continue;
                                }

                                httpContext.Request.Headers.Add(header, value);
                            }
                        }

                        if (method == HttpMethod.Post.Method && request.TryGet("Content", out object content))
                        {
                            if (content is LazyStringValue)
                            {
                                var requestBody = GetRequestBody(content.ToString());
                                HttpContext.Response.RegisterForDispose(requestBody);
                                httpContext.Request.Body = requestBody;
                            }
                            else
                            {
                                var requestBody   = new MemoryStream();
                                var contentWriter = new BlittableJsonTextWriter(context, requestBody);
                                context.Write(contentWriter, (BlittableJsonReaderObject)content);
                                contentWriter.Flush();
                                HttpContext.Response.RegisterForDispose(requestBody);
                                httpContext.Request.Body          = requestBody;
                                httpContext.Request.Body.Position = 0;
                            }
                        }

                        var bytesWrittenBeforeRequest = responseStream.BytesWritten;
                        int statusCode;
                        try
                        {
                            await requestHandler(new RequestHandlerContext
                            {
                                Database    = Database,
                                RavenServer = Server,
                                RouteMatch  = localMatch,
                                HttpContext = httpContext
                            });

                            if (bytesWrittenBeforeRequest == responseStream.BytesWritten)
                            {
                                writer.WriteNull();
                            }

                            statusCode = httpContext.Response.StatusCode == 0
                                ? (int)HttpStatusCode.OK
                                : httpContext.Response.StatusCode;
                        }
                        catch (Exception e)
                        {
                            if (bytesWrittenBeforeRequest != responseStream.BytesWritten)
                            {
                                throw;
                            }

                            statusCode = (int)HttpStatusCode.InternalServerError;

                            var djv = new DynamicJsonValue
                            {
                                [nameof(ExceptionDispatcher.ExceptionSchema.Url)]     = $"{url}{query}",
                                [nameof(ExceptionDispatcher.ExceptionSchema.Type)]    = e.GetType().FullName,
                                [nameof(ExceptionDispatcher.ExceptionSchema.Message)] = e.Message,
                                [nameof(ExceptionDispatcher.ExceptionSchema.Error)]   = e.ToString()
                            };

                            using (var json = context.ReadObject(djv, "exception"))
                                writer.WriteObject(json);
                        }

                        writer.WriteComma();
                        writer.WritePropertyName(statusProperty);
                        writer.WriteInteger(statusCode);
                        writer.WriteComma();

                        writer.WritePropertyName(headersProperty);
                        writer.WriteStartObject();
                        bool headerStart = true;
                        foreach (var header in httpContext.Response.Headers)
                        {
                            foreach (var value in header.Value)
                            {
                                if (headerStart == false)
                                {
                                    writer.WriteComma();
                                }
                                headerStart = false;
                                writer.WritePropertyName(header.Key);
                                writer.WriteString(value);
                            }
                        }
                        writer.WriteEndObject();

                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }
            }
        }
        public Task CommitNonLazyTx()
        {
            var modeStr = GetQueryStringValueAndAssertIfSingleAndNotEmpty("mode");

            if (Enum.TryParse(modeStr, true, out TransactionsMode mode) == false)
            {
                throw new InvalidOperationException("Query string value 'mode' is not a valid mode: " + modeStr);
            }

            var configDuration = Database.Configuration.Storage.TransactionsModeDuration.AsTimeSpan;
            var duration       = GetTimeSpanQueryString("duration", required: false) ?? configDuration;

            using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(("Environments"));
                    writer.WriteStartArray();
                    bool first = true;
                    foreach (var storageEnvironment in Database.GetAllStoragesEnvironment())
                    {
                        if (storageEnvironment == null)
                        {
                            continue;
                        }

                        if (first == false)
                        {
                            writer.WriteComma();
                        }
                        first = false;

                        var result = storageEnvironment.Environment.SetTransactionMode(mode, duration);

                        var djv = new DynamicJsonValue
                        {
                            ["Type"] = storageEnvironment.Type,
                            ["Mode"] = mode.ToString(),
                            ["Path"] = storageEnvironment.Environment.Options.BasePath.FullPath,
                        };

                        switch (result)
                        {
                        case TransactionsModeResult.ModeAlreadySet:
                            djv["Result"] = "Mode Already Set";
                            break;

                        case TransactionsModeResult.SetModeSuccessfully:
                            djv["Result"] = "Mode Set Successfully";
                            break;

                        default:
                            throw new ArgumentOutOfRangeException("Result is unexpected value: " + result);
                        }

                        context.Write(writer, djv);
                    }
                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }
            return(Task.CompletedTask);
        }
Esempio n. 10
0
        public Task PreviewCollection()
        {
            var start        = GetStart();
            var pageSize     = GetPageSize();
            var collection   = GetStringQueryString("collection", required: false);
            var bindings     = GetStringValuesQueryString("binding", required: false);
            var fullBindings = GetStringValuesQueryString("fullBinding", required: false);

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.OpenReadTransaction())
                {
                    Document[] documents;
                    HashSet <LazyStringValue> availableColumns;
                    HashSet <string>          propertiesPreviewToSend;
                    HashSet <string>          fullPropertiesToSend = new HashSet <string>(fullBindings);

                    long   totalResults;
                    string changeVector;
                    string etag = null;

                    if (string.IsNullOrEmpty(collection))
                    {
                        changeVector = DocumentsStorage.GetDatabaseChangeVector(context);
                        totalResults = Database.DocumentsStorage.GetNumberOfDocuments(context);
                        etag         = $"{changeVector}/{totalResults}";
                    }
                    else
                    {
                        changeVector = Database.DocumentsStorage.GetLastDocumentChangeVector(context, collection);
                        totalResults = Database.DocumentsStorage.GetCollection(collection, context).Count;

                        if (changeVector != null)
                        {
                            etag = $"{changeVector}/{totalResults}";
                        }
                    }

                    if (etag != null && GetStringFromHeaders("If-None-Match") == etag)
                    {
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.NotModified;
                        return(Task.CompletedTask);
                    }
                    HttpContext.Response.Headers["ETag"] = "\"" + etag + "\"";

                    if (string.IsNullOrEmpty(collection))
                    {
                        documents               = Database.DocumentsStorage.GetDocumentsInReverseEtagOrder(context, start, pageSize).ToArray();
                        availableColumns        = ExtractColumnNames(documents, context);
                        propertiesPreviewToSend = bindings.Count > 0 ? new HashSet <string>(bindings) : new HashSet <string>();
                    }
                    else
                    {
                        documents               = Database.DocumentsStorage.GetDocumentsInReverseEtagOrder(context, collection, start, pageSize).ToArray();
                        availableColumns        = ExtractColumnNames(documents, context);
                        propertiesPreviewToSend = bindings.Count > 0 ? new HashSet <string>(bindings) : availableColumns.Take(ColumnsSamplingLimit).Select(x => x.ToString()).ToHashSet();
                    }

                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("Results");

                        {
                            writer.WriteStartArray();

                            var first = true;
                            foreach (var document in documents)
                            {
                                if (first == false)
                                {
                                    writer.WriteComma();
                                }
                                first = false;

                                using (document.Data)
                                {
                                    WriteDocument(writer, context, document, propertiesPreviewToSend, fullPropertiesToSend);
                                }
                            }

                            writer.WriteEndArray();
                        }

                        writer.WriteComma();

                        writer.WritePropertyName("TotalResults");
                        writer.WriteInteger(totalResults);

                        writer.WriteComma();

                        writer.WriteArray("AvailableColumns", availableColumns);

                        writer.WriteEndObject();
                    }

                    return(Task.CompletedTask);
                }
        }
Esempio n. 11
0
        public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
        {
            _baseUrl = $"{node.Url}/databases/{node.Database}";

            var request = new HttpRequestMessage
            {
                Method  = HttpMethod.Post,
                Content = new BlittableJsonContent(stream =>
                {
                    using (var writer = new BlittableJsonTextWriter(ctx, stream))
                    {
                        writer.WriteStartObject();

                        var first = true;
                        writer.WritePropertyName("Requests");
                        writer.WriteStartArray();
                        foreach (var command in _commands)
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }

                            first        = false;
                            var cacheKey = GetCacheKey(command, out string _);
                            using (_cache.Get(ctx, cacheKey, out string cachedChangeVector, out var _))
                            {
                                var headers = new Dictionary <string, string>();
                                if (cachedChangeVector != null)
                                {
                                    headers["If-None-Match"] = $"\"{cachedChangeVector}\"";
                                }

                                foreach (var header in command.Headers)
                                {
                                    headers[header.Key] = header.Value;
                                }

                                writer.WriteStartObject();

                                writer.WritePropertyName(nameof(GetRequest.Url));
                                writer.WriteString($"/databases/{node.Database}{command.Url}");
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Query));
                                writer.WriteString(command.Query);
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Method));
                                writer.WriteString(command.Method?.Method);
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Headers));
                                writer.WriteStartObject();

                                var firstInner = true;
                                foreach (var kvp in headers)
                                {
                                    if (firstInner == false)
                                    {
                                        writer.WriteComma();
                                    }

                                    firstInner = false;
                                    writer.WritePropertyName(kvp.Key);
                                    writer.WriteString(kvp.Value);
                                }
                                writer.WriteEndObject();
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Content));
                                if (command.Content != null)
                                {
                                    command.Content.WriteContent(writer, ctx);
                                }
                                else
                                {
                                    writer.WriteNull();
                                }

                                writer.WriteEndObject();
                            }
                        }
                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }
                })
            };

            url = $"{_baseUrl}/multi_get";

            return(request);
        }
Esempio n. 12
0
        private void WriteDocument(BlittableJsonTextWriter writer, JsonOperationContext context, Document document, HashSet <string> propertiesPreviewToSend, HashSet <string> fullPropertiesToSend)
        {
            if (_buffers == null)
            {
                _buffers = new BlittableJsonReaderObject.PropertiesInsertionBuffer();
            }

            writer.WriteStartObject();

            document.Data.TryGet(Constants.Documents.Metadata.Key, out BlittableJsonReaderObject metadata);

            bool first = true;

            var objectsStubs = new HashSet <LazyStringValue>();
            var arraysStubs  = new HashSet <LazyStringValue>();
            var trimmedValue = new HashSet <LazyStringValue>();

            var size = document.Data.GetPropertiesByInsertionOrder(_buffers);
            var prop = new BlittableJsonReaderObject.PropertyDetails();

            for (int i = 0; i < size; i++)
            {
                document.Data.GetPropertyByIndex(_buffers.Properties[i], ref prop);
                var sendFull = fullPropertiesToSend.Contains(prop.Name);
                if (sendFull || propertiesPreviewToSend.Contains(prop.Name))
                {
                    var strategy = sendFull ? ValueWriteStrategy.Passthrough : FindWriteStrategy(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);

                    if (strategy == ValueWriteStrategy.Passthrough || strategy == ValueWriteStrategy.Trim)
                    {
                        if (first == false)
                        {
                            writer.WriteComma();
                        }
                        first = false;
                    }

                    switch (strategy)
                    {
                    case ValueWriteStrategy.Passthrough:
                        writer.WritePropertyName(prop.Name);
                        writer.WriteValue(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                        break;

                    case ValueWriteStrategy.SubstituteWithArrayStub:
                        arraysStubs.Add(prop.Name);
                        break;

                    case ValueWriteStrategy.SubstituteWithObjectStub:
                        objectsStubs.Add(prop.Name);
                        break;

                    case ValueWriteStrategy.Trim:
                        writer.WritePropertyName(prop.Name);
                        WriteTrimmedValue(writer, prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                        trimmedValue.Add(prop.Name);
                        break;
                    }
                }
            }
            if (first == false)
            {
                writer.WriteComma();
            }

            var extraMetadataProperties = new DynamicJsonValue
            {
                [ObjectStubsKey]  = new DynamicJsonArray(objectsStubs),
                [ArrayStubsKey]   = new DynamicJsonArray(arraysStubs),
                [TrimmedValueKey] = new DynamicJsonArray(trimmedValue)
            };

            if (metadata != null)
            {
                metadata.Modifications = extraMetadataProperties;
                metadata = context.ReadObject(metadata, document.Id);
            }
            else
            {
                metadata = context.ReadObject(extraMetadataProperties, document.Id);
            }

            writer.WriteMetadata(document, metadata);
            writer.WriteEndObject();
        }
Esempio n. 13
0
        public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
        {
            _baseUrl = $"{node.Url}/databases/{node.Database}";
            url      = $"{_baseUrl}/multi_get";

            var aggressiveCacheOptions = _requestExecutor.AggressiveCaching.Value;

            if (aggressiveCacheOptions?.Mode == AggressiveCacheMode.TrackChanges)
            {
                Result = new List <GetResponse>();
                foreach (var command in _commands)
                {
                    if (command.CanCacheAggressively == false)
                    {
                        break;
                    }
                    var cacheKey = GetCacheKey(command, out string _);
                    using (var cachedItem = _cache.Get(ctx, cacheKey, out _, out var cached))
                    {
                        if (cached == null ||
                            cachedItem.Age > aggressiveCacheOptions.Duration ||
                            cachedItem.MightHaveBeenModified)
                        {
                            break;
                        }

                        Result.Add(new GetResponse
                        {
                            Result     = cached,
                            StatusCode = HttpStatusCode.NotModified,
                        });
                    }
                }

                if (Result.Count == _commands.Count)
                {
                    return(null);// aggressively cached
                }
                // not all of it is cached, might as well read it all
                Result = null;
            }

            var request = new HttpRequestMessage
            {
                Method  = HttpMethod.Post,
                Content = new BlittableJsonContent(stream =>
                {
                    using (var writer = new BlittableJsonTextWriter(ctx, stream))
                    {
                        writer.WriteStartObject();

                        var first = true;
                        writer.WritePropertyName("Requests");
                        writer.WriteStartArray();
                        foreach (var command in _commands)
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }
                            first        = false;
                            var cacheKey = GetCacheKey(command, out string _);
                            using (_cache.Get(ctx, cacheKey, out string cachedChangeVector, out var _))
                            {
                                var headers = new Dictionary <string, string>();
                                if (cachedChangeVector != null)
                                {
                                    headers["If-None-Match"] = $"\"{cachedChangeVector}\"";
                                }

                                foreach (var header in command.Headers)
                                {
                                    headers[header.Key] = header.Value;
                                }

                                writer.WriteStartObject();

                                writer.WritePropertyName(nameof(GetRequest.Url));
                                writer.WriteString($"/databases/{node.Database}{command.Url}");
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Query));
                                writer.WriteString(command.Query);
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Method));
                                writer.WriteString(command.Method?.Method);
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Headers));
                                writer.WriteStartObject();

                                var firstInner = true;
                                foreach (var kvp in headers)
                                {
                                    if (firstInner == false)
                                    {
                                        writer.WriteComma();
                                    }

                                    firstInner = false;
                                    writer.WritePropertyName(kvp.Key);
                                    writer.WriteString(kvp.Value);
                                }
                                writer.WriteEndObject();
                                writer.WriteComma();

                                writer.WritePropertyName(nameof(GetRequest.Content));
                                if (command.Content != null)
                                {
                                    command.Content.WriteContent(writer, ctx);
                                }
                                else
                                {
                                    writer.WriteNull();
                                }

                                writer.WriteEndObject();
                            }
                        }
                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }
                })
            };

            return(request);
        }
Esempio n. 14
0
        public void FullBackupTo(string backupPath)
        {
            using (var file = SafeFileStream.Create(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.SmugglerValuesFileName, CompressionLevel.Optimal);
                            using (var zipStream = zipArchiveEntry.Open())
                            {
                                var smugglerSource = new DatabaseSource(this, 0);
                                using (DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
                                    using (ctx.OpenReadTransaction())
                                    {
                                        var smugglerDestination = new StreamDestination(zipStream, ctx, smugglerSource);
                                        var databaseSmugglerOptionsServerSide = new DatabaseSmugglerOptionsServerSide
                                        {
                                            AuthorizationStatus = AuthorizationStatus.DatabaseAdmin,
                                            OperateOnTypes      = DatabaseItemType.CompareExchange | DatabaseItemType.Identities
                                        };
                                        var smuggler = new DatabaseSmuggler(this,
                                                                            smugglerSource,
                                                                            smugglerDestination,
                                                                            this.Time,
                                                                            options: databaseSmugglerOptionsServerSide);

                                        smuggler.Execute();
                                    }
                            }

                            zipArchiveEntry = package.CreateEntry(RestoreSettings.SettingsFileName, 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;
                                    var prefix = Helpers.ClusterStateMachineValuesPrefix(Name);
                                    foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, prefix))
                                    {
                                        if (first == false)
                                        {
                                            writer.WriteComma();
                                        }

                                        first = false;

                                        var key = keyValue.Key.ToString().Substring(prefix.Length);
                                        writer.WritePropertyName(key);
                                        context.Write(writer, keyValue.Value);
                                    }

                                    writer.WriteEndObject();
                                    // end of values

                                    writer.WriteEndObject();
                                }

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

                            file.Flush(true); // make sure that we fully flushed to disk
                        }
        }
Esempio n. 15
0
        public Task GetIps()
        {
            AssertOnlyInSetupMode();

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName("MachineName");
                    writer.WriteString(Environment.MachineName);
                    writer.WriteComma();
                    writer.WritePropertyName("NetworkInterfaces");
                    writer.WriteStartArray();
                    var first = true;
                    foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
                    {
                        var ips = netInterface.GetIPProperties().UnicastAddresses
                                  .Where(x =>
                        {
                            // filter 169.254.xxx.xxx out, they are not meaningful for binding
                            if (x.Address.AddressFamily != AddressFamily.InterNetwork)
                            {
                                return(false);
                            }
                            var addressBytes = x.Address.GetAddressBytes();

                            // filter 127.xxx.xxx.xxx out, in docker only
                            if (SetupParameters.Get(ServerStore).IsDocker&& addressBytes[0] == 127)
                            {
                                return(false);
                            }

                            return(addressBytes[0] != 169 || addressBytes[1] != 254);
                        })
                                  .Select(addr => addr.Address.ToString())
                                  .ToList();

                        // If there's a hostname in the server url, add it to the list
                        if (SetupParameters.Get(ServerStore).DockerHostname != null && ips.Contains(SetupParameters.Get(ServerStore).DockerHostname) == false)
                        {
                            ips.Add(SetupParameters.Get(ServerStore).DockerHostname);
                        }

                        if (first == false)
                        {
                            writer.WriteComma();
                        }
                        first = false;

                        writer.WriteStartObject();
                        writer.WritePropertyName("Name");
                        writer.WriteString(netInterface.Name);
                        writer.WriteComma();
                        writer.WritePropertyName("Description");
                        writer.WriteString(netInterface.Description);
                        writer.WriteComma();
                        writer.WriteArray("Addresses", ips);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }

            return(Task.CompletedTask);
        }
Esempio n. 16
0
        public async Task TestJavaScriptIndex()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var input = await context.ReadForMemoryAsync(RequestBodyStream(), "TestJavaScriptIndex");

                if (input.TryGet("Definition", out BlittableJsonReaderObject index) == false)
                {
                    ThrowRequiredPropertyNameInRequest("Definition");
                }

                input.TryGet("Ids", out BlittableJsonReaderArray ids);

                var indexDefinition = JsonDeserializationServer.IndexDefinition(index);

                if (indexDefinition.Maps == null || indexDefinition.Maps.Count == 0)
                {
                    throw new ArgumentException("Index must have a 'Maps' fields");
                }

                indexDefinition.Type = indexDefinition.DetectStaticIndexType();

                if (indexDefinition.Type.IsJavaScript() == false)
                {
                    throw new UnauthorizedAccessException("Testing indexes is only allowed for JavaScript indexes.");
                }

                var compiledIndex = new JavaScriptIndex(indexDefinition, Database.Configuration);

                var inputSize         = GetIntValueQueryString("inputSize", false) ?? defaultInputSizeForTestingJavaScriptIndex;
                var collections       = new HashSet <string>(compiledIndex.Maps.Keys);
                var docsPerCollection = new Dictionary <string, List <DynamicBlittableJson> >();
                using (context.OpenReadTransaction())
                {
                    if (ids == null)
                    {
                        foreach (var collection in collections)
                        {
                            docsPerCollection.Add(collection,
                                                  Database.DocumentsStorage.GetDocumentsFrom(context, collection, 0, 0, inputSize).Select(d => new DynamicBlittableJson(d)).ToList());
                        }
                    }
                    else
                    {
                        var listOfIds = ids.Select(x => x.ToString());
                        var _         = new Reference <int>
                        {
                            Value = 0
                        };
                        var docs = Database.DocumentsStorage.GetDocuments(context, listOfIds, 0, int.MaxValue, _);
                        foreach (var doc in docs)
                        {
                            if (doc.TryGetMetadata(out var metadata) && metadata.TryGet(Constants.Documents.Metadata.Collection, out string collectionStr))
                            {
                                if (docsPerCollection.TryGetValue(collectionStr, out var listOfDocs) == false)
                                {
                                    listOfDocs = docsPerCollection[collectionStr] = new List <DynamicBlittableJson>();
                                }
                                listOfDocs.Add(new DynamicBlittableJson(doc));
                            }
                        }
                    }

                    var mapRes = new List <ObjectInstance>();
                    //all maps
                    foreach (var ListOfFunctions in compiledIndex.Maps)
                    {
                        //multi maps per collection
                        foreach (var mapFunc in ListOfFunctions.Value)
                        {
                            if (docsPerCollection.TryGetValue(ListOfFunctions.Key, out var docs))
                            {
                                foreach (var res in mapFunc(docs))
                                {
                                    mapRes.Add((ObjectInstance)res);
                                }
                            }
                        }
                    }
                    var first = true;
                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("MapResults");
                        writer.WriteStartArray();
                        foreach (var mapResult in mapRes)
                        {
                            if (JavaScriptIndexUtils.StringifyObject(mapResult) is JsString jsStr)
                            {
                                if (first == false)
                                {
                                    writer.WriteComma();
                                }
                                writer.WriteString(jsStr.ToString());
                                first = false;
                            }
                        }
                        writer.WriteEndArray();
                        if (indexDefinition.Reduce != null)
                        {
                            using (var bufferPool = new UnmanagedBuffersPoolWithLowMemoryHandling("JavaScriptIndexTest", Database.Name))
                            {
                                compiledIndex.SetBufferPoolForTestingPurposes(bufferPool);
                                compiledIndex.SetAllocatorForTestingPurposes(context.Allocator);
                                first = true;
                                writer.WritePropertyName("ReduceResults");
                                writer.WriteStartArray();

                                var reduceResults = compiledIndex.Reduce(mapRes.Select(mr => new DynamicBlittableJson(JsBlittableBridge.Translate(context, mr.Engine, mr))));

                                foreach (JsValue reduceResult in reduceResults)
                                {
                                    if (JavaScriptIndexUtils.StringifyObject(reduceResult) is JsString jsStr)
                                    {
                                        if (first == false)
                                        {
                                            writer.WriteComma();
                                        }

                                        writer.WriteString(jsStr.ToString());
                                        first = false;
                                    }
                                }
                            }

                            writer.WriteEndArray();
                        }
                        writer.WriteEndObject();
                    }
                }
            }
        }
Esempio n. 17
0
        public Task GetHosts()
        {
            AssertOnlyInSetupMode();

            using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var certificateJson = context.ReadForMemory(RequestBodyStream(), "setup-certificate"))
                {
                    var certDef = JsonDeserializationServer.CertificateDefinition(certificateJson);

                    X509Certificate2 certificate = null;
                    string           cn;

                    try
                    {
                        certificate = certDef.Password == null
                        ? new X509Certificate2(Convert.FromBase64String(certDef.Certificate))
                        : new X509Certificate2(Convert.FromBase64String(certDef.Certificate), certDef.Password);

                        cn = certificate.GetNameInfo(X509NameType.DnsName, false);
                    }
                    catch (Exception e)
                    {
                        throw new BadRequestException($"Failed to extract the CN property from the certificate {certificate?.FriendlyName}. Maybe the password is wrong?", e);
                    }

                    try
                    {
                        SecretProtection.ValidateKeyUsages("Setup Wizard", certificate);
                    }
                    catch (Exception e)
                    {
                        throw new InvalidOperationException($"Failed to load the uploaded certificate. Did you accidently upload a client certificate?", e);
                    }

                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("CN");
                        writer.WriteString(cn);
                        writer.WriteComma();
                        writer.WritePropertyName("AlternativeNames");
                        writer.WriteStartArray();

                        var first = true;
                        foreach (var value in SetupManager.GetCertificateAlternativeNames(certificate))
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }
                            first = false;

                            writer.WriteString(value);
                        }

                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }
                }

            return(Task.CompletedTask);
        }
Esempio n. 18
0
        public async Task Patch()
        {
            var id = GetQueryStringValueAndAssertIfSingleAndNotEmpty("id");

            var isTest    = GetBoolValueQueryString("test", required: false) ?? false;
            var debugMode = GetBoolValueQueryString("debug", required: false) ?? isTest;
            var skipPatchIfChangeVectorMismatch = GetBoolValueQueryString("skipPatchIfChangeVectorMismatch", required: false) ?? false;

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var request = context.Read(RequestBodyStream(), "ScriptedPatchRequest");
                if (request.TryGet("Patch", out BlittableJsonReaderObject patchCmd) == false || patchCmd == null)
                {
                    throw new ArgumentException("The 'Patch' field in the body request is mandatory");
                }

                var patch = PatchRequest.Parse(patchCmd, out var patchArgs);

                PatchRequest patchIfMissing = null;
                BlittableJsonReaderObject patchIfMissingArgs = null;
                if (request.TryGet("PatchIfMissing", out BlittableJsonReaderObject patchIfMissingCmd) && patchIfMissingCmd != null)
                {
                    patchIfMissing = PatchRequest.Parse(patchIfMissingCmd, out patchIfMissingArgs);
                }

                var changeVector = context.GetLazyString(GetStringFromHeaders("If-Match"));

                using (var command = new PatchDocumentCommand(context,
                                                              id,
                                                              changeVector,
                                                              skipPatchIfChangeVectorMismatch,
                                                              (patch, patchArgs),
                                                              (patchIfMissing, patchIfMissingArgs),
                                                              Database,
                                                              isTest,
                                                              debugMode
                                                              ))
                {
                    if (isTest == false)
                    {
                        await Database.TxMerger.Enqueue(command);
                    }
                    else
                    {
                        // PutDocument requires the write access to the docs storage
                        // testing patching is rare enough not to optimize it
                        using (context.OpenWriteTransaction())
                        {
                            command.Execute(context);
                        }
                    }

                    switch (command.PatchResult.Status)
                    {
                    case PatchStatus.DocumentDoesNotExist:
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                        return;

                    case PatchStatus.Created:
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;
                        break;

                    case PatchStatus.Skipped:
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.NotModified;
                        return;

                    case PatchStatus.Patched:
                    case PatchStatus.NotModified:
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(command.PatchResult.Status));
                        writer.WriteString(command.PatchResult.Status.ToString());
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(command.PatchResult.ModifiedDocument));
                        writer.WriteObject(command.PatchResult.ModifiedDocument);

                        if (debugMode)
                        {
                            writer.WriteComma();
                            writer.WritePropertyName(nameof(command.PatchResult.OriginalDocument));
                            if (isTest)
                            {
                                writer.WriteObject(command.PatchResult.OriginalDocument);
                            }
                            else
                            {
                                writer.WriteNull();
                            }

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(command.PatchResult.Debug));

                            context.Write(writer, new DynamicJsonValue
                            {
                                ["Info"]    = new DynamicJsonArray(command.DebugOutput),
                                ["Actions"] = command.DebugActions?.GetDebugActions()
                            });
                        }


                        writer.WriteEndObject();
                    }
                }
            }
        }
Esempio n. 19
0
        public async Task Put()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var id          = GetQueryStringValueAndAssertIfSingleAndNotEmpty("id");
                var name        = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name");
                var contentType = GetStringQueryString("contentType", false) ?? "";

                AttachmentDetails result;
                using (var streamsTempFile = Database.DocumentsStorage.AttachmentsStorage.GetTempFile("put"))
                    using (var stream = streamsTempFile.StartNewStream())
                    {
                        Stream requestBodyStream = RequestBodyStream();
                        string hash;
                        try
                        {
                            hash = await AttachmentsStorageHelper.CopyStreamToFileAndCalculateHash(context, requestBodyStream, stream, Database.DatabaseShutdown);
                        }
                        catch (Exception)
                        {
                            try
                            {
                                // if we failed to read the entire request body stream, we might leave
                                // data in the pipe still, this will cause us to read and discard the
                                // rest of the attachment stream and return the actual error to the caller
                                requestBodyStream.CopyTo(Stream.Null);
                            }
                            catch (Exception)
                            {
                                // we tried, but we can't clean the request, so let's just kill
                                // the connection
                                HttpContext.Abort();
                            }
                            throw;
                        }

                        var changeVector = context.GetLazyString(GetStringFromHeaders("If-Match"));

                        var cmd = new MergedPutAttachmentCommand
                        {
                            Database             = Database,
                            ExpectedChangeVector = changeVector,
                            DocumentId           = id,
                            Name        = name,
                            Stream      = stream,
                            Hash        = hash,
                            ContentType = contentType
                        };
                        stream.Flush();
                        await Database.TxMerger.Enqueue(cmd);

                        result = cmd.Result;
                    }

                HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    writer.WritePropertyName(nameof(AttachmentDetails.ChangeVector));
                    writer.WriteString(result.ChangeVector);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.Name));
                    writer.WriteString(result.Name);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.DocumentId));
                    writer.WriteString(result.DocumentId);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.ContentType));
                    writer.WriteString(result.ContentType);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.Hash));
                    writer.WriteString(result.Hash);
                    writer.WriteComma();

                    writer.WritePropertyName(nameof(AttachmentDetails.Size));
                    writer.WriteInteger(result.Size);

                    writer.WriteEndObject();
                }
            }
        }
Esempio n. 20
0
        public Task Stats()
        {
            var documentId = GetStringQueryString("docId");

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.OpenReadTransaction())
                {
                    var document = Database.DocumentsStorage.Get(context, documentId, DocumentFields.Data);
                    if (document == null)
                    {
                        HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                        return(Task.CompletedTask);
                    }

                    var timeSeriesNames = new List <string>();

                    if (document.TryGetMetadata(out var metadata))
                    {
                        if (metadata.TryGet(Constants.Documents.Metadata.TimeSeries, out BlittableJsonReaderArray timeSeries) && timeSeries != null)
                        {
                            foreach (object name in timeSeries)
                            {
                                if (name == null)
                                {
                                    continue;
                                }

                                if (name is string || name is LazyStringValue || name is LazyCompressedStringValue)
                                {
                                    timeSeriesNames.Add(name.ToString());
                                }
                            }
                        }
                    }

                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(TimeSeriesStatistics.DocumentId));
                        writer.WriteString(documentId);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(TimeSeriesStatistics.TimeSeries));

                        writer.WriteStartArray();

                        var first = true;
                        foreach (var tsName in timeSeriesNames)
                        {
                            if (first == false)
                            {
                                writer.WriteComma();
                            }
                            first = false;

                            var stats = Database.DocumentsStorage.TimeSeriesStorage.Stats.GetStats(context, documentId, tsName);

                            writer.WriteStartObject();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.Name));
                            writer.WriteString(tsName);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.NumberOfEntries));
                            writer.WriteInteger(stats.Count);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.StartDate));
                            writer.WriteDateTime(stats.Start, isUtc: true);

                            writer.WriteComma();

                            writer.WritePropertyName(nameof(TimeSeriesItemDetail.EndDate));
                            writer.WriteDateTime(stats.End, isUtc: true);

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }
                }

            return(Task.CompletedTask);
        }
Esempio n. 21
0
        public Task RunningQueries()
        {
            var indexes = Database
                          .IndexStore
                          .GetIndexes()
                          .ToList();

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    var isFirst = true;
                    foreach (var index in indexes)
                    {
                        if (isFirst == false)
                        {
                            writer.WriteComma();
                        }
                        isFirst = false;

                        writer.WritePropertyName(index.Name);
                        writer.WriteStartArray();

                        var isFirstInternal = true;
                        foreach (var query in index.CurrentlyRunningQueries)
                        {
                            if (isFirstInternal == false)
                            {
                                writer.WriteComma();
                            }

                            isFirstInternal = false;

                            writer.WriteStartObject();

                            writer.WritePropertyName((nameof(query.Duration)));
                            writer.WriteString(query.Duration.ToString());
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(query.QueryId)));
                            writer.WriteInteger(query.QueryId);
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(query.StartTime)));
                            writer.WriteDateTime(query.StartTime, isUtc: true);
                            writer.WriteComma();

                            writer.WritePropertyName((nameof(query.QueryInfo)));
                            writer.WriteIndexQuery(context, query.QueryInfo);

                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();
                    }

                    writer.WriteEndObject();
                }

            return(Task.CompletedTask);
        }
Esempio n. 22
0
        public static void WriteMoreLikeThisQuery(this BlittableJsonTextWriter writer, DocumentConventions conventions, JsonOperationContext context, MoreLikeThisQuery query)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(query.DocumentId));
            writer.WriteString(query.DocumentId);
            writer.WriteComma();

            if (query.PageSizeSet && query.PageSize >= 0)
            {
                writer.WritePropertyName(nameof(query.PageSize));
                writer.WriteInteger(query.PageSize);
                writer.WriteComma();
            }

            if (query.Boost.HasValue)
            {
                writer.WritePropertyName(nameof(query.Boost));
                writer.WriteBool(query.Boost.Value);
                writer.WriteComma();
            }

            if (query.BoostFactor.HasValue)
            {
                writer.WritePropertyName(nameof(query.BoostFactor));
                writer.WriteDouble(query.BoostFactor.Value);
                writer.WriteComma();
            }

            if (string.IsNullOrEmpty(query.StopWordsDocumentId) == false)
            {
                writer.WritePropertyName(nameof(query.StopWordsDocumentId));
                writer.WriteString(query.StopWordsDocumentId);
                writer.WriteComma();
            }

            if (query.Fields != null && query.Fields.Length > 0)
            {
                writer.WriteArray(nameof(query.Fields), query.Fields);
                writer.WriteComma();
            }

            if (query.Includes != null && query.Includes.Length > 0)
            {
                writer.WriteArray(nameof(query.Includes), query.Includes);
                writer.WriteComma();
            }

            if (query.MaximumDocumentFrequency.HasValue)
            {
                writer.WritePropertyName(nameof(query.MaximumDocumentFrequency));
                writer.WriteInteger(query.MaximumDocumentFrequency.Value);
                writer.WriteComma();
            }

            if (query.MaximumDocumentFrequencyPercentage.HasValue)
            {
                writer.WritePropertyName(nameof(query.MaximumDocumentFrequencyPercentage));
                writer.WriteInteger(query.MaximumDocumentFrequencyPercentage.Value);
                writer.WriteComma();
            }

            if (query.MaximumNumberOfTokensParsed.HasValue)
            {
                writer.WritePropertyName(nameof(query.MaximumNumberOfTokensParsed));
                writer.WriteInteger(query.MaximumNumberOfTokensParsed.Value);
                writer.WriteComma();
            }

            if (query.MaximumQueryTerms.HasValue)
            {
                writer.WritePropertyName(nameof(query.MaximumQueryTerms));
                writer.WriteInteger(query.MaximumQueryTerms.Value);
                writer.WriteComma();
            }

            if (query.MaximumWordLength.HasValue)
            {
                writer.WritePropertyName(nameof(query.MaximumWordLength));
                writer.WriteInteger(query.MaximumWordLength.Value);
                writer.WriteComma();
            }

            if (query.MinimumDocumentFrequency.HasValue)
            {
                writer.WritePropertyName(nameof(query.MinimumDocumentFrequency));
                writer.WriteInteger(query.MinimumDocumentFrequency.Value);
                writer.WriteComma();
            }

            if (query.MinimumTermFrequency.HasValue)
            {
                writer.WritePropertyName(nameof(query.MinimumTermFrequency));
                writer.WriteInteger(query.MinimumTermFrequency.Value);
                writer.WriteComma();
            }

            if (query.MinimumWordLength.HasValue)
            {
                writer.WritePropertyName(nameof(query.MinimumWordLength));
                writer.WriteInteger(query.MinimumWordLength.Value);
                writer.WriteComma();
            }

            if (string.IsNullOrEmpty(query.Document) == false)
            {
                writer.WritePropertyName(nameof(query.Document));
                writer.WriteString(query.Document);
                writer.WriteComma();
            }

            writer.WritePropertyName(nameof(query.Query));
            writer.WriteString(query.Query);

            writer.WriteEndObject();
        }
        public static void WriteIndexDefinition(this BlittableJsonTextWriter writer, JsonOperationContext context, IndexDefinition indexDefinition, bool removeAnalyzers = false)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(indexDefinition.Name));
            writer.WriteString(indexDefinition.Name);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.SourceType));
            writer.WriteString(indexDefinition.SourceType.ToString());
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Type));
            writer.WriteString(indexDefinition.Type.ToString());
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.LockMode));
            if (indexDefinition.LockMode.HasValue)
            {
                writer.WriteString(indexDefinition.LockMode.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Priority));
            if (indexDefinition.Priority.HasValue)
            {
                writer.WriteString(indexDefinition.Priority.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.OutputReduceToCollection));
            writer.WriteString(indexDefinition.OutputReduceToCollection);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.ReduceOutputIndex));

            if (indexDefinition.ReduceOutputIndex.HasValue)
            {
                writer.WriteInteger(indexDefinition.ReduceOutputIndex.Value);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.DeploymentMode));
            if (indexDefinition.DeploymentMode.HasValue)
            {
                writer.WriteString(indexDefinition.DeploymentMode.Value.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.PatternForOutputReduceToCollectionReferences));
            writer.WriteString(indexDefinition.PatternForOutputReduceToCollectionReferences);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.PatternReferencesCollectionName));
            writer.WriteString(indexDefinition.PatternReferencesCollectionName);
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Configuration));
            writer.WriteStartObject();
            var isFirstInternal = true;

            foreach (var kvp in indexDefinition.Configuration)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WritePropertyName(kvp.Key);
                writer.WriteString(kvp.Value);
            }
            writer.WriteEndObject();
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.AdditionalSources));
            writer.WriteStartObject();
            isFirstInternal = true;
            foreach (var kvp in indexDefinition.AdditionalSources)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WritePropertyName(kvp.Key);
                writer.WriteString(kvp.Value);
            }
            writer.WriteEndObject();
            writer.WriteComma();

#if FEATURE_TEST_INDEX
            writer.WritePropertyName(nameof(indexDefinition.IsTestIndex));
            writer.WriteBool(indexDefinition.IsTestIndex);
            writer.WriteComma();
#endif

            writer.WritePropertyName(nameof(indexDefinition.Reduce));
            if (string.IsNullOrWhiteSpace(indexDefinition.Reduce) == false)
            {
                writer.WriteString(indexDefinition.Reduce);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Maps));
            writer.WriteStartArray();
            isFirstInternal = true;
            foreach (var map in indexDefinition.Maps)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;
                writer.WriteString(map);
            }
            writer.WriteEndArray();
            writer.WriteComma();

            writer.WritePropertyName(nameof(indexDefinition.Fields));
            writer.WriteStartObject();
            isFirstInternal = true;
            foreach (var kvp in indexDefinition.Fields)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;
                writer.WritePropertyName(kvp.Key);
                if (kvp.Value != null)
                {
                    writer.WriteIndexFieldOptions(context, kvp.Value, removeAnalyzers);
                }
                else
                {
                    writer.WriteNull();
                }
            }
            writer.WriteEndObject();

            writer.WriteEndObject();
        }
Esempio n. 24
0
        public async Task Try()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                BlittableJsonReaderObject json = await context.ReadForMemoryAsync(RequestBodyStream(), null);

                SubscriptionTryout tryout = JsonDeserializationServer.SubscriptionTryout(json);

                SubscriptionConnection.ParsedSubscription sub = SubscriptionConnection.ParseSubscriptionQuery(tryout.Query);
                SubscriptionPatchDocument patch = null;
                if (string.IsNullOrEmpty(sub.Script) == false)
                {
                    patch = new SubscriptionPatchDocument(sub.Script, sub.Functions);
                }

                if (sub.Collection == null)
                {
                    throw new ArgumentException("Collection must be specified");
                }

                const int maxPageSize = 1024;
                var       pageSize    = GetIntValueQueryString("pageSize") ?? 1;
                if (pageSize > maxPageSize)
                {
                    throw new ArgumentException($"Cannot gather more than {maxPageSize} results during tryouts, but requested number was {pageSize}.");
                }

                var state = new SubscriptionState
                {
                    ChangeVectorForNextBatchStartingPoint = tryout.ChangeVector,
                    Query = tryout.Query
                };

                var fetcher = new SubscriptionDocumentsFetcher(Database, int.MaxValue, -0x42,
                                                               new IPEndPoint(HttpContext.Connection.RemoteIpAddress, HttpContext.Connection.RemotePort), sub.Collection, sub.Revisions, state, patch);

                var includeCmd = new IncludeDocumentsCommand(Database.DocumentsStorage, context, sub.Includes, isProjection: patch != null);

                if (Enum.TryParse(
                        tryout.ChangeVector,
                        out Constants.Documents.SubscriptionChangeVectorSpecialStates changeVectorSpecialValue))
                {
                    switch (changeVectorSpecialValue)
                    {
                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.BeginningOfTime:
                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.DoNotChange:
                        state.ChangeVectorForNextBatchStartingPoint = null;
                        break;

                    case Constants.Documents.SubscriptionChangeVectorSpecialStates.LastDocument:
                        using (context.OpenReadTransaction())
                        {
                            state.ChangeVectorForNextBatchStartingPoint = Database.DocumentsStorage.GetLastDocumentChangeVector(context.Transaction.InnerTransaction, context, sub.Collection);
                        }
                        break;
                    }
                }
                else
                {
                    state.ChangeVectorForNextBatchStartingPoint = tryout.ChangeVector;
                }

                var changeVector = state.ChangeVectorForNextBatchStartingPoint.ToChangeVector();
                var cv           = changeVector.FirstOrDefault(x => x.DbId == Database.DbBase64Id);

                var sp        = Stopwatch.StartNew();
                var timeLimit = TimeSpan.FromSeconds(GetIntValueQueryString("timeLimit", false) ?? 15);
                var startEtag = cv.Etag;

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    using (context.OpenReadTransaction())
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("Results");
                        writer.WriteStartArray();
                        var numberOfDocs = 0;
                        while (numberOfDocs == 0 && sp.Elapsed < timeLimit)
                        {
                            var first    = true;
                            var lastEtag = startEtag;
                            foreach (var itemDetails in fetcher.GetDataToSend(context, includeCmd, startEtag))
                            {
                                if (itemDetails.Doc.Data != null)
                                {
                                    using (itemDetails.Doc.Data)
                                    {
                                        includeCmd.Gather(itemDetails.Doc);

                                        if (first == false)
                                        {
                                            writer.WriteComma();
                                        }

                                        if (itemDetails.Exception == null)
                                        {
                                            writer.WriteDocument(context, itemDetails.Doc, metadataOnly: false);
                                        }
                                        else
                                        {
                                            var documentWithException = new DocumentWithException
                                            {
                                                Exception    = itemDetails.Exception.ToString(),
                                                ChangeVector = itemDetails.Doc.ChangeVector,
                                                Id           = itemDetails.Doc.Id,
                                                DocumentData = itemDetails.Doc.Data
                                            };
                                            writer.WriteObject(context.ReadObject(documentWithException.ToJson(), ""));
                                        }

                                        first = false;

                                        if (++numberOfDocs >= pageSize)
                                        {
                                            break;
                                        }
                                    }
                                }

                                if (sp.Elapsed >= timeLimit)
                                {
                                    break;
                                }

                                lastEtag = itemDetails.Doc.Etag;
                            }

                            if (startEtag == lastEtag)
                            {
                                break;
                            }

                            startEtag = lastEtag;
                        }

                        writer.WriteEndArray();
                        writer.WriteComma();
                        writer.WritePropertyName("Includes");
                        var includes = new List <Document>();
                        includeCmd.Fill(includes);
                        writer.WriteIncludes(context, includes);
                        writer.WriteEndObject();
                    }
            }
        }
        private static void WriteMetadata(BlittableJsonTextWriter writer, Document document, BlittableJsonReaderObject metadata, Func <LazyStringValue, bool> filterMetadataProperty = null)
        {
            writer.WritePropertyName(Constants.Documents.Metadata.Key);
            writer.WriteStartObject();
            bool first = true;

            if (metadata != null)
            {
                var size = metadata.Count;
                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (int i = 0; i < size; i++)
                {
                    metadata.GetPropertyByIndex(i, ref prop);

                    if (filterMetadataProperty != null && filterMetadataProperty(prop.Name))
                    {
                        continue;
                    }

                    if (first == false)
                    {
                        writer.WriteComma();
                    }
                    first = false;
                    writer.WritePropertyName((string)prop.Name);
                    writer.WriteValue(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                }
            }

            if (first == false)
            {
                writer.WriteComma();
            }
            writer.WritePropertyName(Constants.Documents.Metadata.ChangeVector);
            writer.WriteString(document.ChangeVector);

            if (document.Flags != DocumentFlags.None)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Flags);
                writer.WriteString(document.Flags.ToString());
            }
            if (document.Id != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Id);
                writer.WriteString(document.Id);
            }
            if (document.IndexScore != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.IndexScore);
                writer.WriteDouble(document.IndexScore.Value);
            }
            if (document.Distance != null)
            {
                writer.WriteComma();
                var result = document.Distance.Value;
                writer.WritePropertyName(Constants.Documents.Metadata.SpatialResult);
                writer.WriteStartObject();
                writer.WritePropertyName(nameof(result.Distance));
                writer.WriteDouble(result.Distance);
                writer.WriteComma();
                writer.WritePropertyName(nameof(result.Latitude));
                writer.WriteDouble(result.Latitude);
                writer.WriteComma();
                writer.WritePropertyName(nameof(result.Longitude));
                writer.WriteDouble(result.Longitude);
                writer.WriteEndObject();
            }
            if (document.LastModified != DateTime.MinValue)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.LastModified);
                writer.WriteDateTime(document.LastModified, isUtc: true);
            }
            writer.WriteEndObject();
        }
Esempio n. 26
0
        public async Task GetIps()
        {
            AssertOnlyInSetupMode();

            NetworkInterface[] netInterfaces = null;
            try
            {
                netInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            }
            catch (Exception)
            {
                // https://github.com/dotnet/corefx/issues/26476
                // If GetAllNetworkInterfaces is not supported, we'll just return the default: 127.0.0.1
            }

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
            using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                var setupParameters = await SetupParameters.Get(ServerStore);

                writer.WriteStartObject();
                writer.WritePropertyName("MachineName");
                writer.WriteString(Environment.MachineName);
                writer.WriteComma();
                writer.WritePropertyName("NetworkInterfaces");
                writer.WriteStartArray();
                var first = true;

                List<string> ips;
                if (netInterfaces != null)
                {
                    foreach (var netInterface in netInterfaces)
                    {
                        ips = netInterface.GetIPProperties().UnicastAddresses
                            .Where(x =>
                            {
                                // filter 169.254.xxx.xxx out, they are not meaningful for binding
                                if (x.Address.AddressFamily != AddressFamily.InterNetwork)
                                    return false;
                                var addressBytes = x.Address.GetAddressBytes();

                                // filter 127.xxx.xxx.xxx out, in docker only
                                if (setupParameters.IsDocker && addressBytes[0] == 127)
                                    return false;

                                return addressBytes[0] != 169 || addressBytes[1] != 254;
                            })
                            .Select(addr => addr.Address.ToString())
                            .ToList();

                        // If there's a hostname in the server url, add it to the list
                        if (setupParameters.DockerHostname != null && ips.Contains(setupParameters.DockerHostname) == false)
                            ips.Add(setupParameters.DockerHostname);

                        if (first == false)
                            writer.WriteComma();
                        first = false;

                        writer.WriteStartObject();
                        writer.WritePropertyName("Name");
                        writer.WriteString(netInterface.Name);
                        writer.WriteComma();
                        writer.WritePropertyName("Description");
                        writer.WriteString(netInterface.Description);
                        writer.WriteComma();
                        writer.WriteArray("Addresses", ips);
                        writer.WriteEndObject();
                    }
                }
                else
                {
                    // https://github.com/dotnet/corefx/issues/26476
                    // If GetAllNetworkInterfaces is not supported, we'll just return the default: 127.0.0.1
                    ips = new List<string>
                    {
                        "127.0.0.1"
                    };
                    writer.WriteStartObject();
                    writer.WritePropertyName("Name");
                    writer.WriteString("Loopback Interface");
                    writer.WriteComma();
                    writer.WritePropertyName("Description");
                    writer.WriteString("Loopback Interface");
                    writer.WriteComma();
                    writer.WriteArray("Addresses", ips);
                    writer.WriteEndObject();
                }

                writer.WriteEndArray();
                writer.WriteEndObject();
            }
        }
        public static void WriteIndexQuery(this BlittableJsonTextWriter writer, DocumentConventions conventions, JsonOperationContext context, IndexQuery query)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(query.Query));
            writer.WriteString(query.Query);
            writer.WriteComma();

            if (query.PageSizeSet && query.PageSize >= 0)
            {
                writer.WritePropertyName(nameof(query.PageSize));
                writer.WriteInteger(query.PageSize);
                writer.WriteComma();
            }

            if (query.WaitForNonStaleResults)
            {
                writer.WritePropertyName(nameof(query.WaitForNonStaleResults));
                writer.WriteBool(query.WaitForNonStaleResults);
                writer.WriteComma();
            }

            if (query.Start > 0)
            {
                writer.WritePropertyName(nameof(query.Start));
                writer.WriteInteger(query.Start);
                writer.WriteComma();
            }

            if (query.WaitForNonStaleResultsTimeout.HasValue)
            {
                writer.WritePropertyName(nameof(query.WaitForNonStaleResultsTimeout));
                writer.WriteString(query.WaitForNonStaleResultsTimeout.Value.ToInvariantString());
                writer.WriteComma();
            }

            if (query.DisableCaching)
            {
                writer.WritePropertyName(nameof(query.DisableCaching));
                writer.WriteBool(query.DisableCaching);
                writer.WriteComma();
            }

#if FEATURE_SHOW_TIMINGS
            if (query.ShowTimings)
            {
                writer.WritePropertyName(nameof(query.ShowTimings));
                writer.WriteBool(query.ShowTimings);
                writer.WriteComma();
            }
#endif

            if (query.SkipDuplicateChecking)
            {
                writer.WritePropertyName(nameof(query.SkipDuplicateChecking));
                writer.WriteBool(query.SkipDuplicateChecking);
                writer.WriteComma();
            }

            writer.WritePropertyName(nameof(query.QueryParameters));
            if (query.QueryParameters != null)
            {
                writer.WriteObject(EntityToBlittable.ConvertEntityToBlittable(query.QueryParameters, conventions, context, conventions.CreateSerializer(), documentInfo: null));
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
Esempio n. 28
0
        public Task GetHosts()
        {
            AssertOnlyInSetupMode();

            using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            using (var certificateJson = context.ReadForMemory(RequestBodyStream(), "setup-certificate"))
            {
                var certDef = JsonDeserializationServer.CertificateDefinition(certificateJson);

                X509Certificate2 certificate = null;
                string cn;

                try
                {
                    certificate = certDef.Password == null
                        ? new X509Certificate2(Convert.FromBase64String(certDef.Certificate), (string)null, X509KeyStorageFlags.MachineKeySet)
                        : new X509Certificate2(Convert.FromBase64String(certDef.Certificate), certDef.Password, X509KeyStorageFlags.MachineKeySet);

                    cn = certificate.GetNameInfo(X509NameType.SimpleName, false);
                }
                catch (Exception e)
                {
                    throw new BadRequestException($"Failed to extract the CN property from the certificate {certificate?.FriendlyName}. Maybe the password is wrong?", e);
                }

                if (cn == null)
                {
                    throw new BadRequestException($"Failed to extract the CN property from the certificate. CN is null");
                }

                if (cn.LastIndexOf('*') > 0)
                {
                    throw new NotSupportedException("The wildcard CN name contains a '*' which is not at the first character of the string. It is not supported in the Setup Wizard, you can do a manual setup instead.");
                }

                try
                {
                    SecretProtection.ValidateKeyUsages("Setup Wizard", certificate);
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException($"Failed to load the uploaded certificate. Did you accidentally upload a client certificate?", e);
                }

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName("CN");
                    writer.WriteString(cn);
                    writer.WriteComma();
                    writer.WritePropertyName("AlternativeNames");
                    writer.WriteStartArray();

                    var first = true;
                    foreach (var value in SetupManager.GetCertificateAlternativeNames(certificate))
                    {
                        if (first == false)
                            writer.WriteComma();
                        first = false;

                        writer.WriteString(value);
                    }

                    writer.WriteEndArray();

                    writer.WriteEndObject();
                }
            }

            return Task.CompletedTask;
        }
Esempio n. 29
0
            public void WriteDatabaseRecord(DatabaseRecord databaseRecord, SmugglerProgressBase.DatabaseRecordProgress progress, AuthorizationStatus authorizationStatus, DatabaseRecordItemType databaseRecordItemType)
            {
                _writer.WritePropertyName(nameof(databaseRecord.DatabaseName));
                _writer.WriteString(databaseRecord.DatabaseName);
                _writer.WriteComma();

                _writer.WritePropertyName(nameof(databaseRecord.Encrypted));
                _writer.WriteBool(databaseRecord.Encrypted);

                if (databaseRecordItemType.Contain(DatabaseRecordItemType.ConflictSolverConfig))
                {
                    _writer.WriteComma();
                    _writer.WritePropertyName(nameof(databaseRecord.ConflictSolverConfig));
                    WriteConflictSolver(databaseRecord.ConflictSolverConfig);
                }

                if (databaseRecordItemType.Contain(DatabaseRecordItemType.Settings))
                {
                    _writer.WriteComma();
                    _writer.WritePropertyName(nameof(databaseRecord.Settings));
                    WriteSettings(databaseRecord.Settings);
                }

                if (databaseRecordItemType.Contain(DatabaseRecordItemType.Revisions))
                {
                    _writer.WriteComma();
                    _writer.WritePropertyName(nameof(databaseRecord.Revisions));
                    WriteRevisions(databaseRecord.Revisions);
                }

                if (databaseRecordItemType.Contain(DatabaseRecordItemType.Expiration))
                {
                    _writer.WriteComma();
                    _writer.WritePropertyName(nameof(databaseRecord.Expiration));
                    WriteExpiration(databaseRecord.Expiration);
                }

                if (databaseRecordItemType.Contain(DatabaseRecordItemType.Client))
                {
                    _writer.WriteComma();
                    _writer.WritePropertyName(nameof(databaseRecord.Client));
                    WriteClientConfiguration(databaseRecord.Client);
                }

                if (databaseRecordItemType.Contain(DatabaseRecordItemType.Sorters))
                {
                    _writer.WriteComma();
                    _writer.WritePropertyName(nameof(databaseRecord.Sorters));
                    WriteSorters(databaseRecord.Sorters);
                }

                switch (authorizationStatus)
                {
                case AuthorizationStatus.DatabaseAdmin:
                case AuthorizationStatus.Operator:
                case AuthorizationStatus.ClusterAdmin:
                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.RavenConnectionStrings))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.RavenConnectionStrings));
                        WriteRavenConnectionStrings(databaseRecord.RavenConnectionStrings);
                    }

                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.SqlConnectionStrings))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.SqlConnectionStrings));
                        WriteSqlConnectionStrings(databaseRecord.SqlConnectionStrings);
                    }

                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.PeriodicBackups))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.PeriodicBackups));
                        WritePeriodicBackups(databaseRecord.PeriodicBackups);
                    }

                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.ExternalReplications))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.ExternalReplications));
                        WriteExternalReplications(databaseRecord.ExternalReplications);
                    }

                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.RavenEtls))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.RavenEtls));
                        WriteRavenEtls(databaseRecord.RavenEtls);
                    }

                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.SqlEtls))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.SqlEtls));
                        WriteSqlEtls(databaseRecord.SqlEtls);
                    }

                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.HubPullReplications))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.HubPullReplications));
                        WriteHubPullReplications(databaseRecord.HubPullReplications);
                    }

                    if (databaseRecordItemType.Contain(DatabaseRecordItemType.SinkPullReplications))
                    {
                        _writer.WriteComma();
                        _writer.WritePropertyName(nameof(databaseRecord.SinkPullReplications));
                        WriteSinkPullReplications(databaseRecord.SinkPullReplications);
                    }

                    break;
                }
            }
 public void StartResults()
 {
     _writer.WritePropertyName("Results");
     _writer.WriteStartArray();
 }