public void AddReduce(string reduceKeyHash, object reduceObject)
            {
                if (_reduceDocuments.TryGetValue(reduceKeyHash, out var outputs) == false)
                {
                    outputs = new List <BlittableJsonReaderObject>(1);
                    _reduceDocuments.Add(reduceKeyHash, outputs);
                }

                var djv = new DynamicJsonValue();

                if (_index.OutputReduceToCollectionPropertyAccessor == null)
                {
                    _index.OutputReduceToCollectionPropertyAccessor = PropertyAccessor.Create(reduceObject.GetType(), reduceObject);
                }

                foreach (var property in _index.OutputReduceToCollectionPropertyAccessor.PropertiesInOrder)
                {
                    var value = property.Value.GetValue(reduceObject);
                    djv[property.Key] = TypeConverter.ToBlittableSupportedType(value);
                }
                djv[Constants.Documents.Metadata.Key] = new DynamicJsonValue
                {
                    [Constants.Documents.Metadata.Collection] = _outputReduceToCollection
                };

                var doc = _indexContext.ReadObject(djv, "output-of-reduce-doc", BlittableJsonDocumentBuilder.UsageMode.ToDisk);

                outputs.Add(doc);
            }
            public void AddReduce(string reduceKeyHash, object reduceObject)
            {
                var key = _outputReduceToCollection + "/" + reduceKeyHash;

                var djv = new DynamicJsonValue();

                if (_index.OutputReduceToCollectionPropertyAccessor == null)
                {
                    _index.OutputReduceToCollectionPropertyAccessor = PropertyAccessor.Create(reduceObject.GetType());
                }
                foreach (var property in _index.OutputReduceToCollectionPropertyAccessor.PropertiesInOrder)
                {
                    var value = property.Value.GetValue(reduceObject);
                    djv[property.Key] = TypeConverter.ToBlittableSupportedType(value);
                }
                djv[Constants.Documents.Metadata.Key] = new DynamicJsonValue
                {
                    [Constants.Documents.Metadata.Collection] = _outputReduceToCollection
                };

                _reduceDocuments.Add(new OutputReduceDocument
                {
                    Key      = key,
                    Document = _jsonContext.ReadObject(djv, key, BlittableJsonDocumentBuilder.UsageMode.ToDisk)
                });
            }
Пример #3
0
        public static PropertyAccessor GetPropertyAccessorForMapReduceOutput(object value, HashSet <CompiledIndexField> groupByFields)
        {
            var type = value.GetType();

            if (value is Dictionary <string, object> ) // don't use cache when using dictionaries
            {
                return(PropertyAccessor.Create(type, value));
            }

            return(PropertyAccessorCache.GetOrAdd(type, x => PropertyAccessor.CreateMapReduceOutputAccessor(type, value, groupByFields)));
        }
Пример #4
0
        public static PropertyAccessor GetPropertyAccessor(object value)
        {
            var type = value.GetType();

            if (value is Dictionary <string, object> ) // don't use cache when using dictionaries
            {
                return(PropertyAccessor.Create(type, value));
            }

            return(PropertyAccessorCache.GetOrAdd(type, x => PropertyAccessor.Create(type, value)));
        }
        public static void PopulateFromDataReader <T>(this T obj, IDataReader reader, bool tryRead) where T : class, new()
        {
            if (obj == null)
            {
                throw new InvalidOperationException(
                          "Cannot call PopulateFromDataReader<T> on a null object - initialise it before populating.");
            }

            if (!tryRead || reader.Read())
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    string name = reader.GetName(i);
                    KeyValuePair <Type, string> key = new KeyValuePair <Type, string>(typeof(T), name);
                    object value = reader[i].IsDbNull() ? null : reader[i];

                    PropertyInfo propertyInfo = propertyCache[key];
                    if (propertyInfo != null)
                    {
                        try
                        {
                            PropertyAccessor.Create(propertyInfo).Set(obj, value);
                        }
                        catch (Exception ex)
                        {
                            throw new InvalidOperationException(
                                      string.Format("Failed to populate property {0} of type {1} with value of type {2}", name,
                                                    propertyInfo.PropertyType, value != null ? value.GetType().Name : "null"), ex);
                        }
                    }
                    else
                    {
                        try
                        {
                            FieldInfo fieldInfo = fieldCache[key];

                            if (fieldInfo != null)
                            {
                                fieldInfo.SetValue(obj, value);
                            }
                        }
                        catch (ArgumentException ex)
                        {
                            throw new InvalidOperationException(
                                      string.Format("Failed to populate property or field {0} with value of type {1}", name,
                                                    value != null ? value.GetType().Name : "null"), ex);
                        }
                    }
                }
            }
        }
Пример #6
0
 public InvokeTypeInfo(
     TypeAnalysis typeAnalysis)
     : base(
         typeAnalysis.name,
         typeAnalysis.level,
         typeAnalysis.opcode,
         typeAnalysis.keywords,
         typeAnalysis.tags)
 {
     if (typeAnalysis.properties.Length != 0)
     {
         this.properties = typeAnalysis.properties;
         this.accessors  = new PropertyAccessor <ContainerType> [this.properties.Length];
         for (int i = 0; i < this.accessors.Length; i++)
         {
             this.accessors[i] = PropertyAccessor <ContainerType> .Create(this.properties[i]);
         }
     }
 }
Пример #7
0
        protected override AggregationResult AggregateOn(List <BlittableJsonReaderObject> aggregationBatch, TransactionOperationContext indexContext, CancellationToken token)
        {
            _blittableToDynamicWrapper.InitializeForEnumeration(aggregationBatch);

            var resultObjects = new List <object>();

            foreach (var output in _reducingFunc(_blittableToDynamicWrapper))
            {
                token.ThrowIfCancellationRequested();

                if (_propertyAccessor == null)
                {
                    _propertyAccessor = PropertyAccessor.Create(output.GetType(), output);
                }

                resultObjects.Add(output);
            }

            return(new AggregatedAnonymousObjects(resultObjects, _propertyAccessor, indexContext));
        }
        protected override AggregationResult AggregateOn(List <BlittableJsonReaderObject> aggregationBatch, TransactionOperationContext indexContext, IndexingStatsScope stats, CancellationToken token)
        {
            _blittableToDynamicWrapper.InitializeForEnumeration(aggregationBatch);

            var resultObjects = new List <object>();

            var indexingFunctionType = _indexType.IsJavaScript() ? IndexingOperation.Map.Jint : IndexingOperation.Map.Linq;

            var funcStats = stats?.For(indexingFunctionType, start: false);

            foreach (var output in new TimeCountingEnumerable(_reducingFunc(_blittableToDynamicWrapper), funcStats))
            {
                token.ThrowIfCancellationRequested();

                if (_propertyAccessor == null)
                {
                    _propertyAccessor = PropertyAccessor.Create(output.GetType());
                }

                resultObjects.Add(output);
            }

            return(new AggregatedAnonymousObjects(resultObjects, _propertyAccessor, indexContext));
        }
Пример #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
        internal static DataTable ToDataTable <U>(IEnumerable <U> collection)
        {
            string collectionTypeName;
            Type   tableType = typeof(U);

            if (collection == null)
            {
                throw new QueryTalkException("ToDataTable<T>", QueryTalkExceptionType.CollectionNullOrEmpty,
                                             String.Format("data class = {0}", tableType), Text.Method.ToDataTable);
            }

            collectionTypeName = collection.GetType().Name;
            if (collection is Wall.IResult)
            {
                collectionTypeName = Text.Class.ResultSet;
            }

            if (collectionTypeName == Text.Class.ResultSet)
            {
                var resultSet = (ResultSet <U>)collection;

                if (resultSet.DataTable != null)
                {
                    return(resultSet.DataTable);
                }

                // check dynamic - not convertable
                if (tableType == typeof(System.Object))
                {
                    if (collection.Count() == 0)
                    {
                        throw new QueryTalkException("ResultSet.getTableType", QueryTalkExceptionType.EmptyResultset,
                                                     "table type = dynamic", Text.Method.ToDataTable);
                    }

                    // infer type from the item
                    using (var enumerator = collection.GetEnumerator())
                    {
                        enumerator.MoveNext();
                        tableType = enumerator.Current.GetType();
                    }
                }
            }

            DataTable dataTable = new DataTable();

            dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

            // always build new getters (not using cache)
            var getters = new List <IPropertyAccessor>();

            var props = tableType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                        .Where(prop => prop.GetGetMethod() != null)
                        .ToArray();

            PropertyInfo[] properties;

            if (tableType.IsDbRow())
            {
                var node = DbMapping.GetNodeMap(tableType);
                var propsByDatabaseOrder = new List <PropertyInfo>();
                foreach (var column in node.SortedColumnsByDatabaseOrder)
                {
                    var prop = props.Where(a => a.Name.EqualsCS(column.ClrName)).Select(a => a).FirstOrDefault();
                    if (prop != null)
                    {
                        propsByDatabaseOrder.Add(prop);
                    }
                }

                properties = propsByDatabaseOrder.ToArray();
            }
            else
            {
                properties = props;
            }

            if (properties.Length == 0)
            {
                throw new QueryTalkException("ResultSet.ToDataTable", QueryTalkExceptionType.InvalidDataClass,
                                             String.Format("data class = {0}", tableType), Text.Method.ToDataTable);
            }

            int numberOfPropertiesUsed = 0;

            foreach (PropertyInfo property in properties)
            {
                Type clrType;
                QueryTalkException exception;

                var clrTypeMatch = Mapping.CheckClrCompliance(property.PropertyType, out clrType, out exception);
                if (clrTypeMatch == Mapping.ClrTypeMatch.NodeMatch)
                {
                    continue;
                }

                if (clrTypeMatch != Mapping.ClrTypeMatch.ClrMatch)
                {
                    ThrowNotClrCompliantException(exception, property.PropertyType);
                }

                dataTable.Columns.Add(property.Name, clrType);

                getters.Add(PropertyAccessor.Create(tableType, property));

                ++numberOfPropertiesUsed;
            }

            foreach (U item in collection)
            {
                DataRow row = dataTable.NewRow();
                for (int i = 0; i < numberOfPropertiesUsed; i++)
                {
                    row[i] = getters[i].GetValue(item) ?? DBNull.Value;
                }

                dataTable.Rows.Add(row);
            }

            return(dataTable);
        }