Пример #1
0
                public bool MoveNext()
                {
                    if (_enumerator.MoveNext() == false)
                    {
                        return(false);
                    }

                    var output = _enumerator.Current;

                    _parent._indexContext.CachedProperties.NewDocument();

                    using (_createBlittableResult.Start())
                        using (var writer = new BlittableJsonWriter(_parent._indexContext))
                        {
                            IPropertyAccessor accessor;

                            if (_parent._isMultiMap == false)
                            {
                                accessor = _parent._propertyAccessor ??
                                           (_parent._propertyAccessor = PropertyAccessor.CreateMapReduceOutputAccessor(output.GetType(), output, _groupByFields));
                            }
                            else
                            {
                                accessor = TypeConverter.GetPropertyAccessorForMapReduceOutput(output, _groupByFields);
                            }

                            _reduceKeyProcessor.Reset();

                            writer.WriteStartObject();
                            foreach (var property in accessor.GetPropertiesInOrder(output))
                            {
                                var value          = property.Value;
                                var blittableValue = TypeConverter.ToBlittableSupportedType(value, context: _parent._indexContext);

                                writer.WritePropertyName(property.Key);
                                WriteValue(writer, blittableValue);

                                if (property.IsGroupByField)
                                {
                                    var valueForProcessor = property.GroupByField.GetValue(value, blittableValue);
                                    _reduceKeyProcessor.Process(_parent._indexContext.Allocator, valueForProcessor);
                                }
                            }

                            writer.WriteEndObject();

                            if (_reduceKeyProcessor.ProcessedFields != _groupByFields.Count)
                            {
                                ThrowMissingGroupByFieldsInMapOutput(output, _groupByFields, _parent._compiledIndex);
                            }

                            var reduceHashKey = _reduceKeyProcessor.Hash;

                            writer.FinalizeDocument();

                            Current.Data          = writer.CreateReader();
                            Current.ReduceKeyHash = reduceHashKey;
                        }

                    return(true);
Пример #2
0
        public unsafe dynamic LoadDocument(LazyStringValue keyLazy, string keyString, string collectionName)
        {
            using (_loadDocumentStats?.Start() ?? (_loadDocumentStats = _stats?.For(IndexingOperation.LoadDocument)))
            {
                if (keyLazy == null && keyString == null)
                {
                    return(DynamicNullObject.Null);
                }

                var source = Source;
                var id     = GetSourceId(source);

                if (source is DynamicBlittableJson)
                {
                    if (keyLazy != null && id.Equals(keyLazy))
                    {
                        return(source);
                    }

                    if (keyString != null && id.Equals(keyString))
                    {
                        return(source);
                    }
                }

                if (TryGetKeySlice(keyLazy, keyString, out var keySlice) == false)
                {
                    return(DynamicNullObject.Null);
                }

                // we intentionally don't dispose of the scope here, this is being tracked by the references
                // and will be disposed there.

                Slice.From(QueryContext.Documents.Allocator, id, out var idSlice);
                var references = GetReferencesForItem(idSlice);

                references.Add(keySlice);

                // when there is conflict, we need to apply same behavior as if the document would not exist
                var document = _documentsStorage.Get(QueryContext.Documents, keySlice, throwOnConflict: false);

                if (document == null)
                {
                    return(DynamicNullObject.Null);
                }

                if (document.TryGetMetadata(out var metadata) && metadata.TryGet(Constants.Documents.Metadata.Collection, out string collection))
                {
                    if (string.Equals(collection, collectionName, StringComparison.OrdinalIgnoreCase) == false)
                    {
                        return(DynamicNullObject.Null);
                    }
                }

                // we can't share one DynamicBlittableJson instance among all documents because we can have multiple LoadDocuments in a single scope
                return(new DynamicBlittableJson(document));
            }
        }
Пример #3
0
                public bool MoveNext()
                {
                    if (_enumerator.MoveNext() == false)
                    {
                        return(false);
                    }

                    var output = _enumerator.Current;

                    using (_createBlittableResult.Start())
                    {
                        PropertyAccessor accessor;

                        if (_parent._isMultiMap == false)
                        {
                            accessor = _parent._propertyAccessor ??
                                       (_parent._propertyAccessor = PropertyAccessor.CreateMapReduceOutputAccessor(output.GetType(), output, _groupByFields));
                        }
                        else
                        {
                            accessor = TypeConverter.GetPropertyAccessorForMapReduceOutput(output, _groupByFields);
                        }

                        var mapResult = new DynamicJsonValue();

                        _reduceKeyProcessor.Reset();

                        var propertiesInOrder = accessor.PropertiesInOrder;
                        int properties        = propertiesInOrder.Count;

                        for (int i = 0; i < properties; i++)
                        {
                            var field = propertiesInOrder[i];

                            var value          = field.Value.GetValue(output);
                            var blittableValue = TypeConverter.ToBlittableSupportedType(value);
                            mapResult[field.Key] = blittableValue;

                            if (field.Value.IsGroupByField)
                            {
                                var valueForProcessor = field.Value.GroupByField.GetValue(value, blittableValue);
                                _reduceKeyProcessor.Process(_parent._indexContext.Allocator, valueForProcessor);
                            }
                        }

                        if (_reduceKeyProcessor.ProcessedFields != _groupByFields.Count)
                        {
                            ThrowMissingGroupByFieldsInMapOutput(output, _groupByFields, _parent._compiledIndex);
                        }

                        var reduceHashKey = _reduceKeyProcessor.Hash;

                        Current.Data          = _parent._indexContext.ReadObject(mapResult, "map-result");
                        Current.ReduceKeyHash = reduceHashKey;
                    }

                    return(true);
                }
Пример #4
0
        public unsafe List <DynamicAttachment> LoadAttachments(string documentId, IEnumerable <string> attachmentNames)
        {
            using (_loadAttachmentStats?.Start() ?? (_loadAttachmentStats = _stats?.For(IndexingOperation.LoadAttachment)))
            {
                var results = new List <DynamicAttachment>();
                foreach (var attachmentName in attachmentNames)
                {
                    var attachment = _documentsStorage.AttachmentsStorage.GetAttachment(QueryContext.Documents, documentId, attachmentName, AttachmentType.Document, null);
                    if (attachment == null)
                    {
                        continue;
                    }

                    results.Add(new DynamicAttachment(attachment));
                }

                return(results);
            }
        }
Пример #5
0
        public bool MoveNext(out IEnumerable resultsOfCurrentDocument)
        {
            using (_documentReadStats.Start())
            {
                var moveNext = _docsEnumerator.MoveNext();

                _results[0] = _docsEnumerator.Current;
                resultsOfCurrentDocument = _results;

                return(moveNext);
            }
        }
Пример #6
0
        public bool MoveNext(DocumentsOperationContext ctx, out IEnumerable resultsOfCurrentDocument, out long?etag)
        {
            using (_documentReadStats.Start())
            {
                ctx.Transaction.ForgetAbout(_results[0]);
                _results[0]?.Dispose();

                var moveNext = _itemsEnumerator.MoveNext();

                var document = (Document)_itemsEnumerator.Current?.Item;
                _results[0] = document;
                etag        = document?.Etag;
                resultsOfCurrentDocument = _results;

                return(moveNext);
            }
        }
Пример #7
0
                public bool MoveNext()
                {
                    if (_enumerator.MoveNext() == false)
                        return false;

                    var output = _enumerator.Current;

                    using (_createBlittableResult.Start())
                    {
                        IPropertyAccessor accessor;

                        if (_parent._isMultiMap == false)
                            accessor = _parent._propertyAccessor ??
                                       (_parent._propertyAccessor = PropertyAccessor.CreateMapReduceOutputAccessor(output.GetType(), output, _groupByFields));
                        else
                            accessor = TypeConverter.GetPropertyAccessorForMapReduceOutput(output, _groupByFields);

                        var mapResult = new DynamicJsonValue();

                        _reduceKeyProcessor.Reset();

                        foreach (var property in accessor.GetPropertiesInOrder(output))
                        {
                            var value = property.Value;
                            var blittableValue = TypeConverter.ToBlittableSupportedType(value, context: _parent._indexContext);
                            mapResult[property.Key] = blittableValue;

                            if (property.IsGroupByField)
                            {
                                var valueForProcessor = property.GroupByField.GetValue(value, blittableValue);
                                _reduceKeyProcessor.Process(_parent._indexContext.Allocator, valueForProcessor);
                            }
                        }

                        if (_reduceKeyProcessor.ProcessedFields != _groupByFields.Count)
                            ThrowMissingGroupByFieldsInMapOutput(output, _groupByFields, _parent._compiledIndex);

                        var reduceHashKey = _reduceKeyProcessor.Hash;

                        Current.Data = _parent._indexContext.ReadObject(mapResult, "map-result");
                        Current.ReduceKeyHash = reduceHashKey;
                    }

                    return true;
                }
Пример #8
0
        public unsafe dynamic LoadCompareExchangeValue(LazyStringValue keyLazy, string keyString)
        {
            using (_loadCompareExchangeValueStats?.Start() ?? (_loadCompareExchangeValueStats = _stats?.For(IndexingOperation.LoadCompareExchangeValue)))
            {
                if (keyLazy == null && keyString == null)
                {
                    return(DynamicNullObject.Null);
                }

                var source = Source;
                var id     = GetSourceId(source);

                if (TryGetCompareExchangeKeySlice(keyLazy, keyString, out var keySlice) == false)
                {
                    return(DynamicNullObject.Null);
                }

                // we intentionally don't dispose of the scope here, this is being tracked by the references
                // and will be disposed there.
                Slice.From(QueryContext.Documents.Allocator, id, out var idSlice);
                var references = GetCompareExchangeReferencesForItem(idSlice);

                references.Add(keySlice);

                var value = _documentsStorage.DocumentDatabase.ServerStore.Cluster.GetCompareExchangeValue(QueryContext.Server, keySlice);

                if (value.Value == null || value.Value.TryGetMember(Constants.CompareExchange.ObjectFieldName, out object result) == false)
                {
                    return(DynamicNullObject.Null);
                }

                if (result == null)
                {
                    return(DynamicNullObject.ExplicitNull);
                }

                if (result is BlittableJsonReaderObject bjro)
                {
                    return(new DynamicBlittableJson(bjro));
                }

                return(result);
            }
        }
Пример #9
0
                public bool MoveNext()
                {
                    if (_enumerator.MoveNext() == false)
                    {
                        return(false);
                    }

                    var document = _enumerator.Current;

                    using (_createBlittableResult.Start())
                    {
                        var accessor = _parent._propertyAccessor ?? (_parent._propertyAccessor = PropertyAccessor.Create(document.GetType()));

                        var mapResult = new DynamicJsonValue();

                        _reduceKeyProcessor.Reset();

                        foreach (var field in accessor.PropertiesInOrder)
                        {
                            var value          = field.Value.GetValue(document);
                            var blittableValue = TypeConverter.ToBlittableSupportedType(value);
                            mapResult[field.Key] = blittableValue;

                            if (_groupByFields.Contains(field.Key))
                            {
                                _reduceKeyProcessor.Process(_parent._indexContext.Allocator, blittableValue);
                            }
                        }

                        var reduceHashKey = _reduceKeyProcessor.Hash;

                        Current.Data          = _parent._indexContext.ReadObject(mapResult, "map-result");
                        Current.ReduceKeyHash = reduceHashKey;
                    }

                    return(true);
                }
Пример #10
0
 public bool MoveNext()
 {
     using (_stats.Start())
         return(_enumerator.MoveNext());
 }
Пример #11
0
        public unsafe dynamic LoadDocument(LazyStringValue keyLazy, string keyString, string collectionName)
        {
            using (_loadDocumentStats?.Start() ?? (_loadDocumentStats = _stats?.For(IndexingOperation.LoadDocument)))
            {
                if (keyLazy == null && keyString == null)
                {
                    return(DynamicNullObject.Null);
                }

                var source = Source;
                if (source == null)
                {
                    throw new ArgumentException("Cannot execute LoadDocument. Source is not set.");
                }

                var id = source.GetId() as LazyStringValue;
                if (id == null)
                {
                    throw new ArgumentException("Cannot execute LoadDocument. Source does not have a key.");
                }

                if (keyLazy != null && id.Equals(keyLazy))
                {
                    return(source);
                }

                if (keyString != null && id.Equals(keyString))
                {
                    return(source);
                }

                Slice keySlice;
                if (keyLazy != null)
                {
                    if (keyLazy.Length == 0)
                    {
                        return(DynamicNullObject.Null);
                    }

                    // we intentionally don't dispose of the scope here, this is being tracked by the references
                    // and will be disposed there.
                    Slice.External(_documentsContext.Allocator, keyLazy.Buffer, keyLazy.Size, out keySlice);
                }
                else
                {
                    if (keyString.Length == 0)
                    {
                        return(DynamicNullObject.Null);
                    }
                    // we intentionally don't dispose of the scope here, this is being tracked by the references
                    // and will be disposed there.
                    Slice.From(_documentsContext.Allocator, keyString, out keySlice);
                }

                // making sure that we normalize the case of the key so we'll be able to find
                // it in case insensitive manner
                _documentsContext.Allocator.ToLowerCase(ref keySlice.Content);

                var references     = GetReferencesForDocument(id);
                var referenceEtags = GetReferenceEtags();

                references.Add(keySlice);

                // when there is conflict, we need to apply same behavior as if the document would not exist
                var document = _documentsStorage.Get(_documentsContext, keySlice, throwOnConflict: false);

                if (document == null)
                {
                    MaybeUpdateReferenceEtags(referenceEtags, collectionName, 0);
                    return(DynamicNullObject.Null);
                }

                MaybeUpdateReferenceEtags(referenceEtags, collectionName, document.Etag);

                // we can't share one DynamicBlittableJson instance among all documents because we can have multiple LoadDocuments in a single scope
                return(new DynamicBlittableJson(document));
            }
        }