use of org.teiid.core.types.BlobType in project teiid by teiid.
the class TestConnectorWorkItem method testTypeConversion.
@Test
public void testTypeConversion() throws Exception {
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
String str = "hello world";
Object source = new StreamSource(new StringReader(str));
XMLType xml = (XMLType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.XML, null);
assertEquals(str, xml.getString());
source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a/>")));
xml = (XMLType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.XML, null);
XMLInputFactory in = XMLType.getXmlInputFactory();
XMLStreamReader reader = in.createXMLStreamReader(new StringReader(xml.getString()));
assertEquals(XMLEvent.START_DOCUMENT, reader.getEventType());
assertEquals(XMLEvent.START_ELEMENT, reader.next());
assertEquals("a", reader.getLocalName());
assertEquals(XMLEvent.END_ELEMENT, reader.next());
byte[] bytes = str.getBytes(Streamable.ENCODING);
source = new InputStreamFactory.BlobInputStreamFactory(BlobType.createBlob(bytes));
BlobType blob = (BlobType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.BLOB, null);
assertArrayEquals(bytes, ObjectConverterUtil.convertToByteArray(blob.getBinaryStream()));
}
use of org.teiid.core.types.BlobType in project teiid by teiid.
the class TeiidServiceHandler method handleLobResult.
private void handleLobResult(String charSet, Object result, ServiceResponse response) throws SQLException {
if (result == null) {
// or should this be an empty result?
return;
}
if (result instanceof SQLXML) {
if (charSet != null) {
XMLSerialize serialize = new XMLSerialize();
// $NON-NLS-1$
serialize.setTypeString("blob");
serialize.setDeclaration(true);
serialize.setEncoding(charSet);
serialize.setDocument(true);
try {
InputStream content = ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result))).getBinaryStream();
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
} catch (TransformationException e) {
throw new SQLException(e);
}
} else {
InputStream content = ((SQLXML) result).getBinaryStream();
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_XML);
}
} else if (result instanceof Blob) {
InputStream content = ((Blob) result).getBinaryStream();
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
} else if (result instanceof Clob) {
InputStream content = new ReaderInputStream(((Clob) result).getCharacterStream(), charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
response.writeContent(content, 200, false);
response.writeOK(ContentType.TEXT_PLAIN);
} else {
InputStream content = new ByteArrayInputStream(result.toString().getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
}
}
use of org.teiid.core.types.BlobType 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.BlobType in project teiid by teiid.
the class MongoDBDirectQueryExecution method next.
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
final DBObject value = nextRow();
if (value == null) {
return null;
}
BlobType result = new BlobType(new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(JSON.serialize(value).getBytes(Streamable.CHARSET));
}
}));
if (returnsArray) {
List<Object[]> row = new ArrayList<Object[]>(1);
row.add(new Object[] { result });
return row;
}
return Arrays.asList(result);
}
use of org.teiid.core.types.BlobType in project teiid by teiid.
the class LobWorkItem method createLobStream.
/**
* Create a object which can create a sequence of LobChunk objects on a given
* LOB object
* @throws SQLException
*/
private ByteLobChunkStream createLobStream(String referenceStreamId) throws TeiidComponentException, SQLException {
// get the reference object in the buffer manager, and try to stream off
// the original sources.
Streamable<?> streamable = parent.resultsBuffer.getLobReference(referenceStreamId);
if (streamable instanceof XMLType) {
XMLType xml = (XMLType) streamable;
return new ByteLobChunkStream(xml.getBinaryStream(), chunkSize);
} else if (streamable instanceof ClobType) {
ClobType clob = (ClobType) streamable;
return new ByteLobChunkStream(new ReaderInputStream(clob.getCharacterStream(), Charset.forName(Streamable.ENCODING)), chunkSize);
}
BlobType blob = (BlobType) streamable;
return new ByteLobChunkStream(blob.getBinaryStream(), chunkSize);
}
Aggregations