Search in sources :

Example 11 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class ValueLob method getInputStream.

@Override
public InputStream getInputStream(long oneBasedOffset, long length) {
    if (fileName == null) {
        return super.getInputStream(oneBasedOffset, length);
    }
    FileStore store = handler.openFile(fileName, "r", true);
    boolean alwaysClose = SysProperties.lobCloseBetweenReads;
    InputStream inputStream = new BufferedInputStream(new FileStoreInputStream(store, handler, compressed, alwaysClose), Constants.IO_BUFFER_SIZE);
    return rangeInputStream(inputStream, oneBasedOffset, length, store.length());
}
Also used : FileStore(org.h2.store.FileStore) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) RangeInputStream(org.h2.store.RangeInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 12 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class ValueLobDb method getInputStream.

@Override
public InputStream getInputStream(long oneBasedOffset, long length) {
    long byteCount;
    InputStream inputStream;
    if (small != null) {
        return super.getInputStream(oneBasedOffset, length);
    } else if (fileName != null) {
        FileStore store = handler.openFile(fileName, "r", true);
        boolean alwaysClose = SysProperties.lobCloseBetweenReads;
        byteCount = store.length();
        inputStream = new BufferedInputStream(new FileStoreInputStream(store, handler, false, alwaysClose), Constants.IO_BUFFER_SIZE);
    } else {
        byteCount = (type == Value.BLOB) ? precision : -1;
        try {
            inputStream = handler.getLobStorage().getInputStream(this, hmac, byteCount);
        } catch (IOException e) {
            throw DbException.convertIOException(e, toString());
        }
    }
    return ValueLob.rangeInputStream(inputStream, oneBasedOffset, length, byteCount);
}
Also used : FileStore(org.h2.store.FileStore) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 13 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class TestMVStore method testProvidedFileStoreNotOpenedAndClosed.

private void testProvidedFileStoreNotOpenedAndClosed() {
    final AtomicInteger openClose = new AtomicInteger();
    FileStore fileStore = new OffHeapStore() {

        @Override
        public void open(String fileName, boolean readOnly, char[] encryptionKey) {
            openClose.incrementAndGet();
            super.open(fileName, readOnly, encryptionKey);
        }

        @Override
        public void close() {
            openClose.incrementAndGet();
            super.close();
        }
    };
    MVStore store = new MVStore.Builder().fileStore(fileStore).open();
    store.close();
    assertEquals(0, openClose.get());
}
Also used : MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffHeapStore(org.h2.mvstore.OffHeapStore)

Example 14 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class TestMVStore method testOffHeapStorage.

private void testOffHeapStorage() throws Exception {
    OffHeapStore offHeap = new OffHeapStore();
    MVStore s = new MVStore.Builder().fileStore(offHeap).open();
    int count = 1000;
    Map<Integer, String> map = s.openMap("data");
    for (int i = 0; i < count; i++) {
        map.put(i, "Hello " + i);
        s.commit();
    }
    assertTrue(offHeap.getWriteCount() > count);
    s.close();
    s = new MVStore.Builder().fileStore(offHeap).open();
    map = s.openMap("data");
    for (int i = 0; i < count; i++) {
        assertEquals("Hello " + i, map.get(i));
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffHeapStore(org.h2.mvstore.OffHeapStore)

Example 15 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class TestMVStore method testFileHeaderCorruption.

private void testFileHeaderCorruption() throws Exception {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(1000).autoCommitDisabled().open();
    s.setRetentionTime(0);
    MVMap<Integer, byte[]> map;
    map = s.openMap("test");
    map.put(0, new byte[100]);
    for (int i = 0; i < 10; i++) {
        map = s.openMap("test" + i);
        map.put(0, new byte[1000]);
        s.commit();
    }
    FileStore fs = s.getFileStore();
    long size = fs.getFile().size();
    for (int i = 0; i < 100; i++) {
        map = s.openMap("test" + i);
        s.removeMap(map);
        s.commit();
        s.compact(100, 1);
        if (fs.getFile().size() <= size) {
            break;
        }
    }
    // the last chunk is at the end
    s.setReuseSpace(false);
    map = s.openMap("test2");
    map.put(1, new byte[1000]);
    s.close();
    FilePath f = FilePath.get(fileName);
    int blockSize = 4 * 1024;
    // test corrupt file headers
    for (int i = 0; i <= blockSize; i += blockSize) {
        FileChannel fc = f.open("rw");
        if (i == 0) {
            // corrupt the last block (the end header)
            fc.write(ByteBuffer.allocate(256), fc.size() - 256);
        }
        ByteBuffer buff = ByteBuffer.allocate(4 * 1024);
        fc.read(buff, i);
        String h = new String(buff.array(), StandardCharsets.UTF_8).trim();
        int idx = h.indexOf("fletcher:");
        int old = Character.digit(h.charAt(idx + "fletcher:".length()), 16);
        int bad = (old + 1) & 15;
        buff.put(idx + "fletcher:".length(), (byte) Character.forDigit(bad, 16));
        buff.rewind();
        fc.write(buff, i);
        fc.close();
        if (i == 0) {
            // if the first header is corrupt, the second
            // header should be used
            s = openStore(fileName);
            map = s.openMap("test");
            assertEquals(100, map.get(0).length);
            map = s.openMap("test2");
            assertFalse(map.containsKey(1));
            s.close();
        } else {
            // both headers are corrupt
            try {
                s = openStore(fileName);
                fail();
            } catch (Exception e) {
            // expected
            }
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FilePath(org.h2.store.fs.FilePath) FileStore(org.h2.mvstore.FileStore) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

FileStore (org.h2.store.FileStore)10 IOException (java.io.IOException)6 BufferedInputStream (java.io.BufferedInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileStoreInputStream (org.h2.store.FileStoreInputStream)5 InputStream (java.io.InputStream)4 MVStore (org.h2.mvstore.MVStore)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 DbException (org.h2.message.DbException)3 FileStore (org.h2.mvstore.FileStore)3 InputStreamReader (java.io.InputStreamReader)2 OutputStream (java.io.OutputStream)2 OffHeapStore (org.h2.mvstore.OffHeapStore)2 BufferedOutputStream (java.io.BufferedOutputStream)1 LineNumberReader (java.io.LineNumberReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 SequenceInputStream (java.io.SequenceInputStream)1 ByteBuffer (java.nio.ByteBuffer)1