Пример #1
0
        private static Dictionary <string, FieldToFetch> GetFieldsToFetch(string[] fieldsToFetch, IndexDefinitionBase indexDefinition, out bool anyExtractableFromIndex)
        {
            anyExtractableFromIndex = false;

            if (fieldsToFetch == null || fieldsToFetch.Length == 0)
            {
                return(null);
            }

            var result = new Dictionary <string, FieldToFetch>(StringComparer.OrdinalIgnoreCase);

            for (var i = 0; i < fieldsToFetch.Length; i++)
            {
                var fieldToFetch = fieldsToFetch[i];

                if (indexDefinition == null)
                {
                    result[fieldToFetch] = new FieldToFetch(fieldToFetch, false);
                    continue;
                }

                IndexField value;
                var        extract = indexDefinition.TryGetField(fieldToFetch, out value) && value.Storage == FieldStorage.Yes;
                if (extract)
                {
                    anyExtractableFromIndex = true;
                }

                result[fieldToFetch] = new FieldToFetch(fieldToFetch, extract | indexDefinition.HasDynamicFields);
            }

            if (indexDefinition != null)
            {
                anyExtractableFromIndex |= indexDefinition.HasDynamicFields;
            }

            return(result);
        }
Пример #2
0
        private static FieldToFetch GetFieldToFetch(
            IndexDefinitionBaseServerSide indexDefinition,
            QueryMetadata metadata,
            ProjectionBehavior?projectionBehavior,
            SelectField selectField,
            Dictionary <string, FieldToFetch> results,
            out string selectFieldKey,
            ref bool anyExtractableFromIndex,
            ref bool extractAllStoredFields,
            ref bool anyTimeSeries)
        {
            var mustExtractFromIndex  = projectionBehavior.FromIndexOnly();
            var maybeExtractFromIndex = projectionBehavior.FromIndexOrDefault();

            selectFieldKey = selectField.Alias ?? selectField.Name;
            var selectFieldName = selectField.Name;

            if (selectField.ValueTokenType != null)
            {
                return(new FieldToFetch(string.Empty, selectField, selectField.Alias,
                                        canExtractFromIndex: false, isDocumentId: false, isTimeSeries: false));
            }

            if (selectField.Function != null)
            {
                var isTimeSeries = metadata.DeclaredFunctions != null && metadata.DeclaredFunctions.TryGetValue(selectField.Function, out var func) && func.Type == DeclaredFunction.FunctionType.TimeSeries;
                if (isTimeSeries)
                {
                    anyTimeSeries = true;
                }

                var fieldToFetch = new FieldToFetch(
                    selectField.Name,
                    selectField,
                    selectField.Alias,
                    canExtractFromIndex: false,
                    isDocumentId: false,
                    isTimeSeries: isTimeSeries)
                {
                    FunctionArgs = new FieldToFetch[selectField.FunctionArgs.Length]
                };

                for (int j = 0; j < selectField.FunctionArgs.Length; j++)
                {
                    var ignored = false;
                    fieldToFetch.FunctionArgs[j] = GetFieldToFetch(
                        indexDefinition,
                        metadata,
                        projectionBehavior,
                        selectField.FunctionArgs[j],
                        null,
                        out _,
                        ref ignored,
                        ref ignored,
                        ref ignored
                        );
                }
                return(fieldToFetch);
            }

            if (selectField.IsCounter)
            {
                var fieldToFetch = new FieldToFetch(selectField.Name, selectField, selectField.Alias ?? selectField.Name,
                                                    canExtractFromIndex: false, isDocumentId: false, isTimeSeries: false);
                if (selectField.FunctionArgs != null)
                {
                    fieldToFetch.FunctionArgs = new FieldToFetch[0];
                }

                return(fieldToFetch);
            }

            if (selectFieldName == null)
            {
                if (selectField.IsGroupByKey == false)
                {
                    return(null);
                }

                if (selectField.GroupByKeys.Length == 1)
                {
                    selectFieldName = selectField.GroupByKeys[0].Name;

                    if (selectFieldKey == null)
                    {
                        selectFieldKey = selectFieldName;
                    }
                }
                else
                {
                    selectFieldKey = selectFieldKey ?? "Key";
                    return(new FieldToFetch(selectFieldKey, selectField.GroupByKeyNames));
                }
            }

            if (indexDefinition == null)
            {
                return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, canExtractFromIndex: false, isDocumentId: false, isTimeSeries: false));
            }

            if (selectFieldName.Value.Length > 0)
            {
                if (selectFieldName == Constants.Documents.Indexing.Fields.DocumentIdFieldName)
                {
                    anyExtractableFromIndex = maybeExtractFromIndex;

                    return(new FieldToFetch(selectFieldName, selectField, selectField.Alias,
                                            canExtractFromIndex: indexDefinition is MapReduceIndexDefinition or AutoMapReduceIndexDefinition,
                                            isDocumentId: true, isTimeSeries: false));
                }

                if (selectFieldName.Value[0] == '_')
                {
                    if (selectFieldName == Constants.Documents.Indexing.Fields.AllStoredFields)
                    {
                        if (results == null)
                        {
                            ThrowInvalidFetchAllStoredDocuments();
                        }
                        Debug.Assert(results != null);
                        results.Clear(); // __all_stored_fields should only return stored fields so we are ensuring that no other fields will be returned

                        extractAllStoredFields = maybeExtractFromIndex;

                        foreach (var kvp in indexDefinition.MapFields)
                        {
                            var stored = kvp.Value.Storage == FieldStorage.Yes;
                            if (stored == false)
                            {
                                continue;
                            }

                            anyExtractableFromIndex = maybeExtractFromIndex;
                            results[kvp.Key]        = new FieldToFetch(kvp.Key, null, null, canExtractFromIndex: maybeExtractFromIndex, isDocumentId: false, isTimeSeries: false);
                        }

                        return(null);
                    }
                }
            }

            var bySourceAlias = ShouldTryToExtractBySourceAliasName(selectFieldName.Value, selectField);
            var key           = bySourceAlias
                    ? selectField.SourceAlias
                    : selectFieldName;

            var extract = mustExtractFromIndex || (maybeExtractFromIndex && indexDefinition.MapFields.TryGetValue(key, out var value) && value.Storage == FieldStorage.Yes);

            if (extract)
            {
                anyExtractableFromIndex = true;
            }

            if (bySourceAlias == false && maybeExtractFromIndex)
            {
                extract |= indexDefinition.HasDynamicFields;
            }

            return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, extract, isDocumentId: false, isTimeSeries: false));
        }
Пример #3
0
        private static FieldToFetch GetFieldToFetch(
            IndexDefinitionBase indexDefinition,
            SelectField selectField,
            Dictionary <string, FieldToFetch> results,
            out string selectFieldKey,
            ref bool anyExtractableFromIndex,
            ref bool extractAllStoredFields)
        {
            selectFieldKey = selectField.Alias ?? selectField.Name;
            var selectFieldName = selectField.Name;

            if (selectField.ValueTokenType != null)
            {
                return(new FieldToFetch(string.Empty, selectField, selectField.Alias,
                                        canExtractFromIndex: false, isDocumentId: false));
            }
            if (selectField.Function != null)
            {
                var fieldToFetch = new FieldToFetch(selectField.Name, selectField, selectField.Alias,
                                                    canExtractFromIndex: false, isDocumentId: false)
                {
                    FunctionArgs = new FieldToFetch[selectField.FunctionArgs.Length]
                };
                for (int j = 0; j < selectField.FunctionArgs.Length; j++)
                {
                    bool ignored = false;
                    fieldToFetch.FunctionArgs[j] = GetFieldToFetch(indexDefinition,
                                                                   selectField.FunctionArgs[j],
                                                                   null,
                                                                   out _,
                                                                   ref ignored,
                                                                   ref ignored
                                                                   );
                }
                return(fieldToFetch);
            }

            if (selectFieldName == null)
            {
                if (selectField.IsGroupByKey == false)
                {
                    return(null);
                }

                if (selectField.GroupByKeys.Length == 1)
                {
                    selectFieldName = selectField.GroupByKeys[0];

                    if (selectFieldKey == null)
                    {
                        selectFieldKey = selectFieldName;
                    }
                }
                else
                {
                    selectFieldKey = selectFieldKey ?? "Key";
                    return(new FieldToFetch(selectFieldKey, selectField.GroupByKeys));
                }
            }
            if (indexDefinition == null)
            {
                return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, canExtractFromIndex: false, isDocumentId: false));
            }
            if (selectFieldName.Length > 0)
            {
                if (selectFieldName == Constants.Documents.Indexing.Fields.DocumentIdFieldName)
                {
                    anyExtractableFromIndex = true;
                    return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, canExtractFromIndex: false, isDocumentId: true));
                }

                if (selectFieldName[0] == '_')
                {
                    if (selectFieldName == Constants.Documents.Indexing.Fields.AllStoredFields)
                    {
                        if (results == null)
                        {
                            ThrowInvalidFetchAllStoredDocuments();
                        }
                        Debug.Assert(results != null);
                        results.Clear(); // __all_stored_fields should only return stored fields so we are ensuring that no other fields will be returned

                        extractAllStoredFields = true;

                        foreach (var kvp in indexDefinition.MapFields)
                        {
                            var stored = kvp.Value.Storage == FieldStorage.Yes;
                            if (stored == false)
                            {
                                continue;
                            }

                            anyExtractableFromIndex = true;
                            results[kvp.Key]        = new FieldToFetch(kvp.Key, null, null, canExtractFromIndex: true, isDocumentId: false);
                        }

                        return(null);
                    }
                }
            }

            var extract = indexDefinition.MapFields.TryGetValue(selectFieldName, out var value) && value.Storage == FieldStorage.Yes;

            if (extract)
            {
                anyExtractableFromIndex = true;
            }

            return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, extract | indexDefinition.HasDynamicFields, isDocumentId: false));
        }