Пример #1
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);
        }
Пример #2
0
        /// <summary>
        /// Get New Data according to chunk size From Store and Fill data Chunk
        /// Stores New Data Locally also in case chunk not recieve by user next time can send from local
        /// </summary>
        /// <param name="lastChunkId"></param>
        /// <param name="dataChunk"></param>
        protected virtual bool FillNewData(int lastChunkId, ref IDataChunk dataChunk)
        {
            int  count = 0;
            bool check = false;

            _lastChunkData.Clear();

            if (_lastElementCheck)
            {
                IJSONDocument jsonDocument = _store.GetDocument(_lastElement, _context);
                if (jsonDocument != null)
                {
                    jsonDocument = _transform.Transform(jsonDocument);
                    if (jsonDocument != null)
                    {
                        dataChunk.Documents.Add(jsonDocument);
                        _lastChunkData.Add(_lastElement);
                        count++;
                    }
                }

                _lastElementCheck = false;
            }

            while (_enumerator.MoveNext())
            {
                if (check)
                {
                    check             = false;
                    _lastElement      = _enumerator.Current;      ////To Save Round Trip in case Data Chunk fills and Data also finish so that can make the existing chunk as last chunk
                    _lastElementCheck = true;
                    break;
                }

                IJSONDocument jsonDocument = _store.GetDocument(_enumerator.Current, _context);
                if (jsonDocument != null)
                {
                    jsonDocument = _transform.Transform(jsonDocument);
                    if (jsonDocument != null)
                    {
                        dataChunk.Documents.Add(jsonDocument);
                        _lastChunkData.Add(_enumerator.Current);
                        count++;
                    }
                }

                if (count == _chunkSize)
                {
                    check = true;
                }
            }

            if (count < _chunkSize || check)
            {
                dataChunk.IsLastChunk = true;
                _isLastChunk          = true;
            }
            else
            {
                dataChunk.IsLastChunk = false;
            }

            dataChunk.ReaderUID = _id;
            dataChunk.ChunkId   = lastChunkId + 1;
            _lastChunkId        = lastChunkId + 1;

            return(true);
        }