Search in sources :

Example 6 with Store

use of org.h2.mvstore.db.MVTableEngine.Store 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 7 with Store

use of org.h2.mvstore.db.MVTableEngine.Store 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 8 with Store

use of org.h2.mvstore.db.MVTableEngine.Store 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 9 with Store

use of org.h2.mvstore.db.MVTableEngine.Store 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 10 with Store

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

the class MVTable method rebuildIndexBlockMerge.

private void rebuildIndexBlockMerge(Session session, MVIndex index) {
    if (index instanceof MVSpatialIndex) {
        // the spatial index doesn't support multi-way merge sort
        rebuildIndexBuffered(session, index);
    }
    // Read entries in memory, sort them, write to a new map (in sorted
    // order); repeat (using a new map for every block of 1 MB) until all
    // record are read. Merge all maps to the target (using merge sort;
    // duplicates are detected in the target). For randomly ordered data,
    // this should use relatively few write operations.
    // A possible optimization is: change the buffer size from "row count"
    // to "amount of memory", and buffer index keys instead of rows.
    Index scan = getScanIndex(session);
    long remaining = scan.getRowCount(session);
    long total = remaining;
    Cursor cursor = scan.find(session, null, null);
    long i = 0;
    Store store = session.getDatabase().getMvStore();
    int bufferSize = database.getMaxMemoryRows() / 2;
    ArrayList<Row> buffer = new ArrayList<>(bufferSize);
    String n = getName() + ":" + index.getName();
    int t = MathUtils.convertLongToInt(total);
    ArrayList<String> bufferNames = New.arrayList();
    while (cursor.next()) {
        Row row = cursor.get();
        buffer.add(row);
        database.setProgress(DatabaseEventListener.STATE_CREATE_INDEX, n, MathUtils.convertLongToInt(i++), t);
        if (buffer.size() >= bufferSize) {
            sortRows(buffer, index);
            String mapName = store.nextTemporaryMapName();
            index.addRowsToBuffer(buffer, mapName);
            bufferNames.add(mapName);
            buffer.clear();
        }
        remaining--;
    }
    sortRows(buffer, index);
    if (!bufferNames.isEmpty()) {
        String mapName = store.nextTemporaryMapName();
        index.addRowsToBuffer(buffer, mapName);
        bufferNames.add(mapName);
        buffer.clear();
        index.addBufferedRows(bufferNames);
    } else {
        addRowsToIndex(session, buffer, index);
    }
    if (SysProperties.CHECK && remaining != 0) {
        DbException.throwInternalError("rowcount remaining=" + remaining + " " + getName());
    }
}
Also used : ArrayList(java.util.ArrayList) Store(org.h2.mvstore.db.MVTableEngine.Store) Index(org.h2.index.Index) MultiVersionIndex(org.h2.index.MultiVersionIndex) Row(org.h2.result.Row) Cursor(org.h2.index.Cursor) Constraint(org.h2.constraint.Constraint)

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