Search in sources :

Example 41 with Store

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

the class TestOutOfMemory method testMVStoreUsingInMemoryFileSystem.

private void testMVStoreUsingInMemoryFileSystem() {
    FilePath.register(new FilePathMem());
    String fileName = "memFS:" + getTestName();
    final AtomicReference<Throwable> exRef = new AtomicReference<>();
    MVStore store = new MVStore.Builder().fileName(fileName).backgroundExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            exRef.compareAndSet(null, e);
        }
    }).open();
    try {
        Map<Integer, byte[]> map = store.openMap("test");
        Random r = new Random(1);
        try {
            for (int i = 0; i < 100; i++) {
                byte[] data = new byte[10 * 1024 * 1024];
                r.nextBytes(data);
                map.put(i, data);
            }
            Throwable throwable = exRef.get();
            if (throwable instanceof OutOfMemoryError)
                throw (OutOfMemoryError) throwable;
            if (throwable instanceof IllegalStateException)
                throw (IllegalStateException) throwable;
            fail();
        } catch (OutOfMemoryError | IllegalStateException e) {
        // expected
        }
        try {
            store.close();
        } catch (IllegalStateException e) {
        // expected
        }
        store.closeImmediately();
        store = MVStore.open(fileName);
        store.openMap("test");
        store.close();
    } finally {
        // just in case, otherwise if this test suffers a spurious failure,
        // succeeding tests will too, because they will OOM
        store.closeImmediately();
        FileUtils.delete(fileName);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) MVStore(org.h2.mvstore.MVStore) Random(java.util.Random) FilePathMem(org.h2.store.fs.FilePathMem)

Example 42 with Store

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

the class H2Store method close.

/**
 * Release all resources and close this store
 */
public void close() {
    MVStore s = mvstore.getAndSet(null);
    if (s != null) {
        s.close();
    }
    map = null;
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 43 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project elastic-core-maven by OrdinaryDude.

the class DbShellServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
    resp.setHeader("Pragma", "no-cache");
    resp.setDateHeader("Expires", 0);
    if (!API.isAllowed(req.getRemoteHost())) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    String body = null;
    if (!API.disableAdminPassword) {
        if (API.adminPassword.isEmpty()) {
            body = errorNoPasswordIsConfigured;
        } else {
            try {
                API.verifyPassword(req);
                if ("true".equals(req.getParameter("showShell"))) {
                    body = form.replace("{adminPassword}", URLEncoder.encode(req.getParameter("adminPassword"), "UTF-8"));
                }
            } catch (ParameterException exc) {
                String desc = (String) ((JSONObject) JSONValue.parse(JSON.toString(exc.getErrorResponse()))).get("errorDescription");
                body = String.format(passwordFormTemplate, "<p style=\"color:red\">" + desc + "</p>");
            }
        }
    }
    if (body != null) {
        try (PrintStream out = new PrintStream(resp.getOutputStream())) {
            out.print(header);
            out.print(body);
            out.print(barter);
        }
        return;
    }
    String line = Convert.nullToEmpty(req.getParameter("line"));
    try (PrintStream out = new PrintStream(resp.getOutputStream())) {
        out.println("\n> " + line);
        try {
            Shell shell = new Shell();
            shell.setErr(out);
            shell.setOut(out);
            shell.runTool(Db.db.getConnection(), "-sql", line);
        } catch (SQLException e) {
            out.println(e.toString());
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Shell(org.h2.tools.Shell) JSONObject(org.json.simple.JSONObject) SQLException(java.sql.SQLException)

Example 44 with Store

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

the class GridSqlQueryParser method collectOptimizedTableFiltersOrder.

/**
 * @param qry Query.
 */
private void collectOptimizedTableFiltersOrder(Query qry) {
    if (qry instanceof SelectUnion) {
        collectOptimizedTableFiltersOrder(((SelectUnion) qry).getLeft());
        collectOptimizedTableFiltersOrder(((SelectUnion) qry).getRight());
    } else {
        Select select = (Select) qry;
        TableFilter filter = select.getTopTableFilter();
        int i = 0;
        do {
            assert0(filter != null, select);
            assert0(filter.getNestedJoin() == null, select);
            // Here all the table filters must have generated unique aliases,
            // thus we can store them in the same map for all the subqueries.
            optimizedTableFilterOrder.put(filter.getTableAlias(), i++);
            Table tbl = filter.getTable();
            // Go down and collect inside of optimized subqueries.
            if (tbl instanceof TableView) {
                ViewIndex viewIdx = (ViewIndex) filter.getIndex();
                collectOptimizedTableFiltersOrder(viewIdx.getQuery());
            }
            filter = filter.getJoin();
        } while (filter != null);
    }
}
Also used : SelectUnion(org.h2.command.dml.SelectUnion) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) RangeTable(org.h2.table.RangeTable) MetaTable(org.h2.table.MetaTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) TableFilter(org.h2.table.TableFilter) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select) ViewIndex(org.h2.index.ViewIndex) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) TableView(org.h2.table.TableView)

Example 45 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project jackrabbit-oak by apache.

the class PersistentCache method createMapFactory.

private MapFactory createMapFactory(final int generation, final boolean readOnly) {
    MapFactory f = new MapFactory() {

        final String fileName = getFileName(generation);

        MVStore store;

        @Override
        void openStore() {
            if (store != null) {
                return;
            }
            MVStore.Builder builder = new MVStore.Builder();
            try {
                if (compress) {
                    builder.compress();
                }
                if (manualCommit) {
                    builder.autoCommitDisabled();
                }
                if (fileName != null) {
                    builder.fileName(fileName);
                }
                if (memCache >= 0) {
                    builder.cacheSize(memCache);
                }
                if (readOnly) {
                    builder.readOnly();
                }
                if (maxSizeMB < 10) {
                    builder.cacheSize(maxSizeMB);
                }
                if (autoCompact >= 0) {
                    builder.autoCompactFillRate(autoCompact);
                }
                builder.backgroundExceptionHandler(new Thread.UncaughtExceptionHandler() {

                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        exceptionCount++;
                        LOG.debug("Error in the background thread of the persistent cache", e);
                        LOG.warn("Error in the background thread of the persistent cache: " + e);
                    }
                });
                store = builder.open();
                if (appendOnly) {
                    store.setReuseSpace(false);
                }
            } catch (Exception e) {
                exceptionCount++;
                LOG.warn("Could not open the store " + fileName, e);
            }
        }

        @Override
        synchronized void closeStore() {
            if (store == null) {
                return;
            }
            boolean compact = compactOnClose;
            try {
                if (store.getFileStore().isReadOnly()) {
                    compact = false;
                }
                // clear the interrupted flag, if set
                Thread.interrupted();
                store.close();
            } catch (Exception e) {
                exceptionCount++;
                LOG.debug("Could not close the store", e);
                LOG.warn("Could not close the store: " + e);
                store.closeImmediately();
            }
            if (compact) {
                try {
                    MVStoreTool.compact(fileName, true);
                } catch (Exception e) {
                    exceptionCount++;
                    LOG.debug("Could not compact the store", e);
                    LOG.warn("Could not compact the store: " + e);
                }
            }
            store = null;
        }

        @Override
        <K, V> Map<K, V> openMap(String name, Builder<K, V> builder) {
            try {
                if (builder == null) {
                    return store.openMap(name);
                }
                return store.openMap(name, builder);
            } catch (Exception e) {
                exceptionCount++;
                LOG.warn("Could not open the map", e);
                return null;
            }
        }

        @Override
        long getFileSize() {
            try {
                if (store == null) {
                    return 0;
                }
                FileStore fs = store.getFileStore();
                if (fs == null) {
                    return 0;
                }
                return fs.size();
            } catch (Exception e) {
                exceptionCount++;
                LOG.warn("Could not retrieve the map size", e);
                return 0;
            }
        }
    };
    f.openStore();
    return f;
}
Also used : Builder(org.h2.mvstore.MVMap.Builder) MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore)

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