Search in sources :

Example 1 with Store

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

the class BlobCache method addGeneration.

@Override
public void addGeneration(int generation, boolean readOnly) {
    CacheMap<Long, byte[]> d = cache.openMap(generation, "data", new MVMap.Builder<Long, byte[]>());
    data.addReadMap(generation, d);
    CacheMap<String, byte[]> m = cache.openMap(generation, "meta", new MVMap.Builder<String, byte[]>());
    meta.addReadMap(generation, m);
    if (!readOnly) {
        // the order is important:
        // if we switch the data first,
        // we could end up with the data in store 1
        // but the metadata in store 2 - which could
        // result in a data block not found if store 1
        // is removed later on
        meta.setWriteMap(m);
        data.setWriteMap(d);
    }
    if (streamStore == null) {
        streamStore = new StreamStore(data);
    }
}
Also used : StreamStore(org.h2.mvstore.StreamStore) MVMap(org.h2.mvstore.MVMap)

Example 2 with Store

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

the class ScriptBase method openInput.

/**
 * Open the input stream.
 */
void openInput() {
    String file = getFileName();
    if (file == null) {
        return;
    }
    if (isEncrypted()) {
        initStore();
        in = new FileStoreInputStream(store, this, compressionAlgorithm != null, false);
    } else {
        InputStream inStream;
        try {
            inStream = FileUtils.newInputStream(file);
        } catch (IOException e) {
            throw DbException.convertIOException(e, file);
        }
        in = new BufferedInputStream(inStream, Constants.IO_BUFFER_SIZE);
        in = CompressTool.wrapInputStream(in, compressionAlgorithm, SCRIPT_SQL);
        if (in == null) {
            throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, SCRIPT_SQL + " in " + file);
        }
    }
}
Also used : FileStoreInputStream(org.h2.store.FileStoreInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 3 with Store

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

the class BackupCommand method backupTo.

private void backupTo(String fileName) {
    Database db = session.getDatabase();
    if (!db.isPersistent()) {
        throw DbException.get(ErrorCode.DATABASE_IS_NOT_PERSISTENT);
    }
    try {
        Store mvStore = db.getMvStore();
        if (mvStore != null) {
            mvStore.flush();
        }
        String name = db.getName();
        name = FileUtils.getName(name);
        try (OutputStream zip = FileUtils.newOutputStream(fileName, false)) {
            ZipOutputStream out = new ZipOutputStream(zip);
            db.flush();
            if (db.getPageStore() != null) {
                String fn = db.getName() + Constants.SUFFIX_PAGE_FILE;
                backupPageStore(out, fn, db.getPageStore());
            }
            // synchronize on the database, to avoid concurrent temp file
            // creation / deletion / backup
            String base = FileUtils.getParent(db.getName());
            synchronized (db.getLobSyncObject()) {
                String prefix = db.getDatabasePath();
                String dir = FileUtils.getParent(prefix);
                dir = FileLister.getDir(dir);
                ArrayList<String> fileList = FileLister.getDatabaseFiles(dir, name, true);
                for (String n : fileList) {
                    if (n.endsWith(Constants.SUFFIX_LOB_FILE)) {
                        backupFile(out, base, n);
                    }
                    if (n.endsWith(Constants.SUFFIX_MV_FILE) && mvStore != null) {
                        MVStore s = mvStore.getStore();
                        boolean before = s.getReuseSpace();
                        s.setReuseSpace(false);
                        try {
                            InputStream in = mvStore.getInputStream();
                            backupFile(out, base, n, in);
                        } finally {
                            s.setReuseSpace(before);
                        }
                    }
                }
            }
            out.close();
        }
    } catch (IOException e) {
        throw DbException.convertIOException(e, fileName);
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) Database(org.h2.engine.Database) Store(org.h2.mvstore.db.MVTableEngine.Store) PageStore(org.h2.store.PageStore) MVStore(org.h2.mvstore.MVStore) IOException(java.io.IOException)

Example 4 with Store

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

the class BackupCommand method backupPageStore.

private void backupPageStore(ZipOutputStream out, String fileName, PageStore store) throws IOException {
    Database db = session.getDatabase();
    fileName = FileUtils.getName(fileName);
    out.putNextEntry(new ZipEntry(fileName));
    int pos = 0;
    try {
        store.setBackup(true);
        while (true) {
            pos = store.copyDirect(pos, out);
            if (pos < 0) {
                break;
            }
            int max = store.getPageCount();
            db.setProgress(DatabaseEventListener.STATE_BACKUP_FILE, fileName, pos, max);
        }
    } finally {
        store.setBackup(false);
    }
    out.closeEntry();
}
Also used : ZipEntry(java.util.zip.ZipEntry) Database(org.h2.engine.Database)

Example 5 with Store

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

the class Explain method query.

@Override
public ResultInterface query(int maxrows) {
    Column column = new Column("PLAN", Value.STRING);
    Database db = session.getDatabase();
    ExpressionColumn expr = new ExpressionColumn(db, column);
    Expression[] expressions = { expr };
    result = new LocalResult(session, expressions, 1);
    if (maxrows >= 0) {
        String plan;
        if (executeCommand) {
            PageStore store = null;
            Store mvStore = null;
            if (db.isPersistent()) {
                store = db.getPageStore();
                if (store != null) {
                    store.statisticsStart();
                }
                mvStore = db.getMvStore();
                if (mvStore != null) {
                    mvStore.statisticsStart();
                }
            }
            if (command.isQuery()) {
                command.query(maxrows);
            } else {
                command.update();
            }
            plan = command.getPlanSQL();
            Map<String, Integer> statistics = null;
            if (store != null) {
                statistics = store.statisticsEnd();
            } else if (mvStore != null) {
                statistics = mvStore.statisticsEnd();
            }
            if (statistics != null) {
                int total = 0;
                for (Entry<String, Integer> e : statistics.entrySet()) {
                    total += e.getValue();
                }
                if (total > 0) {
                    statistics = new TreeMap<>(statistics);
                    StringBuilder buff = new StringBuilder();
                    if (statistics.size() > 1) {
                        buff.append("total: ").append(total).append('\n');
                    }
                    for (Entry<String, Integer> e : statistics.entrySet()) {
                        int value = e.getValue();
                        int percent = (int) (100L * value / total);
                        buff.append(e.getKey()).append(": ").append(value);
                        if (statistics.size() > 1) {
                            buff.append(" (").append(percent).append("%)");
                        }
                        buff.append('\n');
                    }
                    plan += "\n/*\n" + buff.toString() + "*/";
                }
            }
        } else {
            plan = command.getPlanSQL();
        }
        add(plan);
    }
    result.done();
    return result;
}
Also used : Store(org.h2.mvstore.db.MVTableEngine.Store) PageStore(org.h2.store.PageStore) PageStore(org.h2.store.PageStore) ValueString(org.h2.value.ValueString) ExpressionColumn(org.h2.expression.ExpressionColumn) LocalResult(org.h2.result.LocalResult) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Database(org.h2.engine.Database)

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