Search in sources :

Example 11 with FileStore

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);
    }
}
Also used : FileStore(org.teiid.common.buffer.FileStore) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) BaseLob(org.teiid.core.types.BaseLob) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 12 with FileStore

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;
}
Also used : FileStore(org.teiid.common.buffer.FileStore)

Example 13 with FileStore

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();
        }
    }
}
Also used : ObjectInputStream(java.io.ObjectInputStream) ExtensibleBufferedInputStream(org.teiid.common.buffer.ExtensibleBufferedInputStream) InputStream(java.io.InputStream) ExtensibleBufferedInputStream(org.teiid.common.buffer.ExtensibleBufferedInputStream) IOException(java.io.IOException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) CacheEntry(org.teiid.common.buffer.CacheEntry) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) FileStore(org.teiid.common.buffer.FileStore) TeiidComponentException(org.teiid.core.TeiidComponentException) ObjectInput(java.io.ObjectInput) CacheKey(org.teiid.common.buffer.CacheKey) ObjectInputStream(java.io.ObjectInputStream)

Example 14 with FileStore

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();
        }
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) FileStore(org.teiid.common.buffer.FileStore) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) FilterReader(java.io.FilterReader) Reader(java.io.Reader) BufferManager(org.teiid.common.buffer.BufferManager) ClobImpl(org.teiid.core.types.ClobImpl) Writer(java.io.Writer)

Example 15 with FileStore

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;
}
Also used : FileStore(org.teiid.common.buffer.FileStore) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory)

Aggregations

FileStore (org.teiid.common.buffer.FileStore)24 Test (org.junit.Test)14 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)7 InputStream (java.io.InputStream)5 IOException (java.io.IOException)4 FileStoreOutputStream (org.teiid.common.buffer.FileStore.FileStoreOutputStream)4 TeiidComponentException (org.teiid.core.TeiidComponentException)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3 Reader (java.io.Reader)2 Writer (java.io.Writer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FilterReader (java.io.FilterReader)1 ObjectInput (java.io.ObjectInput)1 ObjectInputStream (java.io.ObjectInputStream)1 OutputStream (java.io.OutputStream)1 SQLException (java.sql.SQLException)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 DataSource (javax.activation.DataSource)1