Exemplo n.º 1
0
        public async Task <FacetedQueryResult> ExecuteFacetedQuery(IndexQueryServerSide query, long?existingResultEtag, DocumentsOperationContext documentsContext, OperationCancelToken token)
        {
            if (query.Metadata.IsDynamic)
            {
                throw new InvalidQueryException("Facet query must be executed against static index.", query.Metadata.QueryText, query.QueryParameters);
            }

            ObjectDisposedException lastException = null;

            for (var i = 0; i < NumberOfRetries; i++)
            {
                try
                {
                    documentsContext.CloseTransaction();
                    var sw = Stopwatch.StartNew();

                    var result = await _static.ExecuteFacetedQuery(query, existingResultEtag, documentsContext, token);

                    result.DurationInMs = (long)sw.Elapsed.TotalMilliseconds;

                    return(result);
                }
                catch (ObjectDisposedException e)
                {
                    if (Database.DatabaseShutdown.IsCancellationRequested)
                    {
                        throw;
                    }

                    lastException = e;
                }
            }

            throw CreateRetriesFailedException(lastException);
        }
Exemplo n.º 2
0
        public override async Task ExecuteStreamQuery(IndexQueryServerSide query, DocumentsOperationContext documentsContext, HttpResponse response, IStreamDocumentQueryResultWriter writer, OperationCancelToken token)
        {
            ObjectDisposedException lastException = null;

            for (var i = 0; i < NumberOfRetries; i++)
            {
                try
                {
                    documentsContext.CloseTransaction();
                    await GetRunner(query).ExecuteStreamQuery(query, documentsContext, response, writer, token);

                    return;
                }
                catch (ObjectDisposedException e)
                {
                    if (Database.DatabaseShutdown.IsCancellationRequested)
                    {
                        throw;
                    }

                    lastException = e;
                }
            }

            throw CreateRetriesFailedException(lastException);
        }
Exemplo n.º 3
0
        public async Task <bool> WaitForNonStaleResults()
        {
            if (_context.Transaction == null || _context.Transaction.Disposed)
            {
                _context.OpenReadTransaction();
            }

            var etag               = DocumentsStorage.ReadLastEtag(_context.Transaction.InnerTransaction);
            var queryDuration      = Stopwatch.StartNew();
            var indexNamesGatherer = new GraphQueryIndexNamesGatherer();

            indexNamesGatherer.Visit(RootQueryStep);
            var indexes      = new List <Index>();
            var indexNames   = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var indexWaiters = new Dictionary <Index, (IndexQueryServerSide, AsyncWaitForIndexing)>();
            var queryTimeout = _query.WaitForNonStaleResultsTimeout ?? Index.DefaultWaitForNonStaleResultsTimeout;

            foreach (var indexName in indexNamesGatherer.Indexes)
            {
                if (indexNames.Add(indexName) == false)
                {
                    continue;
                }
                var index = _database.IndexStore.GetIndex(indexName);
                indexes.Add(index);
                indexWaiters.Add(index, (_query, new AsyncWaitForIndexing(queryDuration, queryTimeout, index)));
            }

            foreach (var qqs in indexNamesGatherer.QueryStepsWithoutExplicitIndex)
            {
                // we need to close the transaction since the DynamicQueryRunner.MatchIndex
                // expects that the transaction will be closed
                _context.CloseTransaction();

                //this will ensure that query step has relevant index
                //if needed, this will create auto-index
                var query = new IndexQueryServerSide(qqs.Query.ToString(), qqs.QueryParameters);
                var index = await _dynamicQueryRunner.MatchIndex(query, true, null, _database.DatabaseShutdown);

                if (indexNames.Add(index.Name) == false)
                {
                    continue;
                }
                indexes.Add(index);
                indexWaiters.Add(index, (_query, new AsyncWaitForIndexing(queryDuration, queryTimeout, index)));
            }

            return(await WaitForNonStaleResultsInternal(etag, indexes, indexWaiters));
        }
Exemplo n.º 4
0
        public override async Task <DocumentQueryResult> ExecuteQuery(IndexQueryServerSide query, DocumentsOperationContext documentsContext, long?existingResultEtag, OperationCancelToken token)
        {
            ObjectDisposedException lastException = null;

            for (var i = 0; i < NumberOfRetries; i++)
            {
                try
                {
                    documentsContext.CloseTransaction();
                    Stopwatch           sw = null;
                    QueryTimingsScope   scope;
                    DocumentQueryResult result;
                    using (scope = query.Timings?.Start())
                    {
                        if (scope == null)
                        {
                            sw = Stopwatch.StartNew();
                        }

                        result = await GetRunner(query).ExecuteQuery(query, documentsContext, existingResultEtag, token);
                    }

                    result.DurationInMs = sw != null ? (long)sw.Elapsed.TotalMilliseconds : (long)scope.Duration.TotalMilliseconds;

                    return(result);
                }
                catch (ObjectDisposedException e)
                {
                    if (Database.DatabaseShutdown.IsCancellationRequested)
                    {
                        throw;
                    }

                    lastException = e;
                }
            }

            throw CreateRetriesFailedException(lastException);
        }
Exemplo n.º 5
0
        public override async Task <IOperationResult> ExecutePatchQuery(IndexQueryServerSide query, QueryOperationOptions options, PatchRequest patch, BlittableJsonReaderObject patchArgs, DocumentsOperationContext context, Action <IOperationProgress> onProgress, OperationCancelToken token)
        {
            ObjectDisposedException lastException = null;

            for (var i = 0; i < NumberOfRetries; i++)
            {
                try
                {
                    context.CloseTransaction();
                    return(await GetRunner(query).ExecutePatchQuery(query, options, patch, patchArgs, context, onProgress, token));
                }
                catch (ObjectDisposedException e)
                {
                    if (Database.DatabaseShutdown.IsCancellationRequested)
                    {
                        throw;
                    }

                    lastException = e;
                }
            }

            throw CreateRetriesFailedException(lastException);
        }
Exemplo n.º 6
0
        public override async Task <IndexEntriesQueryResult> ExecuteIndexEntriesQuery(IndexQueryServerSide query, DocumentsOperationContext context, long?existingResultEtag, OperationCancelToken token)
        {
            ObjectDisposedException lastException = null;

            for (var i = 0; i < NumberOfRetries; i++)
            {
                try
                {
                    context.CloseTransaction();
                    return(await GetRunner(query).ExecuteIndexEntriesQuery(query, context, existingResultEtag, token));
                }
                catch (ObjectDisposedException e)
                {
                    if (Database.DatabaseShutdown.IsCancellationRequested)
                    {
                        throw;
                    }

                    lastException = e;
                }
            }

            throw CreateRetriesFailedException(lastException);
        }
Exemplo n.º 7
0
        public async Task <SuggestionQueryResult> ExecuteSuggestionQuery(IndexQueryServerSide query, DocumentsOperationContext context, long?existingResultEtag, OperationCancelToken token)
        {
            if (query.Metadata.IsDynamic)
            {
                throw new InvalidQueryException("Suggestion query must be executed against static index.", query.Metadata.QueryText, query.QueryParameters);
            }

            ObjectDisposedException lastException = null;

            for (var i = 0; i < NumberOfRetries; i++)
            {
                try
                {
                    context.CloseTransaction();
                    var sw = Stopwatch.StartNew();

                    if (query.Metadata.SelectFields.Length != 1 || query.Metadata.SelectFields[0].IsSuggest == false)
                    {
                        throw new InvalidQueryException("Suggestion query must have one suggest token in SELECT.", query.Metadata.QueryText, query.QueryParameters);
                    }

                    var selectField = (SuggestionField)query.Metadata.SelectFields[0];

                    var index = GetIndex(query.Metadata.IndexName);

                    var indexDefinition = index.GetIndexDefinition();

                    if (indexDefinition.Fields.TryGetValue(selectField.Name, out IndexFieldOptions field) == false)
                    {
                        throw new InvalidOperationException($"Index '{query.Metadata.IndexName}' does not have a field '{selectField.Name}'.");
                    }

                    if (field.Suggestions == null)
                    {
                        throw new InvalidOperationException($"Index '{query.Metadata.IndexName}' does not have suggestions configured for field '{selectField.Name}'.");
                    }

                    if (field.Suggestions.Value == false)
                    {
                        throw new InvalidOperationException($"Index '{query.Metadata.IndexName}' have suggestions explicitly disabled for field '{selectField.Name}'.");
                    }

                    if (existingResultEtag.HasValue)
                    {
                        var etag = index.GetIndexEtag(query.Metadata);
                        if (etag == existingResultEtag.Value)
                        {
                            return(SuggestionQueryResult.NotModifiedResult);
                        }
                    }

                    var result = await index.SuggestionQuery(query, context, token);

                    result.DurationInMs = (int)sw.Elapsed.TotalMilliseconds;
                    return(result);
                }
                catch (ObjectDisposedException e)
                {
                    if (Database.DatabaseShutdown.IsCancellationRequested)
                    {
                        throw;
                    }

                    lastException = e;
                }
            }

            throw CreateRetriesFailedException(lastException);
        }
Exemplo n.º 8
0
        private async Task <IOperationResult> ExecuteOperation(string indexName, IndexQueryServerSide query, QueryOperationOptions options,
                                                               DocumentsOperationContext context, Action <DeterminateProgress> onProgress, Action <string> action, OperationCancelToken token)
        {
            var index = GetIndex(indexName);

            if (index.Type.IsMapReduce())
            {
                throw new InvalidOperationException("Cannot execute bulk operation on Map-Reduce indexes.");
            }

            query = ConvertToOperationQuery(query, options);

            const int BatchSize = 1024;

            RavenTransaction tx = null;
            var           operationsInCurrentBatch = 0;
            List <string> resultKeys;

            try
            {
                var results = await index.Query(query, context, token).ConfigureAwait(false);

                if (options.AllowStale == false && results.IsStale)
                {
                    throw new InvalidOperationException("Cannot perform bulk operation. Query is stale.");
                }

                resultKeys = new List <string>(results.Results.Count);
                foreach (var document in results.Results)
                {
                    resultKeys.Add(document.Key.ToString());
                }
            }
            finally //make sure to close tx if DocumentConflictException is thrown
            {
                context.CloseTransaction();
            }


            var progress = new DeterminateProgress
            {
                Total     = resultKeys.Count,
                Processed = 0
            };

            onProgress(progress);

            using (var rateGate = options.MaxOpsPerSecond.HasValue ? new RateGate(options.MaxOpsPerSecond.Value, TimeSpan.FromSeconds(1)) : null)
            {
                foreach (var document in resultKeys)
                {
                    if (rateGate != null && rateGate.WaitToProceed(0) == false)
                    {
                        using (tx)
                        {
                            tx?.Commit();
                        }

                        tx = null;

                        rateGate.WaitToProceed();
                    }

                    if (tx == null)
                    {
                        operationsInCurrentBatch = 0;
                        tx = context.OpenWriteTransaction();
                    }

                    action(document);

                    operationsInCurrentBatch++;
                    progress.Processed++;

                    if (progress.Processed % 128 == 0)
                    {
                        onProgress(progress);
                    }

                    if (operationsInCurrentBatch < BatchSize)
                    {
                        continue;
                    }

                    using (tx)
                    {
                        tx.Commit();
                    }

                    tx = null;
                }
            }

            using (tx)
            {
                tx?.Commit();
            }

            return(new BulkOperationResult
            {
                Total = progress.Total
            });
        }
Exemplo n.º 9
0
 public void CloseTransaction()
 {
     Documents.CloseTransaction();
     Server?.CloseTransaction();
 }