Search in sources :

Example 16 with Cursor

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

the class PageStore method readMetaData.

private void readMetaData() {
    Cursor cursor = metaIndex.find(pageStoreSession, null, null);
    // first, create all tables
    while (cursor.next()) {
        Row row = cursor.get();
        int type = row.getValue(1).getInt();
        if (type == META_TYPE_DATA_INDEX) {
            addMeta(row, pageStoreSession, false);
        }
    }
    // now create all secondary indexes
    // otherwise the table might not be created yet
    cursor = metaIndex.find(pageStoreSession, null, null);
    while (cursor.next()) {
        Row row = cursor.get();
        int type = row.getValue(1).getInt();
        if (type != META_TYPE_DATA_INDEX) {
            addMeta(row, pageStoreSession, false);
        }
    }
}
Also used : Row(org.h2.result.Row) Cursor(org.h2.index.Cursor)

Example 17 with Cursor

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

the class PageStore method getObjectIds.

public BitSet getObjectIds() {
    BitSet f = new BitSet();
    Cursor cursor = metaIndex.find(pageStoreSession, null, null);
    while (cursor.next()) {
        Row row = cursor.get();
        int id = row.getValue(0).getInt();
        if (id > 0) {
            f.set(id);
        }
    }
    return f;
}
Also used : BitSet(java.util.BitSet) Row(org.h2.result.Row) Cursor(org.h2.index.Cursor)

Example 18 with Cursor

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

the class MVSecondaryIndex method find.

private Cursor find(Session session, SearchRow first, boolean bigger, SearchRow last) {
    ValueArray min = convertToKey(first);
    if (min != null) {
        min.getList()[keyColumns - 1] = ValueLong.MIN;
    }
    TransactionMap<Value, Value> map = getMap(session);
    if (bigger && min != null) {
        // search for the next: first skip 1, then 2, 4, 8, until
        // we have a higher key; then skip 4, 2,...
        // (binary search), until 1
        int offset = 1;
        while (true) {
            ValueArray v = (ValueArray) map.relativeKey(min, offset);
            if (v != null) {
                boolean foundHigher = false;
                for (int i = 0; i < keyColumns - 1; i++) {
                    int idx = columnIds[i];
                    Value b = first.getValue(idx);
                    if (b == null) {
                        break;
                    }
                    Value a = v.getList()[i];
                    if (database.compare(a, b) > 0) {
                        foundHigher = true;
                        break;
                    }
                }
                if (!foundHigher) {
                    offset += offset;
                    min = v;
                    continue;
                }
            }
            if (offset > 1) {
                offset /= 2;
                continue;
            }
            if (map.get(v) == null) {
                min = (ValueArray) map.higherKey(min);
                if (min == null) {
                    break;
                }
                continue;
            }
            min = v;
            break;
        }
        if (min == null) {
            return new MVStoreCursor(session, Collections.<Value>emptyList().iterator(), null);
        }
    }
    return new MVStoreCursor(session, map.keyIterator(min), last);
}
Also used : Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 19 with Cursor

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

Example 20 with Cursor

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

the class MVTable method rebuildIndexBuffered.

private void rebuildIndexBuffered(Session session, Index index) {
    Index scan = getScanIndex(session);
    long remaining = scan.getRowCount(session);
    long total = remaining;
    Cursor cursor = scan.find(session, null, null);
    long i = 0;
    int bufferSize = (int) Math.min(total, database.getMaxMemoryRows());
    ArrayList<Row> buffer = new ArrayList<>(bufferSize);
    String n = getName() + ":" + index.getName();
    int t = MathUtils.convertLongToInt(total);
    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) {
            addRowsToIndex(session, buffer, index);
        }
        remaining--;
    }
    addRowsToIndex(session, buffer, index);
    if (SysProperties.CHECK && remaining != 0) {
        DbException.throwInternalError("rowcount remaining=" + remaining + " " + getName());
    }
}
Also used : ArrayList(java.util.ArrayList) 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

Cursor (org.h2.index.Cursor)24 Value (org.h2.value.Value)20 Index (org.h2.index.Index)11 Row (org.h2.result.Row)11 SearchRow (org.h2.result.SearchRow)11 ArrayList (java.util.ArrayList)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Constraint (org.h2.constraint.Constraint)5 SingleRowCursor (org.h2.index.SingleRowCursor)5 Column (org.h2.table.Column)5 Session (org.h2.engine.Session)4 MultiVersionIndex (org.h2.index.MultiVersionIndex)4 IndexColumn (org.h2.table.IndexColumn)4 H2PkHashIndex (org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex)3 GridH2Row (org.apache.ignite.internal.processors.query.h2.opt.GridH2Row)3 Database (org.h2.engine.Database)3 ValueLong (org.h2.value.ValueLong)3 PreparedStatement (java.sql.PreparedStatement)2 BitSet (java.util.BitSet)2 UUID (java.util.UUID)2