Search in sources :

Example 11 with Page

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

the class PageLog method removeUntil.

/**
 * Remove all pages until the given data page.
 *
 * @param trunkPage the first trunk page
 * @param firstDataPageToKeep the first data page to keep
 * @return the trunk page of the data page to keep
 */
private int removeUntil(int trunkPage, int firstDataPageToKeep) {
    trace.debug("log.removeUntil " + trunkPage + " " + firstDataPageToKeep);
    int last = trunkPage;
    while (true) {
        Page p = store.getPage(trunkPage);
        PageStreamTrunk t = (PageStreamTrunk) p;
        if (t == null) {
            throw DbException.throwInternalError("log.removeUntil not found: " + firstDataPageToKeep + " last " + last);
        }
        logKey = t.getLogKey();
        last = t.getPos();
        if (t.contains(firstDataPageToKeep)) {
            return last;
        }
        trunkPage = t.getNextTrunk();
        IntArray list = new IntArray();
        list.add(t.getPos());
        for (int i = 0; ; i++) {
            int next = t.getPageData(i);
            if (next == -1) {
                break;
            }
            list.add(next);
        }
        freeLogPages(list);
        pageOut.free(t);
    }
}
Also used : IntArray(org.h2.util.IntArray)

Example 12 with Page

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

the class PageStore method writeVariableHeader.

private void writeVariableHeader() {
    trace.debug("writeVariableHeader");
    if (logMode == LOG_MODE_SYNC) {
        file.sync();
    }
    Data page = createData();
    page.writeInt(0);
    page.writeLong(getWriteCountTotal());
    page.writeInt(logKey);
    page.writeInt(logFirstTrunkPage);
    page.writeInt(logFirstDataPage);
    CRC32 crc = new CRC32();
    crc.update(page.getBytes(), 4, pageSize - 4);
    page.setInt(0, (int) crc.getValue());
    file.seek(pageSize);
    file.write(page.getBytes(), 0, pageSize);
    file.seek(pageSize + pageSize);
    file.write(page.getBytes(), 0, pageSize);
// don't increment the write counter, because it was just written
}
Also used : CRC32(java.util.zip.CRC32) CreateTableData(org.h2.command.ddl.CreateTableData)

Example 13 with Page

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

the class PageStore method readVariableHeader.

private void readVariableHeader() {
    Data page = createData();
    for (int i = 1; ; i++) {
        if (i == 3) {
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, fileName);
        }
        page.reset();
        readPage(i, page);
        CRC32 crc = new CRC32();
        crc.update(page.getBytes(), 4, pageSize - 4);
        int expected = (int) crc.getValue();
        int got = page.readInt();
        if (expected == got) {
            writeCountBase = page.readLong();
            logKey = page.readInt();
            logFirstTrunkPage = page.readInt();
            logFirstDataPage = page.readInt();
            break;
        }
    }
}
Also used : CRC32(java.util.zip.CRC32) CreateTableData(org.h2.command.ddl.CreateTableData)

Example 14 with Page

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

the class PageStore method getPage.

/**
 * Read a page from the store.
 *
 * @param pageId the page id
 * @return the page
 */
public synchronized Page getPage(int pageId) {
    Page p = (Page) cache.get(pageId);
    if (p != null) {
        return p;
    }
    Data data = createData();
    readPage(pageId, data);
    int type = data.readByte();
    if (type == Page.TYPE_EMPTY) {
        return null;
    }
    data.readShortInt();
    data.readInt();
    if (!checksumTest(data.getBytes(), pageId, pageSize)) {
        throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "wrong checksum");
    }
    switch(type & ~Page.FLAG_LAST) {
        case Page.TYPE_FREE_LIST:
            p = PageFreeList.read(this, data, pageId);
            break;
        case Page.TYPE_DATA_LEAF:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageDataIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a data index " + indexId + " " + idx);
                }
                PageDataIndex index = (PageDataIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageDataLeaf.read(index, data, pageId);
                break;
            }
        case Page.TYPE_DATA_NODE:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageDataIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a data index " + indexId + " " + idx);
                }
                PageDataIndex index = (PageDataIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageDataNode.read(index, data, pageId);
                break;
            }
        case Page.TYPE_DATA_OVERFLOW:
            {
                p = PageDataOverflow.read(this, data, pageId);
                if (statistics != null) {
                    statisticsIncrement("overflow read");
                }
                break;
            }
        case Page.TYPE_BTREE_LEAF:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageBtreeIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a btree index " + indexId + " " + idx);
                }
                PageBtreeIndex index = (PageBtreeIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageBtreeLeaf.read(index, data, pageId);
                break;
            }
        case Page.TYPE_BTREE_NODE:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageBtreeIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a btree index " + indexId + " " + idx);
                }
                PageBtreeIndex index = (PageBtreeIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageBtreeNode.read(index, data, pageId);
                break;
            }
        case Page.TYPE_STREAM_TRUNK:
            p = PageStreamTrunk.read(this, data, pageId);
            break;
        case Page.TYPE_STREAM_DATA:
            p = PageStreamData.read(this, data, pageId);
            break;
        default:
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "page=" + pageId + " type=" + type);
    }
    cache.put(p);
    return p;
}
Also used : PageDataIndex(org.h2.index.PageDataIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) CreateTableData(org.h2.command.ddl.CreateTableData) PageIndex(org.h2.index.PageIndex)

Example 15 with Page

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

Aggregations

CreateTableData (org.h2.command.ddl.CreateTableData)9 Value (org.h2.value.Value)8 Data (org.h2.store.Data)7 Column (org.h2.table.Column)6 Page (org.h2.mvstore.Page)5 IndexColumn (org.h2.table.IndexColumn)5 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)4 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)4 UnsafeMemoryProvider (org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider)4 PageMemory (org.apache.ignite.internal.pagemem.PageMemory)4 PageMemoryNoStoreImpl (org.apache.ignite.internal.pagemem.impl.PageMemoryNoStoreImpl)4 DataRegionMetricsImpl (org.apache.ignite.internal.processors.cache.persistence.DataRegionMetricsImpl)4 GridCacheSqlQuery (org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)4 Row (org.h2.result.Row)4 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 CRC32 (java.util.zip.CRC32)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3