com.sleepycat.je.LockMode Java Examples

The following examples show how to use com.sleepycat.je.LockMode. 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: FileLineIndex.java    From bboxdb with Apache License 2.0 6 votes vote down vote up
/**
 * Get the content of the line
 * @param lineNumber
 * @return
 */
public long locateLine(final long line) {

	if(database == null) {
		throw new IllegalArgumentException("No database is open, please index file first");
	}

	if(line >= indexedLines) {
		throw new IllegalArgumentException("Line " + line + " is higher then indexedLines: " + (indexedLines - 1));
	}

	final DatabaseEntry key = buildDatabaseEntry(line);
    final DatabaseEntry value = new DatabaseEntry();

    final OperationStatus result = database.get(null, key, value, LockMode.DEFAULT);

    if (result != OperationStatus.SUCCESS) {
        throw new RuntimeException("Data fetch got status " + result + " for " + line);
    }

    final long bytePos = DataEncoderHelper.readLongFromByte(value.getData());

    return bytePos;
}
 
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: AbstractBDBMessageStore.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private void visitMessagesInternal(MessageHandler handler, EnvironmentFacade environmentFacade)
{
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    MessageMetaDataBinding valueBinding = MessageMetaDataBinding.getInstance();

    try(Cursor cursor = getMessageMetaDataDb().openCursor(null, null))
    {
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS)
        {
            long messageId = LongBinding.entryToLong(key);
            StorableMessageMetaData metaData = valueBinding.entryToObject(value);
            StoredBDBMessage message = createStoredBDBMessage(messageId, metaData, true);
            if (!handler.handle(message))
            {
                break;
            }
        }
    }
    catch (RuntimeException e)
    {
        throw environmentFacade.handleDatabaseException("Cannot visit messages", e);
    }
}
 
Example #4
Source File: AbstractBDBMessageStore.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private StoredBDBMessage<?> getMessageInternal(long messageId, EnvironmentFacade environmentFacade)
{
    try
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        MessageMetaDataBinding valueBinding = MessageMetaDataBinding.getInstance();
        LongBinding.longToEntry(messageId, key);
        if(getMessageMetaDataDb().get(null, key, value, LockMode.READ_COMMITTED) == OperationStatus.SUCCESS)
        {
            StorableMessageMetaData metaData = valueBinding.entryToObject(value);
            StoredBDBMessage message = createStoredBDBMessage(messageId, metaData, true);
            return message;
        }
        else
        {
            return null;
        }

    }
    catch (RuntimeException e)
    {
        throw environmentFacade.handleDatabaseException("Cannot visit messages", e);
    }
}
 
Example #5
Source File: AbstractBDBPreferenceStore.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private Collection<PreferenceRecord> getPreferenceRecords(final EnvironmentFacade environmentFacade)
{
    Collection<PreferenceRecord> records = new LinkedHashSet<>();

    try(Cursor cursor = getPreferencesDb().openCursor(null, null))
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
        MapBinding valueBinding = MapBinding.getInstance();

        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS)
        {
            UUID preferenceId = keyBinding.entryToObject(key);
            Map<String, Object> preferenceAttributes = valueBinding.entryToObject(value);
            PreferenceRecord record = new PreferenceRecordImpl(preferenceId, preferenceAttributes);
            records.add(record);
        }
    }
    catch (RuntimeException e)
    {
        throw environmentFacade.handleDatabaseException("Cannot visit preferences", e);
    }
    return records;
}
 
Example #6
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 #7
Source File: BerkeleyDB.java    From SPADE with GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
public void readData(int limit)
{
    Cursor cursor = scaffoldDatabase.openCursor(null, null);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry();
    int i = 0;
    while(cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
    {
        if(i >= limit)
            break;
        String keyString = new String(key.getData());
        System.out.println("hash: " + keyString);
        i++;
    }
    cursor.close();
}
 
Example #8
Source File: TempTableDisk.java    From Rel with Apache License 2.0 6 votes vote down vote up
public boolean hasNext() {
	if (current != null)
		return true;
	try {
		if (cursor == null)
			cursor = storage.openCursor(null, null);
	    DatabaseEntry foundKey = new DatabaseEntry();
	    DatabaseEntry foundData = new DatabaseEntry();	
		if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			current = (ValueTuple)database.getTupleBinding().entryToObject(foundKey); 
			return true;
		}
	} catch (DatabaseException exp) {
		throw new ExceptionFatal("RS0321: Unable to get next tuple: " + exp.getMessage());					
	}
	return false;
}
 
Example #9
Source File: UpgradeFrom7To8.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private int getConfigVersion(Database configVersionDb)
{
    Cursor cursor = null;
    try
    {
        cursor = configVersionDb.openCursor(null, null);
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        while (cursor.getNext(key, value, LockMode.RMW) == OperationStatus.SUCCESS)
        {
            return IntegerBinding.entryToInt(value);
        }
        return -1;
    }
    finally
    {
        if (cursor != null)
        {
            cursor.close();
        }
    }
}
 
Example #10
Source File: TempIndexDisk.java    From Rel with Apache License 2.0 6 votes vote down vote up
public void put(ValueTuple keyTuple, ValueTuple valueTuple) throws DatabaseException {
	DatabaseEntry theKey = new DatabaseEntry();
	database.getTupleBinding().objectToEntry(keyTuple, theKey);

	DatabaseEntry theID = new DatabaseEntry();
	
	OperationStatus status = keys.get(null, theKey, theID, LockMode.DEFAULT);
	if (status == OperationStatus.NOTFOUND) {
		LongBinding.longToEntry(id++, theID);
		keys.put(null, theKey, theID);
	} else if (status != OperationStatus.SUCCESS)
		throw new ExceptionFatal("RS0318: Insertion failure in put().");
	
	DatabaseEntry theData = new DatabaseEntry();
	database.getTupleBinding().objectToEntry(valueTuple, theData);
	
	values.put(null, theID, theData);
}
 
Example #11
Source File: JE_Table.java    From tddl with Apache License 2.0 6 votes vote down vote up
@Override
public CloneableRecord get(ExecutionContext context, CloneableRecord key, IndexMeta indexMeta, String dbName) {

    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    keyEntry.setData(indexCodecMap.get(indexMeta.getName()).getKey_codec().encode(key));
    OperationStatus status = getDatabase(dbName).get(context.getTransaction() == null ? null : ((JE_Transaction) context.getTransaction()).txn,
        keyEntry,
        valueEntry,
        LockMode.DEFAULT);
    if (OperationStatus.SUCCESS != status) {
        return null;
    }
    if (valueEntry.getSize() != 0) {
        return indexCodecMap.get(indexMeta.getName()).getValue_codec().decode(valueEntry.getData());
    } else {
        return null;
    }
}
 
Example #12
Source File: BerkeleyDBStore.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
@Override
public V load(K key) {
    try {
        DatabaseEntry keyEntry = objectToEntry(key);
        DatabaseEntry valueEntry = new DatabaseEntry();
        OperationStatus status = _db.get(null, keyEntry, valueEntry, LockMode.DEFAULT);
        if (status == OperationStatus.SUCCESS) {
            return (V) entryToObject(valueEntry);
        } else {
            return null;
        }
    } catch (Exception e) {
        _logger.log(Level.SEVERE, e.getMessage(), e);
        return null;
    }
}
 
Example #13
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public HGPersistentHandle[] getLink(HGPersistentHandle handle)
{
	try
	{
		DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
		DatabaseEntry value = new DatabaseEntry();
		if (data_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
			return (HGPersistentHandle[]) linkBinding.entryToObject(value);
		else
			return null;
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle, ex);
	}
}
 
Example #14
Source File: BDBTupleStore.java    From bboxdb with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #15
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public byte[] getData(HGPersistentHandle handle)
{
	try
	{
		DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
		DatabaseEntry value = new DatabaseEntry();
		if (primitive_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
			return value.getData();
		else
			return null;
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle, ex);
	}
}
 
Example #16
Source File: BerkeleyDBManager.java    From WebCollector with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void inject(CrawlDatums datums, boolean force) throws Exception {
    Database database = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    for (int i = 0; i < datums.size(); i++) {
        CrawlDatum datum = datums.get(i);
        DatabaseEntry key = BerkeleyDBUtils.strToEntry(datum.key());
        DatabaseEntry value = new DatabaseEntry();
        if (!force) {
            if (database.get(null, key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                continue;
            }
        }
        value = BerkeleyDBUtils.strToEntry(datum.asJsonArray().toString());
        database.put(null, key, value);
    }
    database.close();
}
 
Example #17
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public boolean containsData(HGPersistentHandle handle)
{
	DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
	DatabaseEntry value = new DatabaseEntry();
	value.setPartial(0, 0, true);
	try
	{
		if (primitive_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
		{
			return true;
		}
	}
	catch (DatabaseException ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle + ": " + ex.toString(), ex);
	}
	return false;
}
 
Example #18
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public boolean containsLink(HGPersistentHandle handle)
{
	DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
	DatabaseEntry value = new DatabaseEntry();
	value.setPartial(0, 0, true);
	try
	{
		if (data_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
		{
			// System.out.println(value.toString());
			return true;
		}
	}
	catch (DatabaseException ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle + ": " + ex.toString(), ex);
	}

	return false;
}
 
Example #19
Source File: JE_Table.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Override
public CloneableRecord get(ExecutionContext context, CloneableRecord key, IndexMeta indexMeta, String dbName) {

    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    keyEntry.setData(indexCodecMap.get(indexMeta.getName()).getKey_codec().encode(key));
    OperationStatus status = getDatabase(dbName).get(context.getTransaction() == null ? null : ((JE_Transaction) context.getTransaction()).txn,
        keyEntry,
        valueEntry,
        LockMode.DEFAULT);
    if (OperationStatus.SUCCESS != status) {
        return null;
    }
    if (valueEntry.getSize() != 0) {
        return indexCodecMap.get(indexMeta.getName()).getValue_codec().decode(valueEntry.getData());
    } else {
        return null;
    }
}
 
Example #20
Source File: DefaultBiIndexImpl.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public long countKeys(ValueType value) {
	DatabaseEntry keyEntry = new DatabaseEntry(valueConverter.toByteArray(value));
	DatabaseEntry valueEntry = new DatabaseEntry();
	SecondaryCursor cursor = null;
	
	try {
		cursor = secondaryDb.openCursor(txn().getBJETransaction(), cursorConfig);
		OperationStatus status = cursor.getSearchKey(keyEntry, valueEntry, dummy, LockMode.DEFAULT);
		if (status == OperationStatus.SUCCESS)
			return cursor.count();
		else
			return 0;
	}
	catch (DatabaseException ex) {
		throw new HGException(ex);
	}
	finally {
		if (cursor != null) {
			try {
				cursor.close();
			}
			catch (Throwable t) {
			}
		}
	}
}
 
Example #21
Source File: KeyScanResultSet.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public KeyScanResultSet(BJETxCursor cursor, DatabaseEntry keyIn, ByteArrayConverter<T> converter) {
	this.converter = converter;
	this.cursor = cursor;
	this.key = new DatabaseEntry();
	
	if (keyIn != null) {
		assignData(key, keyIn.getData());
	}
	
	try {
		cursor.cursor().getCurrent(key, data, LockMode.DEFAULT);
		next = converter.fromByteArray(key.getData(), key.getOffset(), key.getSize());
		lookahead = 1;
	}
	catch (Throwable t) {
		throw new HGException(t);
	}
}
 
Example #22
Source File: DefaultIndexImpl.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public ValueType getData(KeyType key)
{
	checkOpen();
	DatabaseEntry keyEntry = new DatabaseEntry(keyConverter.toByteArray(key));
	DatabaseEntry value = new DatabaseEntry();
	ValueType result = null;

	try
	{
		OperationStatus status = db.get(txn().getBJETransaction(), keyEntry, value, LockMode.DEFAULT);
		if (status == OperationStatus.SUCCESS)
			result = valueConverter.fromByteArray(value.getData(), value.getOffset(), value.getSize());
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to lookup index '" + name + "': " + ex.toString(), ex);
	}
	return result;
}
 
Example #23
Source File: BdbDumpTask.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(ImmutableMultimap<String, String> parameters, PrintWriter output) throws Exception {
  if (!parameters.keySet().containsAll(REQUIRED_PARAMS)) {
    output.write("Make sure you provide the following parameters: " + REQUIRED_PARAMS);
    return;
  }

  BdbWrapper<String, String> database = environmentCreator.getDatabase(
    getParam(parameters, OWNER),
    getParam(parameters, DATA_SET),
    getParam(parameters, DATABASE),
    true,
    STRING_BINDER,
    STRING_BINDER,
    new StringStringIsCleanHandler()
  );

  String prefix = parameters.containsKey("prefix") ? getParam(parameters, "prefix") : "";
  int start = parameters.containsKey("start") ? Integer.parseInt(getParam(parameters, "start")) : 0;
  int count = parameters.containsKey("count") ? Integer.parseInt(getParam(parameters, "count")) : 10;
  output.write("uncommitted data: " + database.dump(prefix, start, count, LockMode.READ_UNCOMMITTED));
  output.write("committed data: " + database.dump(prefix, start, count, LockMode.READ_COMMITTED));
}
 
Example #24
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 #25
Source File: IndexResultSet.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * Construct a result set matching a specific key.
 * </p>
 * 
 * @param cursor
 * @param key
 */
public IndexResultSet(BJETxCursor cursor, DatabaseEntry keyIn, ByteArrayConverter<T> converter) {
	/*
	 * id = idcounter++; System.out.println("Constructing index set with id " + id); StackTraceElement
	 * e[]=Thread.currentThread().getStackTrace(); for (int i=0; i <e.length; i++) { System.out.println(e[i]);
	 * }
	 */
	this.converter = converter;
	this.cursor = cursor;
	this.key = new DatabaseEntry();
	
	if (keyIn != null) {
		assignData(this.key, keyIn.getData());
	}
	
	try {
		cursor.cursor().getCurrent(key, data, LockMode.DEFAULT);
		next = converter.fromByteArray(data.getData(), data.getOffset(), data.getSize());
		lookahead = 1;
	}
	catch (Throwable t) {
		throw new HGException(t);
	}
}
 
Example #26
Source File: IndexResultSet.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public void goBeforeFirst() {
	try {
		if (cursor.cursor().getFirst(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			current = UNKNOWN;
			prev = null;
			next = converter.fromByteArray(data.getData(), data.getOffset(), data.getSize());
			lookahead = 1;
		}
		else {
			prev = next = null;
			current = UNKNOWN;
			lookahead = 0;
		}
	}
	catch (Throwable t) {
		closeNoException();
		throw new HGException(t);
	}
}
 
Example #27
Source File: IndexResultSet.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public void goAfterLast() {
	try {
		if (cursor.cursor().getLast(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			current = UNKNOWN;
			next = null;
			prev = converter.fromByteArray(data.getData(), data.getOffset(), data.getSize());
			lookahead = -1;
		}
		else {
			prev = next = null;
			current = UNKNOWN;
			lookahead = 0;
		}
	}
	catch (Throwable t) {
		closeNoException();
		throw new HGException(t);
	}
}
 
Example #28
Source File: IVFPQ.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * This is a utility method that can be used to dump the contents of the iidToIvfpqDB to a txt file.<br>
 * Currently, only the list id of each item is dumped.
 * 
 * @param dumpFilename
 *            Full path to the file where the dump will be written.
 * @throws Exception
 */
public void dumpIidToIvfpqDB(String dumpFilename) throws Exception {
	DatabaseEntry foundKey = new DatabaseEntry();
	DatabaseEntry foundData = new DatabaseEntry();

	ForwardCursor cursor = iidToIvfpqDB.openCursor(null, null);
	BufferedWriter out = new BufferedWriter(new FileWriter(new File(dumpFilename)));
	while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
		int iid = IntegerBinding.entryToInt(foundKey);
		TupleInput input = TupleBinding.entryToInput(foundData);
		int listId = input.readInt();
		out.write(iid + " " + listId + "\n");
	}
	cursor.close();
	out.close();
}
 
Example #29
Source File: SingleValueResultSet.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public SingleValueResultSet(BJETxCursor cursor, DatabaseEntry keyIn, ByteArrayConverter<T> converter) {
	//
	// The following is bit hacky because we want to avoid some of the default behavior
	// of the super constructor, which is incorrect when the "values" we are interested in 
	// are the DB's primary keys. So we duplicate its bebavior and override instantiation
	// of the current value.
	this.converter = converter;
	this.cursor = cursor;
	this.key = new DatabaseEntry();
	if (keyIn != null) {
		assignData(key, keyIn.getData());
	}
	
	try {
		((SecondaryCursor)cursor.cursor()).getCurrent(key, pkey, data, LockMode.DEFAULT);
		next = converter.fromByteArray(pkey.getData(), pkey.getOffset(), pkey.getSize());
		lookahead = 1;
	}
	catch (Throwable t) {
		throw new HGException(t);
	}

}
 
Example #30
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);
    }
  }
}