Java Code Examples for com.sleepycat.je.Cursor#close()

The following examples show how to use com.sleepycat.je.Cursor#close() . 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: 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 2
Source File: BdbWrapper.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
public List<String> dump(String prefix, int start, int count, LockMode lockMode) {
  EntryBinding<String> binder = TupleBinding.getPrimitiveBinding(String.class);
  DatabaseEntry key = new DatabaseEntry();
  binder.objectToEntry(prefix, key);
  DatabaseEntry value = new DatabaseEntry();

  Cursor cursor = database.openCursor(null, null);
  OperationStatus status = cursor.getSearchKeyRange(key, value, LockMode.READ_UNCOMMITTED);
  List<String> result = new ArrayList<>();
  int index = 0;
  while (status == OperationStatus.SUCCESS && index < (start + count)) {
    if (index >= start) {
      result.add(
        binder.entryToObject(key) + " -> " + binder.entryToObject(value)
      );
    }
    index++;
    status = cursor.getNext(key, value, LockMode.READ_UNCOMMITTED);
  }
  cursor.close();
  return result;
}
 
Example 3
Source File: BerkeleyKeyValueStore.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<byte[]> getDuplicates(String tableName, byte[] keyBytes, DatabaseSession databaseSession) throws BimserverDatabaseException {
	DatabaseEntry key = new DatabaseEntry(keyBytes);
	DatabaseEntry value = new DatabaseEntry();
	try {
		TableWrapper tableWrapper = getTableWrapper(tableName);
		Cursor cursor = tableWrapper.getDatabase().openCursor(getTransaction(databaseSession, tableWrapper), getCursorConfig(tableWrapper));
		try {
			OperationStatus operationStatus = cursor.getSearchKey(key, value, LockMode.DEFAULT);
			List<byte[]> result = new ArrayList<byte[]>();
			while (operationStatus == OperationStatus.SUCCESS) {
				result.add(value.getData());
				operationStatus = cursor.getNextDup(key, value, LockMode.DEFAULT);
			}
			return result;
		} finally {
			cursor.close();
		}
	} catch (DatabaseException e) {
		LOGGER.error("", e);
	}
	return null;
}
 
Example 4
Source File: BerkeleyDBStore.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
private Set privateLoadAllKeys() {
    Set keys = new java.util.HashSet((int) _db.count());
    Cursor cursor = null;
    try {
        cursor = _db.openCursor(null, null);
        DatabaseEntry foundKey = new DatabaseEntry();
        DatabaseEntry foundData = new DatabaseEntry();

        while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            keys.add((K) entryToObject(foundKey));
        }
    } catch (Exception e) {
        _logger.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        cursor.close();
    }

    _logger.log(Level.INFO, this.getClass().getCanonicalName() + ":" + _mapName + ":loadAllKeys:" + keys.size());

    return keys;
}
 
Example 5
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 6
Source File: DefaultIndexImpl.java    From hypergraphdb with Apache License 2.0 5 votes vote down vote up
public void removeEntry(KeyType key, ValueType value)
{
	checkOpen();
	DatabaseEntry keyEntry = new DatabaseEntry(keyConverter.toByteArray(key));
	DatabaseEntry valueEntry = new DatabaseEntry(valueConverter.toByteArray(value));
	Cursor cursor = null;

	try
	{
		cursor = db.openCursor(txn().getBJETransaction(), cursorConfig);
		if (cursor.getSearchBoth(keyEntry, valueEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS)
			cursor.delete();
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to lookup index '" + name + "': " + ex.toString(), ex);
	}
	finally
	{
		if (cursor != null)
		{
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
		}
	}
}
 
Example 7
Source File: DefaultIndexImpl.java    From hypergraphdb with Apache License 2.0 5 votes vote down vote up
public ValueType findFirst(KeyType key)
{
	checkOpen();
	DatabaseEntry keyEntry = new DatabaseEntry(keyConverter.toByteArray(key));
	DatabaseEntry value = new DatabaseEntry();
	ValueType result = null;
	Cursor cursor = null;

	try
	{
		cursor = db.openCursor(txn().getBJETransaction(), cursorConfig);
		OperationStatus status = cursor.getSearchKey(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);
	}
	finally
	{
		if (cursor != null)
		{
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
		}
	}
	return result;
}
 
Example 8
Source File: DefaultIndexImpl.java    From hypergraphdb with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Find the last entry, assuming ordered duplicates, corresponding to the
 * given key.
 * </p>
 * 
 * @param key
 *            The key whose last entry is sought.
 * @return The last (i.e. greatest, i.e. maximum) data value for that key or
 *         null if the set of entries for the key is empty.
 */
public ValueType findLast(KeyType key)
{
	checkOpen();
	DatabaseEntry keyEntry = new DatabaseEntry(keyConverter.toByteArray(key));
	DatabaseEntry value = new DatabaseEntry();
	ValueType result = null;
	Cursor cursor = null;
	try
	{
		cursor = db.openCursor(txn().getBJETransaction(), cursorConfig);
		OperationStatus status = cursor.getLast(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);
	}
	finally
	{
		if (cursor != null)
		{
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
		}
	}
	return result;
}
 
Example 9
Source File: DefaultIndexImpl.java    From hypergraphdb with Apache License 2.0 5 votes vote down vote up
public long count(KeyType key)
{
	Cursor cursor = null;
	try
	{
		cursor = db.openCursor(txn().getBJETransaction(), cursorConfig);
		DatabaseEntry keyEntry = new DatabaseEntry(keyConverter.toByteArray(key));
		DatabaseEntry value = new DatabaseEntry();
		OperationStatus status = cursor.getSearchKey(keyEntry, value, 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 10
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public HGRandomAccessResult<HGPersistentHandle> getIncidenceResultSet(HGPersistentHandle handle)
{
	if (handle == null)
		throw new NullPointerException("HGStore.getIncidenceSet called with a null handle.");

	Cursor cursor = null;
	try
	{
		DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
		DatabaseEntry value = new DatabaseEntry();
		TransactionBJEImpl tx = txn();
		cursor = incidence_db.openCursor(tx.getBJETransaction(), cursorConfig);
		OperationStatus status = cursor.getSearchKey(key, value, LockMode.DEFAULT);
		if (status == OperationStatus.NOTFOUND)
		{
			cursor.close();
			return (HGRandomAccessResult<HGPersistentHandle>) HGSearchResult.EMPTY;
		}
		else
			return new SingleKeyResultSet<HGPersistentHandle>(tx.attachCursor(cursor), key,
					BAtoHandle.getInstance(handleFactory));
	}
	catch (Throwable ex)
	{
		if (cursor != null)
			try
			{
				cursor.close();
			}
			catch (Throwable t)
			{
			}
		throw new HGException("Failed to retrieve incidence set for handle " + handle + ": " + ex.toString(), ex);
	}
}
 
Example 11
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 5 votes vote down vote up
public long getIncidenceSetCardinality(HGPersistentHandle handle)
{
	if (handle == null)
		throw new NullPointerException("HGStore.getIncidenceSetCardinality called with a null handle.");

	Cursor cursor = null;
	try
	{
		DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
		DatabaseEntry value = new DatabaseEntry();
		cursor = incidence_db.openCursor(txn().getBJETransaction(), cursorConfig);
		OperationStatus status = cursor.getSearchKey(key, value, LockMode.DEFAULT);
		if (status == OperationStatus.NOTFOUND)
			return 0;
		else
			return cursor.count();
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve incidence set for handle " + handle + ": " + ex.toString(), ex);
	}
	finally
	{
		try
		{
			cursor.close();
		}
		catch (Throwable t)
		{
		}
	}
}
 
Example 12
Source File: BerkeleyDBManager.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void merge() throws Exception {
    LOG.info("start merge");
    Database crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    /*合并fetch库*/
    LOG.info("merge fetch database");
    Database fetchDatabase = env.openDatabase(null, "fetch", BerkeleyDBUtils.defaultDBConfig);
    Cursor fetchCursor = fetchDatabase.openCursor(null, null);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    while (fetchCursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
        crawldbDatabase.put(null, key, value);
    }
    fetchCursor.close();
    fetchDatabase.close();
    /*合并link库*/
    LOG.info("merge link database");
    Database linkDatabase = env.openDatabase(null, "link", BerkeleyDBUtils.defaultDBConfig);
    Cursor linkCursor = linkDatabase.openCursor(null, null);
    while (linkCursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
        if (!(crawldbDatabase.get(null, key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)) {
            crawldbDatabase.put(null, key, value);
        }
    }
    linkCursor.close();
    linkDatabase.close();
    LOG.info("end merge");
    crawldbDatabase.close();

    env.removeDatabase(null, "fetch");
    LOG.debug("remove fetch database");
    env.removeDatabase(null, "link");
    LOG.debug("remove link database");

}
 
Example 13
Source File: UpgradeFrom7To8.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private long getMaximumMessageId(Database messageMetaDataDb)
{
    Cursor cursor = null;
    long maximumMessageId = 0;  // Our hand-rolled sequences value always began at zero
    try
    {
        cursor = messageMetaDataDb.openCursor(null, null);
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        MessageMetaDataBinding valueBinding = MessageMetaDataBinding.getInstance();

        while (cursor.getNext(key, value, LockMode.RMW) == OperationStatus.SUCCESS)
        {
            long messageId = LongBinding.entryToLong(key);
            maximumMessageId = Math.max(messageId, maximumMessageId);
        }
    }
    finally
    {
        if (cursor != null)
        {
            cursor.close();
        }
    }

    return maximumMessageId;
}
 
Example 14
Source File: Upgrader.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
int getSourceVersion(Database versionDb)
{
    int version = -1;

    Cursor cursor = null;
    try
    {
        cursor = versionDb.openCursor(null, null);

        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();

        while(cursor.getNext(key, value, null) == OperationStatus.SUCCESS)
        {
            int ver = IntegerBinding.entryToInt(key);
            if(ver > version)
            {
                version = ver;
            }
        }
    }
    finally
    {
        if(cursor != null)
        {
            cursor.close();
        }
    }


    return version;
}
 
Example 15
Source File: BerkeleyJETx.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
private void closeOpenIterators() throws BackendException {
    for (Cursor cursor : openCursors) {
        cursor.close();
    }
}
 
Example 16
Source File: TestKDTreeSplit.java    From bboxdb with Apache License 2.0 4 votes vote down vote up
/**
 * Split the region
 * @param sampleSize
 * @param numberOfElements
 * @return
 */
private void splitRegion(final Hyperrectangle boundingBoxToSplit) {

	final int parentBoxDimension = boxDimension.get(boundingBoxToSplit) % dataDimension;

	final double splitPosition = getSplitPosition(boundingBoxToSplit, parentBoxDimension);

	final Hyperrectangle leftBBox = boundingBoxToSplit.splitAndGetLeft(splitPosition,
			parentBoxDimension, true);

	final Hyperrectangle rightBBox = boundingBoxToSplit.splitAndGetRight(splitPosition,
			parentBoxDimension, false);

	// Write the box dimension
	boxDimension.put(leftBBox, parentBoxDimension + 1);
	boxDimension.put(rightBBox, parentBoxDimension + 1);

	// Insert new boxes and remove old one
	elements.put(leftBBox, buildNewDatabase());
	elements.put(rightBBox, buildNewDatabase());

	final Database database = elements.remove(boundingBoxToSplit);

	// Data to redistribute
    final Cursor cursor = database.openCursor(null, null);

    final DatabaseEntry foundKey = new DatabaseEntry();
    final DatabaseEntry foundData = new DatabaseEntry();

    while(cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
        final Hyperrectangle box = Hyperrectangle.fromByteArray(foundData.getData());

        boolean redistributed = false;

        if(leftBBox.intersects(box)) {
			elements.get(leftBBox).put(null, foundKey, foundData);
			elementCounter.computeIfAbsent(leftBBox, l -> new AtomicLong(0)).incrementAndGet();
			redistributed = true;
		}

		if(rightBBox.intersects(box)) {
			elements.get(rightBBox).put(null, foundKey, foundData);
			elementCounter.computeIfAbsent(rightBBox, l -> new AtomicLong(0)).incrementAndGet();
			redistributed = true;
		}

		if(! redistributed) {
			System.err.println("Unable to redistribute: " + box + " left / right "
					+ leftBBox + " " + rightBBox);
		}
    }


    cursor.close();

    // Name needs to be fetched before database is closed
    final String databaseName = database.getDatabaseName();

    database.close();

	dbEnv.removeDatabase(null, databaseName);
}
 
Example 17
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;
}