Search in sources :

Example 1 with Operation

use of org.apache.ignite.internal.metastorage.server.Operation in project ignite-3 by apache.

the class RocksDbKeyValueStorage method invoke.

/**
 * {@inheritDoc}
 */
@Override
public boolean invoke(Condition condition, Collection<Operation> success, Collection<Operation> failure) {
    rwLock.writeLock().lock();
    try {
        Entry[] entries = getAll(Arrays.asList(condition.keys())).toArray(new Entry[] {});
        boolean branch = condition.test(entries);
        Collection<Operation> ops = branch ? success : failure;
        applyOperations(ops);
        return branch;
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Operation(org.apache.ignite.internal.metastorage.server.Operation)

Example 2 with Operation

use of org.apache.ignite.internal.metastorage.server.Operation in project ignite-3 by apache.

the class RocksDbKeyValueStorage method applyOperations.

private void applyOperations(Collection<Operation> ops) throws RocksDBException {
    long curRev = rev + 1;
    boolean modified = false;
    long counter = updCntr;
    List<byte[]> updatedKeys = new ArrayList<>();
    try (WriteBatch batch = new WriteBatch()) {
        for (Operation op : ops) {
            byte[] key = op.key();
            switch(op.type()) {
                case PUT:
                    counter++;
                    addDataToBatch(batch, key, op.value(), curRev, counter);
                    updatedKeys.add(key);
                    modified = true;
                    break;
                case REMOVE:
                    counter++;
                    boolean removed = addToBatchForRemoval(batch, key, curRev, counter);
                    if (!removed) {
                        counter--;
                    } else {
                        updatedKeys.add(key);
                    }
                    modified |= removed;
                    break;
                case NO_OP:
                    break;
                default:
                    throw new IllegalArgumentException("Unknown operation type: " + op.type());
            }
        }
        if (modified) {
            for (byte[] key : updatedKeys) {
                updateKeysIndex(batch, key, curRev);
            }
            fillAndWriteBatch(batch, curRev, counter);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Operation(org.apache.ignite.internal.metastorage.server.Operation) WriteBatch(org.rocksdb.WriteBatch)

Aggregations

Operation (org.apache.ignite.internal.metastorage.server.Operation)2 ArrayList (java.util.ArrayList)1 Entry (org.apache.ignite.internal.metastorage.server.Entry)1 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)1 RocksDBException (org.rocksdb.RocksDBException)1 WriteBatch (org.rocksdb.WriteBatch)1