public override IEnumerable <KeyValuePair <AttributeValue, long> > Enumerate(QueryCriteria value)
        {
            IQueryStore   tempCollection = value.SubstituteStore;
            IJSONDocument doc            = JSONType.CreateNew();

            doc.Add("$count(*)", _count);
            doc.Key = Guid.NewGuid().ToString();
            tempCollection.InsertDocument(doc, null);
            var rowid = tempCollection.GetRowId(new DocumentKey(doc.Key));

            value.Store        = tempCollection;
            value.GroupByField = new AllField(Field.FieldType.Grouped);
            yield return(new KeyValuePair <AttributeValue, long>(NullValue.Null, rowid));
        }
示例#2
0
        public override IEnumerable <KeyValuePair <AttributeValue, long> > Enumerate(QueryCriteria value)
        {
            IQueryStore tempCollection = value.SubstituteStore;
            var         key            = new DocumentKey();
            var         finalResultSet = new SortedDictionary <AttributeValue, long>();

            if (_childPredicates != null)
            {
                foreach (var childPredicate in _childPredicates)
                {
                    var predicate = childPredicate as TerminalPredicate;
                    if (predicate != null)
                    {
                        foreach (var kvp in predicate.Enumerate(value))
                        {
                            var newDocument = value.Store.GetDocument(kvp.Value, null);
                            if (newDocument == null)
                            {
                                continue;
                            }
                            AttributeValue newKey;
                            if (value.GroupByField.GetAttributeValue(newDocument, out newKey))
                            {
                                key.Value = newKey.ValueInString;

                                if (finalResultSet.ContainsKey(newKey))
                                {
                                    if (value.ContainsAggregations)
                                    {
                                        var groupDocument = tempCollection.GetDocument(finalResultSet[newKey], null);
                                        foreach (var aggregation in value.Aggregations)
                                        {
                                            IJsonValue calculatedValue;
                                            if (aggregation.Evaluation.Evaluate(out calculatedValue, newDocument))
                                            {
                                                aggregation.Aggregation.Value = groupDocument[aggregation.FieldName];
                                                aggregation.Aggregation.ApplyValue(calculatedValue.Value);
                                                groupDocument[aggregation.FieldName] = aggregation.Aggregation.Value;
                                            }
                                        }
                                        tempCollection.UpdateDocument(finalResultSet[newKey], groupDocument, new OperationContext());
                                    }
                                }
                                else
                                {
                                    var aggregateDocument = JSONType.CreateNew();
                                    value.GroupByField.FillWithAttributes(newDocument, aggregateDocument);
                                    if (value.ContainsAggregations)
                                    {
                                        foreach (var aggregation in value.Aggregations)
                                        {
                                            IJsonValue calculatedValue;
                                            if (aggregation.Evaluation.Evaluate(out calculatedValue, newDocument))
                                            {
                                                aggregation.Reset();
                                                aggregation.Aggregation.ApplyValue(calculatedValue.Value);
                                                aggregateDocument[aggregation.FieldName] = aggregation.Aggregation.Value;
                                            }
                                        }
                                    }

                                    aggregateDocument.Key = key.Value as string;
                                    tempCollection.InsertDocument(aggregateDocument,
                                                                  new OperationContext());
                                    long newRowId = tempCollection.GetRowId(key);
                                    finalResultSet.Add(newKey, newRowId);
                                }
                            }
                        }
                    }
                }
            }

            if (finalResultSet.Count == 0)
            {
                var emptyDoc = JSONType.CreateNew();
                if (value.ContainsAggregations)
                {
                    foreach (var aggregation in value.Aggregations)
                    {
                        emptyDoc[aggregation.FieldName] = 0;
                    }
                }
                key.Value    = value.GroupByField.FieldId.ToString();
                emptyDoc.Key = (string)key.Value;
                tempCollection.InsertDocument(emptyDoc, new OperationContext());
                finalResultSet.Add(new NullValue(), tempCollection.GetRowId(key));
            }
            value.Store = tempCollection;
            return(finalResultSet);
        }