Search in sources :

Example 16 with Page

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

the class PageStore method addMeta.

private void addMeta(Row row, Session session, boolean redo) {
    int id = row.getValue(0).getInt();
    int type = row.getValue(1).getInt();
    int parent = row.getValue(2).getInt();
    int rootPageId = row.getValue(3).getInt();
    String[] options = StringUtils.arraySplit(row.getValue(4).getString(), ',', false);
    String columnList = row.getValue(5).getString();
    String[] columns = StringUtils.arraySplit(columnList, ',', false);
    Index meta;
    if (trace.isDebugEnabled()) {
        trace.debug("addMeta id=" + id + " type=" + type + " root=" + rootPageId + " parent=" + parent + " columns=" + columnList);
    }
    if (redo && rootPageId != 0) {
        // ensure the page is empty, but not used by regular data
        writePage(rootPageId, createData());
        allocatePage(rootPageId);
    }
    metaRootPageId.put(id, rootPageId);
    if (type == META_TYPE_DATA_INDEX) {
        CreateTableData data = new CreateTableData();
        if (SysProperties.CHECK) {
            if (columns == null) {
                throw DbException.throwInternalError(row.toString());
            }
        }
        for (int i = 0, len = columns.length; i < len; i++) {
            Column col = new Column("C" + i, Value.INT);
            data.columns.add(col);
        }
        data.schema = metaSchema;
        data.tableName = "T" + id;
        data.id = id;
        data.temporary = options[2].equals("temp");
        data.persistData = true;
        data.persistIndexes = true;
        data.create = false;
        data.session = session;
        RegularTable table = new RegularTable(data);
        boolean binaryUnsigned = SysProperties.SORT_BINARY_UNSIGNED;
        if (options.length > 3) {
            binaryUnsigned = Boolean.parseBoolean(options[3]);
        }
        CompareMode mode = CompareMode.getInstance(options[0], Integer.parseInt(options[1]), binaryUnsigned);
        table.setCompareMode(mode);
        meta = table.getScanIndex(session);
    } else {
        Index p = metaObjects.get(parent);
        if (p == null) {
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "Table not found:" + parent + " for " + row + " meta:" + metaObjects);
        }
        RegularTable table = (RegularTable) p.getTable();
        Column[] tableCols = table.getColumns();
        int len = columns.length;
        IndexColumn[] cols = new IndexColumn[len];
        for (int i = 0; i < len; i++) {
            String c = columns[i];
            IndexColumn ic = new IndexColumn();
            int idx = c.indexOf('/');
            if (idx >= 0) {
                String s = c.substring(idx + 1);
                ic.sortType = Integer.parseInt(s);
                c = c.substring(0, idx);
            }
            ic.column = tableCols[Integer.parseInt(c)];
            cols[i] = ic;
        }
        IndexType indexType;
        if (options[3].equals("d")) {
            indexType = IndexType.createPrimaryKey(true, false);
            Column[] tableColumns = table.getColumns();
            for (IndexColumn indexColumn : cols) {
                tableColumns[indexColumn.column.getColumnId()].setNullable(false);
            }
        } else {
            indexType = IndexType.createNonUnique(true);
        }
        meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null);
    }
    PageIndex index;
    if (meta instanceof MultiVersionIndex) {
        index = (PageIndex) ((MultiVersionIndex) meta).getBaseIndex();
    } else {
        index = (PageIndex) meta;
    }
    metaObjects.put(id, index);
}
Also used : Index(org.h2.index.Index) PageIndex(org.h2.index.PageIndex) PageDelegateIndex(org.h2.index.PageDelegateIndex) MultiVersionIndex(org.h2.index.MultiVersionIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) PageDataIndex(org.h2.index.PageDataIndex) ValueString(org.h2.value.ValueString) PageIndex(org.h2.index.PageIndex) CreateTableData(org.h2.command.ddl.CreateTableData) IndexColumn(org.h2.table.IndexColumn) IndexColumn(org.h2.table.IndexColumn) Column(org.h2.table.Column) MultiVersionIndex(org.h2.index.MultiVersionIndex) RegularTable(org.h2.table.RegularTable) CompareMode(org.h2.value.CompareMode) IndexType(org.h2.index.IndexType)

Example 17 with Page

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

the class PageStore method readPage.

/**
 * Read a page.
 *
 * @param pos the page id
 * @return the page
 */
public synchronized Data readPage(int pos) {
    Data page = createData();
    readPage(pos, page);
    return page;
}
Also used : CreateTableData(org.h2.command.ddl.CreateTableData)

Example 18 with Page

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

the class PageStore method writeStaticHeader.

private void writeStaticHeader() {
    Data page = Data.create(database, new byte[pageSize - FileStore.HEADER_LENGTH]);
    page.writeInt(pageSize);
    page.writeByte((byte) WRITE_VERSION);
    page.writeByte((byte) READ_VERSION);
    file.seek(FileStore.HEADER_LENGTH);
    file.write(page.getBytes(), 0, pageSize - FileStore.HEADER_LENGTH);
    writeCount++;
}
Also used : CreateTableData(org.h2.command.ddl.CreateTableData)

Example 19 with Page

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

the class MVRTreeMap method remove.

@Override
protected synchronized Object remove(Page p, long writeVersion, Object key) {
    Object result = null;
    if (p.isLeaf()) {
        for (int i = 0; i < p.getKeyCount(); i++) {
            if (keyType.equals(p.getKey(i), key)) {
                result = p.getValue(i);
                p.remove(i);
                break;
            }
        }
        return result;
    }
    for (int i = 0; i < p.getKeyCount(); i++) {
        if (contains(p, i, key)) {
            Page cOld = p.getChildPage(i);
            // this will mark the old page as deleted
            // so we need to update the parent in any case
            // (otherwise the old page might be deleted again)
            Page c = cOld.copy(writeVersion);
            long oldSize = c.getTotalCount();
            result = remove(c, writeVersion, key);
            p.setChild(i, c);
            if (oldSize == c.getTotalCount()) {
                continue;
            }
            if (c.getTotalCount() == 0) {
                // this child was deleted
                p.remove(i);
                if (p.getKeyCount() == 0) {
                    c.removePage();
                }
                break;
            }
            Object oldBounds = p.getKey(i);
            if (!keyType.isInside(key, oldBounds)) {
                p.setKey(i, getBounds(c));
            }
            break;
        }
    }
    return result;
}
Also used : Page(org.h2.mvstore.Page)

Example 20 with Page

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

the class MVRTreeMap method putOrAdd.

private synchronized Object putOrAdd(SpatialKey key, V value, boolean alwaysAdd) {
    beforeWrite();
    long v = writeVersion;
    Page p = root.copy(v);
    Object result;
    if (alwaysAdd || get(key) == null) {
        if (p.getMemory() > store.getPageSplitSize() && p.getKeyCount() > 3) {
            // only possible if this is the root, else we would have
            // split earlier (this requires pageSplitSize is fixed)
            long totalCount = p.getTotalCount();
            Page split = split(p, v);
            Object k1 = getBounds(p);
            Object k2 = getBounds(split);
            Object[] keys = { k1, k2 };
            Page.PageReference[] children = { new Page.PageReference(p, p.getPos(), p.getTotalCount()), new Page.PageReference(split, split.getPos(), split.getTotalCount()), new Page.PageReference(null, 0, 0) };
            p = Page.create(this, v, keys, null, children, totalCount, 0);
        // now p is a node; continues
        }
        add(p, v, key, value);
        result = null;
    } else {
        result = set(p, v, key, value);
    }
    newRoot(p);
    return result;
}
Also used : Page(org.h2.mvstore.Page)

Aggregations

CreateTableData (org.h2.command.ddl.CreateTableData)8 Page (org.h2.mvstore.Page)7 Data (org.h2.store.Data)7 Column (org.h2.table.Column)5 IndexColumn (org.h2.table.IndexColumn)5 Value (org.h2.value.Value)5 MVStore (org.h2.mvstore.MVStore)4 Row (org.h2.result.Row)4 SearchRow (org.h2.result.SearchRow)4 IOException (java.io.IOException)3 Connection (java.sql.Connection)3 CRC32 (java.util.zip.CRC32)3 PageBtreeIndex (org.h2.index.PageBtreeIndex)3 PageDataIndex (org.h2.index.PageDataIndex)3 PageIndex (org.h2.index.PageIndex)3 DbException (org.h2.message.DbException)3 Page (org.h2.store.Page)3 ValueString (org.h2.value.ValueString)3 PrintWriter (java.io.PrintWriter)2 ResultSet (java.sql.ResultSet)2