Java Code Examples for com.sleepycat.je.Environment#openDatabase()

The following examples show how to use com.sleepycat.je.Environment#openDatabase() . 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: BdbNonPersistentEnvironmentCreator.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public <KeyT, ValueT> BdbWrapper<KeyT, ValueT> getDatabase(String userId, String dataSetId, String databaseName,
                                                           boolean allowDuplicates, EntryBinding<KeyT> keyBinder,
                                                           EntryBinding<ValueT> valueBinder,
                                                           IsCleanHandler<KeyT, ValueT> isCleanHandler)
  throws BdbDbCreationException {
  try {
    DatabaseConfig config = new DatabaseConfig();
    config.setAllowCreate(true);
    config.setDeferredWrite(true);
    config.setSortedDuplicates(allowDuplicates);

    String environmentKey = environmentKey(userId, dataSetId);
    File envHome = new File(dbHome, environmentKey);
    envHome.mkdirs();
    Environment dataSetEnvironment = new Environment(envHome, configuration);
    Database database = dataSetEnvironment.openDatabase(null, databaseName, config);
    databases.put(environmentKey + "_" + databaseName, database);
    environmentMap.put(environmentKey, dataSetEnvironment);
    return new BdbWrapper<>(dataSetEnvironment, database, config, keyBinder, valueBinder, isCleanHandler);
  } catch (DatabaseException e) {
    throw new BdbDbCreationException(e);
  }
}
 
Example 2
Source File: OrphanConfigurationRecordPurger.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private int getVersion(final Environment env, final DatabaseConfig dbConfig)
{
    try (Database versionDb = env.openDatabase(null, VERSION_DB_NAME, dbConfig);
         Cursor cursor = versionDb.openCursor(null, null))
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();

        int version = 0;

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

        return version;
    }
}
 
Example 3
Source File: UpgraderTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private int getStoreVersion(Environment environment)
{
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    int storeVersion = -1;
    try(Database versionDb = environment.openDatabase(null, Upgrader.VERSION_DB_NAME, dbConfig);
        Cursor cursor = versionDb.openCursor(null, null))
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        while (cursor.getNext(key, value, null) == OperationStatus.SUCCESS)
        {
            int version = IntegerBinding.entryToInt(key);
            if (storeVersion < version)
            {
                storeVersion = version;
            }
        }
    }
    return storeVersion;
}
 
Example 4
Source File: BerkeleyDBHandle.java    From SPADE with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void clear() throws Exception{
	if(environmentHandle == null){
		throw new Exception("NULL enviroment handle");
	}else{
		Environment environment = environmentHandle.getEnvironment();
		if(environment == null){
			throw new Exception("NULL enviroment");
		}else{
			database.close();
			environment.removeDatabase(null, dbName);
			DatabaseConfig databaseConfig = new DatabaseConfig();
			databaseConfig.setAllowCreate(true).setExclusiveCreate(true);
			database = environment.openDatabase(null, dbName, databaseConfig);
		}
	}
}
 
Example 5
Source File: ClassCatalog.java    From Rel with Apache License 2.0 6 votes vote down vote up
ClassCatalog(String directory, EnvironmentConfig environmentConfig, DatabaseConfig dbConfig) {
	// This should be main database directory
	dirClassLoader = new DirClassLoader(directory);
	
    // Open the environment in subdirectory of the above
	String classesDir = directory + java.io.File.separator + "classes";
	RelDatabase.mkdir(classesDir);
    environment = new Environment(new File(classesDir), environmentConfig);
    
    // Open the class catalog db. This is used to optimize class serialization.
    classCatalogDb = environment.openDatabase(null, "_ClassCatalog", dbConfig);

    // Create our class catalog
    classCatalog = new StoredClassCatalog(classCatalogDb);
    
    // Need a serial binding for metadata
    relvarMetadataBinding = new SerialBinding<RelvarMetadata>(classCatalog, RelvarMetadata.class);
    
    // Need serial binding for data
    tupleBinding = new SerialBinding<ValueTuple>(classCatalog, ValueTuple.class) {
    	public ClassLoader getClassLoader() {
    		return dirClassLoader;
    	}
    };   	
}
 
Example 6
Source File: AbstractFrontier.java    From codes-scratch-crawler with Apache License 2.0 6 votes vote down vote up
public AbstractFrontier(String homeDirectory) {
  EnvironmentConfig envConfig = new EnvironmentConfig();
  envConfig.setTransactional(true);
  envConfig.setAllowCreate(true);
  env = new Environment(new File(homeDirectory), envConfig);
  // 设置databaseconfig
  DatabaseConfig dbConfig = new DatabaseConfig();
  dbConfig.setTransactional(true);
  dbConfig.setAllowCreate(true);
  // 打开
  catalogdatabase = env.openDatabase(null, CLASS_CATALOG, dbConfig);
  javaCatalog = new StoredClassCatalog(catalogdatabase);
  // 设置databaseconfig
  DatabaseConfig dbConfig0 = new DatabaseConfig();
  dbConfig0.setTransactional(true);
  dbConfig0.setAllowCreate(true);
  database = env.openDatabase(null, "URL", dbConfig0);
}
 
Example 7
Source File: FileLineIndex.java    From bboxdb with Apache License 2.0 6 votes vote down vote up
/**
 * Open the Berkeley DB
 * @throws IOException
 */
protected void openDatabase() throws IOException {
	final EnvironmentConfig envConfig = new EnvironmentConfig();
	envConfig.setTransactional(false);
	envConfig.setAllowCreate(true);
    envConfig.setSharedCache(true);

    tmpDatabaseDir = Files.createTempDirectory(null);
	dbEnv = new Environment(tmpDatabaseDir.toFile(), envConfig);

	logger.info("Database dir is {}", tmpDatabaseDir);

	// Delete database on exit
	FileUtil.deleteDirOnExit(tmpDatabaseDir);

	final DatabaseConfig dbConfig = new DatabaseConfig();
	dbConfig.setTransactional(false);
	dbConfig.setAllowCreate(true);
	dbConfig.setDeferredWrite(true);

	database = dbEnv.openDatabase(null, "lines", dbConfig);
}
 
Example 8
Source File: OSMBDBNodeStore.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Init a new BDB environment in the given folder
 * @param folder
 */
@SuppressWarnings("unused")
protected void initNewBDBEnvironment(final File folder, final EnvironmentConfig envConfig) {

	final Environment dbEnv = new Environment(folder, envConfig);

	Transaction txn = null;
	if(USE_TRANSACTIONS) {
		txn = dbEnv.beginTransaction(null, null);
	}
	
	final DatabaseConfig dbConfig = new DatabaseConfig();
	dbConfig.setTransactional(USE_TRANSACTIONS);
	dbConfig.setAllowCreate(true);
	dbConfig.setSortedDuplicates(true);
	dbConfig.setDeferredWrite(true);
	//dbConfig.setKeyPrefixing(true);
	//dbConfig.setNodeMaxEntries(128);

	final Database database = dbEnv.openDatabase(txn, "osm", dbConfig);

	if(txn != null) {
		txn.commit();
	}
	
	environments.add(dbEnv);
	databases.add(database);
}
 
Example 9
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 10
Source File: UpgradeFrom6To7.java    From qpid-broker-j with 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 11
Source File: BDBTupleStore.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	final EnvironmentConfig envConfig = new EnvironmentConfig();
	envConfig.setTransactional(USE_TRANSACTIONS);
	envConfig.setAllowCreate(true);

	environment = new Environment(dir, envConfig);

	Transaction txn = null;
	if(USE_TRANSACTIONS) {
		txn = environment.beginTransaction(null, null);
	}
	
	final DatabaseConfig dbConfig = new DatabaseConfig();
	dbConfig.setTransactional(USE_TRANSACTIONS);
	dbConfig.setAllowCreate(true);
	//dbConfig.setSortedDuplicates(true);
	dbConfig.setDeferredWrite(true);
	//dbConfig.setKeyPrefixing(true);
	//dbConfig.setNodeMaxEntries(128);

	database = environment.openDatabase(txn, "test", dbConfig);

	if(txn != null) {
		txn.commit();
	}	
}
 
Example 12
Source File: CompressedStorage.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This method is invoked by the kernel to initialize the storage.
 *
 * @param arguments The arguments with which this storage is to be
 *                  initialized.
 * @return True if the storage was initialized successfully.
 */
public boolean initialize(String filePath) {
	//clock = System.currentTimeMillis();
	countUpdates = 0;
	annotationsTime = 0;
	scaffoldTime = 0;
	clockScaffold = 0;
	clockAnnotations = 0;
	scaffoldInMemory = new HashMap<Integer, Pair<SortedSet<Integer>, SortedSet<Integer>>>();
	edgesInMemory = 0;
	hashToID = new HashMap<String, Integer>();
	idToHash = new HashMap<Integer, String>();
	alreadyRenamed = new Vector<String>();
	compresser = new Deflater(Deflater.BEST_COMPRESSION);
	W=10;
	L=5;
	nextVertexID = 0;
	try {
		benchmarks = new PrintWriter("/Users/melanie/Documents/benchmarks/compression_time_berkeleyDB.txt", "UTF-8");
		// Open the environment. Create it if it does not already exist.
		EnvironmentConfig envConfig = new EnvironmentConfig();
		envConfig.setAllowCreate(true);
		DatabaseEnvironment1 = new Environment(new File(filePath + "/scaffold"), 
				envConfig);
		DatabaseEnvironment2 = new Environment(new File(filePath + "/annotations"), 
				envConfig);
		// Open the databases. Create it if it does not already exist.
		DatabaseConfig DatabaseConfig1 = new DatabaseConfig();
		DatabaseConfig1.setAllowCreate(true);
		scaffoldDatabase = DatabaseEnvironment1.openDatabase(null, "spade_scaffold", DatabaseConfig1); 
		annotationsDatabase = DatabaseEnvironment2.openDatabase(null, "spade_annotations", DatabaseConfig1); 

		return true;

	} catch (Exception ex) {
		// Exception handling goes here
		logger.log(Level.SEVERE, "Compressed Storage Initialized not successful!", ex);
		return false;
	}
}
 
Example 13
Source File: BerkeleyDBManager.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates BerkeleyDB database object 
 * 
 * @param environment BerkeleyDB environment object
 * @param databaseName name of the database
 * @param create true - must create database, false - database must already exist.
 * @return BerkeleyDB database object or error
 */
private synchronized Result<Database> createOpenDatabase(Environment environment, String databaseName, boolean create){
	DatabaseConfig databaseConfig = new DatabaseConfig();
	databaseConfig.setAllowCreate(create).setExclusiveCreate(create);
	try{
		Database database = environment.openDatabase(null, databaseName, databaseConfig);
		return Result.successful(database);
	}catch(Exception e){
		return Result.failed("Failed to create database", e, null);
	}
}
 
Example 14
Source File: EnvironmentUtils.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
public static Map<String,Object> getDatabaseStatistics(Environment environment, String database, boolean reset)
{
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setReadOnly(true);
    DbInternal.setUseExistingConfig(dbConfig, true);
    try (Database db = environment.openDatabase(null, database, dbConfig))
    {
        StatsConfig config = new StatsConfig();
        config.setClear(reset);
        config.setFast(false);

        BtreeStats stats = (BtreeStats) db.getStats(config);

        Map<String, Object> results = new TreeMap<>();
        results.put(BTREE_BIN_COUNT.getName(), stats.getBottomInternalNodeCount());
        results.put(BTREE_DELETED_LN_COUNT.getName(), stats.getDeletedLeafNodeCount());
        results.put(BTREE_IN_COUNT.getName(), stats.getInternalNodeCount());
        results.put(BTREE_LN_COUNT.getName(), stats.getLeafNodeCount());
        results.put(BTREE_MAINTREE_MAXDEPTH.getName(), stats.getMainTreeMaxDepth());
        results.put(BTREE_INS_BYLEVEL.getName(), Arrays.asList(stats.getINsByLevel()));
        results.put(BTREE_BINS_BYLEVEL.getName(), Arrays.asList(stats.getBINsByLevel()));
        results.put(BTREE_BIN_ENTRIES_HISTOGRAM.getName(), Arrays.asList(stats.getBINEntriesHistogram()));
        results.put(BTREE_RELATCHES_REQUIRED.getName(), stats.getRelatches());
        results.put(BTREE_ROOT_SPLITS.getName(), stats.getRootSplits());

        return results;
    }

}
 
Example 15
Source File: JE_Repository.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
private Database buildPrimaryIndex(DatabaseConfig dbConfig, Environment _env, String dbName) {
    Database database = _env.openDatabase(null, dbName, dbConfig);
    return database;
}
 
Example 16
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 4 votes vote down vote up
public void startup(HGStore store, HGConfiguration config)
{
	this.store = store;
	this.handleFactory = config.getHandleFactory();
	this.linkBinding = new LinkBinding(handleFactory);
	EnvironmentConfig envConfig = configuration.getEnvironmentConfig();
	envConfig.setConfigParam(EnvironmentConfig.CLEANER_THREADS, "5");
	envConfig.setClassLoader(new HGClassLoaderDelegate(config));
	if (config.isTransactional())
	{
		configuration.configureTransactional();
	}

	File envDir = new File(store.getDatabaseLocation());
	envDir.mkdirs();

	try
	{
		env = new Environment(envDir, envConfig);
		data_db = env.openDatabase(null, DATA_DB_NAME, configuration.getDatabaseConfig().clone());
		primitive_db = env.openDatabase(null, PRIMITIVE_DB_NAME, configuration.getDatabaseConfig().clone());

		DatabaseConfig incConfig = configuration.getDatabaseConfig().clone();
		incConfig.setSortedDuplicates(true);
		incidence_db = env.openDatabase(null, INCIDENCE_DB_NAME, incConfig);

		openIndices = new HashMap<String, HGIndex<?, ?>>(); // force reset
															// since startup
															// can follow a
															// shutdown on
															// same opened
															// class

		if (config.isTransactional())
		{
			CheckpointConfig ckptConfig = new CheckpointConfig();
			// System.out.println("checkpoint kbytes:" +
			// ckptConfig.getKBytes());
			// System.out.println("checkpoint minutes:" +
			// ckptConfig.getMinutes());
			env.checkpoint(null);
			checkPointThread = new CheckPointThread();
			checkPointThread.start();
		}
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to initialize HyperGraph data store: " + ex.toString(), ex);
	}
}
 
Example 17
Source File: JE_Repository.java    From tddl with Apache License 2.0 4 votes vote down vote up
private Database buildPrimaryIndex(DatabaseConfig dbConfig, Environment _env, String dbName) {
    Database database = _env.openDatabase(null, dbName, dbConfig);
    return database;
}
 
Example 18
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);
}
 
Example 19
Source File: UpgradeFrom8To9.java    From qpid-broker-j with 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;
        }
    }
}