Ejemplo n.º 1
0
 public void WriteString(string str, bool skipEscaping = false)
 {
     using (var lazyStr = _context.GetLazyString(str))
     {
         WriteString(lazyStr, skipEscaping);
     }
 }
Ejemplo n.º 2
0
        private void SetupMetadataFilterMethod(JsonOperationContext context)
        {
            var skipCountersMetadata    = _options.OperateOnTypes.HasFlag(DatabaseItemType.CounterGroups) == false;
            var skipAttachmentsMetadata = _options.OperateOnTypes.HasFlag(DatabaseItemType.Attachments) == false;

            if (skipCountersMetadata == false && skipAttachmentsMetadata == false)
            {
                _filterMetadataProperty = null;
            }
            else if (skipCountersMetadata == false)
            {
                var attachments = context.GetLazyString(Constants.Documents.Metadata.Attachments);

                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(attachments);
            }
            else if (skipAttachmentsMetadata == false)
            {
                var counters = context.GetLazyString(Constants.Documents.Metadata.Counters);

                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(counters);
            }
            else
            {
                var attachments = context.GetLazyString(Constants.Documents.Metadata.Attachments);
                var counters    = context.GetLazyString(Constants.Documents.Metadata.Counters);

                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(attachments) || metadataProperty.Equals(counters);
            }
        }
Ejemplo n.º 3
0
        public static void TrySaving(string databaseName, string topologyHash, Topology topology, DocumentConventions conventions, JsonOperationContext context)
        {
            try
            {
                var path = GetPath(databaseName, topologyHash, conventions);
                if (topology == null)
                {
                    Clear(databaseName);
                    return;
                }

                var existingTopology = TryLoad(path, context);
                if (existingTopology?.Etag >= topology.Etag)
                {
                    if (_logger.IsInfoEnabled)
                    {
                        _logger.Info($"Skipping save topology with etag {topology.Etag} to cache " +
                                     $"as the cache already have a topology with etag: {existingTopology.Etag}");
                    }
                    return;
                }

                using (var stream = SafeFileStream.Create(path, FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (var writer = new BlittableJsonTextWriter(context, stream))
                    {
                        writer.WriteStartObject();

                        writer.WritePropertyName(context.GetLazyString(nameof(topology.Nodes)));
                        writer.WriteStartArray();
                        for (var i = 0; i < topology.Nodes.Count; i++)
                        {
                            var node = topology.Nodes[i];
                            if (i != 0)
                            {
                                writer.WriteComma();
                            }
                            WriteNode(writer, node, context);
                        }
                        writer.WriteEndArray();

                        writer.WriteComma();
                        writer.WritePropertyName(context.GetLazyString(nameof(topology.Etag)));
                        writer.WriteInteger(topology.Etag);

                        writer.WriteComma();
                        writer.WritePropertyName(context.GetLazyString("PersistedAt"));
                        writer.WriteString(DateTimeOffset.UtcNow.ToString(DefaultFormat.DateTimeOffsetFormatsToWrite));

                        writer.WriteEndObject();
                    }
            }
            catch (Exception e)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Could not persist the database topology", e);
                }
            }
        }
Ejemplo n.º 4
0
 private static void WriteNode(BlittableJsonTextWriter writer, ServerNode node, JsonOperationContext context)
 {
     writer.WriteStartObject();
     writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.Url)));
     writer.WriteString(context.GetLazyString(node.Url));
     writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.Database)));
     writer.WriteString(context.GetLazyString(node.Database));
     writer.WriteEndObject();
 }
Ejemplo n.º 5
0
        public void Reads_unicode(string expected)
        {
            using (var lazyString = _ctx.GetLazyString(expected))
            {
                var stringResult = LazyStringReader.GetStringFor(lazyString);
                var readerResult = _sut.GetTextReaderFor(lazyString);

                Assert.Equal(expected, stringResult);
                Assert.Equal(expected, readerResult.ReadToEnd());
            }
        }
Ejemplo n.º 6
0
        public Document create_doc(DynamicJsonValue document, string id)
        {
            var data = _ctx.ReadObject(document, id);

            _docs.Add(data);

            return(new Document
            {
                Data = data,
                Key = _ctx.GetLazyString(id),
                LoweredKey = _ctx.GetLazyString(id.ToLowerInvariant())
            });
        }
Ejemplo n.º 7
0
        public void Can_get_simple_values()
        {
            var now = SystemTime.UtcNow;

            using (var lazyStringValue = _ctx.GetLazyString("22.0"))
            {
                var stringValue = _ctx.GetLazyString("Arek");
                var doc         = create_doc(new DynamicJsonValue
                {
                    ["Name"]    = "Arek",
                    ["Address"] = new DynamicJsonValue
                    {
                        ["City"] = "NYC"
                    },
                    ["NullField"] = null,
                    ["Age"]       = new LazyNumberValue(lazyStringValue),
                    ["LazyName"]  = stringValue,
                    ["Friends"]   = new DynamicJsonArray
                    {
                        new DynamicJsonValue
                        {
                            ["Name"] = "Joe"
                        },
                        new DynamicJsonValue
                        {
                            ["Name"] = "John"
                        }
                    },
                    [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                    {
                        [Constants.Documents.Metadata.Collection]   = "Users",
                        [Constants.Documents.Metadata.LastModified] = now.GetDefaultRavenFormat(true)
                    }
                }, "users/1");

                dynamic user = new DynamicBlittableJson(doc);

                Assert.Equal("Arek", user.Name);
                Assert.Equal("NYC", user.Address.City);
                Assert.Equal("users/1", user.Id);
                Assert.Equal(DynamicNullObject.Null, user.NullField);
                Assert.Equal(22.0, user.Age);
                Assert.Equal("Arek", user.LazyName);
                Assert.Equal(2, user.Friends.Length);
                Assert.Equal("Users", user[Constants.Documents.Metadata.Key][Constants.Documents.Metadata.Collection]);
                Assert.Equal(now, user[Constants.Documents.Metadata.Key].Value <DateTime>(Constants.Documents.Metadata.LastModified));
                _ctx.ReturnMemory(stringValue.AllocatedMemoryData);
            }
        }
Ejemplo n.º 8
0
        public Document create_doc(DynamicJsonValue document, string id)
        {
            var data = _ctx.ReadObject(document, id);

            _docs.Add(data);

            //_lazyStrings.
            var lazyStringValueRegular   = _ctx.GetLazyString(id);
            var lazyStringValueLowerCase = _ctx.GetLazyString(id.ToLowerInvariant());

            return(new Document
            {
                Data = data,
                Id = lazyStringValueRegular,
                LowerId = lazyStringValueLowerCase
            });
        }
        public static Dictionary <string, CompareExchangeValue <T> > GetValues(JsonOperationContext context, BlittableJsonReaderObject response, DocumentConventions conventions)
        {
            if (response.TryGet("Results", out BlittableJsonReaderArray items) == false)
            {
                throw new InvalidDataException("Response is invalid. Results is missing.");
            }

            LazyStringValue lsv     = null;
            var             results = new Dictionary <string, CompareExchangeValue <T> >();

            foreach (BlittableJsonReaderObject item in items)
            {
                if (item == null)
                {
                    throw new InvalidDataException("Response is invalid. Item is null.");
                }

                if (item.TryGet("Key", out string key) == false)
                {
                    throw new InvalidDataException("Response is invalid. Key is missing.");
                }
                if (item.TryGet("Index", out long index) == false)
                {
                    throw new InvalidDataException("Response is invalid. Index is missing.");
                }
                if (item.TryGet("Value", out BlittableJsonReaderObject raw) == false)
                {
                    throw new InvalidDataException("Response is invalid. Value is missing.");
                }

                if (typeof(T).GetTypeInfo().IsPrimitive || typeof(T) == typeof(string))
                {
                    // simple
                    T value = default;
                    raw?.TryGet("Object", out value);
                    results[key] = new CompareExchangeValue <T>(key, index, value);
                }
                else
                {
                    if (lsv == null)
                    {
                        lsv = context.GetLazyString("Object");
                    }

                    if (raw == null || raw.Contains(lsv) == false)
                    {
                        results[key] = new CompareExchangeValue <T>(key, index, default);
                    }
                    else
                    {
                        var converted = (ResultHolder)EntityToBlittable.ConvertToEntity(typeof(ResultHolder), null, raw, conventions);
                        results[key] = new CompareExchangeValue <T>(key, index, converted.Object);
                    }
                }
            }

            return(results);
        }
Ejemplo n.º 10
0
        public Document Clone(JsonOperationContext context)
        {
            return(new Document
            {
                Etag = Etag,
                StorageId = StorageId,
                IndexScore = IndexScore,
                Distance = Distance,
                ChangeVector = ChangeVector,
                LastModified = LastModified,
                Flags = Flags,
                NonPersistentFlags = NonPersistentFlags,
                TransactionMarker = TransactionMarker,

                Id = context.GetLazyString(Id),
                LowerId = context.GetLazyString(LowerId),
                Data = Data.Clone(context),
            });
        }
Ejemplo n.º 11
0
        public Document CloneWith(JsonOperationContext context, BlittableJsonReaderObject newData)
        {
            var newId      = context.GetLazyString(Id);
            var newLowerId = context.GetLazyString(LowerId);

            return(new Document
            {
                Etag = Etag,
                StorageId = StorageId,
                IndexScore = IndexScore,
                Distance = Distance,
                ChangeVector = ChangeVector,
                LastModified = LastModified,
                Flags = Flags,
                NonPersistentFlags = NonPersistentFlags,
                TransactionMarker = TransactionMarker,
                Id = newId,
                LowerId = newLowerId,
                Data = newData,
            });
        }
        public override BlittableJsonReaderObject GetUpdatedValue(JsonOperationContext context, BlittableJsonReaderObject previousValue, long index)
        {
            if (string.IsNullOrWhiteSpace(Value.Name))
            {
                Value.Name = GenerateTaskName(previousValue);
            }

            if (Value.ExcludedDatabases != null &&
                Value.ExcludedDatabases.Any(string.IsNullOrWhiteSpace))
            {
                throw new RachisApplyException($"{nameof(ServerWideExternalReplication.ExcludedDatabases)} cannot contain null or empty database names");
            }

            var originTaskId = Value.TaskId;

            Value.TaskId = index;

            if (previousValue != null)
            {
                var lazy = context.GetLazyString(Value.Name);
                if (previousValue.Contains(lazy) == false)
                {
                    //The name have might modified so we search by index/TaskId
                    foreach (var propertyName in previousValue.GetPropertyNames())
                    {
                        var value = previousValue[propertyName] as BlittableJsonReaderObject;
                        Debug.Assert(value != null);
                        if (value.TryGet(nameof(ServerWideExternalReplication.TaskId), out long taskId) && taskId == originTaskId)
                        {
                            previousValue.Modifications = new DynamicJsonValue(previousValue);
                            previousValue.Modifications.Remove(propertyName);
                            break;
                        }
                    }
                }
                previousValue.Modifications ??= new DynamicJsonValue();
                previousValue.Modifications[Value.Name] = Value.ToJson();

                return(context.ReadObject(previousValue, Name));
            }

            var djv = new DynamicJsonValue
            {
                [Value.Name] = Value.ToJson()
            };

            return(context.ReadObject(djv, Name));
        }
Ejemplo n.º 13
0
        private static unsafe BlittableJsonReaderObject ParseJsonAllAtOnce(string s, JsonOperationContext ctx)
        {
            var jsonParserState = new JsonParserState();
            var parser          = new UnmanagedJsonParser(ctx, jsonParserState, "test");
            var builder         = new BlittableJsonDocumentBuilder(ctx, BlittableJsonDocumentBuilder.UsageMode.ToDisk, "test", parser, jsonParserState);

            builder.ReadObjectDocument();
            var value = ctx.GetLazyString(s);

            parser.SetBuffer(value.Buffer, value.Size);
            Assert.True(builder.Read());
            builder.FinalizeDocument();
            var reader = builder.CreateReader();

            return(reader);
        }
Ejemplo n.º 14
0
        private static void WriteNode(BlittableJsonTextWriter writer, ServerNode node, JsonOperationContext context)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.Url)));
            writer.WriteString(context.GetLazyString(node.Url));

            writer.WriteComma();
            writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.Database)));
            writer.WriteString(context.GetLazyString(node.Database));

            // ClusterTag and ServerRole included for debugging purpose only
            writer.WriteComma();
            writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.ClusterTag)));
            writer.WriteString(context.GetLazyString(node.ClusterTag));

            writer.WriteComma();
            writer.WritePropertyName(context.GetLazyString(nameof(ServerNode.ServerRole)));
            writer.WriteString(context.GetLazyString(node.ServerRole.ToString()));

            writer.WriteEndObject();
        }
Ejemplo n.º 15
0
        public static void TrySavingTopologyToLocalCache(string serverHash, Topology topology, JsonOperationContext context)
        {
            try
            {
                var path = GetTopologyPath(serverHash);
                if (topology == null)
                {
                    ClearTopologyFromLocalCache(serverHash);
                    return;
                }

                using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (var writer = new BlittableJsonTextWriter(context, stream))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName(context.GetLazyString(nameof(Topology.Nodes)));
                        writer.WriteStartArray();
                        for (var index = 0; index < topology.Nodes.Count; index++)
                        {
                            var node = topology.Nodes[index];
                            if (index != 0)
                            {
                                writer.WriteComma();
                            }
                            WriteNode(writer, node, context);
                        }
                        writer.WriteEndArray();
                        writer.WriteComma();
                        writer.WriteEndObject();

                        writer.WriteEndObject();
                    }
            }
            catch (Exception e)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Could not persist the replication information", e);
                }
            }
        }
Ejemplo n.º 16
0
        private static async Task GetIdentitiesValues(JsonOperationContext ctx, DocumentDatabase database, ServerStore serverStore,
                                                      List <string> identities, List <int> positionInListToCommandIndex, CommandData[] cmds)
        {
            var newIds = await serverStore.GenerateClusterIdentitiesBatchAsync(database.Name, identities);

            Debug.Assert(newIds.Count == identities.Count);

            var emptyChangeVector = ctx.GetLazyString("");

            for (var index = 0; index < positionInListToCommandIndex.Count; index++)
            {
                var value = positionInListToCommandIndex[index];
                cmds[value].Id = cmds[value].Id.Substring(0, cmds[value].Id.Length - 1) + "/" + newIds[index];

                if (string.IsNullOrEmpty(cmds[value].ChangeVector) == false)
                {
                    ThrowInvalidUsageOfChangeVectorWithIdentities(cmds[value]);
                }
                cmds[value].ChangeVector = emptyChangeVector;
            }
        }
Ejemplo n.º 17
0
 public AllowedPathsValidator(string[] allowedPaths)
 {
     _allowedPathsContext = JsonOperationContext.ShortTermSingleUse();
     _documentInfoHelper = new DocumentInfoHelper(_allowedPathsContext);
     _allowedPaths = new List<LazyStringValue>();
     _allowedPathsPrefixes = new List<LazyStringValue>();
     foreach (var t in allowedPaths)
     {
         var lazyStringValue = _allowedPathsContext.GetLazyString(t);
         if (lazyStringValue.Size == 0)
             continue; // shouldn't happen, but let's be safe
         if (lazyStringValue[lazyStringValue.Size - 1] == '*')
         {
             lazyStringValue.Truncate(lazyStringValue.Size - 1);
             _allowedPathsPrefixes.Add(lazyStringValue);
         }
         else
         {
             _allowedPaths.Add(lazyStringValue);
         }
     }
 }
Ejemplo n.º 18
0
        public static void TrySavingTopologyToLocalCache(string serverHash, Topology topology, JsonOperationContext context)
        {
            try
            {
                var path = GetTopologyPath(serverHash);
                if (topology == null)
                {
                    ClearTopologyFromLocalCache(serverHash);
                    return;
                }

                using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (var writer = new BlittableJsonTextWriter(context, stream))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName(context.GetLazyString(nameof(Topology.LeaderNode)));
                        WriteNode(writer, topology.LeaderNode, context);

                        writer.WritePropertyName(context.GetLazyString(nameof(Topology.Nodes)));
                        writer.WriteStartArray();
                        foreach (var node in topology.Nodes)
                        {
                            WriteNode(writer, node, context);
                        }
                        writer.WriteEndArray();

                        writer.WritePropertyName(context.GetLazyString(nameof(Topology.ReadBehavior)));
                        writer.WriteString(context.GetLazyString(topology.ReadBehavior.ToString()));

                        writer.WritePropertyName(context.GetLazyString(nameof(Topology.WriteBehavior)));
                        writer.WriteString(context.GetLazyString(topology.WriteBehavior.ToString()));
                        writer.WriteEndObject();
                    }
            }
            catch (Exception e)
            {
                Log.ErrorException("Could not persist the replication information", e);
            }
        }
Ejemplo n.º 19
0
        protected Document GetProjection(Lucene.Net.Documents.Document input, float score, string lowerId, IState state)
        {
            Document doc = null;

            if (FieldsToFetch.AnyExtractableFromIndex == false)
            {
                doc = DirectGet(input, lowerId, state);

                if (doc == null)
                {
                    return(null);
                }

                return(GetProjectionFromDocument(doc, input, score, FieldsToFetch, _context, state));
            }

            var documentLoaded = false;

            var result = new DynamicJsonValue();

            Dictionary <string, FieldsToFetch.FieldToFetch> fields = null;

            if (FieldsToFetch.ExtractAllFromIndex)
            {
                fields = input.GetFields()
                         .Where(x => x.Name != Constants.Documents.Indexing.Fields.DocumentIdFieldName &&
                                x.Name != Constants.Documents.Indexing.Fields.ReduceKeyHashFieldName &&
                                x.Name != Constants.Documents.Indexing.Fields.ReduceKeyValueFieldName &&
                                FieldUtil.GetRangeTypeFromFieldName(x.Name) == RangeType.None)
                         .Distinct(UniqueFieldNames.Instance)
                         .ToDictionary(x => x.Name, x => new FieldsToFetch.FieldToFetch(x.Name, null, null, x.IsStored, isDocumentId: false));
            }

            if (fields == null)
            {
                fields = FieldsToFetch.Fields;
            }
            else if (FieldsToFetch.Fields != null && FieldsToFetch.Fields.Count > 0)
            {
                foreach (var kvp in FieldsToFetch.Fields)
                {
                    if (fields.ContainsKey(kvp.Key))
                    {
                        continue;
                    }

                    fields[kvp.Key] = kvp.Value;
                }
            }

            foreach (var fieldToFetch in fields.Values)
            {
                if (TryExtractValueFromIndex(fieldToFetch, input, result, state))
                {
                    continue;
                }

                if (documentLoaded == false)
                {
                    doc = DirectGet(input, lowerId, state);

                    documentLoaded = true;
                }

                if (doc == null)
                {
                    continue;
                }

                if (TryGetValue(fieldToFetch, doc, input, state, out var fieldVal))
                {
                    if (FieldsToFetch.SingleBodyOrMethodWithNoAlias)
                    {
                        if (fieldVal is BlittableJsonReaderObject nested)
                        {
                            doc.Data = nested;
                        }
                        else if (fieldVal is Document d)
                        {
                            doc = d;
                        }
                        else
                        {
                            ThrowInvalidQueryBodyResponse(fieldVal);
                        }
                        doc.IndexScore = score;
                        return(doc);
                    }
                    if (fieldVal is List <object> list)
                    {
                        fieldVal = new DynamicJsonArray(list);
                    }
                    result[fieldToFetch.ProjectedName ?? fieldToFetch.Name.Value] = fieldVal;
                }
            }

            if (doc == null)
            {
                doc = new Document
                {
                    Id = _context.GetLazyString(lowerId)
                };
            }

            return(ReturnProjection(result, doc, score, _context));
        }
Ejemplo n.º 20
0
 private static unsafe LazyStringValue GetLazyStringValue(JsonOperationContext ctx, JsonParserState state)
 {
     return(ctx.GetLazyString(Encodings.Utf8.GetString(state.StringBuffer, state.StringSize)));
 }
        private static void WriteIndexFieldOptions(this BlittableJsonTextWriter writer, JsonOperationContext context, IndexFieldOptions options, bool removeAnalyzers)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(options.Analyzer));
            if (string.IsNullOrWhiteSpace(options.Analyzer) == false && !removeAnalyzers)
            {
                writer.WriteString(options.Analyzer);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Indexing));
            if (options.Indexing.HasValue)
            {
                writer.WriteString(options.Indexing.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Storage));
            if (options.Storage.HasValue)
            {
                writer.WriteString(options.Storage.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Suggestions));
            if (options.Suggestions.HasValue)
            {
                writer.WriteBool(options.Suggestions.Value);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.TermVector));
            if (options.TermVector.HasValue)
            {
                writer.WriteString(options.TermVector.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Spatial));
            if (options.Spatial != null)
            {
                writer.WriteStartObject();

                writer.WritePropertyName(nameof(options.Spatial.Type));
                writer.WriteString(options.Spatial.Type.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxTreeLevel));
                writer.WriteInteger(options.Spatial.MaxTreeLevel);
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxX));
                LazyStringValue lazyStringValue;
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MaxX)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxY));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MaxY)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MinX));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MinX)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MinY));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MinY)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.Strategy));
                writer.WriteString(options.Spatial.Strategy.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.Units));
                writer.WriteString(options.Spatial.Units.ToString());

                writer.WriteEndObject();
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
        public static void WriteFacetResult(this BlittableJsonTextWriter writer, JsonOperationContext context, FacetResult result)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(result.RemainingHits));
            writer.WriteInteger(result.RemainingHits);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.RemainingTermsCount));
            writer.WriteInteger(result.RemainingTermsCount);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.RemainingTerms));
            writer.WriteStartArray();
            var isFirstInternal = true;

            foreach (var term in result.RemainingTerms)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WriteString(term);
            }
            writer.WriteEndArray();
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.Values));
            writer.WriteStartArray();
            isFirstInternal = true;
            foreach (var value in result.Values)
            {
                if (isFirstInternal == false)
                {
                    writer.WriteComma();
                }

                isFirstInternal = false;

                writer.WriteStartObject();

                writer.WritePropertyName(nameof(value.Average));
                if (value.Average.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Average.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Count));
                if (value.Count.HasValue)
                {
                    writer.WriteInteger(value.Count.Value);
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Hits));
                writer.WriteInteger(value.Hits);
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Max));
                if (value.Max.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Max.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Min));
                if (value.Min.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Min.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Range));
                writer.WriteString(value.Range);
                writer.WriteComma();

                writer.WritePropertyName(nameof(value.Sum));
                if (value.Sum.HasValue)
                {
                    using (var lazyStringValue = context.GetLazyString(value.Sum.ToInvariantString()))
                        writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                }
                else
                {
                    writer.WriteNull();
                }

                writer.WriteEndObject();
            }
            writer.WriteEndArray();

            writer.WriteEndObject();
        }
        protected Document GetProjection(Lucene.Net.Documents.Document input, Lucene.Net.Search.ScoreDoc scoreDoc, string lowerId, IState state)
        {
            using (_projectionScope = _projectionScope?.Start() ?? RetrieverScope?.For(nameof(QueryTimingsScope.Names.Projection)))
            {
                Document doc = null;
                if (FieldsToFetch.AnyExtractableFromIndex == false)
                {
                    using (_projectionStorageScope = _projectionStorageScope?.Start() ?? _projectionScope?.For(nameof(QueryTimingsScope.Names.Storage)))
                        doc = DirectGet(input, lowerId, DocumentFields.All, state);

                    if (doc == null)
                    {
                        return(null);
                    }
                    return(GetProjectionFromDocument(doc, input, scoreDoc, FieldsToFetch, _context, state));
                }

                var result = new DynamicJsonValue();

                Dictionary <string, FieldsToFetch.FieldToFetch> fields = null;
                if (FieldsToFetch.ExtractAllFromIndex)
                {
                    fields = input.GetFields()
                             .Where(x => x.Name != Constants.Documents.Indexing.Fields.DocumentIdFieldName &&
                                    x.Name != Constants.Documents.Indexing.Fields.ReduceKeyHashFieldName &&
                                    x.Name != Constants.Documents.Indexing.Fields.ReduceKeyValueFieldName &&
                                    FieldUtil.GetRangeTypeFromFieldName(x.Name) == RangeType.None)
                             .Distinct(UniqueFieldNames.Instance)
                             .ToDictionary(x => x.Name, x => new FieldsToFetch.FieldToFetch(x.Name, null, null, x.IsStored, isDocumentId: false));
                }

                if (fields == null)
                {
                    fields = FieldsToFetch.Fields;
                }
                else if (FieldsToFetch.Fields != null && FieldsToFetch.Fields.Count > 0)
                {
                    foreach (var kvp in FieldsToFetch.Fields)
                    {
                        if (fields.ContainsKey(kvp.Key))
                        {
                            continue;
                        }

                        fields[kvp.Key] = kvp.Value;
                    }
                }

                foreach (var fieldToFetch in fields.Values)
                {
                    if (TryExtractValueFromIndex(fieldToFetch, input, result, state))
                    {
                        continue;
                    }

                    if (doc == null)
                    {
                        using (_projectionStorageScope = _projectionStorageScope?.Start() ?? _projectionScope?.For(nameof(QueryTimingsScope.Names.Storage)))
                            doc = DirectGet(input, lowerId, DocumentFields.All, state);

                        if (doc == null)
                        {
                            // we don't return partial results
                            return(null);
                        }
                    }

                    if (TryGetValue(fieldToFetch, doc, input, state, FieldsToFetch.IndexFields, FieldsToFetch.AnyDynamicIndexFields, out var key, out var fieldVal))
                    {
                        if (FieldsToFetch.SingleBodyOrMethodWithNoAlias)
                        {
                            if (fieldVal is BlittableJsonReaderObject nested)
                            {
                                doc.Data = nested;
                            }
                            else if (fieldVal is Document d)
                            {
                                doc = d;
                            }
                            else
                            {
                                ThrowInvalidQueryBodyResponse(fieldVal);
                            }
                            FinishDocumentSetup(doc, scoreDoc);
                            return(doc);
                        }

                        if (fieldVal is List <object> list)
                        {
                            fieldVal = new DynamicJsonArray(list);
                        }

                        if (fieldVal is Document d2)
                        {
                            fieldVal = d2.Data;
                        }

                        result[key] = fieldVal;
                    }
                }

                if (doc == null)
                {
                    // the fields were projected from the index
                    doc = new Document
                    {
                        Id = _context.GetLazyString(lowerId)
                    };
                }

                return(ReturnProjection(result, doc, scoreDoc, _context));
            }
        }
Ejemplo n.º 24
0
        private void SetupMetadataFilterMethod(JsonOperationContext context)
        {
            var skipCountersMetadata    = _options.OperateOnTypes.HasFlag(DatabaseItemType.CounterGroups) == false;
            var skipAttachmentsMetadata = _options.OperateOnTypes.HasFlag(DatabaseItemType.Attachments) == false;
            var skipTimeSeriesMetadata  = _options.OperateOnTypes.HasFlag(DatabaseItemType.TimeSeries) == false;

            var flags = 0;

            if (skipCountersMetadata)
            {
                flags += 1;
            }
            if (skipAttachmentsMetadata)
            {
                flags += 2;
            }
            if (skipTimeSeriesMetadata)
            {
                flags += 4;
            }

            if (flags == 0)
            {
                return;
            }

            var counters    = context.GetLazyString(Constants.Documents.Metadata.Counters);
            var attachments = context.GetLazyString(Constants.Documents.Metadata.Attachments);
            var timeSeries  = context.GetLazyString(Constants.Documents.Metadata.TimeSeries);

            switch (flags)
            {
            case 1:     // counters
                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(counters);
                break;

            case 2:     // attachments
                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(attachments);
                break;

            case 3:     // counters, attachments
                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(counters) || metadataProperty.Equals(attachments);
                break;

            case 4:     // timeseries
                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(timeSeries);
                break;

            case 5:     // counters, timeseries
                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(counters) || metadataProperty.Equals(timeSeries);
                break;

            case 6:     // attachments, timeseries
                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(attachments) || metadataProperty.Equals(timeSeries);
                break;

            case 7:     // counters, attachments, timeseries
                _filterMetadataProperty = metadataProperty => metadataProperty.Equals(counters) || metadataProperty.Equals(attachments) || metadataProperty.Equals(timeSeries);
                break;

            default:
                throw new NotSupportedException($"Not supported value: {flags}");
            }
        }