Search in sources :

Example 71 with Row

use of org.h2.result.Row 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 72 with Row

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

Example 73 with Row

use of org.h2.result.Row in project h2database by h2database.

the class ResultTempTable method find.

private Cursor find(Row row) {
    if (index == null) {
        // for the case "in(select ...)", the query might
        // use an optimization and not create the index
        // up front
        createIndex();
    }
    Cursor cursor = index.find(session, row, row);
    while (cursor.next()) {
        SearchRow found = cursor.getSearchRow();
        boolean ok = true;
        Database db = session.getDatabase();
        for (int i = 0; i < row.getColumnCount(); i++) {
            if (!db.areEqual(row.getValue(i), found.getValue(i))) {
                ok = false;
                break;
            }
        }
        if (ok) {
            return cursor;
        }
    }
    return null;
}
Also used : Database(org.h2.engine.Database) Cursor(org.h2.index.Cursor)

Example 74 with Row

use of org.h2.result.Row in project h2database by h2database.

the class ResultTempTable method removeRow.

@Override
public int removeRow(Value[] values) {
    Row row = convertToRow(values);
    Cursor cursor = find(row);
    if (cursor != null) {
        row = cursor.get();
        table.removeRow(session, row);
        rowCount--;
    }
    return rowCount;
}
Also used : Cursor(org.h2.index.Cursor)

Example 75 with Row

use of org.h2.result.Row in project h2database by h2database.

the class RowList method readRow.

private Row readRow(Data buff) {
    if (buff.readByte() == 0) {
        return null;
    }
    int mem = buff.readInt();
    int columnCount = buff.readInt();
    long key = buff.readLong();
    int version = buff.readInt();
    if (readUncached) {
        key = 0;
    }
    boolean deleted = buff.readInt() == 1;
    int sessionId = buff.readInt();
    Value[] values = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        Value v;
        if (buff.readByte() == 0) {
            v = null;
        } else {
            v = buff.readValue();
            if (v.isLinkedToTable()) {
                // a temporary entry
                if (v.getTableId() == 0) {
                    session.removeAtCommit(v);
                }
            }
        }
        values[i] = v;
    }
    Row row = session.createRow(values, mem);
    row.setKey(key);
    row.setVersion(version);
    row.setDeleted(deleted);
    row.setSessionId(sessionId);
    return row;
}
Also used : Value(org.h2.value.Value)

Aggregations

Value (org.h2.value.Value)118 Row (org.h2.result.Row)49 Column (org.h2.table.Column)48 DbException (org.h2.message.DbException)44 SearchRow (org.h2.result.SearchRow)37 SQLException (java.sql.SQLException)29 Index (org.h2.index.Index)28 IndexColumn (org.h2.table.IndexColumn)24 Cursor (org.h2.index.Cursor)21 PreparedStatement (java.sql.PreparedStatement)20 ResultSet (java.sql.ResultSet)20 StatementBuilder (org.h2.util.StatementBuilder)20 ArrayList (java.util.ArrayList)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 Expression (org.h2.expression.Expression)17 Statement (java.sql.Statement)16 Constraint (org.h2.constraint.Constraint)16 ValueString (org.h2.value.ValueString)16 Connection (java.sql.Connection)15 SimpleResultSet (org.h2.tools.SimpleResultSet)15