Search in sources :

Example 11 with TransactionMap

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

the class Recover method dumpMVStoreFile.

private void dumpMVStoreFile(PrintWriter writer, String fileName) {
    writer.println("-- MVStore");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB FOR \"" + this.getClass().getName() + ".readBlob\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB FOR \"" + this.getClass().getName() + ".readClob\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB_DB FOR \"" + this.getClass().getName() + ".readBlobDb\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB_DB FOR \"" + this.getClass().getName() + ".readClobDb\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB_MAP FOR \"" + this.getClass().getName() + ".readBlobMap\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB_MAP FOR \"" + this.getClass().getName() + ".readClobMap\";");
    resetSchema();
    setDatabaseName(fileName.substring(0, fileName.length() - Constants.SUFFIX_MV_FILE.length()));
    MVStore mv = new MVStore.Builder().fileName(fileName).readOnly().open();
    dumpLobMaps(writer, mv);
    writer.println("-- Meta");
    dumpMeta(writer, mv);
    writer.println("-- Tables");
    TransactionStore store = new TransactionStore(mv);
    try {
        store.init();
    } catch (Throwable e) {
        writeError(writer, e);
    }
    try {
        for (String mapName : mv.getMapNames()) {
            if (!mapName.startsWith("table.")) {
                continue;
            }
            String tableId = mapName.substring("table.".length());
            ValueDataType keyType = new ValueDataType(null, this, null);
            ValueDataType valueType = new ValueDataType(null, this, null);
            TransactionMap<Value, Value> dataMap = store.begin().openMap(mapName, keyType, valueType);
            Iterator<Value> dataIt = dataMap.keyIterator(null);
            boolean init = false;
            while (dataIt.hasNext()) {
                Value rowId = dataIt.next();
                Value[] values = ((ValueArray) dataMap.get(rowId)).getList();
                recordLength = values.length;
                if (!init) {
                    setStorage(Integer.parseInt(tableId));
                    // init the column types
                    for (valueId = 0; valueId < recordLength; valueId++) {
                        String columnName = storageName + "." + valueId;
                        getSQL(columnName, values[valueId]);
                    }
                    createTemporaryTable(writer);
                    init = true;
                }
                StringBuilder buff = new StringBuilder();
                buff.append("INSERT INTO O_").append(tableId).append(" VALUES(");
                for (valueId = 0; valueId < recordLength; valueId++) {
                    if (valueId > 0) {
                        buff.append(", ");
                    }
                    String columnName = storageName + "." + valueId;
                    buff.append(getSQL(columnName, values[valueId]));
                }
                buff.append(");");
                writer.println(buff.toString());
                if (storageId == 0) {
                    try {
                        SimpleRow r = new SimpleRow(values);
                        MetaRecord meta = new MetaRecord(r);
                        schema.add(meta);
                        if (meta.getObjectType() == DbObject.TABLE_OR_VIEW) {
                            String sql = values[3].getString();
                            String name = extractTableOrViewName(sql);
                            tableMap.put(meta.getId(), name);
                        }
                    } catch (Throwable t) {
                        writeError(writer, t);
                    }
                }
            }
        }
        writeSchema(writer);
        writer.println("DROP ALIAS READ_BLOB_MAP;");
        writer.println("DROP ALIAS READ_CLOB_MAP;");
        writer.println("DROP TABLE IF EXISTS INFORMATION_SCHEMA.LOB_BLOCKS;");
    } catch (Throwable e) {
        writeError(writer, e);
    } finally {
        mv.close();
    }
}
Also used : ValueDataType(org.h2.mvstore.db.ValueDataType) MVStore(org.h2.mvstore.MVStore) MetaRecord(org.h2.engine.MetaRecord) TransactionStore(org.h2.mvstore.db.TransactionStore) Value(org.h2.value.Value) SimpleRow(org.h2.result.SimpleRow) ValueArray(org.h2.value.ValueArray)

Example 12 with TransactionMap

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

the class MVSecondaryIndex method findFirstOrLast.

@Override
public Cursor findFirstOrLast(Session session, boolean first) {
    TransactionMap<Value, Value> map = getMap(session);
    Value key = first ? map.firstKey() : map.lastKey();
    while (true) {
        if (key == null) {
            return new MVStoreCursor(session, Collections.<Value>emptyList().iterator(), null);
        }
        if (((ValueArray) key).getList()[0] != ValueNull.INSTANCE) {
            break;
        }
        key = first ? map.higherKey(key) : map.lowerKey(key);
    }
    ArrayList<Value> list = New.arrayList();
    list.add(key);
    MVStoreCursor cursor = new MVStoreCursor(session, list.iterator(), null);
    cursor.next();
    return cursor;
}
Also used : Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 13 with TransactionMap

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

the class MVSecondaryIndex method remove.

@Override
public void remove(Session session, Row row) {
    ValueArray array = convertToKey(row);
    TransactionMap<Value, Value> map = getMap(session);
    try {
        Value old = map.remove(array);
        if (old == null) {
            throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL() + ": " + row.getKey());
        }
    } catch (IllegalStateException e) {
        throw mvTable.convertException(e);
    }
}
Also used : Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 14 with TransactionMap

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

the class MVSecondaryIndex method add.

@Override
public void add(Session session, Row row) {
    TransactionMap<Value, Value> map = getMap(session);
    ValueArray array = convertToKey(row);
    ValueArray unique = null;
    if (indexType.isUnique()) {
        // this will detect committed entries only
        unique = convertToKey(row);
        unique.getList()[keyColumns - 1] = ValueLong.MIN;
        if (mayHaveNullDuplicates(row)) {
            // No further unique checks required
            unique = null;
        } else {
            requireUnique(row, map, unique);
        }
    }
    try {
        map.put(array, ValueNull.INSTANCE);
    } catch (IllegalStateException e) {
        throw mvTable.convertException(e);
    }
    if (unique != null) {
        // This code expects that mayHaveDuplicates(row) == false
        Iterator<Value> it = map.keyIterator(unique, true);
        while (it.hasNext()) {
            ValueArray k = (ValueArray) it.next();
            if (compareRows(row, convertToSearchRow(k)) != 0) {
                break;
            }
            if (map.isSameTransaction(k)) {
                continue;
            }
            if (map.get(k) != null) {
                // committed
                throw getDuplicateKeyException(k.toString());
            }
            throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, table.getName());
        }
    }
}
Also used : Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 15 with TransactionMap

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

the class MVPrimaryIndex method findFirstOrLast.

@Override
public Cursor findFirstOrLast(Session session, boolean first) {
    TransactionMap<Value, Value> map = getMap(session);
    ValueLong v = (ValueLong) (first ? map.firstKey() : map.lastKey());
    if (v == null) {
        return new MVStoreCursor(session, Collections.<Entry<Value, Value>>emptyList().iterator());
    }
    Value value = map.get(v);
    Entry<Value, Value> e = new DataUtils.MapEntry<Value, Value>(v, value);
    List<Entry<Value, Value>> list = Collections.singletonList(e);
    MVStoreCursor c = new MVStoreCursor(session, list.iterator());
    c.next();
    return c;
}
Also used : Entry(java.util.Map.Entry) ValueLong(org.h2.value.ValueLong) Value(org.h2.value.Value)

Aggregations

Transaction (org.h2.mvstore.db.TransactionStore.Transaction)19 MVStore (org.h2.mvstore.MVStore)17 TransactionStore (org.h2.mvstore.db.TransactionStore)17 Value (org.h2.value.Value)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ValueArray (org.h2.value.ValueArray)6 Random (java.util.Random)5 VersionedValue (org.h2.mvstore.db.TransactionStore.VersionedValue)4 SpatialKey (org.h2.mvstore.rtree.SpatialKey)4 Task (org.h2.util.Task)4 ValueLong (org.h2.value.ValueLong)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Entry (java.util.Map.Entry)1 MetaRecord (org.h2.engine.MetaRecord)1 Session (org.h2.engine.Session)1 DbException (org.h2.message.DbException)1 Change (org.h2.mvstore.db.TransactionStore.Change)1