Search in sources :

Example 1 with BaseLob

use of org.teiid.core.types.BaseLob in project teiid by teiid.

the class SizeUtility method getSize.

/**
 * Get size of object
 * @return Size in bytes
 */
public static long getSize(Object obj, boolean accountForValueCache) {
    if (obj == null) {
        return 0;
    }
    Class<? extends Object> clazz = obj.getClass();
    if (clazz == DataTypeManager.DefaultDataClasses.STRING) {
        int length = ((String) obj).length();
        if (length > 0) {
            return alignMemory(40 + (2 * length));
        }
        return 40;
    } else if (clazz == DataTypeManager.DefaultDataClasses.VARBINARY) {
        int length = ((BinaryType) obj).getLength();
        if (length > 0) {
            return alignMemory(16 + length);
        }
        return 16;
    } else if (clazz == DataTypeManager.DefaultDataClasses.BIG_DECIMAL) {
        int bitLength = ((BigDecimal) obj).unscaledValue().bitLength();
        // TODO: this does not account for the possibility of a cached string
        long result = 88 + alignMemory(4 + (bitLength >> 3));
        return result;
    } else if (clazz == DataTypeManager.DefaultDataClasses.BIG_INTEGER) {
        int bitLength = ((BigInteger) obj).bitLength();
        long result = 40 + alignMemory(4 + (bitLength >> 3));
        return result;
    } else if (obj instanceof Iterable<?>) {
        Iterable<?> i = (Iterable<?>) obj;
        long total = 16;
        for (Object object : i) {
            total += getSize(object, false) + REFERENCE_SIZE;
        }
        return total;
    } else if (clazz.isArray() || obj instanceof ArrayImpl) {
        int overhead = 0;
        if (obj instanceof ArrayImpl) {
            obj = ((ArrayImpl) obj).getValues();
            clazz = obj.getClass();
            overhead += 2 * REFERENCE_SIZE;
        }
        Class<?> componentType = clazz.getComponentType();
        if (!componentType.isPrimitive()) {
            Object[] rows = (Object[]) obj;
            // Array overhead
            long total = overhead + 16 + alignMemory(rows.length * REFERENCE_SIZE);
            for (int i = 0; i < rows.length; i++) {
                total += getSize(rows[i], false);
            }
            return total;
        }
        int length = Array.getLength(obj);
        int primitiveSize = 8;
        if (componentType == boolean.class) {
            primitiveSize = 4;
        } else if (componentType == byte.class) {
            primitiveSize = 1;
        } else if (componentType == short.class) {
            primitiveSize = 2;
        } else if (componentType == int.class || componentType == float.class) {
            primitiveSize = 4;
        }
        return overhead + alignMemory(length * primitiveSize) + 16;
    } else if (obj instanceof Streamable<?>) {
        try {
            Streamable<?> s = (Streamable) obj;
            Object o = s.getReference();
            if (o instanceof BaseLob) {
                InputStreamFactory isf = ((BaseLob) o).getStreamFactory();
                if (isf.getStorageMode() == StorageMode.MEMORY) {
                    long length = isf.getLength();
                    if (length >= 0) {
                        return 40 + alignMemory(length);
                    }
                } else if (isf.getStorageMode() == StorageMode.PERSISTENT) {
                    long length = isf.getLength();
                    return 40 + alignMemory(Math.min(DataTypeManager.MAX_LOB_MEMORY_BYTES, length));
                }
            }
        } catch (Exception e) {
        }
    } else {
        if (SIZE_ESTIMATES.containsKey(clazz)) {
            return getSize(accountForValueCache, clazz);
        }
        // assume we can get a plausable estimate from the serialized size
        if (obj instanceof Serializable) {
            // we're ignoring classloader differences here
            ClassStats stats = objectEstimates.get(clazz.getName());
            if (stats == null) {
                stats = new ClassStats();
                objectEstimates.put(clazz.getName(), stats);
            }
            int samples = stats.samples.getAndIncrement();
            if (samples < 1000 || (samples & 1023) == 1023) {
                try {
                    DummyOutputStream os = new DummyOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os) {

                        @Override
                        protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
                        }

                        @Override
                        protected void writeStreamHeader() throws IOException {
                        }
                    };
                    oos.writeObject(obj);
                    oos.close();
                    int result = (int) alignMemory(os.getBytes() * 3);
                    if (result > stats.averageSize) {
                        stats.averageSize = (stats.averageSize + result * 2) / 3;
                    } else {
                        stats.averageSize = (stats.averageSize + result) / 2;
                    }
                    return result;
                } catch (Exception e) {
                }
            }
            return stats.averageSize;
        }
    }
    return getSize(accountForValueCache, clazz);
}
Also used : Serializable(java.io.Serializable) ArrayImpl(org.teiid.core.types.ArrayImpl) BaseLob(org.teiid.core.types.BaseLob) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) Streamable(org.teiid.core.types.Streamable) ObjectStreamClass(java.io.ObjectStreamClass)

Example 2 with BaseLob

use of org.teiid.core.types.BaseLob in project teiid by teiid.

the class PreparedStatementRequest method resolveParameterValues.

/**
 * @param params
 * @param values
 * @throws QueryResolverException
 * @throws QueryValidatorException
 */
public static void resolveParameterValues(List<Reference> params, List values, CommandContext context, QueryMetadataInterface metadata) throws QueryResolverException, TeiidComponentException, QueryValidatorException {
    VariableContext result = new VariableContext();
    // the size of the values must be the same as that of the parameters
    if (params.size() != values.size()) {
        // $NON-NLS-1$
        String msg = QueryPlugin.Util.getString("QueryUtil.wrong_number_of_values", new Object[] { new Integer(values.size()), new Integer(params.size()) });
        throw new QueryResolverException(QueryPlugin.Event.TEIID30556, msg);
    }
    boolean embedded = context != null && context.getSession() != null && context.getSession().isEmbedded();
    // to that of the reference
    for (int i = 0; i < params.size(); i++) {
        Reference param = params.get(i);
        Object value = values.get(i);
        if (value != null) {
            if (embedded && value instanceof BaseLob) {
                createStreamCopy(context, i, param, value);
            }
            try {
                String targetTypeName = DataTypeManager.getDataTypeName(param.getType());
                Expression expr = ResolverUtil.convertExpression(new Constant(DataTypeManager.convertToRuntimeType(value, param.getType() != DataTypeManager.DefaultDataClasses.OBJECT)), targetTypeName, metadata);
                value = Evaluator.evaluate(expr);
            } catch (ExpressionEvaluationException e) {
                // $NON-NLS-1$
                String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
                throw new QueryResolverException(QueryPlugin.Event.TEIID30557, e, msg);
            } catch (QueryResolverException e) {
                // $NON-NLS-1$
                String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
                throw new QueryResolverException(QueryPlugin.Event.TEIID30558, e, msg);
            }
        }
        if (param.getConstraint() != null) {
            param.getConstraint().validate(value);
        }
        // bind variable
        result.setGlobalValue(param.getContextSymbol(), value);
    }
    context.setVariableContext(result);
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) Expression(org.teiid.query.sql.symbol.Expression) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) BaseLob(org.teiid.core.types.BaseLob) VariableContext(org.teiid.query.sql.util.VariableContext) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 3 with BaseLob

use of org.teiid.core.types.BaseLob in project teiid by teiid.

the class PreparedStatementRequest method createStreamCopy.

/**
 * embedded lobs can be sent with just a reference to a stream,
 * create a copy instead
 * @param context
 * @param i
 * @param param
 * @param value
 * @throws QueryResolverException
 */
private static void createStreamCopy(CommandContext context, int i, Reference param, Object value) throws QueryResolverException {
    try {
        InputStreamFactory isf = ((BaseLob) value).getStreamFactory();
        InputStream initial = isf.getInputStream();
        InputStream other = isf.getInputStream();
        if (initial == other) {
            // this violates the expectation that the inputstream is a new instance,
            // $NON-NLS-1$
            FileStore fs = context.getBufferManager().createFileStore("bytes");
            FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
            SaveOnReadInputStream is = new SaveOnReadInputStream(initial, fsisf);
            context.addCreatedLob(fsisf);
            ((BaseLob) value).setStreamFactory(is.getInputStreamFactory());
        } else {
            initial.close();
            other.close();
        }
    } catch (SQLException e) {
        // $NON-NLS-1$
        String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
        throw new QueryResolverException(QueryPlugin.Event.TEIID30557, e, msg);
    } catch (IOException e) {
        // $NON-NLS-1$
        String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
        throw new QueryResolverException(QueryPlugin.Event.TEIID30557, e, msg);
    }
}
Also used : FileStore(org.teiid.common.buffer.FileStore) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) BaseLob(org.teiid.core.types.BaseLob) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Aggregations

BaseLob (org.teiid.core.types.BaseLob)3 IOException (java.io.IOException)2 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)2 InputStreamFactory (org.teiid.core.types.InputStreamFactory)2 InputStream (java.io.InputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ObjectStreamClass (java.io.ObjectStreamClass)1 Serializable (java.io.Serializable)1 SQLException (java.sql.SQLException)1 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)1 FileStore (org.teiid.common.buffer.FileStore)1 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)1 ArrayImpl (org.teiid.core.types.ArrayImpl)1 Streamable (org.teiid.core.types.Streamable)1 Constant (org.teiid.query.sql.symbol.Constant)1 Expression (org.teiid.query.sql.symbol.Expression)1 Reference (org.teiid.query.sql.symbol.Reference)1 VariableContext (org.teiid.query.sql.util.VariableContext)1