Search in sources :

Example 6 with Change

use of org.h2.mvstore.db.TransactionStore.Change in project h2database by h2database.

the class Session method rollbackTo.

/**
 * Partially roll back the current transaction.
 *
 * @param savepoint the savepoint to which should be rolled back
 * @param trimToSize if the list should be trimmed
 */
public void rollbackTo(Savepoint savepoint, boolean trimToSize) {
    int index = savepoint == null ? 0 : savepoint.logIndex;
    while (undoLog.size() > index) {
        UndoLogRecord entry = undoLog.getLast();
        entry.undo(this);
        undoLog.removeLast(trimToSize);
    }
    if (transaction != null) {
        long savepointId = savepoint == null ? 0 : savepoint.transactionSavepoint;
        HashMap<String, MVTable> tableMap = database.getMvStore().getTables();
        Iterator<Change> it = transaction.getChanges(savepointId);
        while (it.hasNext()) {
            Change c = it.next();
            MVTable t = tableMap.get(c.mapName);
            if (t != null) {
                long key = ((ValueLong) c.key).getLong();
                ValueArray value = (ValueArray) c.value;
                short op;
                Row row;
                if (value == null) {
                    op = UndoLogRecord.INSERT;
                    row = t.getRow(this, key);
                } else {
                    op = UndoLogRecord.DELETE;
                    row = createRow(value.getList(), Row.MEMORY_CALCULATE);
                }
                row.setKey(key);
                UndoLogRecord log = new UndoLogRecord(t, op, row);
                log.undo(this);
            }
        }
    }
    if (savepoints != null) {
        String[] names = savepoints.keySet().toArray(new String[savepoints.size()]);
        for (String name : names) {
            Savepoint sp = savepoints.get(name);
            int savepointIndex = sp.logIndex;
            if (savepointIndex > index) {
                savepoints.remove(name);
            }
        }
    }
}
Also used : ValueLong(org.h2.value.ValueLong) MVTable(org.h2.mvstore.db.MVTable) ValueString(org.h2.value.ValueString) Change(org.h2.mvstore.db.TransactionStore.Change) Constraint(org.h2.constraint.Constraint) Row(org.h2.result.Row) ValueArray(org.h2.value.ValueArray)

Example 7 with Change

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

Example 8 with Change

use of org.h2.mvstore.db.TransactionStore.Change in project h2database by h2database.

the class MVSecondaryIndex method addBufferedRows.

@Override
public void addBufferedRows(List<String> bufferNames) {
    ArrayList<String> mapNames = new ArrayList<>(bufferNames);
    CompareMode compareMode = database.getCompareMode();
    int buffersCount = bufferNames.size();
    Queue<Source> queue = new PriorityQueue<>(buffersCount, new Source.Comparator(compareMode));
    for (String bufferName : bufferNames) {
        Iterator<ValueArray> iter = openMap(bufferName).keyIterator(null);
        if (iter.hasNext()) {
            queue.add(new Source(iter));
        }
    }
    try {
        while (!queue.isEmpty()) {
            Source s = queue.remove();
            ValueArray rowData = s.next();
            if (indexType.isUnique()) {
                Value[] array = rowData.getList();
                // don't change the original value
                array = array.clone();
                array[keyColumns - 1] = ValueLong.MIN;
                ValueArray unique = ValueArray.get(array);
                SearchRow row = convertToSearchRow(rowData);
                if (!mayHaveNullDuplicates(row)) {
                    requireUnique(row, dataMap, unique);
                }
            }
            dataMap.putCommitted(rowData, ValueNull.INSTANCE);
            if (s.hasNext()) {
                queue.offer(s);
            }
        }
    } finally {
        for (String tempMapName : mapNames) {
            MVMap<ValueArray, Value> map = openMap(tempMapName);
            map.getStore().removeMap(map);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PriorityQueue(java.util.PriorityQueue) Value(org.h2.value.Value) CompareMode(org.h2.value.CompareMode) ValueArray(org.h2.value.ValueArray) SearchRow(org.h2.result.SearchRow)

Example 9 with Change

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

use of org.h2.mvstore.db.TransactionStore.Change in project h2database by h2database.

the class CipherFactory method setKeystore.

private static void setKeystore() throws IOException {
    Properties p = System.getProperties();
    if (p.getProperty(KEYSTORE_KEY) == null) {
        String fileName = KEYSTORE;
        byte[] data = getKeyStoreBytes(getKeyStore(KEYSTORE_PASSWORD), KEYSTORE_PASSWORD);
        boolean needWrite = true;
        if (FileUtils.exists(fileName) && FileUtils.size(fileName) == data.length) {
            // don't need to overwrite the file if it did not change
            InputStream fin = FileUtils.newInputStream(fileName);
            byte[] now = IOUtils.readBytesAndClose(fin, 0);
            if (now != null && Arrays.equals(data, now)) {
                needWrite = false;
            }
        }
        if (needWrite) {
            try {
                OutputStream out = FileUtils.newOutputStream(fileName, false);
                out.write(data);
                out.close();
            } catch (Exception e) {
                throw DbException.convertToIOException(e);
            }
        }
        String absolutePath = FileUtils.toRealPath(fileName);
        System.setProperty(KEYSTORE_KEY, absolutePath);
    }
    if (p.getProperty(KEYSTORE_PASSWORD_KEY) == null) {
        System.setProperty(KEYSTORE_PASSWORD_KEY, KEYSTORE_PASSWORD);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) SysProperties(org.h2.engine.SysProperties) Properties(java.util.Properties) DbException(org.h2.message.DbException) IOException(java.io.IOException)

Aggregations

DbException (org.h2.message.DbException)7 IOException (java.io.IOException)5 PreparedStatement (java.sql.PreparedStatement)5 Statement (java.sql.Statement)4 ArrayList (java.util.ArrayList)4 ExpressionColumn (org.h2.expression.ExpressionColumn)4 Row (org.h2.result.Row)4 SearchRow (org.h2.result.SearchRow)4 Column (org.h2.table.Column)4 SQLException (java.sql.SQLException)3 Properties (java.util.Properties)3 Index (org.h2.index.Index)3 MVStore (org.h2.mvstore.MVStore)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Document (org.apache.lucene.document.Document)2 QueryParser (org.apache.lucene.queryParser.QueryParser)2 Constraint (org.h2.constraint.Constraint)2