Search in sources :

Example 36 with FileStore

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

the class TestMVStore method testOffHeapStorage.

private void testOffHeapStorage() {
    OffHeapStore offHeap = new OffHeapStore();
    int count = 1000;
    try (MVStore s = new MVStore.Builder().fileStore(offHeap).open()) {
        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);
    }
    try (MVStore s = new MVStore.Builder().fileStore(offHeap).open()) {
        Map<Integer, String> map = s.openMap("data");
        for (int i = 0; i < count; i++) {
            assertEquals("Hello " + i, map.get(i));
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffHeapStore(org.h2.mvstore.OffHeapStore)

Example 37 with FileStore

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

the class TestMVStore method testFileHeaderCorruption.

private void testFileHeaderCorruption() throws Exception {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore.Builder builder = new MVStore.Builder().fileName(fileName).pageSplitSize(1000).autoCommitDisabled();
    try (MVStore s = builder.open()) {
        s.setRetentionTime(0);
        MVMap<Integer, byte[]> 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]);
    }
    FilePath f = FilePath.get(fileName);
    int blockSize = 4 * 1024;
    // test corrupt file headers
    for (int i = 0; i <= blockSize; i += blockSize) {
        try (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));
            // note that headers may be overwritten upon successfull opening
            for (int b = 0; b <= i; b += blockSize) {
                buff.rewind();
                fc.write(buff, b);
            }
        }
        if (i == 0) {
            // header should be used
            try (MVStore s = openStore(fileName)) {
                MVMap<Integer, byte[]> map = s.openMap("test");
                assertEquals(100, map.get(0).length);
                map = s.openMap("test2");
                assertFalse(map.containsKey(1));
            }
        } else {
            // both headers are corrupt
            assertThrows(Exception.class, () -> openStore(fileName));
        }
    }
}
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)

Example 38 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class ValueClob method createTemporary.

/**
 * Create a CLOB in a temporary file.
 */
private static ValueClob createTemporary(DataHandler handler, Reader in, long remaining) throws IOException {
    String fileName = ValueLob.createTempLobFileName(handler);
    FileStore tempFile = handler.openFile(fileName, "rw", false);
    tempFile.autoDelete();
    long octetLength = 0L, charLength = 0L;
    try (FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null)) {
        char[] buff = new char[Constants.IO_BUFFER_SIZE];
        while (true) {
            int len = ValueLob.getBufferSize(handler, remaining);
            len = IOUtils.readFully(in, buff, len);
            if (len == 0) {
                break;
            }
            // TODO reduce memory allocation
            byte[] data = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
            out.write(data);
            octetLength += data.length;
            charLength += len;
        }
    }
    return new ValueClob(new LobDataFile(handler, fileName, tempFile), octetLength, charLength);
}
Also used : FileStore(org.h2.store.FileStore) LobDataFile(org.h2.value.lob.LobDataFile) FileStoreOutputStream(org.h2.store.FileStoreOutputStream)

Example 39 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class LobDataFile method getInputStream.

@Override
public InputStream getInputStream(long precision) {
    FileStore store = handler.openFile(fileName, "r", true);
    boolean alwaysClose = SysProperties.lobCloseBetweenReads;
    return new BufferedInputStream(new FileStoreInputStream(store, false, alwaysClose), Constants.IO_BUFFER_SIZE);
}
Also used : FileStore(org.h2.store.FileStore) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream)

Example 40 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

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)

Aggregations

FileStore (org.h2.store.FileStore)20 FileStore (org.h2.mvstore.FileStore)16 IOException (java.io.IOException)14 MVStore (org.h2.mvstore.MVStore)11 BufferedInputStream (java.io.BufferedInputStream)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 FileStoreInputStream (org.h2.store.FileStoreInputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 BitSet (java.util.BitSet)6 HashSet (java.util.HashSet)6 DbException (org.h2.message.DbException)6 FileStoreOutputStream (org.h2.store.FileStoreOutputStream)6 InputStream (java.io.InputStream)5 InputStreamReader (java.io.InputStreamReader)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Setting (org.h2.engine.Setting)5 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OutputStream (java.io.OutputStream)3