public QueryResult Query(string index, IndexQuery query) { var list = new List<JObject>(); var stale = false; DateTime indexTimestamp = DateTime.MinValue; TransactionalStorage.Batch( actions => { string entityName = null; var abstractViewGenerator = IndexDefinitionStorage.GetViewGenerator(index); if (abstractViewGenerator != null) entityName = abstractViewGenerator.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 collection = from queryResult in IndexStorage.Query(index, query, result => docRetriever.ShouldIncludeResultInQuery(result, query.FieldsToFetch)) select docRetriever.RetrieveDocumentForQuery(queryResult, query.FieldsToFetch) into doc where doc != null select doc; var transformerErrors = new List<string>(); IEnumerable<JObject> results; if (abstractViewGenerator != null && abstractViewGenerator.TransformResultsDefinition != null) { var robustEnumerator = new RobustEnumerator { OnError = (exception, o) => transformerErrors.Add(string.Format("Doc '{0}', Error: {1}", Index.TryGetDocKey(o), exception.Message)) }; results = robustEnumerator.RobustEnumeration( collection.Select(x => new DynamicJsonObject(x.ToJson())), source => abstractViewGenerator.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 { Results = list, IsStale = stale, SkippedResults = query.SkippedResults.Value, TotalResults = query.TotalSize.Value, IndexTimestamp = indexTimestamp }; }
public QueryResult Query(string index, IndexQuery query) { var list = new List<JObject>(); var stale = false; TransactionalStorage.Batch( actions => { string entityName = null; var abstractViewGenerator = IndexDefinitionStorage.GetViewGenerator(index); if (abstractViewGenerator != null) entityName = abstractViewGenerator.ForEntityName; stale = actions.Tasks.IsIndexStale(index, query.Cutoff, entityName); var indexFailureInformation = actions.Indexing.GetFailureRate(index); if (indexFailureInformation.IsInvalidIndex) { throw new IndexDisabledException(indexFailureInformation); } var docRetriever = new DocumentRetriever(actions, ReadTriggers); var collection = from queryResult in IndexStorage.Query(index, query, docRetriever.ShouldIncludeResultInQuery) select docRetriever.RetrieveDocumentForQuery(queryResult) into doc where doc != null select doc.ToJson(); list.AddRange(collection); }); return new QueryResult { Results = list, IsStale = stale, SkippedResults = query.SkippedResults.Value, TotalResults = query.TotalSize.Value }; }