use of org.teiid.core.types.BlobImpl in project teiid by teiid.
the class AccumuloDataTypeManager method deserialize.
public static Object deserialize(final byte[] value, final Class<?> expectedType) {
if (value == null || Arrays.equals(value, EMPTY_BYTES)) {
return null;
}
try {
if (expectedType.isAssignableFrom(Clob.class)) {
return new ClobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
}, -1);
} else if (expectedType.isAssignableFrom(Blob.class)) {
return new BlobType(new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
}));
} else if (expectedType.isAssignableFrom(SQLXML.class)) {
return new SQLXMLImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
});
} else if (expectedType.isAssignableFrom(BinaryType.class)) {
return new BinaryType(value);
} else if (expectedType.isAssignableFrom(GeometryType.class)) {
GeometryType result = new GeometryType(Arrays.copyOf(value, value.length - 4));
int srid = (((value[value.length - 4] & 0xff) << 24) + ((value[value.length - 3] & 0xff) << 16) + ((value[value.length - 2] & 0xff) << 8) + ((value[value.length - 1] & 0xff) << 0));
result.setSrid(srid);
return result;
} else if (expectedType.isAssignableFrom(byte[].class)) {
return value;
} else if (expectedType.isAssignableFrom(String.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Byte.class) || expectedType.isAssignableFrom(Short.class) || expectedType.isAssignableFrom(Character.class) || expectedType.isAssignableFrom(Integer.class) || expectedType.isAssignableFrom(Long.class) || expectedType.isAssignableFrom(BigInteger.class) || expectedType.isAssignableFrom(BigDecimal.class) || expectedType.isAssignableFrom(Float.class) || expectedType.isAssignableFrom(Double.class) || expectedType.isAssignableFrom(Date.class) || expectedType.isAssignableFrom(Time.class) || expectedType.isAssignableFrom(Timestamp.class)) {
return DataTypeManager.transformValue(new String(value, UTF_8), expectedType);
} else {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(value));
Object obj = ois.readObject();
ois.close();
return obj;
}
} catch (ClassNotFoundException e) {
throw new TeiidRuntimeException(e);
} catch (IOException e) {
throw new TeiidRuntimeException(e);
} catch (TransformationException e) {
throw new TeiidRuntimeException(e);
}
}
use of org.teiid.core.types.BlobImpl in project teiid by teiid.
the class FunctionMethods method toBytes.
@TeiidFunction(category = FunctionCategoryConstants.CONVERSION, name = "to_bytes")
public static BlobType toBytes(ClobType value, String encoding, boolean wellFormed) throws IOException, SQLException {
Charset cs = getCharset(encoding);
ClobInputStreamFactory cisf = new ClobInputStreamFactory(value.getReference());
cisf.setCharset(cs);
if (!wellFormed || CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) || CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) {
// validate that the binary conversion is possible
// TODO: cache the result in a filestore
InputStream is = new ReaderInputStream(value.getCharacterStream(), cs.newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT));
try {
while (is.read() != -1) {
}
} catch (IOException e) {
CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class);
if (cce != null) {
throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10083, cs.displayName()), cce);
}
throw e;
} finally {
is.close();
}
}
return new BlobType(new BlobImpl(cisf));
}
use of org.teiid.core.types.BlobImpl in project teiid by teiid.
the class GeometryUtils method geometryToEwkb.
/**
* We'll take the wkb format and add the extended flag/srid
* @param geometry
* @return
*/
public static BlobType geometryToEwkb(final GeometryType geometry) {
final Blob b = geometry.getReference();
BlobImpl blobImpl = new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
PushbackInputStream pbis;
try {
pbis = new PushbackInputStream(b.getBinaryStream(), 9);
} catch (SQLException e) {
throw new IOException(e);
}
int byteOrder = pbis.read();
if (byteOrder == -1) {
return pbis;
}
byte[] typeInt = new byte[4];
int bytesRead = pbis.read(typeInt);
if (bytesRead == 4) {
int srid = geometry.getSrid();
byte[] sridInt = new byte[4];
ByteOrderValues.putInt(srid, sridInt, byteOrder == 0 ? ByteOrderValues.BIG_ENDIAN : ByteOrderValues.LITTLE_ENDIAN);
pbis.unread(sridInt);
typeInt[byteOrder == 0 ? 0 : 3] |= 0x20;
}
pbis.unread(typeInt, 0, bytesRead);
pbis.unread(byteOrder);
return pbis;
}
});
return new BlobType(blobImpl);
}
use of org.teiid.core.types.BlobImpl 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.BlobImpl 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;
}
Aggregations