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

The following examples show how to use com.sleepycat.je.Database#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
@Override
public Database openDatabase(String name, DatabaseConfig databaseConfig)
{
    Database cachedHandle = _cachedDatabases.get(name);
    if (cachedHandle == null)
    {
        Database handle = getEnvironment().openDatabase(null, name, databaseConfig);
        Database existingHandle = _cachedDatabases.putIfAbsent(name, handle);
        if (existingHandle == null)
        {
            cachedHandle = handle;
        }
        else
        {
            cachedHandle = existingHandle;
            handle.close();
        }
    }
    return cachedHandle;
}
 
Example 2
@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 3
public void close() throws DatabaseException, IOException {
  for (Database database : databases.values()) {
    database.close();
  }
  boolean wasDeleted = false;
  int tries = 0;
  while (!wasDeleted) {
    try {
      FileUtils.cleanDirectory(dbHome);
      wasDeleted = true;
    } catch (IOException e) {
      tries++;
      if (tries >= 10) {
        wasDeleted = true;
      } else {
        try {
          Thread.sleep(1);
        } catch (InterruptedException e1) {
          LOG.error("Trying to clean up and delete directory, but it failed the first time around and then the " +
            "thread was interrupted");
          wasDeleted = true;
        }
      }
    }
  }
}
 
Example 4
Source Project: qpid-broker-j   File: UpgradeFrom6To7.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void performUpgrade(Environment environment, UpgradeInteractionHandler handler, ConfiguredObject<?> parent)
{
    reportStarting(environment, 6);
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    Database versionDb = environment.openDatabase(null, "CONFIG_VERSION", dbConfig);

    if(versionDb.count() == 0L)
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        IntegerBinding.intToEntry(DEFAULT_CONFIG_VERSION, value);
        ByteBinding.byteToEntry((byte) 0, key);
        OperationStatus status = versionDb.put(null, key, value);
        if (status != OperationStatus.SUCCESS)
        {
            throw new StoreException("Error initialising config version: " + status);
        }
    }

    versionDb.close();

    reportFinished(environment, 7);
}
 
Example 5
Source Project: qpid-broker-j   File: DatabaseTemplate.java    License: Apache License 2.0 5 votes vote down vote up
private void closeDatabase(Database database)
{
    if (database != null)
    {
        try
        {
            database.close();
        }
        catch (Exception e)
        {
            LOGGER.error("Unable to close database", e);
        }
    }
}
 
Example 6
@Override
public void closeDatabase(final String name)
{
    Database cachedHandle = _cachedDatabases.remove(name);
    if (cachedHandle != null)
    {
        cachedHandle.close();
    }
}
 
Example 7
boolean checkIndexExisting(String name)
{
	if (openIndices.get(name) != null)
	{
		return true;
	}
	else
	{
		DatabaseConfig cfg = new DatabaseConfig();
		cfg.setAllowCreate(false);
		Database db = null;

		try
		{
			db = env.openDatabase(null, DefaultIndexImpl.DB_NAME_PREFIX + name, cfg);
		}
		catch (Exception ex)
		{
		}

		if (db != null)
		{
			try
			{
				db.close();
			}
			catch (Throwable t)
			{
				t.printStackTrace();
			}
			return true;
		}
		else
		{
			return false;
		}
	}
}
 
Example 8
@Override
public void inject(CrawlDatum datum, boolean force) throws Exception {
    Database database = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    DatabaseEntry key = BerkeleyDBUtils.strToEntry(datum.key());
    DatabaseEntry value = new DatabaseEntry();
    if (!force) {
        if (database.get(null, key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            database.close();
            return;
        }
    }
    value = BerkeleyDBUtils.strToEntry(datum.asJsonArray().toString());
    database.put(null, key, value);
    database.close();
}
 
Example 9
@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 10
Source Project: qpid-broker-j   File: Upgrader.java    License: Apache License 2.0 4 votes vote down vote up
public void upgradeIfNecessary()
{
    boolean isEmpty = _environment.getDatabaseNames().isEmpty();
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    Database versionDb = null;
    try
    {
        versionDb = _environment.openDatabase(null, VERSION_DB_NAME, dbConfig);

        if(versionDb.count() == 0L)
        {

            int sourceVersion = isEmpty ? BDBConfigurationStore.VERSION: identifyOldStoreVersion();
            DatabaseEntry key = new DatabaseEntry();
            IntegerBinding.intToEntry(sourceVersion, key);
            DatabaseEntry value = new DatabaseEntry();
            LongBinding.longToEntry(System.currentTimeMillis(), value);

            versionDb.put(null, key, value);
        }

        int version = getSourceVersion(versionDb);

        if (LOGGER.isDebugEnabled())
        {
            LOGGER.debug("Source message store version is " + version);
        }

        if(version > BDBConfigurationStore.VERSION)
        {
            throw new StoreException("Database version " + version
                                        + " is higher than the most recent known version: "
                                        + BDBConfigurationStore.VERSION);
        }
        performUpgradeFromVersion(version, versionDb);
    }
    finally
    {
        if (versionDb != null)
        {
            versionDb.close();
        }
    }
}
 
Example 11
Source Project: qpid-broker-j   File: UpgradeFrom8To9.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void performUpgrade(final Environment environment,
                           final UpgradeInteractionHandler handler,
                           final ConfiguredObject<?> parent)
{
    reportStarting(environment, 8);

    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    final Transaction transaction = environment.beginTransaction(null, null);
    try
    {
        Database userPreferencesDb = environment.openDatabase(transaction, "USER_PREFERENCES", dbConfig);
        userPreferencesDb.close();

        try (Database userPreferencesVersionDb = environment.openDatabase(transaction,
                                                                          "USER_PREFERENCES_VERSION",
                                                                          dbConfig))
        {
            if (userPreferencesVersionDb.count() == 0L)
            {
                DatabaseEntry key = new DatabaseEntry();
                DatabaseEntry value = new DatabaseEntry();
                StringBinding.stringToEntry(DEFAULT_VERSION, key);
                LongBinding.longToEntry(System.currentTimeMillis(), value);
                OperationStatus status = userPreferencesVersionDb.put(transaction, key, value);
                if (status != OperationStatus.SUCCESS)
                {
                    throw new StoreException("Error initialising user preference version: " + status);
                }
            }
        }

        transaction.commit();
        reportFinished(environment, 9);
    }
    catch (RuntimeException e)
    {
        try
        {
            if (transaction.isValid())
            {
                transaction.abort();
            }
        }
        finally
        {
            throw e;
        }
    }
}
 
Example 12
Source Project: bboxdb   File: TestKDTreeSplit.java    License: 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);
}