Package com.sleepycat.je

Examples of com.sleepycat.je.Transaction


    return new DatabaseEntry(makeBytes(lookup, SEPARATOR, revision));
  }

  @Override
  public Collection<RevValue> getRecord(User user, byte[] key) throws IOException {
    Transaction txn = null;
    try {
      txn = env.beginTransaction(null, null);
      OperationStatus lookupExists = db.getSearchBoth(txn, makeStoresKey(user), new DatabaseEntry(key), null);
      if (OperationStatus.SUCCESS != lookupExists){
        txn.commit();
        return null;
      }
      Collection<RevValue> collection = new ArrayList<RevValue>();
      Cursor cursor = db.openCursor(txn, null);
      try {
        byte[] lookup = makeLookupBytes(user, key);
        DatabaseEntry lookupKey = new DatabaseEntry(lookup);

        DatabaseEntry revision = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        DatabaseEntry revisionKey;
        OperationStatus status = cursor.getSearchKey(lookupKey, revision, null);
        for (; OperationStatus.SUCCESS == status; status =
            cursor.getNextDup(lookupKey, revision, null)) {
          revisionKey = makeValueKey(lookup, revision.getData());
          OperationStatus valStatus = db.get(txn, revisionKey, value, null);
          if (OperationStatus.SUCCESS == valStatus) {
            collection.add(new RevValue(revision.getData(), value.getData()));
          } else {
            // TODO(drt24) revision exists but value does not.
          }
        }
      } finally {
        cursor.close();
      }
      txn.commit();
      return collection;

    } catch (DatabaseException e) {
      severe("Exception while getting record", e);
      try {
        if (txn != null)
          txn.abort();
      } catch (DatabaseException e1) {
        // we already had a failure, ignore this one.
      }
      throw new IOException(e);
    }
View Full Code Here


    }
  }

  @Override
  public Collection<byte[]> getIndices(User user) throws IOException {
    Transaction txn = null;
    Cursor cursor = null;
    try {
      txn = env.beginTransaction(null, null);
      Collection<byte[]> indices = new ArrayList<byte[]>();
      cursor = db.openCursor(txn, null);
      try {
        DatabaseEntry storeKey = makeStoresKey(user);
        DatabaseEntry index = new DatabaseEntry();
        for (OperationStatus indexStatus = cursor.getSearchKey(storeKey, index, null); OperationStatus.SUCCESS == indexStatus;
            indexStatus = cursor.getNextDup(storeKey, index, null)) {
          indices.add(index.getData());
        }
      } finally {
        cursor.close();
      }
      txn.commit();
      return indices;
    } catch (DatabaseException e) {
      try {
        if (cursor != null)
          cursor.close();
      } catch (DatabaseException e1) {
        // already caught one so ignore this one
      }
      try {
        if (txn != null)
          txn.abort();
      } catch (DatabaseException e1) {
        // already caught one so ignore this one
      }
      throw new IOException(e);
    }
View Full Code Here

    }
  }

  @Override
  public Collection<byte[]> getRevisions(User user, byte[] key) throws IOException {
    Transaction txn = null;
    try {
      txn = env.beginTransaction(null, null);
      DatabaseEntry lookup = new DatabaseEntry(makeLookupBytes(user, key));
      DatabaseEntry revision = new DatabaseEntry();

      Collection<byte[]> revisions = new ArrayList<byte[]>();
      Cursor cursor = db.openCursor(txn, null);
      try {
        for (OperationStatus revisionStatus = cursor.getSearchKey(lookup, revision, null); OperationStatus.SUCCESS == revisionStatus;
            revisionStatus = cursor.getNextDup(lookup, revision, null)) {
          revisions.add(revision.getData());
        }
      } finally {
        cursor.close();
      }
      txn.commit();
      if (revisions.size() == 0){
        return null;
      }
      return revisions;
    } catch (DatabaseException e) {
      if (txn != null) {
        try {
          txn.abort();
        } catch (DatabaseException e1) {
          // we already had a failure, ignore this one.
        }
      }
      throw new IOException(e);
View Full Code Here

    return new DatabaseEntry(makeBytes("stores/".getBytes(), user.getPublicHash()));
  }

  @Override
  public boolean putRecord(User user, byte[] key, byte[] revision, byte[] data) {
    Transaction txn = null;
    try {
      txn = env.beginTransaction(null, null);
      DatabaseEntry storesKey = makeStoresKey(user);
      DatabaseEntry lookup = new DatabaseEntry(key);
      OperationStatus lookupExists = db.getSearchBoth(txn, storesKey, lookup, null);
      if (OperationStatus.NOTFOUND == lookupExists){
        // insert lookup
        lookupExists = db.put(txn, storesKey, lookup);
      }
      if (OperationStatus.SUCCESS != lookupExists){
        txn.abort();
        return false;
      }
      byte[] lookupKey = makeLookupBytes(user,key);
      DatabaseEntry lookupEntry = new DatabaseEntry(lookupKey);
      DatabaseEntry revisionEntry = new DatabaseEntry(revision);
      OperationStatus revisionExists = db.getSearchBoth(txn, lookupEntry, revisionEntry, null);
      if (OperationStatus.SUCCESS == revisionExists){// already exists, abort
        txn.abort();
        return false;
      }
      // Does not exist, make it exist.
      revisionExists = db.put(txn, lookupEntry, revisionEntry);
      if (OperationStatus.SUCCESS != revisionExists){
        txn.abort();
        log.warning("Could not put revision: " + revisionExists.toString());
        return false;
      }
      OperationStatus putValue = db.put(txn, new DatabaseEntry(makeBytes(lookupKey,SEPARATOR,revision)), new DatabaseEntry(data));
      if (OperationStatus.SUCCESS != putValue){
        txn.abort();
        log.warning("Could not put value: " + putValue.toString());
        return false;
      }
      txn.commit();
      return true;
    } catch (DatabaseException e){
      severe("Exception while putting record", e);
      try {
        if (txn != null)
          txn.abort();
      } catch (DatabaseException e1) {
        // we already had a failure, ignore this one.
      }
      return false;
    }
View Full Code Here

  }

  @Override
  public boolean deleteRecord(User user, byte[] key) {
    Transaction txn = null;
    try {
      txn = env.beginTransaction(null, null);
      boolean result = false;
      Cursor cursor = db.openCursor(txn, null);
      try {
      OperationStatus lookupStatus =
          cursor.getSearchBoth(makeStoresKey(user), new DatabaseEntry(key), null);
      if (OperationStatus.SUCCESS == lookupStatus) {
        OperationStatus lookupDelete = cursor.delete();
        if (OperationStatus.SUCCESS == lookupDelete) {
          result = true;
        }
      }
      } finally {
        cursor.close();
      }
      result |= deleteRevisions(user, key, txn);
      txn.commit();
      return result;
    } catch (DatabaseException e) {
      severe("Exception while deleting record", e);
      try {
        if (txn != null)
          txn.abort();
      } catch (DatabaseException e1) {
        // we already had a failure, ignore this one.
      }
      return false;
    }
View Full Code Here

             * Attempt to insert the same key as was just inserted by the other
             * thread.  We need to keep incrementing the key, since the error
             * only occurs for a non-existing key value.
             */
            int val = nextKey++ / 2;
            Transaction txn = txnBegin();
            key.setData(TestUtils.getTestArray(val));
            data.setData(TestUtils.getTestArray(val));
      boolean commit = true;
      try {
    OperationStatus status = db.put(txn, key, data);
View Full Code Here

            int keyVal = val / 2;
            int dataVal = val % 2;
            key.setData(TestUtils.getTestArray(keyVal));
            data.setData(TestUtils.getTestArray(dataVal));
            while (true) {
                Transaction txn = txnBegin();
                boolean commit = true;
                try {
                    db.putNoOverwrite(txn, key, data);
                } catch (DeadlockException DE) {
                    commit = false;
View Full Code Here

        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(isTransactional);
        dbConfig.setAllowCreate(true);
        dbConfig.setSortedDuplicates(dups);

        Transaction txn = txnBegin();
        try {
            return env.openDatabase(txn, name, dbConfig);
        } finally {
            txnCommit(txn);
        }
View Full Code Here

        config.setAllowCreate(true);

        /* Duplicates not allowed. */

        Database db = openDb("dups", true);
        Transaction txn = txnBegin();
        try {
            db.openSequence(txn, key, config);
            fail();
        } catch (IllegalArgumentException expected) {
            String msg = expected.getMessage();
View Full Code Here

        DatabaseEntry data = new DatabaseEntry();

        SequenceConfig config = new SequenceConfig();
        config.setAllowCreate(true);

        Transaction txn = txnBegin();
        Sequence seq = db.openSequence(txn, key, config);
        txnCommit(txn);

        txn = txnBegin();
View Full Code Here

TOP

Related Classes of com.sleepycat.je.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.