Esempio n. 1
0
        private static AutoIndexDefinitionBase CreateAutoDefinition(AutoIndexDefinition definition)
        {
            var mapFields = definition
                            .MapFields
                            .Select(x =>
            {
                var field         = AutoIndexField.Create(x.Key, x.Value);
                field.Aggregation = x.Value.Aggregation;

                Debug.Assert(x.Value.GroupByArrayBehavior == GroupByArrayBehavior.NotApplicable);

                return(field);
            })
                            .ToArray();

            if (definition.Type == IndexType.AutoMap)
            {
                var result = new AutoMapIndexDefinition(definition.Collection, mapFields);

                if (definition.LockMode.HasValue)
                {
                    result.LockMode = definition.LockMode.Value;
                }

                if (definition.Priority.HasValue)
                {
                    result.Priority = definition.Priority.Value;
                }

                return(result);
            }

            if (definition.Type == IndexType.AutoMapReduce)
            {
                var groupByFields = definition
                                    .GroupByFields
                                    .Select(x =>
                {
                    var field                  = AutoIndexField.Create(x.Key, x.Value);
                    field.Aggregation          = x.Value.Aggregation;
                    field.GroupByArrayBehavior = x.Value.GroupByArrayBehavior;

                    return(field);
                })
                                    .ToArray();

                var result = new AutoMapReduceIndexDefinition(definition.Collection, mapFields, groupByFields);
                if (definition.LockMode.HasValue)
                {
                    result.LockMode = definition.LockMode.Value;
                }

                if (definition.Priority.HasValue)
                {
                    result.Priority = definition.Priority.Value;
                }

                return(result);
            }

            throw new NotSupportedException("Cannot create auto-index from " + definition.Type);
        }