Search in sources :

Example 1 with PageStore

use of org.h2.store.PageStore 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 2 with PageStore

use of org.h2.store.PageStore 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 3 with PageStore

use of org.h2.store.PageStore in project h2database by h2database.

the class PageBtreeLeaf method moveTo.

@Override
public void moveTo(Session session, int newPos) {
    PageStore store = index.getPageStore();
    readAllRows();
    PageBtreeLeaf p2 = PageBtreeLeaf.create(index, newPos, parentPageId);
    store.logUndo(this, data);
    store.logUndo(p2, null);
    p2.rows = rows;
    p2.entryCount = entryCount;
    p2.offsets = offsets;
    p2.onlyPosition = onlyPosition;
    p2.parentPageId = parentPageId;
    p2.start = start;
    store.update(p2);
    if (parentPageId == ROOT) {
        index.setRootPageId(session, newPos);
    } else {
        PageBtreeNode p = (PageBtreeNode) store.getPage(parentPageId);
        p.moveChild(getPos(), newPos);
    }
    store.free(getPos());
}
Also used : PageStore(org.h2.store.PageStore)

Example 4 with PageStore

use of org.h2.store.PageStore in project h2database by h2database.

the class PageBtreeNode method moveTo.

@Override
public void moveTo(Session session, int newPos) {
    PageStore store = index.getPageStore();
    store.logUndo(this, data);
    PageBtreeNode p2 = PageBtreeNode.create(index, newPos, parentPageId);
    readAllRows();
    p2.rowCountStored = rowCountStored;
    p2.rowCount = rowCount;
    p2.childPageIds = childPageIds;
    p2.rows = rows;
    p2.entryCount = entryCount;
    p2.offsets = offsets;
    p2.onlyPosition = onlyPosition;
    p2.parentPageId = parentPageId;
    p2.start = start;
    store.update(p2);
    if (parentPageId == ROOT) {
        index.setRootPageId(session, newPos);
    } else {
        Page p = store.getPage(parentPageId);
        if (!(p instanceof PageBtreeNode)) {
            throw DbException.throwInternalError();
        }
        PageBtreeNode n = (PageBtreeNode) p;
        n.moveChild(getPos(), newPos);
    }
    for (int i = 0; i < entryCount + 1; i++) {
        int child = childPageIds[i];
        PageBtree p = index.getPage(child);
        p.setParentPageId(newPos);
        store.update(p);
    }
    store.free(getPos());
}
Also used : PageStore(org.h2.store.PageStore) Page(org.h2.store.Page)

Example 5 with PageStore

use of org.h2.store.PageStore in project h2database by h2database.

the class PageStore method addMeta.

/**
 * Add the meta data of an index.
 *
 * @param index the index to add
 * @param session the session
 */
public void addMeta(PageIndex index, Session session) {
    Table table = index.getTable();
    if (SysProperties.CHECK) {
        if (!table.isTemporary()) {
            // the Database lock before we take the PageStore lock
            synchronized (database) {
                synchronized (this) {
                    database.verifyMetaLocked(session);
                }
            }
        }
    }
    synchronized (this) {
        int type = index instanceof PageDataIndex ? META_TYPE_DATA_INDEX : META_TYPE_BTREE_INDEX;
        IndexColumn[] columns = index.getIndexColumns();
        StatementBuilder buff = new StatementBuilder();
        for (IndexColumn col : columns) {
            buff.appendExceptFirst(",");
            int id = col.column.getColumnId();
            buff.append(id);
            int sortType = col.sortType;
            if (sortType != 0) {
                buff.append('/');
                buff.append(sortType);
            }
        }
        String columnList = buff.toString();
        CompareMode mode = table.getCompareMode();
        String options = mode.getName() + "," + mode.getStrength() + ",";
        if (table.isTemporary()) {
            options += "temp";
        }
        options += ",";
        if (index instanceof PageDelegateIndex) {
            options += "d";
        }
        options += "," + mode.isBinaryUnsigned();
        Row row = metaTable.getTemplateRow();
        row.setValue(0, ValueInt.get(index.getId()));
        row.setValue(1, ValueInt.get(type));
        row.setValue(2, ValueInt.get(table.getId()));
        row.setValue(3, ValueInt.get(index.getRootPageId()));
        row.setValue(4, ValueString.get(options));
        row.setValue(5, ValueString.get(columnList));
        row.setKey(index.getId() + 1);
        metaIndex.add(session, row);
    }
}
Also used : RegularTable(org.h2.table.RegularTable) Table(org.h2.table.Table) PageDelegateIndex(org.h2.index.PageDelegateIndex) PageDataIndex(org.h2.index.PageDataIndex) StatementBuilder(org.h2.util.StatementBuilder) CompareMode(org.h2.value.CompareMode) ValueString(org.h2.value.ValueString) Row(org.h2.result.Row) IndexColumn(org.h2.table.IndexColumn)

Aggregations

PageStore (org.h2.store.PageStore)8 Constraint (org.h2.constraint.Constraint)3 Row (org.h2.result.Row)3 ValueString (org.h2.value.ValueString)3 IOException (java.io.IOException)2 Database (org.h2.engine.Database)2 DbException (org.h2.message.DbException)2 Store (org.h2.mvstore.db.MVTableEngine.Store)2 StatementBuilder (org.h2.util.StatementBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Properties (java.util.Properties)1