use of org.teiid.common.buffer.FileStore 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.common.buffer.FileStore in project teiid by teiid.
the class BlockStore method writeToStorageBlock.
int writeToStorageBlock(PhysicalInfo info, InputStream is) throws IOException {
int block = getAndSetNextClearBit(info);
int segment = block / blocksInUse.getBitsPerSegment();
boolean success = false;
this.locks[segment].writeLock().lock();
try {
FileStore fs = stores[segment];
long blockOffset = (block % blocksInUse.getBitsPerSegment()) * blockSize;
// TODO: there is still an extra buffer being created here, we could FileChannels to do better
byte[] b = new byte[BufferFrontedFileStoreCache.BLOCK_SIZE];
int read = 0;
long newLength = blockOffset + blockSize;
if (fs.getLength() < newLength) {
// grow by whole blocks
// TODO: could pad the growth
fs.setLength(newLength);
}
while ((read = is.read(b, 0, b.length)) != -1) {
fs.write(blockOffset, b, 0, read);
blockOffset += read;
}
success = true;
} finally {
locks[segment].writeLock().unlock();
if (!success) {
blocksInUse.clear(block);
block = BufferFrontedFileStoreCache.EMPTY_ADDRESS;
}
}
return block;
}
use of org.teiid.common.buffer.FileStore in project teiid by teiid.
the class BufferFrontedFileStoreCache method get.
@Override
public CacheEntry get(PhysicalInfo info, Long oid, WeakReference<? extends Serializer<?>> ref) throws TeiidComponentException {
if (info == null) {
return null;
}
Serializer<?> serializer = ref.get();
if (serializer == null) {
return null;
}
readAttempts.incrementAndGet();
InputStream is = null;
Lock lock = null;
ExtensibleBufferedInputStream eis = null;
int memoryBlocks = 0;
try {
synchronized (info) {
// load should be locked
assert !info.pinned && info.loading;
// not necessary, but should make things safer
info.await(true, false);
if (info.inode != EMPTY_ADDRESS) {
info.pinned = true;
memoryBufferEntries.touch(info);
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_BUFFER_MGR, MessageLevel.DETAIL)) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_BUFFER_MGR, "Getting object at inode", info.inode, serializer.getId(), oid);
}
BlockManager manager = getBlockManager(serializer.getId(), oid, info.inode);
is = new BlockInputStream(manager, info.memoryBlockCount);
} else if (info.block != EMPTY_ADDRESS) {
info.pinned = true;
memoryBufferEntries.recordAccess(info);
storageReads.incrementAndGet();
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_BUFFER_MGR, MessageLevel.DETAIL)) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_BUFFER_MGR, "Getting object at block", info.block, info.sizeIndex, serializer.getId(), oid);
}
BlockStore blockStore = sizeBasedStores[info.sizeIndex];
int segment = info.block / blockStore.blocksInUse.getBitsPerSegment();
FileStore fs = blockStore.stores[segment];
long blockOffset = (info.block % blockStore.blocksInUse.getBitsPerSegment()) * blockStore.blockSize;
eis = fs.createInputStream(blockOffset, info.memoryBlockCount << LOG_BLOCK_SIZE);
lock = blockStore.locks[segment].writeLock();
memoryBlocks = info.memoryBlockCount;
} else {
return null;
}
}
if (lock != null) {
is = readIntoMemory(info, eis, lock, memoryBlocks);
}
for (int i = 0; i < HEADER_BYTES; i++) {
is.read();
}
ObjectInput dis = new ObjectInputStream(is);
CacheEntry ce = new CacheEntry(new CacheKey(oid, 1, 1), info.sizeEstimate, serializer.deserialize(dis), ref, true);
return ce;
} catch (IOException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30048, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30048, info.gid, oid));
} catch (ClassNotFoundException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30048, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30048, info.gid, oid));
} catch (InterruptedException e) {
throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30049, e);
} finally {
synchronized (info) {
info.pinned = false;
info.notifyAll();
}
}
}
use of org.teiid.common.buffer.FileStore in project teiid by teiid.
the class FunctionMethods method concat.
public static ClobType concat(CommandContext context, ClobType str1, ClobType str2) throws IOException, SQLException {
BufferManager bm = context.getBufferManager();
// $NON-NLS-1$
FileStore fs = bm.createFileStore("clob");
FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
boolean remove = true;
try (Reader characterStream = str1.getCharacterStream();
Reader characterStream2 = str2.getCharacterStream()) {
Writer writer = fsisf.getWriter();
int chars = ObjectConverterUtil.write(writer, characterStream, -1, false);
chars += ObjectConverterUtil.write(writer, characterStream2, -1, false);
writer.close();
if (fsisf.getStorageMode() == StorageMode.MEMORY) {
// detach if just in memory
byte[] bytes = fsisf.getMemoryBytes();
return new ClobType(new ClobImpl(new String(bytes, Streamable.ENCODING)));
}
remove = false;
context.addCreatedLob(fsisf);
return new ClobType(new ClobImpl(fsisf, chars));
} finally {
if (remove) {
fs.remove();
}
}
}
use of org.teiid.common.buffer.FileStore in project teiid by teiid.
the class StringAgg method buildResult.
private FileStoreInputStreamFactory buildResult(CommandContext context) {
// $NON-NLS-1$
FileStore fs = context.getBufferManager().createFileStore("string_agg");
FileStoreInputStreamFactory fisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
return fisf;
}
Aggregations