Search in sources :

Example 41 with Transaction

use of com.sleepycat.je.Transaction in project jvarkit by lindenb.

the class Biostar92368 method doWork.

@Override
public int doWork(List<String> args) {
    if (this.dbHome == null) {
        LOG.error("Undefined DB-Home");
        return -1;
    }
    environment = null;
    EnvironmentConfig envCfg = new EnvironmentConfig();
    this.database = null;
    Transaction txn = null;
    PrintStream out = null;
    try {
        envCfg.setAllowCreate(true);
        this.environment = new Environment(dbHome, envCfg);
        DatabaseConfig cfg = new DatabaseConfig();
        cfg.setAllowCreate(true);
        cfg.setTemporary(true);
        this.database = environment.openDatabase(txn, "interactions", cfg);
        if (args.isEmpty()) {
            LOG.info("reading stdin");
            LineIterator r = IOUtils.openStdinForLineIterator();
            load(txn, r);
            CloserUtil.close(r);
        } else {
            for (String filename : args) {
                LOG.info("reading " + filename);
                LineIterator r = IOUtils.openURIForLineIterator(filename);
                load(txn, r);
                CloserUtil.close(r);
            }
        }
        out = super.openFileOrStdoutAsPrintStream(outputFile);
        DatabaseEntry key1 = new DatabaseEntry();
        DatabaseEntry data1 = new DatabaseEntry();
        DatabaseEntry key2 = new DatabaseEntry();
        DatabaseEntry data2 = new DatabaseEntry();
        Cursor c1 = this.database.openCursor(txn, null);
        Cursor c2 = this.database.openCursor(txn, null);
        while (c1.getNext(key1, data1, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            String prot1 = StringBinding.entryToString(key1);
            boolean first = true;
            while ((first ? c2.getFirst(key2, data2, LockMode.DEFAULT) : c2.getNext(key2, data2, LockMode.DEFAULT)) == OperationStatus.SUCCESS) {
                first = false;
                String prot2 = StringBinding.entryToString(key2);
                if (prot2.compareTo(prot1) <= 0)
                    continue;
                Stack<String> path = new Stack<String>();
                path.push(prot1);
                int depth = recursive(txn, prot2, path, -1, maxDepth);
                if (depth != -1) {
                    out.println(prot1 + "\t" + prot2 + "\t" + depth);
                } else {
                // System.out.println(prot1+"\t"+prot2+"\t"+depth);
                }
                if (out.checkError())
                    break;
            }
        }
        CloserUtil.close(c2);
        CloserUtil.close(c1);
        out.flush();
        out.close();
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(this.database);
        CloserUtil.close(this.environment);
    }
}
Also used : PrintStream(java.io.PrintStream) EnvironmentConfig(com.sleepycat.je.EnvironmentConfig) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor) LineIterator(htsjdk.tribble.readers.LineIterator) Stack(java.util.Stack) Transaction(com.sleepycat.je.Transaction) Environment(com.sleepycat.je.Environment) DatabaseConfig(com.sleepycat.je.DatabaseConfig)

Example 42 with Transaction

use of com.sleepycat.je.Transaction in project bboxdb by jnidzwetzki.

the class BDBTupleStore method readTuple.

@Override
public Tuple readTuple(final String key) throws IOException {
    final DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes());
    final DatabaseEntry value = new DatabaseEntry();
    Transaction txn = null;
    if (USE_TRANSACTIONS) {
        txn = environment.beginTransaction(null, null);
    }
    final OperationStatus result = database.get(null, keyEntry, value, LockMode.DEFAULT);
    if (result != OperationStatus.SUCCESS) {
        throw new RuntimeException("Data fetch got status " + result + " for " + key);
    }
    if (txn != null) {
        txn.commit();
    }
    final ByteBuffer byteBuffer = ByteBuffer.wrap(value.getData());
    return TupleHelper.decodeTuple(byteBuffer);
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) ByteBuffer(java.nio.ByteBuffer)

Example 43 with Transaction

use of com.sleepycat.je.Transaction in project voldemort by voldemort.

the class BdbStorageEngine method put.

@Override
public void put(ByteArray key, Versioned<byte[]> value, byte[] transforms) throws PersistenceFailureException {
    long startTimeNs = -1;
    if (logger.isTraceEnabled())
        startTimeNs = System.nanoTime();
    StoreUtils.assertValidKey(key);
    DatabaseEntry keyEntry = new DatabaseEntry(key.get());
    DatabaseEntry valueEntry = new DatabaseEntry();
    boolean succeeded = false;
    Transaction transaction = null;
    List<Versioned<byte[]>> vals = null;
    try {
        transaction = environment.beginTransaction(null, null);
        // do a get for the existing values
        OperationStatus status = getBdbDatabase().get(transaction, keyEntry, valueEntry, LockMode.RMW);
        if (OperationStatus.SUCCESS == status) {
            // update
            vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
            // compare vector clocks and throw out old ones, for updates
            Iterator<Versioned<byte[]>> iter = vals.iterator();
            while (iter.hasNext()) {
                Versioned<byte[]> curr = iter.next();
                Occurred occurred = value.getVersion().compare(curr.getVersion());
                if (occurred == Occurred.BEFORE)
                    throw new ObsoleteVersionException("Key " + new String(hexCodec.encode(key.get())) + " " + value.getVersion().toString() + " is obsolete, it is no greater than the current version of " + curr.getVersion().toString() + ".");
                else if (occurred == Occurred.AFTER)
                    iter.remove();
            }
        } else {
            // insert
            vals = new ArrayList<Versioned<byte[]>>(1);
        }
        // update the new value
        vals.add(value);
        valueEntry.setData(StoreBinaryFormat.toByteArray(vals));
        status = getBdbDatabase().put(transaction, keyEntry, valueEntry);
        if (status != OperationStatus.SUCCESS)
            throw new PersistenceFailureException("Put operation failed with status: " + status);
        succeeded = true;
    } catch (DatabaseException e) {
        this.bdbEnvironmentStats.reportException(e);
        logger.error("Error in put for store " + this.getName(), e);
        throw new PersistenceFailureException(e);
    } finally {
        if (succeeded)
            attemptCommit(transaction);
        else
            attemptAbort(transaction);
        if (logger.isTraceEnabled()) {
            logger.trace("Completed PUT (" + getName() + ") to key " + key + " (keyRef: " + System.identityHashCode(key) + " value " + value + " in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
        }
    }
}
Also used : Versioned(voldemort.versioning.Versioned) DatabaseEntry(com.sleepycat.je.DatabaseEntry) PersistenceFailureException(voldemort.store.PersistenceFailureException) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) AsyncOperationStatus(voldemort.server.protocol.admin.AsyncOperationStatus) DatabaseException(com.sleepycat.je.DatabaseException) Occurred(voldemort.versioning.Occurred)

Example 44 with Transaction

use of com.sleepycat.je.Transaction in project voldemort by voldemort.

the class BdbStorageEngine method delete.

@Override
public boolean delete(ByteArray key, Version version) throws PersistenceFailureException {
    StoreUtils.assertValidKey(key);
    long startTimeNs = -1;
    if (logger.isTraceEnabled())
        startTimeNs = System.nanoTime();
    Transaction transaction = null;
    try {
        transaction = this.environment.beginTransaction(null, null);
        DatabaseEntry keyEntry = new DatabaseEntry(key.get());
        if (version == null) {
            // unversioned delete. Just blow away the whole thing
            OperationStatus status = getBdbDatabase().delete(transaction, keyEntry);
            if (OperationStatus.SUCCESS == status)
                return true;
            else
                return false;
        } else {
            // versioned deletes; need to determine what to delete
            DatabaseEntry valueEntry = new DatabaseEntry();
            // do a get for the existing values
            OperationStatus status = getBdbDatabase().get(transaction, keyEntry, valueEntry, LockMode.RMW);
            // key does not exist to begin with.
            if (OperationStatus.NOTFOUND == status)
                return false;
            List<Versioned<byte[]>> vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
            Iterator<Versioned<byte[]>> iter = vals.iterator();
            int numVersions = vals.size();
            int numDeletedVersions = 0;
            // supplied version
            while (iter.hasNext()) {
                Versioned<byte[]> curr = iter.next();
                Version currentVersion = curr.getVersion();
                if (currentVersion.compare(version) == Occurred.BEFORE) {
                    iter.remove();
                    numDeletedVersions++;
                }
            }
            if (numDeletedVersions < numVersions) {
                // we still have some valid versions
                valueEntry.setData(StoreBinaryFormat.toByteArray(vals));
                getBdbDatabase().put(transaction, keyEntry, valueEntry);
            } else {
                // we have deleted all the versions; so get rid of the entry
                // in the database
                getBdbDatabase().delete(transaction, keyEntry);
            }
            return numDeletedVersions > 0;
        }
    } catch (DatabaseException e) {
        this.bdbEnvironmentStats.reportException(e);
        logger.error(e);
        throw new PersistenceFailureException(e);
    } finally {
        attemptCommit(transaction);
        if (logger.isTraceEnabled()) {
            logger.trace("Completed DELETE (" + getName() + ") of key " + ByteUtils.toHexString(key.get()) + " (keyRef: " + System.identityHashCode(key) + ") in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
        }
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) Versioned(voldemort.versioning.Versioned) Version(voldemort.versioning.Version) OperationStatus(com.sleepycat.je.OperationStatus) AsyncOperationStatus(voldemort.server.protocol.admin.AsyncOperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseException(com.sleepycat.je.DatabaseException) PersistenceFailureException(voldemort.store.PersistenceFailureException)

Example 45 with Transaction

use of com.sleepycat.je.Transaction in project voldemort by voldemort.

the class BdbStorageEngine method releaseLock.

@Override
public void releaseLock(KeyLockHandle<byte[]> handle) {
    Transaction transaction = (Transaction) handle.getKeyLock();
    if (transaction != null) {
        attemptAbort(transaction);
    }
    handle.close();
}
Also used : Transaction(com.sleepycat.je.Transaction)

Aggregations

Transaction (com.sleepycat.je.Transaction)74 DatabaseEntry (com.sleepycat.je.DatabaseEntry)55 Database (com.sleepycat.je.Database)35 OperationStatus (com.sleepycat.je.OperationStatus)20 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)12 UUID (java.util.UUID)11 Cursor (com.sleepycat.je.Cursor)10 DatabaseConfig (com.sleepycat.je.DatabaseConfig)7 DatabaseException (com.sleepycat.je.DatabaseException)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Environment (com.sleepycat.je.Environment)5 HashMap (java.util.HashMap)5 FieldTable (org.apache.qpid.server.protocol.v0_8.FieldTable)5 NewPreparedTransaction (org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom5To6.NewPreparedTransaction)5 OldPreparedTransaction (org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom5To6.OldPreparedTransaction)5 EnvironmentConfig (com.sleepycat.je.EnvironmentConfig)4 StoreException (org.apache.qpid.server.store.StoreException)4 TupleOutput (com.sleepycat.bind.tuple.TupleOutput)3 LockMode (com.sleepycat.je.LockMode)3