Search in sources :

Example 6 with Page

use of org.h2.store.Page in project ignite by apache.

the class GridMapQueryExecutor method onQueryRequest0.

/**
 * @param node Node authored request.
 * @param reqId Request ID.
 * @param segmentId index segment ID.
 * @param schemaName Schema name.
 * @param qrys Queries to execute.
 * @param cacheIds Caches which will be affected by these queries.
 * @param topVer Topology version.
 * @param partsMap Partitions map for unstable topology.
 * @param parts Explicit partitions for current node.
 * @param pageSize Page size.
 * @param distributedJoinMode Query distributed join mode.
 * @param lazy Streaming flag.
 */
private void onQueryRequest0(final ClusterNode node, final long reqId, final int segmentId, final String schemaName, final Collection<GridCacheSqlQuery> qrys, final List<Integer> cacheIds, final AffinityTopologyVersion topVer, final Map<UUID, int[]> partsMap, final int[] parts, final int pageSize, final DistributedJoinMode distributedJoinMode, final boolean enforceJoinOrder, final boolean replicated, final int timeout, final Object[] params, boolean lazy) {
    if (lazy && MapQueryLazyWorker.currentWorker() == null) {
        // Lazy queries must be re-submitted to dedicated workers.
        MapQueryLazyWorkerKey key = new MapQueryLazyWorkerKey(node.id(), reqId, segmentId);
        MapQueryLazyWorker worker = new MapQueryLazyWorker(ctx.igniteInstanceName(), key, log, this);
        worker.submit(new Runnable() {

            @Override
            public void run() {
                onQueryRequest0(node, reqId, segmentId, schemaName, qrys, cacheIds, topVer, partsMap, parts, pageSize, distributedJoinMode, enforceJoinOrder, replicated, timeout, params, true);
            }
        });
        if (lazyWorkerBusyLock.enterBusy()) {
            try {
                MapQueryLazyWorker oldWorker = lazyWorkers.put(key, worker);
                if (oldWorker != null)
                    oldWorker.stop();
                IgniteThread thread = new IgniteThread(worker);
                thread.start();
            } finally {
                lazyWorkerBusyLock.leaveBusy();
            }
        } else
            log.info("Ignored query request (node is stopping) [nodeId=" + node.id() + ", reqId=" + reqId + ']');
        return;
    }
    // Prepare to run queries.
    GridCacheContext<?, ?> mainCctx = !F.isEmpty(cacheIds) ? ctx.cache().context().cacheContext(cacheIds.get(0)) : null;
    MapNodeResults nodeRess = resultsForNode(node.id());
    MapQueryResults qr = null;
    List<GridReservable> reserved = new ArrayList<>();
    try {
        if (topVer != null) {
            // Reserve primary for topology version or explicit partitions.
            if (!reservePartitions(cacheIds, topVer, parts, reserved)) {
                // Unregister lazy worker because re-try may never reach this node again.
                if (lazy)
                    stopAndUnregisterCurrentLazyWorker();
                sendRetry(node, reqId, segmentId);
                return;
            }
        }
        qr = new MapQueryResults(h2, reqId, qrys.size(), mainCctx, MapQueryLazyWorker.currentWorker());
        if (nodeRess.put(reqId, segmentId, qr) != null)
            throw new IllegalStateException();
        // Prepare query context.
        GridH2QueryContext qctx = new GridH2QueryContext(ctx.localNodeId(), node.id(), reqId, segmentId, replicated ? REPLICATED : MAP).filter(h2.backupFilter(topVer, parts)).partitionsMap(partsMap).distributedJoinMode(distributedJoinMode).pageSize(pageSize).topologyVersion(topVer).reservations(reserved);
        Connection conn = h2.connectionForSchema(schemaName);
        H2Utils.setupConnection(conn, distributedJoinMode != OFF, enforceJoinOrder);
        GridH2QueryContext.set(qctx);
        // qctx is set, we have to release reservations inside of it.
        reserved = null;
        try {
            if (nodeRess.cancelled(reqId)) {
                GridH2QueryContext.clear(ctx.localNodeId(), node.id(), reqId, qctx.type());
                nodeRess.cancelRequest(reqId);
                throw new QueryCancelledException();
            }
            // Run queries.
            int qryIdx = 0;
            boolean evt = mainCctx != null && mainCctx.events().isRecordable(EVT_CACHE_QUERY_EXECUTED);
            for (GridCacheSqlQuery qry : qrys) {
                ResultSet rs = null;
                // If we are not the target node for this replicated query, just ignore it.
                if (qry.node() == null || (segmentId == 0 && qry.node().equals(ctx.localNodeId()))) {
                    rs = h2.executeSqlQueryWithTimer(conn, qry.query(), F.asList(qry.parameters(params)), true, timeout, qr.queryCancel(qryIdx));
                    if (evt) {
                        ctx.event().record(new CacheQueryExecutedEvent<>(node, "SQL query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.SQL.name(), mainCctx.name(), null, qry.query(), null, null, params, node.id(), null));
                    }
                    assert rs instanceof JdbcResultSet : rs.getClass();
                }
                qr.addResult(qryIdx, qry, node.id(), rs, params);
                if (qr.cancelled()) {
                    qr.result(qryIdx).close();
                    throw new QueryCancelledException();
                }
                // Send the first page.
                sendNextPage(nodeRess, node, qr, qryIdx, segmentId, pageSize);
                qryIdx++;
            }
            // All request results are in the memory in result set already, so it's ok to release partitions.
            if (!lazy)
                releaseReservations();
        } catch (Throwable e) {
            releaseReservations();
            throw e;
        }
    } catch (Throwable e) {
        if (qr != null) {
            nodeRess.remove(reqId, segmentId, qr);
            qr.cancel(false);
        }
        // Unregister worker after possible cancellation.
        if (lazy)
            stopAndUnregisterCurrentLazyWorker();
        if (X.hasCause(e, GridH2RetryException.class))
            sendRetry(node, reqId, segmentId);
        else {
            U.error(log, "Failed to execute local query.", e);
            sendError(node, reqId, e);
            if (e instanceof Error)
                throw (Error) e;
        }
    } finally {
        if (reserved != null) {
            // Release reserved partitions.
            for (int i = 0; i < reserved.size(); i++) reserved.get(i).release();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) GridH2RetryException(org.apache.ignite.internal.processors.query.h2.opt.GridH2RetryException) GridReservable(org.apache.ignite.internal.processors.cache.distributed.dht.GridReservable) ResultSet(java.sql.ResultSet) JdbcResultSet(org.h2.jdbc.JdbcResultSet) IgniteThread(org.apache.ignite.thread.IgniteThread) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext) JdbcResultSet(org.h2.jdbc.JdbcResultSet)

Example 7 with Page

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

the class PageBtreeIndex method writeRow.

/**
 * Write a row to the data page at the given offset.
 *
 * @param data the data
 * @param offset the offset
 * @param onlyPosition whether only the position of the row is stored
 * @param row the row to write
 */
void writeRow(Data data, int offset, SearchRow row, boolean onlyPosition) {
    data.setPos(offset);
    data.writeVarLong(row.getKey());
    if (!onlyPosition) {
        for (Column col : columns) {
            int idx = col.getColumnId();
            data.writeValue(row.getValue(idx));
        }
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn)

Example 8 with Page

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

the class PageBtreeIndex method readRow.

/**
 * Read a row from the data page at the given offset.
 *
 * @param data the data
 * @param offset the offset
 * @param onlyPosition whether only the position of the row is stored
 * @param needData whether the row data is required
 * @return the row
 */
SearchRow readRow(Data data, int offset, boolean onlyPosition, boolean needData) {
    synchronized (data) {
        data.setPos(offset);
        long key = data.readVarLong();
        if (onlyPosition) {
            if (needData) {
                return tableData.getRow(null, key);
            }
            SearchRow row = table.getTemplateSimpleRow(true);
            row.setKey(key);
            return row;
        }
        SearchRow row = table.getTemplateSimpleRow(columns.length == 1);
        row.setKey(key);
        for (Column col : columns) {
            int idx = col.getColumnId();
            row.setValue(idx, data.readValue());
        }
        return row;
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) SearchRow(org.h2.result.SearchRow)

Example 9 with Page

use of org.h2.store.Page 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 10 with Page

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

the class PageBtreeNode method remove.

@Override
SearchRow remove(SearchRow row) {
    int at = find(row, false, false, true);
    // merge is not implemented to allow concurrent usage
    // TODO maybe implement merge
    PageBtree page = index.getPage(childPageIds[at]);
    SearchRow last = page.remove(row);
    index.getPageStore().logUndo(this, data);
    updateRowCount(-1);
    written = false;
    changeCount = index.getPageStore().getChangeCount();
    if (last == null) {
        // the last row didn't change - nothing to do
        return null;
    } else if (last == row) {
        // this child is now empty
        index.getPageStore().free(page.getPos());
        if (entryCount < 1) {
            // no more children - this page is empty as well
            return row;
        }
        if (at == entryCount) {
            // removing the last child
            last = getRow(at - 1);
        } else {
            last = null;
        }
        removeChild(at);
        index.getPageStore().update(this);
        return last;
    }
    // the last row is in the last child
    if (at == entryCount) {
        return last;
    }
    int child = childPageIds[at];
    removeChild(at);
    // TODO this can mean only the position is now stored
    // should split at the next possible moment
    addChild(at, child, last);
    // remove and add swapped two children, fix that
    int temp = childPageIds[at];
    childPageIds[at] = childPageIds[at + 1];
    childPageIds[at + 1] = temp;
    index.getPageStore().update(this);
    return null;
}
Also used : SearchRow(org.h2.result.SearchRow)

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