示例#1
0
 public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
 {
     Result = (TResult)(object)response;
 }
示例#2
0
 public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
 {
     Result = JsonDeserializationClient.AttachmentDetails(response);
 }
 public RavenCommand <ModifySolverResult> GetCommand(DocumentConventions conventions, JsonOperationContext ctx)
 {
     return(new ModifyConflictSolverCommand(conventions, _database, this));
 }
 public RavenCommand GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new StartTransactionsRecordingCommand(_filePath));
 }
示例#5
0
 protected override Task PerformHealthCheck(ServerNode serverNode, int nodeIndex, JsonOperationContext context)
 {
     return(ExecuteAsync(serverNode, nodeIndex, context, new GetTcpInfoCommand("health-check"), shouldRetry: false, sessionInfo: null, token: CancellationToken.None));
 }
示例#6
0
 public RavenCommand GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new StopIndexingCommand());
 }
示例#7
0
 public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
 {
     Result = JsonDeserializationClient.PutIndexesResponse(response).Results;
 }
示例#8
0
        private async Task HandleRequestAsync(
            BlittableJsonReaderObject request,
            JsonOperationContext context,
            MultiGetHttpResponseStream responseStream,
            BlittableJsonTextWriter writer,
            HttpContext httpContext,
            HostString host,
            string scheme,
            LazyStringValue resultProperty,
            LazyStringValue statusProperty,
            LazyStringValue headersProperty,
            StringBuilder trafficWatchStringBuilder)
        {
            writer.WriteStartObject();

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

            if (request.TryGet(nameof(GetRequest.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)
            {
                HandleNoRoute(context, writer, method, url, query, statusProperty, resultProperty);
                return;
            }

            var requestHandler = routeInformation.GetRequestHandler();

            writer.WritePropertyName(resultProperty);
            writer.Flush();

            PrepareHttpContext(request, context, httpContext, method, query, host, scheme, trafficWatchStringBuilder, out var content);

            var bytesWrittenBeforeRequest = responseStream.BytesWritten;
            int statusCode;

            try
            {
                if (Server.Configuration.Security.AuthenticationEnabled == false ||
                    Server.Router.TryAuthorize(routeInformation, httpContext, Database, out var status))
                {
                    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;

                HandleException(context, writer, e, url, query);
            }

            writer.WriteComma();

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

            writer.WriteComma();

            WriteHeaders(writer, httpContext, headersProperty);

            writer.WriteEndObject();

            trafficWatchStringBuilder?.Append(content).AppendLine();
        }
示例#9
0
 public RavenCommand GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new ResetIndexCommand(_indexName));
 }
示例#10
0
        internal static unsafe BlittableJsonReaderObject ParseJsonStringIntoBlittable(string json, JsonOperationContext context)
        {
            var bytes = Encoding.UTF8.GetBytes(json);

            fixed(byte *ptr = bytes)
            {
                var blittableJson = context.ParseBuffer(ptr, bytes.Length, "MoreLikeThis/ExtractTermsFromJson", BlittableJsonDocumentBuilder.UsageMode.None);

                blittableJson.BlittableValidation(); //precaution, needed because this is user input..
                return(blittableJson);
            }
        }
示例#11
0
        public static void WaitForIndexing(IDocumentStore store, string dbName = null, TimeSpan?timeout = null)
        {
            var admin = store.Admin.ForDatabase(dbName);

            timeout = timeout ?? (Debugger.IsAttached
                          ? TimeSpan.FromMinutes(15)
                          : TimeSpan.FromMinutes(1));

            var sp = Stopwatch.StartNew();

            while (sp.Elapsed < timeout.Value)
            {
                var databaseStatistics = admin.Send(new GetStatisticsOperation());
                var indexes            = databaseStatistics.Indexes
                                         .Where(x => x.State != IndexState.Disabled);

                if (indexes.All(x => x.IsStale == false && x.Name.StartsWith("ReplacementOf/") == false))
                {
                    return;
                }

                if (databaseStatistics.Indexes.Any(x => x.State == IndexState.Error))
                {
                    break;
                }
                Thread.Sleep(32);
            }

            var perf   = admin.Send(new GetIndexPerformanceStatisticsOperation());
            var errors = admin.Send(new GetIndexErrorsOperation());
            var stats  = admin.Send(new GetIndexesStatisticsOperation());

            var total = new
            {
                Errors      = errors,
                Stats       = stats,
                Performance = perf
            };

            var file = Path.GetTempFileName() + ".json";

            using (var stream = File.Open(file, FileMode.OpenOrCreate))
                using (var context = JsonOperationContext.ShortTermSingleUse())
                    using (var writer = new BlittableJsonTextWriter(context, stream))
                    {
                        var djv  = (DynamicJsonValue)TypeConverter.ToBlittableSupportedType(total);
                        var json = context.ReadObject(djv, "errors");
                        writer.WriteObject(json);
                        writer.Flush();
                    }

            var statistics = admin.Send(new GetStatisticsOperation());

            var corrupted = statistics.Indexes.Where(x => x.State == IndexState.Error).ToList();

            if (corrupted.Count > 0)
            {
                throw new InvalidOperationException(
                          $"The following indexes are with error state: {string.Join(",", corrupted.Select(x => x.Name))} - details at " + file);
            }

            throw new TimeoutException("The indexes stayed stale for more than " + timeout.Value + ", stats at " + file);
        }
示例#12
0
        private Dictionary <string, Dictionary <string, string[]> > GetHighlighterResults(IndexQueryServerSide query, IndexSearcher searcher, ScoreDoc scoreDoc, Document document, global::Lucene.Net.Documents.Document luceneDocument, JsonOperationContext context)
        {
            Debug.Assert(_highlighter != null);
            Debug.Assert(_highlighterQuery != null);

            var results = new Dictionary <string, Dictionary <string, string[]> >();

            foreach (var highlighting in query.Metadata.Highlightings)
            {
                var fieldName      = highlighting.Field.Value;
                var indexFieldName = query.Metadata.IsDynamic
                    ? AutoIndexField.GetSearchAutoIndexFieldName(fieldName)
                    : fieldName;

                var fragments = _highlighter.GetBestFragments(
                    _highlighterQuery,
                    searcher.IndexReader,
                    scoreDoc.Doc,
                    indexFieldName,
                    highlighting.FragmentLength,
                    highlighting.FragmentCount,
                    _state);

                if (fragments == null || fragments.Length == 0)
                {
                    continue;
                }

                var options = highlighting.GetOptions(context, query.QueryParameters);

                string key;
                if (options != null && string.IsNullOrWhiteSpace(options.GroupKey) == false)
                {
                    key = luceneDocument.Get(options.GroupKey, _state);
                }
                else
                {
                    key = document.Id;
                }

                if (results.TryGetValue(fieldName, out var result) == false)
                {
                    results[fieldName] = result = new Dictionary <string, string[]>();
                }

                if (result.TryGetValue(key, out var innerResult))
                {
                    Array.Resize(ref innerResult, innerResult.Length + fragments.Length);
                    Array.Copy(fragments, 0, innerResult, innerResult.Length, fragments.Length);
                }
                else
                {
                    result[key] = fragments;
                }
            }

            return(results);
        }
示例#13
0
 public override TransactionOperationsMerger.IReplayableCommandDto <TransactionOperationsMerger.MergedTransactionCommand> ToDto(JsonOperationContext context)
 {
     throw new NotSupportedException($"ToDto() of {nameof(ExecuteRateLimitedOperations<T>)} Should not be called");
 }
示例#14
0
        public async Task PutDifferentAttachmentsShouldConflict()
        {
            using (var store1 = GetDocumentStore(options: new Options
            {
                ModifyDatabaseRecord = record =>
                {
                    record.ConflictSolverConfig = new ConflictSolver();
                }
            }))
                using (var store2 = GetDocumentStore(options: new Options
                {
                    ModifyDatabaseRecord = record =>
                    {
                        record.ConflictSolverConfig = new ConflictSolver();
                    }
                }))
                {
                    await SetDatabaseId(store1, new Guid("00000000-48c4-421e-9466-000000000000"));
                    await SetDatabaseId(store2, new Guid("99999999-48c4-421e-9466-999999999999"));

                    using (var session = store1.OpenAsyncSession())
                    {
                        var x = new User {
                            Name = "Fitzchak"
                        };
                        await session.StoreAsync(x, "users/1");

                        await session.SaveChangesAsync();

                        using (var a1 = new MemoryStream(new byte[] { 1, 2, 3 }))
                        {
                            await store1.Operations.SendAsync(new PutAttachmentOperation("users/1", "a1", a1, "a1/png"));
                        }

                        using (var session2 = store2.OpenSession())
                        {
                            session2.Store(new User {
                                Name = "Fitzchak"
                            }, "users/1");
                            session2.SaveChanges();

                            using (var a2 = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }))
                            {
                                store2.Operations.Send(new PutAttachmentOperation("users/1", "a1", a2, "a1/png"));
                            }

                            await SetupReplicationAsync(store1, store2);

                            await session.StoreAsync(new User { Name = "Toli" }, "users/2");

                            await session.SaveChangesAsync();

                            WaitForDocumentToReplicate <User>(store2, "users/2", 3000);

                            var conflicts = (await store2.Commands().GetConflictsForAsync("users/1")).ToList();
                            Assert.Equal(2, conflicts.Count);
                            var requestExecutor = store2.GetRequestExecutor();

                            using (var context = JsonOperationContext.ShortTermSingleUse())
                                using (var stringStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(_conflictedDocument)))
                                    using (var blittableJson = context.Read(stringStream, "Reading of foo/bar"))
                                    {
                                        var result = new InMemoryDocumentSessionOperations.SaveChangesData((InMemoryDocumentSessionOperations)session2);
                                        result.SessionCommands.Add(new PutCommandDataWithBlittableJson("users/1", null, blittableJson));
                                        var sbc = new SingleNodeBatchCommand(DocumentConventions.Default, context, result.SessionCommands, result.Options);
                                        await requestExecutor.ExecuteAsync(sbc, context);
                                    }
                        }
                    }

                    using (var session = store1.OpenAsyncSession())
                    {
                        var conflicts = (await store2.Commands().GetConflictsForAsync("users/1")).ToList();
                        Assert.Equal(0, conflicts.Count);

                        Assert.True(await session.Advanced.Attachments.ExistsAsync("users/1", "a1"));
                    }
                }
        }
示例#15
0
            public override void SetResponseRaw(HttpResponseMessage response, Stream stream, JsonOperationContext context)
            {
                if (response == null)
                {
                    return;
                }

                var ms = new MemoryStream();

                stream.CopyTo(ms);

                Result = new CertificateRawData
                {
                    RawData = ms.ToArray()
                };
            }
示例#16
0
        protected override void LoadInternal(IEnumerable <SqlTableWithRecords> records, JsonOperationContext context)
        {
            using (var writer = new RelationalDatabaseWriter(this, Database))
            {
                foreach (var table in records)
                {
                    var stats = writer.Write(table, null, CancellationToken);

                    LogStats(stats, table);
                }

                writer.Commit();
            }
        }
示例#17
0
 public RavenCommand <CertificateRawData> GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new CreateClientCertificateCommand(_name, _permissions, _clearance, _password));
 }
 public RavenCommand <ConfigureTimeSeriesOperationResult> GetCommand(DocumentConventions conventions, JsonOperationContext ctx)
 {
     return(new ConfigureTimeSeriesValueNamesCommand(_parameters));
 }
示例#19
0
 public RavenCommand <PutIndexResult[]> GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new PutIndexesCommand(conventions, context, _indexToAdd));
 }
示例#20
0
 public RavenCommand <DatabasePutResult> GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new CreateDatabaseOperation.CreateDatabaseCommand(_databaseRecord, _replicationFactor));
 }
示例#21
0
 public PatchDocumentCommand(
     JsonOperationContext context,
     string id,
     LazyStringValue expectedChangeVector,
     bool skipPatchIfChangeVectorMismatch,
     (PatchRequest run, BlittableJsonReaderObject args) patch,
        public void ValidateFailedRevisionsSubscriptionScriptExceptionHandling()
        {
            using (var store = GetDocumentStore())
            {
                using (var context = JsonOperationContext.ShortTermSingleUse())
                {
                    var configuration = new RevisionsConfiguration
                    {
                        Default = new RevisionsCollectionConfiguration
                        {
                            Disabled = false,
                            MinimumRevisionsToKeep = 5,
                        },
                        Collections = new Dictionary <string, RevisionsCollectionConfiguration>
                        {
                            ["Users"] = new RevisionsCollectionConfiguration
                            {
                                Disabled = false
                            },
                            ["Dons"] = new RevisionsCollectionConfiguration
                            {
                                Disabled = false
                            }
                        }
                    };

                    AsyncHelpers.RunSync(() => Server.ServerStore.ModifyDatabaseRevisions(context,
                                                                                          store.Database,
                                                                                          EntityToBlittable.ConvertCommandToBlittable(configuration,
                                                                                                                                      context), Guid.NewGuid().ToString()));
                }

                var subscriptionId = store.Subscriptions.Create(new SubscriptionCreationOptions()
                {
                    Query = @"
declare function project(d){
    throw 'nice';
    return d;
}
from Users (Revisions = true) as d
select project(d)
"
                });

                var subscription = store.Subscriptions.GetSubscriptionWorker <User>(new SubscriptionWorkerOptions(subscriptionId)
                {
                    TimeToWaitBeforeConnectionRetry = TimeSpan.FromSeconds(5)
                });

                var exceptions = new List <Exception>();

                var mre          = new ManualResetEvent(false);
                var receivedItem = new SubscriptionBatch <User> .Item();

                var userId = string.Empty;

                using (var session = store.OpenSession())
                {
                    var newUser = new User();
                    session.Store(newUser);
                    session.SaveChanges();
                    userId = session.Advanced.GetDocumentId(newUser);
                }

                subscription.Run(x =>
                {
                    foreach (var item in x.Items)
                    {
                        receivedItem = item;
                        try
                        {
                            var res = item;
                        }
                        catch (Exception e)
                        {
                            exceptions.Add(e);
                        }
                    }
                    mre.Set();
                });



                Assert.True(mre.WaitOne(_reasonableWaitTime));
                Assert.NotNull(receivedItem);
                Assert.Throws <InvalidOperationException>(() => receivedItem.Result);
                Assert.NotNull(receivedItem.Metadata);
                Assert.Equal(receivedItem.Id, userId);
            }
        }
示例#23
0
 public override TransactionOperationsMerger.IReplayableCommandDto <TransactionOperationsMerger.MergedTransactionCommand> ToDto(JsonOperationContext context)
 {
     return(new ExecuteCounterBatchCommandDto
     {
         Dictionary = _dictionary,
         ReplyWithAllNodesValues = _replyWithAllNodesValues,
         FromEtl = _fromEtl
     });
 }
 public RavenCommand <CertificateMetadata> GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new GetCertificateMetadataCommand(_thumbprint));
 }
示例#25
0
 public RavenCommand <AttachmentDetails> GetCommand(IDocumentStore store, DocumentConventions conventions, JsonOperationContext context, HttpCache cache)
 {
     return(new PutAttachmentCommand(_documentId, _name, _stream, _contentType, _changeVector));
 }
示例#26
0
        protected override int GetFields <T>(T instance, LazyStringValue key, object document, JsonOperationContext indexContext)
        {
            int newFields = 0;

            if (key != null)
            {
                instance.Add(GetOrCreateKeyField(key));
                newFields++;
            }

            var boostedValue      = document as BoostedValue;
            var documentToProcess = boostedValue == null ? document : boostedValue.Value;

            PropertyAccessor accessor;

            if (_isMultiMap == false)
            {
                accessor = _propertyAccessor ?? (_propertyAccessor = PropertyAccessor.Create(documentToProcess.GetType()));
            }
            else
            {
                accessor = TypeConverter.GetPropertyAccessor(documentToProcess);
            }

            var reduceResult = _reduceOutput ? new DynamicJsonValue() : null;

            foreach (var property in accessor.PropertiesInOrder)
            {
                var value = property.Value.GetValue(documentToProcess);

                IndexField field;

                try
                {
                    field = _fields[property.Key];
                }
                catch (KeyNotFoundException e)
                {
                    throw new InvalidOperationException($"Field '{property.Key}' is not defined. Available fields: {string.Join(", ", _fields.Keys)}.", e);
                }

                var boostedFields = GetRegularFields(instance, field, value, indexContext);

                newFields += boostedFields;

                if (boostedValue != null)
                {
                    var fields = instance.GetFields();
                    for (int idx = fields.Count - 1; boostedFields > 0; boostedFields--, idx--)
                    {
                        var luceneField = fields[idx];
                        luceneField.Boost     = boostedValue.Boost;
                        luceneField.OmitNorms = false;
                    }
                }

                if (reduceResult != null)
                {
                    reduceResult[property.Key] = TypeConverter.ToBlittableSupportedType(value, flattenArrays: true);
                }
            }

            if (_reduceOutput)
            {
                instance.Add(GetReduceResultValueField(Scope.CreateJson(reduceResult, indexContext)));
                newFields++;
            }

            return(newFields);
        }
示例#27
0
 protected override BlittableJsonReaderObject GetUpdatedValue(long index, RawDatabaseRecord record, JsonOperationContext context, BlittableJsonReaderObject existingValue)
 {
     throw new NotImplementedException();
 }
 public RavenCommand <DatabasePutResult> GetCommand(DocumentConventions conventions, JsonOperationContext ctx)
 {
     return(new CreateDatabaseCommand(conventions, _databaseRecord, this));
 }
示例#29
0
 public RavenCommand <ReplicationPerformance> GetCommand(DocumentConventions conventions, JsonOperationContext context)
 {
     return(new GetReplicationPerformanceStatisticsCommand(conventions));
 }
        public static async Task TrySavingAsync(string topologyHash, ClusterTopologyResponse clusterTopology, DocumentConventions conventions, JsonOperationContext context, CancellationToken token)
        {
            try
            {
                if (conventions.DisableTopologyCache)
                {
                    return;
                }

                var path = GetPath(topologyHash, conventions);
                if (clusterTopology == null)
                {
                    Clear(path);
                    return;
                }

                using (var stream = SafeFileStream.Create(path, FileMode.Create, FileAccess.Write, FileShare.Read))
                    await using (var writer = new AsyncBlittableJsonTextWriter(context, stream))
                    {
                        var json = new DynamicJsonValue
                        {
                            [nameof(clusterTopology.Topology)] = clusterTopology.Topology.ToJson(),
                            [nameof(clusterTopology.Leader)]   = clusterTopology.Leader,
                            [nameof(clusterTopology.NodeTag)]  = clusterTopology.NodeTag,
                            [nameof(clusterTopology.Etag)]     = clusterTopology.Etag,
                            ["PersistedAt"] = DateTimeOffset.UtcNow.ToString(DefaultFormat.DateTimeOffsetFormatsToWrite),
                        };

                        context.Write(writer, json);
                        await writer.FlushAsync(token).ConfigureAwait(false);
                    }
            }
            catch (Exception e)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Could not persist the cluster topology", e);
                }
            }
        }