Ejemplo n.º 1
0
        public QueryResult Query(string index, IndexQuery query)
        {
            var list  = new List <JObject>();
            var stale = false;

            TransactionalStorage.Batch(
                actions =>
            {
                stale = actions.Tasks.DoesTasksExistsForIndex(index, query.Cutoff);
                var indexFailureInformation = actions.Indexing.GetFailureRate(index);
                if (indexFailureInformation.IsInvalidIndex)
                {
                    throw new IndexDisabledException(indexFailureInformation);
                }
                var loadedIds  = new HashSet <string>();
                var collection = from queryResult in IndexStorage.Query(index, query)
                                 select RetrieveDocument(actions, queryResult, loadedIds)
                                 into doc
                                 let processedDoc = ExecuteReadTriggersOnRead(ProcessReadVetoes(doc, null, ReadOperation.Query), null, ReadOperation.Query)
                                                    where processedDoc != null
                                                    select processedDoc.ToJson();
                list.AddRange(collection);
            });
            return(new QueryResult
            {
                Results = list.ToArray(),
                IsStale = stale,
                TotalResults = query.TotalSize.Value
            });
        }
Ejemplo n.º 2
0
        public IEnumerable <string> QueryDocumentIds(string index, IndexQuery query, out bool stale)
        {
            bool             isStale   = false;
            HashSet <string> loadedIds = null;

            TransactionalStorage.Batch(
                actions =>
            {
                isStale = actions.Tasks.DoesTasksExistsForIndex(index, query.Cutoff);
                var indexFailureInformation = actions.Indexing.GetFailureRate(index)
                ;
                if (indexFailureInformation.IsInvalidIndex)
                {
                    throw new IndexDisabledException(indexFailureInformation);
                }
                loadedIds = new HashSet <string>(from queryResult in IndexStorage.Query(index, query)
                                                 select queryResult.Key);
            });
            stale = isStale;
            return(loadedIds);
        }
Ejemplo n.º 3
0
        public IEnumerable <string> QueryDocumentIds(string index, IndexQuery query, out bool stale)
        {
            index = IndexDefinitionStorage.FixupIndexName(index);
            bool             isStale   = false;
            HashSet <string> loadedIds = null;

            TransactionalStorage.Batch(
                actions =>
            {
                isStale = actions.Staleness.IsIndexStale(index, query.Cutoff, null);
                var indexFailureInformation = actions.Indexing.GetFailureRate(index)
                ;
                if (indexFailureInformation.IsInvalidIndex)
                {
                    throw new IndexDisabledException(indexFailureInformation);
                }
                loadedIds = new HashSet <string>(from queryResult in IndexStorage.Query(index, query, result => true, new FieldsToFetch(null, AggregationOperation.None, Raven.Abstractions.Data.Constants.DocumentIdFieldName))
                                                 select queryResult.Key);
            });
            stale = isStale;
            return(loadedIds);
        }
Ejemplo n.º 4
0
        public QueryResult Query(string index, IndexQuery query)
        {
            index = IndexDefinitionStorage.FixupIndexName(index);
            var list  = new List <JObject>();
            var stale = false;
            Tuple <DateTime, Guid> indexTimestamp = null;

            TransactionalStorage.Batch(
                actions =>
            {
                string entityName = null;


                var viewGenerator = IndexDefinitionStorage.GetViewGenerator(index);
                if (viewGenerator == null)
                {
                    throw new InvalidOperationException("Could not find index named: " + index);
                }

                entityName = viewGenerator.ForEntityName;

                stale          = actions.Staleness.IsIndexStale(index, query.Cutoff, entityName);
                indexTimestamp = actions.Staleness.IndexLastUpdatedAt(index);
                var indexFailureInformation = actions.Indexing.GetFailureRate(index);
                if (indexFailureInformation.IsInvalidIndex)
                {
                    throw new IndexDisabledException(indexFailureInformation);
                }
                var docRetriever    = new DocumentRetriever(actions, ReadTriggers);
                var indexDefinition = GetIndexDefinition(index);
                var fieldsToFetch   = new FieldsToFetch(query.FieldsToFetch, query.AggregationOperation,
                                                        viewGenerator.ReduceDefinition == null
                                                                                ? Abstractions.Data.Constants.DocumentIdFieldName
                                                                                : Abstractions.Data.Constants.ReduceKeyFieldName);
                var collection = from queryResult in IndexStorage.Query(index, query, result => docRetriever.ShouldIncludeResultInQuery(result, indexDefinition, fieldsToFetch), fieldsToFetch)
                                 select docRetriever.RetrieveDocumentForQuery(queryResult, indexDefinition, fieldsToFetch)
                                 into doc
                                 where doc != null
                                 select doc;

                var transformerErrors = new List <string>();
                IEnumerable <JObject> results;
                if (viewGenerator != null &&
                    query.SkipTransformResults == false &&
                    viewGenerator.TransformResultsDefinition != null)
                {
                    var robustEnumerator = new RobustEnumerator
                    {
                        OnError =
                            (exception, o) =>
                            transformerErrors.Add(string.Format("Doc '{0}', Error: {1}", Index.TryGetDocKey(o),
                                                                exception.Message))
                    };
                    var dynamicJsonObjects = collection.Select(x => new DynamicJsonObject(x.ToJson())).ToArray();
                    results =
                        robustEnumerator.RobustEnumeration(
                            dynamicJsonObjects,
                            source => viewGenerator.TransformResultsDefinition(docRetriever, source))
                        .Select(JsonExtensions.ToJObject);
                }
                else
                {
                    results = collection.Select(x => x.ToJson());
                }

                list.AddRange(results);

                if (transformerErrors.Count > 0)
                {
                    throw new InvalidOperationException("The transform results function failed.\r\n" + string.Join("\r\n", transformerErrors));
                }
            });
            return(new QueryResult
            {
                IndexName = index,
                Results = list,
                IsStale = stale,
                SkippedResults = query.SkippedResults.Value,
                TotalResults = query.TotalSize.Value,
                IndexTimestamp = indexTimestamp.Item1,
                IndexEtag = indexTimestamp.Item2
            });
        }