Search in sources :

Example 6 with FileStore

use of org.teiid.common.buffer.FileStore in project teiid by teiid.

the class TestFileStorageManager method testClose.

@Test
public void testClose() throws Exception {
    FileStorageManager sm = getStorageManager(null, null);
    FileStore store = sm.createFileStore("0");
    FileStoreOutputStream fsos = store.createOutputStream(2);
    fsos.write(new byte[100000]);
    fsos.close();
    fsos.close();
}
Also used : FileStore(org.teiid.common.buffer.FileStore) FileStoreOutputStream(org.teiid.common.buffer.FileStore.FileStoreOutputStream) Test(org.junit.Test)

Example 7 with FileStore

use of org.teiid.common.buffer.FileStore in project teiid by teiid.

the class TestFileStorageManager method testPositionalWrite.

@Test
public void testPositionalWrite() throws Exception {
    FileStorageManager sm = getStorageManager(null, null);
    // $NON-NLS-1$
    String tsID = "0";
    FileStore store = sm.createFileStore(tsID);
    byte[] expectedBytes = writeBytes(store, 2048);
    assertEquals(4096, sm.getUsedBufferSpace());
    writeBytes(store, 4096);
    assertEquals(6144, sm.getUsedBufferSpace());
    byte[] bytesRead = new byte[2048];
    store.readFully(2048, bytesRead, 0, bytesRead.length);
    assertArrayEquals(expectedBytes, bytesRead);
    store.remove();
    assertEquals(0, sm.getUsedBufferSpace());
}
Also used : FileStore(org.teiid.common.buffer.FileStore) Test(org.junit.Test)

Example 8 with FileStore

use of org.teiid.common.buffer.FileStore in project teiid by teiid.

the class TestSplittableStorageManager method testTruncate.

@Test
public void testTruncate() throws Exception {
    MemoryStorageManager msm = new MemoryStorageManager();
    SplittableStorageManager ssm = new SplittableStorageManager(msm);
    ssm.setMaxFileSizeDirect(2048);
    // $NON-NLS-1$
    String tsID = "0";
    // Add one batch
    FileStore store = ssm.createFileStore(tsID);
    TestFileStorageManager.writeBytes(store);
    assertEquals(1, msm.getCreated());
    TestFileStorageManager.writeBytes(store);
    assertEquals(2, msm.getCreated());
    store.setLength(100);
    assertEquals(1, msm.getRemoved());
}
Also used : FileStore(org.teiid.common.buffer.FileStore) Test(org.junit.Test)

Example 9 with FileStore

use of org.teiid.common.buffer.FileStore in project teiid by teiid.

the class TestSplittableStorageManager method testCreatesSpillFiles.

@Test
public void testCreatesSpillFiles() throws Exception {
    MemoryStorageManager msm = new MemoryStorageManager();
    SplittableStorageManager ssm = new SplittableStorageManager(msm);
    ssm.setMaxFileSizeDirect(2048);
    // $NON-NLS-1$
    String tsID = "0";
    // Add one batch
    FileStore store = ssm.createFileStore(tsID);
    TestFileStorageManager.writeBytes(store);
    assertEquals(1, msm.getCreated());
    TestFileStorageManager.writeBytes(store);
    assertEquals(2, msm.getCreated());
    store.setLength(10000);
    assertEquals(5, msm.getCreated());
    store.setLength(100);
    assertEquals(4, msm.getRemoved());
    store.remove();
    assertEquals(5, msm.getRemoved());
}
Also used : FileStore(org.teiid.common.buffer.FileStore) Test(org.junit.Test)

Example 10 with FileStore

use of org.teiid.common.buffer.FileStore in project teiid by teiid.

the class ConnectorWorkItem method convertToRuntimeType.

static Object convertToRuntimeType(BufferManager bm, Object value, Class<?> desiredType, CommandContext context) throws TransformationException {
    if (desiredType != DataTypeManager.DefaultDataClasses.XML || !(value instanceof Source)) {
        if (value instanceof DataSource) {
            final DataSource ds = (DataSource) value;
            try {
                // Teiid uses the datasource interface in a degenerate way that
                // reuses the stream, so we test for that here
                InputStream initial = ds.getInputStream();
                InputStream other = null;
                try {
                    other = ds.getInputStream();
                } catch (IOException e) {
                // likely streaming
                }
                if (other != null && initial != other) {
                    initial.close();
                    other.close();
                    if (value instanceof InputStreamFactory) {
                        return asLob((InputStreamFactory) value, desiredType);
                    }
                    return asLob(new InputStreamFactory() {

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return ds.getInputStream();
                        }
                    }, desiredType);
                }
                // $NON-NLS-1$
                FileStore fs = bm.createFileStore("bytes");
                // TODO: guess at the encoding from the content type
                FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                SaveOnReadInputStream is = new SaveOnReadInputStream(initial, fsisf);
                if (context != null) {
                    context.addCreatedLob(fsisf);
                }
                return asLob(is.getInputStreamFactory(), desiredType);
            } catch (IOException e) {
                throw new TransformationException(QueryPlugin.Event.TEIID30500, e, e.getMessage());
            }
        }
        if (value instanceof InputStreamFactory) {
            return asLob((InputStreamFactory) value, desiredType);
        }
        if (value instanceof GeometryInputSource) {
            GeometryInputSource gis = (GeometryInputSource) value;
            try {
                InputStream is = gis.getEwkb();
                if (is != null) {
                    return GeometryUtils.geometryFromEwkb(is, gis.getSrid());
                }
            } catch (Exception e) {
                throw new TransformationException(e);
            }
            try {
                Reader r = gis.getGml();
                if (r != null) {
                    return GeometryUtils.geometryFromGml(r, gis.getSrid());
                }
            } catch (Exception e) {
                throw new TransformationException(e);
            }
        }
    }
    if (value instanceof Source) {
        if (!(value instanceof InputStreamFactory)) {
            if (value instanceof StreamSource) {
                StreamSource ss = (StreamSource) value;
                InputStream is = ss.getInputStream();
                Reader r = ss.getReader();
                if (is == null && r != null) {
                    is = new ReaderInputStream(r, Streamable.CHARSET);
                }
                // $NON-NLS-1$
                final FileStore fs = bm.createFileStore("xml");
                final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                value = new SaveOnReadInputStream(is, fsisf).getInputStreamFactory();
                if (context != null) {
                    context.addCreatedLob(fsisf);
                }
            } else if (value instanceof StAXSource) {
                // TODO: do this lazily.  if the first access to get the STaXSource, then
                // it's more efficient to let the processing happen against STaX
                StAXSource ss = (StAXSource) value;
                try {
                    // $NON-NLS-1$
                    final FileStore fs = bm.createFileStore("xml");
                    final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                    value = new SaveOnReadInputStream(new XMLInputStream(ss, XMLSystemFunctions.getOutputFactory(true)), fsisf).getInputStreamFactory();
                    if (context != null) {
                        context.addCreatedLob(fsisf);
                    }
                } catch (XMLStreamException e) {
                    throw new TransformationException(e);
                }
            } else {
                // maybe dom or some other source we want to get out of memory
                StandardXMLTranslator sxt = new StandardXMLTranslator((Source) value);
                SQLXMLImpl sqlxml;
                try {
                    sqlxml = XMLSystemFunctions.saveToBufferManager(bm, sxt, context);
                } catch (TeiidComponentException e) {
                    throw new TransformationException(e);
                } catch (TeiidProcessingException e) {
                    throw new TransformationException(e);
                }
                return new XMLType(sqlxml);
            }
        }
        return new XMLType(new SQLXMLImpl((InputStreamFactory) value));
    }
    return DataTypeManager.convertToRuntimeType(value, desiredType != DataTypeManager.DefaultDataClasses.OBJECT);
}
Also used : SaveOnReadInputStream(org.teiid.dqp.internal.process.SaveOnReadInputStream) XMLInputStream(org.teiid.util.XMLInputStream) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) XMLInputStream(org.teiid.util.XMLInputStream) SaveOnReadInputStream(org.teiid.dqp.internal.process.SaveOnReadInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) GeometryInputSource(org.teiid.GeometryInputSource) Reader(java.io.Reader) IOException(java.io.IOException) StAXSource(javax.xml.transform.stax.StAXSource) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) GeometryInputSource(org.teiid.GeometryInputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) DataSource(javax.activation.DataSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) ResourceException(javax.resource.ResourceException) TeiidComponentException(org.teiid.core.TeiidComponentException) XMLStreamException(javax.xml.stream.XMLStreamException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidException(org.teiid.core.TeiidException) IOException(java.io.IOException) DataSource(javax.activation.DataSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) FileStore(org.teiid.common.buffer.FileStore) ReaderInputStream(org.teiid.core.util.ReaderInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) TeiidComponentException(org.teiid.core.TeiidComponentException)

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