Search in sources :

Example 21 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestMVRTree method testExample.

private void testExample() {
    // create an in-memory store
    MVStore s = MVStore.open(null);
    // open an R-tree map
    MVRTreeMap<String> r = s.openMap("data", new MVRTreeMap.Builder<String>());
    // add two key-value pairs
    // the first value is the key id (to make the key unique)
    // then the min x, max x, min y, max y
    r.add(new SpatialKey(0, -3f, -2f, 2f, 3f), "left");
    r.add(new SpatialKey(1, 3f, 4f, 4f, 5f), "right");
    // iterate over the intersecting keys
    Iterator<SpatialKey> it = r.findIntersectingKeys(new SpatialKey(0, 0f, 9f, 3f, 6f));
    for (SpatialKey k; it.hasNext(); ) {
        k = it.next();
        // System.out.println(k + ": " + r.get(k));
        assertNotNull(k);
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap)

Example 22 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestTransactionStore method testStopWhileCommitting.

private void testStopWhileCommitting() throws Exception {
    String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
    FileUtils.delete(fileName);
    Random r = new Random(0);
    for (int i = 0; i < 10; ) {
        MVStore s;
        TransactionStore ts;
        Transaction tx;
        TransactionMap<Integer, String> m;
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        ts.init();
        tx = ts.begin();
        s.setReuseSpace(false);
        m = tx.openMap("test");
        final String value = "x" + i;
        for (int j = 0; j < 1000; j++) {
            m.put(j, value);
        }
        final AtomicInteger state = new AtomicInteger();
        final MVStore store = s;
        final MVMap<Integer, String> other = s.openMap("other");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                for (int i = 0; !stop; i++) {
                    state.set(i);
                    other.put(i, value);
                    store.commit();
                }
            }
        };
        task.execute();
        // wait for the task to start
        while (state.get() < 1) {
            Thread.yield();
        }
        // commit while writing in the task
        tx.commit();
        // wait for the task to stop
        task.get();
        store.close();
        s = MVStore.open(fileName);
        // roll back a bit, until we have some undo log entries
        assertTrue(s.hasMap("undoLog"));
        for (int back = 0; back < 100; back++) {
            int minus = r.nextInt(10);
            s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
            MVMap<?, ?> undo = s.openMap("undoLog");
            if (undo.size() > 0) {
                break;
            }
        }
        // re-open the store, because we have opened
        // the undoLog map with the wrong data type
        s.close();
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        List<Transaction> list = ts.getOpenTransactions();
        if (list.size() != 0) {
            tx = list.get(0);
            if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
                i++;
            }
        }
        s.close();
        FileUtils.delete(fileName);
        assertFalse(FileUtils.exists(fileName));
    }
}
Also used : Task(org.h2.util.Task) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 23 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestStreamStore method testTreeStructure.

private void testTreeStructure() throws IOException {
    final AtomicInteger reads = new AtomicInteger();
    Map<Long, byte[]> map = new HashMap<Long, byte[]>() {

        private static final long serialVersionUID = 1L;

        @Override
        public byte[] get(Object k) {
            reads.incrementAndGet();
            return super.get(k);
        }
    };
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(100);
    byte[] id = store.put(new ByteArrayInputStream(new byte[10000]));
    InputStream in = store.get(id);
    assertEquals(0, in.read(new byte[0]));
    assertEquals(0, in.read());
    assertEquals(3, reads.get());
}
Also used : StreamStore(org.h2.mvstore.StreamStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 24 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestStreamStore method testFormat.

private void testFormat() throws IOException {
    Map<Long, byte[]> map = new HashMap<>();
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(20);
    store.setNextKey(123);
    byte[] id;
    id = store.put(new ByteArrayInputStream(new byte[200]));
    assertEquals(200, store.length(id));
    assertEquals("02c8018801", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[0]));
    assertEquals("", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[1]));
    assertEquals("000100", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[3]));
    assertEquals("0003000000", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[10]));
    assertEquals("010a8901", StringUtils.convertBytesToHex(id));
    byte[] combined = StringUtils.convertHexToBytes("0001aa0002bbcc");
    assertEquals(3, store.length(combined));
    InputStream in = store.get(combined);
    assertEquals(1, in.skip(1));
    assertEquals(0xbb, in.read());
    assertEquals(1, in.skip(1));
}
Also used : StreamStore(org.h2.mvstore.StreamStore) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 25 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestStreamStore method testWithExistingData.

private void testWithExistingData() throws IOException {
    final AtomicInteger tests = new AtomicInteger();
    Map<Long, byte[]> map = new HashMap<Long, byte[]>() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean containsKey(Object k) {
            tests.incrementAndGet();
            return super.containsKey(k);
        }
    };
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(20);
    store.setNextKey(0);
    for (int i = 0; i < 10; i++) {
        store.put(new ByteArrayInputStream(new byte[20]));
    }
    assertEquals(10, map.size());
    assertEquals(10, tests.get());
    for (int i = 0; i < 10; i++) {
        map.containsKey((long) i);
    }
    assertEquals(20, tests.get());
    store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(20);
    store.setNextKey(0);
    assertEquals(0, store.getNextKey());
    for (int i = 0; i < 5; i++) {
        store.put(new ByteArrayInputStream(new byte[20]));
    }
    assertEquals(88, tests.get());
    assertEquals(15, store.getNextKey());
    assertEquals(15, map.size());
    for (int i = 0; i < 15; i++) {
        map.containsKey((long) i);
    }
}
Also used : StreamStore(org.h2.mvstore.StreamStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

MVStore (org.h2.mvstore.MVStore)29 IOException (java.io.IOException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)8 HashMap (java.util.HashMap)8 DbException (org.h2.message.DbException)8 StreamStore (org.h2.mvstore.StreamStore)8 PageStore (org.h2.store.PageStore)8 Random (java.util.Random)7 OutputStream (java.io.OutputStream)6 FileStore (org.h2.store.FileStore)6 BufferedInputStream (java.io.BufferedInputStream)5 ArrayList (java.util.ArrayList)5 FileStore (org.h2.mvstore.FileStore)5 Store (org.h2.mvstore.db.MVTableEngine.Store)5 Row (org.h2.result.Row)5 FileStoreInputStream (org.h2.store.FileStoreInputStream)5 PrintWriter (java.io.PrintWriter)4 Database (org.h2.engine.Database)4