Esempio n. 1
0
        public JArray GetDocuments(int start, int pageSize, Guid? etag)
        {
            var list = new JArray();
            TransactionalStorage.Batch(actions =>
            {
                IEnumerable<JsonDocument> documents;
                if (etag == null)
                    documents = actions.Documents.GetDocumentsByReverseUpdateOrder(start);
                else
                    documents = actions.Documents.GetDocumentsAfter(etag.Value);
                var documentRetriever = new DocumentRetriever(actions, ReadTriggers);
                foreach (var doc in documents.Take(pageSize))
                {
                    var document = documentRetriever.ExecuteReadTriggers(doc, null,
						// here we want to have the Load semantic, not Query, because we need this to be
						// as close as possible to the full database contents
						ReadOperation.Load); 
                    if (document == null)
                        continue;
                    if (document.Metadata.Property("@id") == null)
                        document.Metadata.Add("@id", new JValue(doc.Key));

                    list.Add(document.ToJson());
                }
            });
            return list;
        }
Esempio n. 2
0
        public JArray GetDocuments(int start, int pageSize, Guid? etag)
        {
            var list = new JArray();
            TransactionalStorage.Batch(actions =>
            {
                IEnumerable<JsonDocument> documents;
                if (etag == null)
                    documents = actions.Documents.GetDocumentsByReverseUpdateOrder(start);
                else
                    documents = actions.Documents.GetDocumentsAfter(etag.Value);
                var documentRetriever = new DocumentRetriever(actions, ReadTriggers);
                foreach (var doc in documents.Take(pageSize))
                {
                    var document = documentRetriever.ExecuteReadTriggers(doc, null, ReadOperation.Query);
                    if (document == null)
                        continue;
                    if (document.Metadata.Property("@id") == null)
                        document.Metadata.Add("@id", new JValue(doc.Key));

                    list.Add(document.ToJson());
                }
            });
            return list;
        }
Esempio n. 3
0
        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
            };
        }
Esempio n. 4
0
        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
            };
        }