Search in sources :

Example 1 with MVStore

use of org.h2.mvstore.MVStore in project jackrabbit-oak by apache.

the class PersistentCacheCommand method dump.

static void dump(PrintWriter write, String path, String revision, String map, String fileName, boolean values, boolean raw) {
    MVStore s = new MVStore.Builder().readOnly().fileName(fileName).open();
    Map<String, String> meta = s.getMetaMap();
    boolean statsOnly = "".equalsIgnoreCase(map) && "".equals(revision) && "".equals(path);
    if (!statsOnly) {
        if (raw) {
            write.println("map" + "\t" + "key" + "\t" + "length" + "\t" + "value");
        } else if (values) {
            write.println("map,path,revision,p2,length,value");
        } else {
            write.println("map,path,revision,p2,length");
        }
    }
    for (String n : meta.keySet()) {
        if (n.startsWith("name.")) {
            String mapName = n.substring(5, n.length());
            if (map.length() > 0 && !map.equalsIgnoreCase(mapName)) {
                continue;
            }
            MVMap.Builder<String, String> b = new MVMap.Builder<String, String>().keyType(StringDataType.INSTANCE).valueType(StringDataType.INSTANCE);
            MVMap<String, String> m = s.openMap(mapName, b);
            if (statsOnly) {
                statistics(write, m);
            } else if (raw) {
                dumpRaw(write, m);
            } else {
                dump(write, m, path, revision, values);
            }
        }
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) MVMap(org.h2.mvstore.MVMap)

Example 2 with MVStore

use of org.h2.mvstore.MVStore 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 3 with MVStore

use of org.h2.mvstore.MVStore 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)

Example 4 with MVStore

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

the class LobStorageMap method init.

@Override
public void init() {
    if (init) {
        return;
    }
    init = true;
    Store s = database.getMvStore();
    MVStore mvStore;
    if (s == null) {
        // in-memory database
        mvStore = MVStore.open(null);
    } else {
        mvStore = s.getStore();
    }
    lobMap = mvStore.openMap("lobMap");
    refMap = mvStore.openMap("lobRef");
    dataMap = mvStore.openMap("lobData");
    streamStore = new StreamStore(dataMap);
    // garbage collection of the last blocks
    if (database.isReadOnly()) {
        return;
    }
    if (dataMap.isEmpty()) {
        return;
    }
    // search for the last block
    // (in theory, only the latest lob can have unreferenced blocks,
    // but the latest lob could be a copy of another one, and
    // we don't know that, so we iterate over all lobs)
    long lastUsedKey = -1;
    for (Entry<Long, Object[]> e : lobMap.entrySet()) {
        long lobId = e.getKey();
        Object[] v = e.getValue();
        byte[] id = (byte[]) v[0];
        long max = streamStore.getMaxBlockKey(id);
        // a lob may not have a referenced blocks if data is kept inline
        if (max != -1 && max > lastUsedKey) {
            lastUsedKey = max;
            if (TRACE) {
                trace("lob " + lobId + " lastUsedKey=" + lastUsedKey);
            }
        }
    }
    if (TRACE) {
        trace("lastUsedKey=" + lastUsedKey);
    }
    // delete all blocks that are newer
    while (true) {
        Long last = dataMap.lastKey();
        if (last == null || last <= lastUsedKey) {
            break;
        }
        if (TRACE) {
            trace("gc " + last);
        }
        dataMap.remove(last);
    }
    // don't re-use block ids, except at the very end
    Long last = dataMap.lastKey();
    if (last != null) {
        streamStore.setNextKey(last + 1);
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) StreamStore(org.h2.mvstore.StreamStore) Store(org.h2.mvstore.db.MVTableEngine.Store) MVStore(org.h2.mvstore.MVStore) StreamStore(org.h2.mvstore.StreamStore)

Example 5 with MVStore

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

the class MVStoreTool method info.

/**
 * Read the summary information of the file and write them to system out.
 *
 * @param fileName the name of the file
 * @param writer the print writer
 * @return null if successful (if there was no error), otherwise the error
 *         message
 */
public static String info(String fileName, Writer writer) {
    PrintWriter pw = new PrintWriter(writer, true);
    if (!FilePath.get(fileName).exists()) {
        pw.println("File not found: " + fileName);
        return "File not found: " + fileName;
    }
    long fileLength = FileUtils.size(fileName);
    MVStore store = new MVStore.Builder().fileName(fileName).readOnly().open();
    try {
        MVMap<String, String> meta = store.getMetaMap();
        Map<String, Object> header = store.getStoreHeader();
        long fileCreated = DataUtils.readHexLong(header, "created", 0L);
        TreeMap<Integer, Chunk> chunks = new TreeMap<>();
        long chunkLength = 0;
        long maxLength = 0;
        long maxLengthLive = 0;
        long maxLengthNotEmpty = 0;
        for (Entry<String, String> e : meta.entrySet()) {
            String k = e.getKey();
            if (k.startsWith("chunk.")) {
                Chunk c = Chunk.fromString(e.getValue());
                chunks.put(c.id, c);
                chunkLength += c.len * MVStore.BLOCK_SIZE;
                maxLength += c.maxLen;
                maxLengthLive += c.maxLenLive;
                if (c.maxLenLive > 0) {
                    maxLengthNotEmpty += c.maxLen;
                }
            }
        }
        pw.printf("Created: %s\n", formatTimestamp(fileCreated, fileCreated));
        pw.printf("Last modified: %s\n", formatTimestamp(FileUtils.lastModified(fileName), fileCreated));
        pw.printf("File length: %d\n", fileLength);
        pw.printf("The last chunk is not listed\n");
        pw.printf("Chunk length: %d\n", chunkLength);
        pw.printf("Chunk count: %d\n", chunks.size());
        pw.printf("Used space: %d%%\n", getPercent(chunkLength, fileLength));
        pw.printf("Chunk fill rate: %d%%\n", maxLength == 0 ? 100 : getPercent(maxLengthLive, maxLength));
        pw.printf("Chunk fill rate excluding empty chunks: %d%%\n", maxLengthNotEmpty == 0 ? 100 : getPercent(maxLengthLive, maxLengthNotEmpty));
        for (Entry<Integer, Chunk> e : chunks.entrySet()) {
            Chunk c = e.getValue();
            long created = fileCreated + c.time;
            pw.printf("  Chunk %d: %s, %d%% used, %d blocks", c.id, formatTimestamp(created, fileCreated), getPercent(c.maxLenLive, c.maxLen), c.len);
            if (c.maxLenLive == 0) {
                pw.printf(", unused: %s", formatTimestamp(fileCreated + c.unused, fileCreated));
            }
            pw.printf("\n");
        }
        pw.printf("\n");
    } catch (Exception e) {
        pw.println("ERROR: " + e);
        e.printStackTrace(pw);
        return e.getMessage();
    } finally {
        store.close();
    }
    pw.flush();
    return null;
}
Also used : TreeMap(java.util.TreeMap) IOException(java.io.IOException) DbException(org.h2.message.DbException) PrintWriter(java.io.PrintWriter)

Aggregations

MVStore (org.h2.mvstore.MVStore)123 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)69 Random (java.util.Random)23 Task (org.h2.util.Task)20 TransactionStore (org.h2.mvstore.db.TransactionStore)19 Transaction (org.h2.mvstore.db.TransactionStore.Transaction)17 MVRTreeMap (org.h2.mvstore.rtree.MVRTreeMap)8 SpatialKey (org.h2.mvstore.rtree.SpatialKey)8 IOException (java.io.IOException)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 FileStore (org.h2.mvstore.FileStore)5 StreamStore (org.h2.mvstore.StreamStore)5 ArrayList (java.util.ArrayList)4 TreeMap (java.util.TreeMap)4 Store (org.h2.mvstore.db.MVTableEngine.Store)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 PreparedStatement (java.sql.PreparedStatement)3