use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class TestObjectDecoderInputStream method testReplaceObject.
@Test
public void testReplaceObject() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectEncoderOutputStream out = new ObjectEncoderOutputStream(new DataOutputStream(baos), 512);
ClobImpl clob = new ClobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
// $NON-NLS-1$
return new ReaderInputStream(new StringReader("Clob contents"), Charset.forName(Streamable.ENCODING));
}
}, -1);
out.writeObject(clob);
ObjectDecoderInputStream in = new ObjectDecoderInputStream(new AccessibleBufferedInputStream(new ByteArrayInputStream(baos.toByteArray()), 1024), Thread.currentThread().getContextClassLoader(), 1024);
Object result = in.readObject();
assertTrue(result instanceof ClobImpl);
}
use of org.teiid.core.types.InputStreamFactory 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);
}
use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class LobManager method persistLob.
public static void persistLob(final Streamable<?> lob, final FileStore store, byte[] bytes, boolean inlineLobs, int maxMemoryBytes) throws TeiidComponentException {
long byteLength = Integer.MAX_VALUE;
try {
byteLength = lob.length() * (lob instanceof ClobType ? 2 : 1);
} catch (SQLException e) {
// just ignore for now - for a single read resource computing the length invalidates
// TODO - inline small persisted lobs
}
try {
// inline
if (lob.getReferenceStreamId() == null || (inlineLobs && (byteLength <= maxMemoryBytes))) {
lob.setReferenceStreamId(null);
if (InputStreamFactory.getStorageMode(lob) == StorageMode.MEMORY) {
return;
}
if (lob instanceof BlobType) {
BlobType b = (BlobType) lob;
byte[] blobBytes = b.getBytes(1, (int) byteLength);
b.setReference(new SerialBlob(blobBytes));
} else if (lob instanceof ClobType) {
ClobType c = (ClobType) lob;
// $NON-NLS-1$
String s = "";
// some clob impls return null for 0 length
if (byteLength != 0) {
s = c.getSubString(1, (int) (byteLength >>> 1));
}
c.setReference(new ClobImpl(s));
} else {
XMLType x = (XMLType) lob;
String s = x.getString();
x.setReference(new SQLXMLImpl(s));
}
return;
}
InputStream is = null;
if (lob instanceof BlobType) {
is = new BlobInputStreamFactory((Blob) lob).getInputStream();
} else if (lob instanceof ClobType) {
is = new ClobInputStreamFactory((Clob) lob).getInputStream();
} else {
is = new SQLXMLInputStreamFactory((SQLXML) lob).getInputStream();
}
long offset = store.getLength();
OutputStream fsos = store.createOutputStream();
byteLength = ObjectConverterUtil.write(fsos, is, bytes, -1);
// re-construct the new lobs based on the file store
final long lobOffset = offset;
final long lobLength = byteLength;
/*
* Using an inner class here will hold a reference to the LobManager
* which prevents the removal of the FileStore until all of the
* lobs have been gc'd
*/
InputStreamFactory isf = new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return store.createInputStream(lobOffset, lobLength);
}
@Override
public StorageMode getStorageMode() {
return StorageMode.PERSISTENT;
}
};
isf.setLength(byteLength);
if (lob instanceof BlobType) {
((BlobType) lob).setReference(new BlobImpl(isf));
} else if (lob instanceof ClobType) {
long length = -1;
try {
length = ((ClobType) lob).length();
} catch (SQLException e) {
// could be streaming
}
((ClobType) lob).setReference(new ClobImpl(isf, length));
} else {
((XMLType) lob).setReference(new SQLXMLImpl(isf));
}
} catch (SQLException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30037, e);
} catch (IOException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30036, e);
}
}
use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class MongoDBExecutionFactory method retrieveValue.
/**
* @param field
* @param expectedClass
* @return
* @throws TranslatorException
*/
public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException {
if (value == null) {
return null;
}
if (value.getClass().equals(expectedClass)) {
return value;
}
if (value instanceof DBRef) {
Object obj = ((DBRef) value).getId();
if (obj instanceof BasicDBObject) {
BasicDBObject bdb = (BasicDBObject) obj;
return bdb.get(colName);
}
return obj;
} else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) {
return new java.sql.Date(((java.util.Date) value).getTime());
} else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) {
return new java.sql.Timestamp(((java.util.Date) value).getTime());
} else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) {
return new java.sql.Time(((java.util.Date) value).getTime());
} else if (value instanceof String && expectedClass.equals(BigDecimal.class)) {
return new BigDecimal((String) value);
} else if (value instanceof String && expectedClass.equals(BigInteger.class)) {
return new BigInteger((String) value);
} else if (value instanceof String && expectedClass.equals(Character.class)) {
return new Character(((String) value).charAt(0));
} else if (value instanceof String && expectedClass.equals(BinaryType.class)) {
return new BinaryType(((String) value).getBytes());
} else if (value instanceof String && expectedClass.equals(Blob.class)) {
GridFS gfs = new GridFS(mongoDB, fqn);
final GridFSDBFile resource = gfs.findOne((String) value);
if (resource == null) {
return null;
}
return new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return resource.getInputStream();
}
});
} else if (value instanceof String && expectedClass.equals(Clob.class)) {
GridFS gfs = new GridFS(mongoDB, fqn);
final GridFSDBFile resource = gfs.findOne((String) value);
if (resource == null) {
return null;
}
return new ClobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return resource.getInputStream();
}
}, -1);
} else if (value instanceof String && expectedClass.equals(SQLXML.class)) {
GridFS gfs = new GridFS(mongoDB, fqn);
final GridFSDBFile resource = gfs.findOne((String) value);
if (resource == null) {
return null;
}
return new SQLXMLImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return resource.getInputStream();
}
});
} else if (value instanceof BasicDBList) {
BasicDBList arrayValues = (BasicDBList) value;
// array
if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) {
Class arrayType = expectedClass.getComponentType();
Object array = Array.newInstance(arrayType, arrayValues.size());
for (int i = 0; i < arrayValues.size(); i++) {
Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName);
Array.set(array, i, arrayItem);
}
value = array;
}
} else if (value instanceof org.bson.types.ObjectId) {
org.bson.types.ObjectId id = (org.bson.types.ObjectId) value;
value = id.toHexString();
} else {
Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass);
if (transform != null) {
try {
value = transform.transform(value, expectedClass);
} catch (TransformationException e) {
throw new TranslatorException(e);
}
}
}
return value;
}
use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class WSProcedureExecution method getOutputParameterValues.
@Override
public List<?> getOutputParameterValues() throws TranslatorException {
Object result = returnValue;
if (returnValue != null && (returnValue instanceof StAXSource) && procedure.getArguments().size() > 4 && procedure.getArguments().get(4).getDirection() == Direction.IN && Boolean.TRUE.equals(procedure.getArguments().get(4).getArgumentValue().getValue())) {
SQLXMLImpl sqlXml = new StAXSQLXML((StAXSource) returnValue);
XMLType xml = new XMLType(sqlXml);
xml.setType(Type.DOCUMENT);
result = xml;
} else if (returnValue != null && returnValue instanceof DOMSource) {
final DOMSource xmlSource = (DOMSource) returnValue;
SQLXMLImpl sqlXml = new SQLXMLImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Result outputTarget = new StreamResult(outputStream);
try {
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
} catch (Exception e) {
throw new IOException(e);
}
return new ByteArrayInputStream(outputStream.toByteArray());
}
});
XMLType xml = new XMLType(sqlXml);
xml.setType(Type.DOCUMENT);
result = xml;
}
return Arrays.asList(result);
}
Aggregations