Exemplo n.º 1
0
        private static Expression GetOrAddParameterRefToCompilationContext(ParsedRequest parsedRequest, PqlCompilerState compilerState, int parameterOrdinal)
        {
            Tuple <ParameterExpression, Expression> refTuple;

            if (compilerState.ParamRefs.TryGetValue(parameterOrdinal, out refTuple))
            {
                return(refTuple.Item1);
            }

            ParameterExpression paramRef;
            Expression          paramExtractor;
            var localOrdinal = parsedRequest.Params.OrdinalToLocalOrdinal[parameterOrdinal];
            var dbType       = parsedRequest.Params.DataTypes[parameterOrdinal];

            if (BitVector.Get(compilerState.RequestParameters.IsCollectionFlags, parameterOrdinal))
            {
                var rowData     = Expression.Field(compilerState.Context, "InputParametersCollections");
                var hashsetType = typeof(HashSet <>).MakeGenericType(DriverRowData.DeriveSystemType(dbType));
                paramExtractor = Expression.Convert(Expression.ArrayIndex(rowData, Expression.Constant(localOrdinal, typeof(int))), hashsetType);
                paramRef       = Expression.Variable(paramExtractor.Type);
            }
            else
            {
                var rowData = Expression.Field(compilerState.Context, "InputParametersRow");
                paramExtractor = DriverRowData.CreateReadAccessor(rowData, dbType, localOrdinal);
                paramRef       = Expression.Variable(paramExtractor.Type);
            }

            compilerState.ParamRefs.Add(
                parameterOrdinal, new Tuple <ParameterExpression, Expression>(paramRef, paramExtractor));
            return(paramRef);
        }
        private static ColumnDataBase CreateColumnStore(DbType dbType, IUnmanagedAllocator allocator, ColumnDataBase migrated)
        {
            var dataType        = DriverRowData.DeriveSystemType(dbType);
            var columnStoreType = typeof(ColumnData <>).MakeGenericType(dataType);

            return(migrated == null
                       ? (ColumnDataBase)Activator.CreateInstance(columnStoreType, dbType, allocator)
                       : (ColumnDataBase)Activator.CreateInstance(columnStoreType, migrated, allocator));
        }
Exemplo n.º 3
0
        private Action <int, DriverRowData, int> GenerateAssignToDriverRowAction()
        {
            // NOTE: this method assumes that source value is NOT NULL
            // this should be verified by caller

            var docIndex        = Expression.Parameter(typeof(int), "docIndex");
            var rowData         = Expression.Parameter(typeof(DriverRowData), "rowData");
            var fieldArrayIndex = Expression.Parameter(typeof(int), "indexInArray");

            var arrayData   = Expression.Field(Expression.Constant(this), "DataArray");
            var dataBlock   = Expression.Call(arrayData, "GetBlock", null, docIndex);
            var localIndex  = Expression.Call(arrayData, "GetLocalIndex", null, docIndex);
            var dataElement = Expression.ArrayIndex(dataBlock, localIndex);

            Expression dest;
            Expression assign;
            string     subPropName;

            var storageType = DriverRowData.DeriveRepresentationType(DbType);

            switch (storageType)
            {
            case DriverRowData.DataTypeRepresentation.ByteArray:
                dest = Expression.ArrayAccess(Expression.Field(rowData, "BinaryData"), fieldArrayIndex);
                var copyFrom = typeof(SizableArrayOfByte).GetMethod("CopyFrom", new[] { typeof(SizableArrayOfByte) });
                // we assume that DriverRowData always has destination byte array initialized
                assign = Expression.Call(dest, copyFrom, dataElement);
                break;

            case DriverRowData.DataTypeRepresentation.String:
                dest   = Expression.ArrayAccess(Expression.Field(rowData, "StringData"), fieldArrayIndex);
                assign = Expression.Assign(dest, dataElement);
                break;

            case DriverRowData.DataTypeRepresentation.Value8Bytes:
                subPropName = DriverRowData.DeriveSystemType(DbType).Name;
                dest        = Expression.Field(Expression.ArrayAccess(Expression.Field(rowData, "ValueData8Bytes"), fieldArrayIndex), "As" + subPropName);
                assign      = Expression.Assign(dest, dataElement);
                break;

            case DriverRowData.DataTypeRepresentation.Value16Bytes:
                subPropName = DriverRowData.DeriveSystemType(DbType).Name;
                dest        = Expression.Field(Expression.ArrayAccess(Expression.Field(rowData, "ValueData16Bytes"), fieldArrayIndex), "As" + subPropName);
                assign      = Expression.Assign(dest, dataElement);
                break;

            default:
                throw new InvalidOperationException("Invalid value for DbType: " + DbType);
            }

            var lambda = Expression.Lambda(
                Expression.GetActionType(new[] { typeof(int), typeof(DriverRowData), typeof(int) }),
                assign, docIndex, rowData, fieldArrayIndex);

            return((Action <int, DriverRowData, int>)lambda.Compile());
        }
        private static Type MakeNullableType(DbType dbType)
        {
            var underlyingType = DriverRowData.DeriveSystemType(dbType);

            return(underlyingType.IsValueType ? typeof(UnboxableNullable <>).MakeGenericType(underlyingType) : underlyingType);
        }
Exemplo n.º 5
0
        private Action <int, DriverRowData, int> GenerateAssignFromDriverRowAction()
        {
            // NOTE: this method assumes that source value is NOT NULL
            // this should be verified by caller

            var docIndex        = Expression.Parameter(typeof(int), "docIndex");
            var rowData         = Expression.Parameter(typeof(DriverRowData), "rowData");
            var fieldArrayIndex = Expression.Parameter(typeof(int), "indexInArray");

            var        arrayData  = Expression.Field(Expression.Constant(this), "DataArray");
            var        dataBlock  = Expression.Call(arrayData, "GetBlock", null, docIndex);
            var        localIndex = Expression.Call(arrayData, "GetLocalIndex", null, docIndex);
            Expression source;
            Expression assign;
            string     subPropName;

            var storageType = DriverRowData.DeriveRepresentationType(DbType);

            switch (storageType)
            {
            case DriverRowData.DataTypeRepresentation.ByteArray:
                // column data may have this destination element uninitialized yet
                // use Interlocked.CompareExchange when setting its value
                var target = Expression.Variable(typeof(SizableArrayOfByte), "target");

                var initIfNull = Expression.IfThen(
                    Expression.ReferenceEqual(Expression.Constant(null), target),
                    Expression.Block(
                        Expression.Assign(target, Expression.New(typeof(SizableArrayOfByte))),
                        Expression.Call(
                            typeof(Interlocked), "CompareExchange", new [] { typeof(SizableArrayOfByte) },
                            Expression.ArrayIndex(dataBlock, localIndex), target, Expression.Constant(null, typeof(SizableArrayOfByte))))
                    );

                source = Expression.ArrayIndex(Expression.Field(rowData, "BinaryData"), fieldArrayIndex);
                var copyFrom = typeof(SizableArrayOfByte).GetMethod("CopyFrom", new [] { typeof(SizableArrayOfByte) });
                var setter   = Expression.Call(target, copyFrom, source);

                assign = Expression.Block(
                    new [] { target },
                    Expression.Assign(target, Expression.ArrayIndex(dataBlock, localIndex)),
                    initIfNull,
                    setter);
                break;

            case DriverRowData.DataTypeRepresentation.String:
                source = Expression.ArrayIndex(Expression.Field(rowData, "StringData"), fieldArrayIndex);
                assign = Expression.Assign(Expression.ArrayAccess(dataBlock, localIndex), source);
                break;

            case DriverRowData.DataTypeRepresentation.Value8Bytes:
                subPropName = DriverRowData.DeriveSystemType(DbType).Name;
                source      = Expression.Field(Expression.ArrayIndex(Expression.Field(rowData, "ValueData8Bytes"), fieldArrayIndex), "As" + subPropName);
                assign      = Expression.Assign(Expression.ArrayAccess(dataBlock, localIndex), source);
                break;

            case DriverRowData.DataTypeRepresentation.Value16Bytes:
                subPropName = DriverRowData.DeriveSystemType(DbType).Name;
                source      = Expression.Field(Expression.ArrayIndex(Expression.Field(rowData, "ValueData16Bytes"), fieldArrayIndex), "As" + subPropName);
                assign      = Expression.Assign(Expression.ArrayAccess(dataBlock, localIndex), source);
                break;

            default:
                throw new InvalidOperationException("Invalid value for DbType: " + DbType);
            }

            var lambda = Expression.Lambda(
                Expression.GetActionType(new [] { typeof(int), typeof(DriverRowData), typeof(int) }),
                assign, docIndex, rowData, fieldArrayIndex);

            return((Action <int, DriverRowData, int>)lambda.Compile());
        }