com.sleepycat.je.CursorConfig Java Examples

The following examples show how to use com.sleepycat.je.CursorConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CursorTemplate.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
public void processEntries()
{
    _cursor = _database.openCursor(_transaction, CursorConfig.READ_COMMITTED);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();

    try
    {
        _iterating = true;
        while (_iterating && _cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
        {
            _databaseEntryCallback.processEntry(_database, _transaction, key, value);
        }
    }
    finally
    {
        _cursor.close();
    }
}
 
Example #2
Source File: BerkeleyDBManager.java    From WebCollector with GNU General Public License v3.0 6 votes vote down vote up
public void list() throws Exception {
        if (env == null) {
            open();
        }
        Cursor cursor = null;
        Database crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
        cursor = crawldbDatabase.openCursor(null, CursorConfig.DEFAULT);
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();

        while (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            CrawlDatum datum = BerkeleyDBUtils.createCrawlDatum(key, value);
            System.out.println(CrawlDatumFormater.datumToString(datum));
//            try {
//                CrawlDatum datum = BerkeleyDBUtils.createCrawlDatum(key, value);
//                System.out.println(CrawlDatumFormater.datumToString(datum));
//            } catch (Exception ex) {
//                LOG.info("Exception when generating", ex);
//                continue;
//            }
        }

    }
 
Example #3
Source File: BdbWrapper.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
public void replace(KeyT key, ValueT initialValue, Function<ValueT, ValueT> replacer) throws DatabaseWriteException {
  synchronized (keyEntry) {
    try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
      keyBinder.objectToEntry(key, keyEntry);
      OperationStatus searchResult = cursor.getSearchKey(keyEntry, valueEntry, LockMode.DEFAULT);
      ValueT newValue = initialValue;
      if (searchResult.equals(OperationStatus.SUCCESS)) {
        newValue = replacer.apply(valueBinder.entryToObject(valueEntry));
      }
      valueBinder.objectToEntry(newValue, valueEntry);
      cursor.putCurrent(valueEntry);
    } catch (Exception e) {
      throw new DatabaseWriteException(e);
    }
  }
}
 
Example #4
Source File: BdbWrapper.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
public boolean put(KeyT key, ValueT value) throws DatabaseWriteException {
  synchronized (keyEntry) {
    try {
      keyBinder.objectToEntry(key, keyEntry);
      if (databaseConfig.getSortedDuplicates()) {
        valueBinder.objectToEntry(value, valueEntry);
        OperationStatus operationStatus = database.putNoDupData(transaction, keyEntry, valueEntry);
        // operation status is only SUCCESS if the data was not in the database before
        return operationStatus.equals(OperationStatus.SUCCESS);
      } else {
        try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
          OperationStatus searchResult = cursor.getSearchKey(keyEntry, valueEntry, LockMode.DEFAULT);
          if (searchResult == SUCCESS && Objects.equals(value, valueBinder.entryToObject(valueEntry))) {
            return false;
          } else {
            valueBinder.objectToEntry(value, valueEntry);
            database.put(transaction, keyEntry, valueEntry); // returns OperationStatus.SUCCESS or throws an exception
            return true;
          }
        }
      }
    } catch (Exception e) {
      throw new DatabaseWriteException(e);
    }
  }
}
 
Example #5
Source File: BdbWrapper.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
public boolean delete(KeyT key, ValueT value) throws DatabaseWriteException {
  boolean wasChange = false;
  synchronized (keyEntry) {
    try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
      keyBinder.objectToEntry(key, keyEntry);
      valueBinder.objectToEntry(value, valueEntry);
      OperationStatus searchResult = cursor.getSearchBoth(keyEntry, valueEntry, LockMode.DEFAULT);
      if (searchResult.equals(OperationStatus.SUCCESS)) {
        wasChange = cursor.delete() == OperationStatus.SUCCESS;
      }
    } catch (Exception e) {
      throw new DatabaseWriteException(e);
    }
  }
  return wasChange;
}
 
Example #6
Source File: BerkeleyManager.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public K maximumIdentity(BerkeleyTransactor transactor, K from, K to) {
	CursorConfig cursorModel = transactor == null ? null : transactor.getIsolation().getCursorModel();
	Transaction transaction = transactor == null ? null : transactor.getTransaction();
	try (EntityCursor<K> cursor = primaryIndex.keys(transaction, from, true, to, false, cursorModel)) {
		return cursor.last();
	}
}
 
Example #7
Source File: BerkeleyManager.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public K minimumIdentity(BerkeleyTransactor transactor, K from, K to) {
	CursorConfig cursorModel = transactor == null ? null : transactor.getIsolation().getCursorModel();
	Transaction transaction = transactor == null ? null : transactor.getTransaction();
	try (EntityCursor<K> cursor = primaryIndex.keys(transaction, from, true, to, false, cursorModel)) {
		return cursor.first();
	}
}
 
Example #8
Source File: JE_Table.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
public ISchematicCursor getCursor(ITransaction txn, IndexMeta indexMeta, String isolation, String actualTableName)
                                                                                                                  throws TddlException {
    Database db = getDatabase(actualTableName);
    if (db == null) {
        throw new IllegalArgumentException("table don't contains indexName:" + actualTableName);
    }
    CursorConfig cc = CursorConfig.DEFAULT;
    LockMode lm = LockMode.DEFAULT;
    if (txn != null) {
        com.sleepycat.je.TransactionConfig _config = ((JE_Transaction) txn).config;
        if (_config.getReadUncommitted()) {
            cc = CursorConfig.READ_UNCOMMITTED;
            lm = LockMode.READ_UNCOMMITTED;
        } else if (_config.getReadCommitted()) {
            cc = CursorConfig.READ_COMMITTED;
            // lm = LockMode.READ_COMMITTED;
        }
    } else {
        if (Isolation.READ_COMMITTED.equals(isolation)) {
            cc = CursorConfig.READ_COMMITTED;
            // lm = LockMode.READ_COMMITTED;//not support
        } else if (Isolation.READ_UNCOMMITTED.equals(isolation)) {
            cc = CursorConfig.READ_UNCOMMITTED;
            lm = LockMode.READ_UNCOMMITTED;
        } else if (Isolation.REPEATABLE_READ.equals(isolation)) {
            // default
        } else if (Isolation.SERIALIZABLE.equals(isolation)) {
            // txn_config
        }
    }
    JE_Cursor je_cursor = new JE_Cursor(indexMeta, db.openCursor(txn == null ? null : ((JE_Transaction) txn).txn,
        cc), lm);
    if (txn != null) {
        ((JE_Transaction) txn).addCursor(je_cursor);
    }
    return new SchematicCursor(je_cursor, je_cursor.getiCursorMeta(), ExecUtils.getOrderBy(indexMeta));
}
 
Example #9
Source File: JE_Table.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Override
public ISchematicCursor getCursor(ExecutionContext executionContext, IndexMeta indexMeta, String actualTableName)
                                                                                                                 throws TddlException {
    Database db = getDatabase(actualTableName);
    if (db == null) {
        throw new IllegalArgumentException("table don't contains indexName:" + actualTableName);
    }
    ITransaction txn = executionContext.getTransaction();
    CursorConfig cc = CursorConfig.DEFAULT;
    LockMode lm = LockMode.DEFAULT;
    if (txn != null) {
        com.sleepycat.je.TransactionConfig _config = ((JE_Transaction) txn).config;
        if (_config.getReadUncommitted()) {
            cc = CursorConfig.READ_UNCOMMITTED;
            lm = LockMode.READ_UNCOMMITTED;
        } else if (_config.getReadCommitted()) {
            cc = CursorConfig.READ_COMMITTED;
            // lm = LockMode.READ_COMMITTED;
        }
    } else {

        cc = CursorConfig.READ_COMMITTED;

    }
    JE_Cursor je_cursor = new JE_Cursor(indexMeta, db.openCursor(txn == null ? null : ((JE_Transaction) txn).txn,
        cc), lm);
    if (txn != null) {
        ((JE_Transaction) txn).addCursor(je_cursor);
    }
    return new SchematicCursor(je_cursor, je_cursor.getiCursorMeta(), ExecUtils.getOrderBy(indexMeta));
}
 
Example #10
Source File: JE_Table.java    From tddl with Apache License 2.0 5 votes vote down vote up
public ISchematicCursor getCursor(ITransaction txn, IndexMeta indexMeta, String isolation, String actualTableName)
                                                                                                                  throws TddlException {
    Database db = getDatabase(actualTableName);
    if (db == null) {
        throw new TddlException("table don't contains indexName:" + actualTableName);
    }
    CursorConfig cc = CursorConfig.DEFAULT;
    LockMode lm = LockMode.DEFAULT;
    if (txn != null) {
        com.sleepycat.je.TransactionConfig _config = ((JE_Transaction) txn).config;
        if (_config.getReadUncommitted()) {
            cc = CursorConfig.READ_UNCOMMITTED;
            lm = LockMode.READ_UNCOMMITTED;
        } else if (_config.getReadCommitted()) {
            cc = CursorConfig.READ_COMMITTED;
            // lm = LockMode.READ_COMMITTED;
        }
    } else {
        if (Isolation.READ_COMMITTED.equals(isolation)) {
            cc = CursorConfig.READ_COMMITTED;
            // lm = LockMode.READ_COMMITTED;//not support
        } else if (Isolation.READ_UNCOMMITTED.equals(isolation)) {
            cc = CursorConfig.READ_UNCOMMITTED;
            lm = LockMode.READ_UNCOMMITTED;
        } else if (Isolation.REPEATABLE_READ.equals(isolation)) {
            // default
        } else if (Isolation.SERIALIZABLE.equals(isolation)) {
            // txn_config
        }
    }
    JE_Cursor je_cursor = new JE_Cursor(indexMeta,

    db.openCursor(txn == null ? null : ((JE_Transaction) txn).txn, cc), lm);
    if (txn != null) {
        ((JE_Transaction) txn).addCursor(je_cursor);
    }
    return new SchematicCursor(je_cursor, je_cursor.getiCursorMeta(), ExecUtils.getOrderBy(indexMeta));
}
 
Example #11
Source File: JE_Table.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Override
public ISchematicCursor getCursor(ExecutionContext executionContext, IndexMeta indexMeta, String actualTableName)
                                                                                                                 throws TddlException {
    Database db = getDatabase(actualTableName);
    if (db == null) {
        throw new TddlException("table don't contains indexName:" + actualTableName);
    }
    ITransaction txn = executionContext.getTransaction();
    CursorConfig cc = CursorConfig.DEFAULT;
    LockMode lm = LockMode.DEFAULT;
    if (txn != null) {
        com.sleepycat.je.TransactionConfig _config = ((JE_Transaction) txn).config;
        if (_config.getReadUncommitted()) {
            cc = CursorConfig.READ_UNCOMMITTED;
            lm = LockMode.READ_UNCOMMITTED;
        } else if (_config.getReadCommitted()) {
            cc = CursorConfig.READ_COMMITTED;
            // lm = LockMode.READ_COMMITTED;
        }
    } else {

        cc = CursorConfig.READ_COMMITTED;

    }
    JE_Cursor je_cursor = new JE_Cursor(indexMeta,

    db.openCursor(txn == null ? null : ((JE_Transaction) txn).txn, cc), lm);
    if (txn != null) {
        ((JE_Transaction) txn).addCursor(je_cursor);
    }
    return new SchematicCursor(je_cursor, je_cursor.getiCursorMeta(), ExecUtils.getOrderBy(indexMeta));
}
 
Example #12
Source File: BerkeleyDBReader.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
public BerkeleyDBReader(String crawlPath) {
    this.crawlPath = crawlPath;
    File dir = new File(crawlPath);
    EnvironmentConfig environmentConfig = new EnvironmentConfig();
    environmentConfig.setAllowCreate(true);
    env = new Environment(dir, environmentConfig);
    crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    cursor = crawldbDatabase.openCursor(null, CursorConfig.DEFAULT);
}
 
Example #13
Source File: BerkeleyKeyValueStore.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public CursorConfig getCursorConfig(TableWrapper tableWrapper) {
	if (tableWrapper.isTransactional()) {
		return safeCursorConfig;
	} else {
		return unsafeCursorConfig;
	}
}
 
Example #14
Source File: UpgradeFrom5To6.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
/**
 * @return a (sorted) map of offset -> data for the given message id
 */
private SortedMap<Integer, byte[]> getMessageData(final long messageId, final Database oldDatabase)
{
    TreeMap<Integer, byte[]> data = new TreeMap<Integer, byte[]>();

    Cursor cursor = oldDatabase.openCursor(null, CursorConfig.READ_COMMITTED);
    try
    {
        DatabaseEntry contentKeyEntry = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        CompoundKeyBinding binding = new CompoundKeyBinding();
        binding.objectToEntry(new CompoundKey(messageId, 0), contentKeyEntry);

        OperationStatus status = cursor.getSearchKeyRange(contentKeyEntry, value, LockMode.DEFAULT);
        OldDataBinding dataBinding = new OldDataBinding();

        while (status == OperationStatus.SUCCESS)
        {
            CompoundKey compoundKey = binding.entryToObject(contentKeyEntry);
            long id = compoundKey.getMessageId();

            if (id != messageId)
            {
                // we have exhausted all chunks for this message id, break
                break;
            }

            int offsetInMessage = compoundKey.getOffset();
            OldDataValue dataValue = dataBinding.entryToObject(value);
            data.put(offsetInMessage, dataValue.getData());

            status = cursor.getNext(contentKeyEntry, value, LockMode.DEFAULT);
        }
    }
    finally
    {
        cursor.close();
    }

    return data;
}
 
Example #15
Source File: BerkeleyGenerator.java    From WebCollector with GNU General Public License v3.0 4 votes vote down vote up
public BerkeleyGenerator(Environment env) {
    this.env = env;
    crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    cursor = crawldbDatabase.openCursor(null, CursorConfig.DEFAULT);
}