use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class CommandContext method close.
public void close() {
synchronized (this.globalState) {
if (this.globalState.reservedBuffers > 0) {
long toRelease = this.globalState.reservedBuffers;
this.globalState.reservedBuffers = 0;
this.globalState.bufferManager.releaseOrphanedBuffers(toRelease);
}
if (this.globalState.reusableExecutions != null) {
for (List<ReusableExecution<?>> reusableExecutions : this.globalState.reusableExecutions.values()) {
for (ReusableExecution<?> reusableExecution : reusableExecutions) {
try {
reusableExecution.dispose();
} catch (Exception e) {
LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30030));
}
}
}
this.globalState.reusableExecutions.clear();
}
if (this.globalState.commandListeners != null) {
for (CommandListener listener : this.globalState.commandListeners) {
try {
listener.commandClosed(this);
} catch (Exception e) {
LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30031));
}
}
this.globalState.commandListeners.clear();
}
if (this.globalState.lookups != null) {
for (TupleSource ts : this.globalState.lookups.values()) {
ts.closeSource();
}
this.globalState.lookups = null;
}
if (this.globalState.created != null) {
for (InputStreamFactory isf : this.globalState.created) {
try {
isf.free();
} catch (IOException e) {
}
}
this.globalState.created.clear();
}
}
}
use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class MySQLExecutionFactory method toGeometryType.
/**
* It appears that mysql will actually return a byte array or a blob backed by a byte array
* but just to be safe we'll assume that there may be true blob and that we should back the
* geometry value with that blob.
* @param val
* @return
* @throws SQLException
*/
GeometryType toGeometryType(final Blob val) throws SQLException {
if (val == null) {
return null;
}
// create a wrapper for that will handle the srid
long length = val.length() - 4;
InputStreamFactory streamFactory = new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
InputStream is;
try {
is = val.getBinaryStream();
} catch (SQLException e) {
throw new IOException(e);
}
for (int i = 0; i < 4; i++) {
is.read();
}
return is;
}
};
// read the little endian srid
InputStream is = val.getBinaryStream();
int srid = 0;
try {
for (int i = 0; i < 4; i++) {
try {
int b = is.read();
srid += (b << i * 8);
} catch (IOException e) {
// could not determine srid
srid = GeometryType.UNKNOWN_SRID;
}
}
} finally {
try {
is.close();
} catch (IOException e) {
// i
}
}
streamFactory.setLength(length);
Blob b = new BlobImpl(streamFactory);
GeometryType geom = new GeometryType(b);
geom.setSrid(srid);
return geom;
}
use of org.teiid.core.types.InputStreamFactory 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.InputStreamFactory 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);
}
}
use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class TestLobManager method testInlining.
@Test
public void testInlining() throws Exception {
BufferManager buffMgr = BufferManagerFactory.getStandaloneBufferManager();
FileStore fs = buffMgr.createFileStore("temp");
ClobType clob = new ClobType(new ClobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return new ReaderInputStream(new StringReader("small"), Charset.forName(Streamable.ENCODING));
}
}, 5));
assertEquals(StorageMode.OTHER, InputStreamFactory.getStorageMode(clob));
LobManager lobManager = new LobManager(new int[] { 0 }, fs);
lobManager.updateReferences(Arrays.asList(clob), ReferenceMode.CREATE);
assertEquals(StorageMode.MEMORY, InputStreamFactory.getStorageMode(clob));
}
Aggregations