Search in sources :

Example 16 with OperationStatus

use of com.sleepycat.je.OperationStatus 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 17 with OperationStatus

use of com.sleepycat.je.OperationStatus 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 18 with OperationStatus

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

the class BdbStorageEngine method get.

@Override
public List<Versioned<byte[]>> get(ByteArray key, byte[] transforms) throws PersistenceFailureException {
    StoreUtils.assertValidKey(key);
    DatabaseEntry keyEntry = new DatabaseEntry(key.get());
    DatabaseEntry valueEntry = new DatabaseEntry();
    long startTimeNs = -1;
    if (logger.isTraceEnabled())
        startTimeNs = System.nanoTime();
    try {
        // uncommitted reads are perfectly fine now, since we have no
        // je-delete() in put()
        OperationStatus status = getBdbDatabase().get(null, keyEntry, valueEntry, readLockMode);
        if (OperationStatus.SUCCESS == status) {
            return StoreBinaryFormat.fromByteArray(valueEntry.getData());
        } else {
            return Collections.emptyList();
        }
    } catch (DatabaseException e) {
        this.bdbEnvironmentStats.reportException(e);
        logger.error(e);
        throw new PersistenceFailureException(e);
    } finally {
        if (logger.isTraceEnabled()) {
            logger.trace("Completed GET (" + getName() + ") from key " + key + " (keyRef: " + System.identityHashCode(key) + ") in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
        }
    }
}
Also used : 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 19 with OperationStatus

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

the class BdbStorageEngine method putAndUnlock.

@Override
public void putAndUnlock(ByteArray key, KeyLockHandle<byte[]> handle) {
    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;
    try {
        transaction = (Transaction) handle.getKeyLock();
        valueEntry.setData(StoreBinaryFormat.toByteArray(handle.getValues()));
        OperationStatus status = getBdbDatabase().put(transaction, keyEntry, valueEntry);
        if (status != OperationStatus.SUCCESS)
            throw new PersistenceFailureException("putAndUnlock operation failed with status: " + status);
        succeeded = true;
    } catch (DatabaseException e) {
        this.bdbEnvironmentStats.reportException(e);
        logger.error("Error in putAndUnlock for store " + this.getName(), e);
        throw new PersistenceFailureException(e);
    } finally {
        if (succeeded)
            attemptCommit(transaction);
        else
            attemptAbort(transaction);
        if (logger.isTraceEnabled()) {
            logger.trace("Completed PUTANDUNLOCK (" + getName() + ") to key " + key + " (keyRef: " + System.identityHashCode(key) + " in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
        }
        handle.close();
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) AsyncOperationStatus(voldemort.server.protocol.admin.AsyncOperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) PersistenceFailureException(voldemort.store.PersistenceFailureException) DatabaseException(com.sleepycat.je.DatabaseException)

Example 20 with OperationStatus

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

the class BdbConvertNewDupToPidScan method transfer.

@Override
public void transfer() throws Exception {
    cursor = srcDB.openCursor(null, null);
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    HashFunction hash = new FnvHashFunction();
    int totalPartitions = cluster.getNumberOfPartitions();
    List<Versioned<byte[]>> vals;
    long startTime = System.currentTimeMillis();
    int scanCount = 0;
    int keyCount = 0;
    while (cursor.getNext(keyEntry, valueEntry, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
        keyCount++;
        vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
        scanCount += vals.size();
        int partition = BdbConvertData.abs(hash.hash(keyEntry.getData())) % (Math.max(1, totalPartitions));
        OperationStatus putStatus = dstDB.put(null, new DatabaseEntry(StoreBinaryFormat.makePrefixedKey(keyEntry.getData(), partition)), valueEntry);
        if (OperationStatus.SUCCESS != putStatus) {
            String errorStr = "Put failed with " + putStatus + " for key" + BdbConvertData.writeAsciiString(keyEntry.getData());
            logger.error(errorStr);
            throw new Exception(errorStr);
        }
        if (scanCount % 1000000 == 0)
            logger.info("Reverted " + scanCount + " entries in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
    }
    logger.info("Converted " + scanCount + " entries and " + keyCount + " keys in " + (System.currentTimeMillis() - startTime) / 1000 + " secs");
}
Also used : FnvHashFunction(voldemort.utils.FnvHashFunction) HashFunction(voldemort.utils.HashFunction) Versioned(voldemort.versioning.Versioned) FnvHashFunction(voldemort.utils.FnvHashFunction) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry)

Aggregations

OperationStatus (com.sleepycat.je.OperationStatus)24 DatabaseEntry (com.sleepycat.je.DatabaseEntry)23 Transaction (com.sleepycat.je.Transaction)13 Versioned (voldemort.versioning.Versioned)9 DatabaseException (com.sleepycat.je.DatabaseException)6 Cursor (com.sleepycat.je.Cursor)5 AsyncOperationStatus (voldemort.server.protocol.admin.AsyncOperationStatus)5 PersistenceFailureException (voldemort.store.PersistenceFailureException)5 ArrayList (java.util.ArrayList)4 CursorConfig (com.sleepycat.je.CursorConfig)2 LockMode (com.sleepycat.je.LockMode)2 ObjectId (org.locationtech.geogig.api.ObjectId)2 IdentitySerializer (voldemort.serialization.IdentitySerializer)2 VersionedSerializer (voldemort.serialization.VersionedSerializer)2 FnvHashFunction (voldemort.utils.FnvHashFunction)2 HashFunction (voldemort.utils.HashFunction)2 VectorClock (voldemort.versioning.VectorClock)2 EnvironmentLockedException (com.sleepycat.je.EnvironmentLockedException)1 DuplicateEntryException (com.sleepycat.je.tree.DuplicateEntryException)1 WebURL (edu.uci.ics.crawler4j.url.WebURL)1