Example #1
0
  public AbstractFrontier(String homeDirectory) throws DatabaseException, FileNotFoundException {
    // 打开env
    File file = new File(homeDirectory);
    if (!file.exists()) {
      file.mkdirs();
    }
    System.out.println("Opening environment in: " + 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);
    dbConfig.setSortedDuplicates(false);
    // 打开
    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", dbConfig);
  }
Example #2
0
  /** SR #11123 Make sure that BINDeltas are applied only to non-deleted nodes. */
  public void testBINDelta() throws Throwable {

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    turnOffEnvDaemons(envConfig);
    envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "4");
    envConfig.setConfigParam(EnvironmentParams.BIN_DELTA_PERCENT.getName(), "75");
    envConfig.setAllowCreate(true);

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

    EnvironmentConfig restartConfig = TestUtils.initEnvConfig();
    turnOffEnvDaemons(restartConfig);
    envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "4");

    testOneCase(
        DB_NAME,
        envConfig,
        dbConfig,
        new TestGenerator() {
          void generateData(Database db) throws DatabaseException {
            addData(db);
          }
        },
        restartConfig,
        new DatabaseConfig());
  }
Example #3
0
  public void run(File envHomeDirectory) throws DatabaseException, IOException {

    /* Create the environment object. */
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    Environment env = new Environment(envHomeDirectory, envConfig);

    /* Create the database object. */
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    Database db = env.openDatabase(null, DB_NAME, dbConfig);

    /* Create the sequence oject. */
    SequenceConfig config = new SequenceConfig();
    config.setAllowCreate(true);
    DatabaseEntry key = new DatabaseEntry(KEY_NAME.getBytes("UTF-8"));
    Sequence seq = db.openSequence(null, key, config);

    /* Allocate a few sequence numbers. */
    for (int i = 0; i < 10; i++) {
      long seqnum = seq.get(null, 1);
      System.out.println("Got sequence number: " + seqnum);
    }

    /* Close all. */
    seq.close();
    db.close();
    env.close();
  }
Example #4
0
 private void setEnvironment() {
   envLocation = ConfigManager.getDbDir();
   envConfig = new EnvironmentConfig();
   envConfig.setAllowCreate(true);
   envConfig.setLocking(false);
   myDbEnvironment = new Environment(new File(envLocation), envConfig);
 }
Example #5
0
  public static void main(String[] args) {

    /*
     * Don't write to System.out in this process because the parent
     * process only reads System.err.
     */
    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setTransactional(true);
      envConfig.setReadOnly(true);

      File envHome = new File(System.getProperty(TestUtils.DEST_DIR));
      Environment env = new Environment(envHome, envConfig);

      // System.err.println("Opened read-only: " + envHome);
      // System.err.println(System.getProperty("java.class.path"));

      /* Notify the test that this process has opened the environment. */
      ReadOnlyLockingTest.createProcessFile();

      /* Sleep until the parent process kills me. */
      Thread.sleep(Long.MAX_VALUE);
    } catch (Exception e) {

      e.printStackTrace(System.err);
      System.exit(1);
    }
  }
 private EnvironmentConfig setupEnvConfig() {
   EnvironmentConfig envConfig = TestUtils.initEnvConfig();
   turnOffEnvDaemons(envConfig);
   envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "4");
   envConfig.setAllowCreate(true);
   return envConfig;
 }
Example #7
0
  public boolean initEngine(File envHome) {
    EnvironmentConfig envConfig = new EnvironmentConfig();
    m_DbConfig = new DatabaseConfig();

    // If the environment is read-only, then
    // make the databases read-only too.
    envConfig.setReadOnly(false);
    m_DbConfig.setReadOnly(false);

    // If the environment is opened for write, then we want to be
    // able to create the environment and databases if
    // they do not exist.
    envConfig.setAllowCreate(true);
    m_DbConfig.setAllowCreate(true);

    // Allow transactions if we are writing to the database
    envConfig.setTransactional(false);
    m_DbConfig.setTransactional(false);

    m_DbConfig.setDeferredWrite(true);

    envConfig.setLocking(false); // No locking

    m_DbConfig.setBtreeComparator(BtreeKeyComparator.class);

    // Open the environment
    try {
      m_env = new Environment(envHome, envConfig);
      return true;
    } catch (DatabaseException e) {
      return false;
    }
  }
Example #8
0
 static {
   envConfig.setConfigParam(EnvironmentParams.ENV_CHECK_LEAKS.getName(), "false");
   envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
   envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC));
   envConfig.setLockTimeout(1); // to speed up intentional deadlocks
   envConfig.setAllowCreate(true);
 }
Example #9
0
  protected void setUp() throws DatabaseException, IOException {

    /* Remove files to start with a clean slate. */
    TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    DbInternal.disableParameterValidation(envConfig);
    envConfig.setConfigParam(
        EnvironmentParams.LOG_FILE_MAX.getName(), new Integer(FILE_SIZE).toString());
    /* Yank the cache size way down. */
    envConfig.setConfigParam(EnvironmentParams.LOG_FILE_CACHE_SIZE.getName(), "3");
    envConfig.setAllowCreate(true);
    envImpl = new EnvironmentImpl(envHome, envConfig);

    /* Make a standalone file manager for this test. */
    envImpl.close();
    envImpl.open(); /* Just sets state to OPEN. */
    fileManager = new FileManager(envImpl, envHome, false);

    /*
     * Remove any files after the environment is created again!  We want to
     * remove the files made by recovery, so we can test the file manager
     * in controlled cases.
     */
    TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
  }
  public BDB_GIDLookupImpl(String aim) {

    EnvironmentConfig environmentConfig = new EnvironmentConfig();

    // will be created if not existing -> will crash if folder not found
    environmentConfig.setAllowCreate(true);

    // no transaction yet -> might be needed later
    // not sure if needed to be set in environment and database
    environmentConfig.setTransactional(false);

    File file = new File(aim);

    if (!file.exists()) {
      file.mkdirs();
    }

    environment = new Environment(file, environmentConfig);
    DatabaseConfig databaseConfig = new DatabaseConfig();

    // will be created if not existing -> will crash if folder not found
    databaseConfig.setAllowCreate(true);

    // no transaction yet -> might be needed later
    // not sure if needed to be set in environment and database
    databaseConfig.setTransactional(false);

    // create 2 "tables" one for relations-gid one for node-gid
    RelationBDB = environment.openDatabase(null, "Relation", databaseConfig);
    NodeBDB = environment.openDatabase(null, "Node", databaseConfig);
  }
  private EnvironmentImpl createEnvImpl(File envDir) {
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(true);

    Environment backEnv = new Environment(envDir, envConfig);

    return DbInternal.getEnvironmentImpl(backEnv);
  }
Example #12
0
  public void setUp() throws IOException, DatabaseException {

    TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
    envConfig.setAllowCreate(true);
    env = new EnvironmentImpl(envHome, envConfig);
  }
    /* Open read only standalone Environment on replicated nodes. */
    private void openEnvironments() throws Exception {

      EnvironmentConfig envConfig = new EnvironmentConfig();
      envConfig.setReadOnly(true);
      envConfig.setAllowCreate(false);

      master = new Environment(masterEnvHome, envConfig);
      replica = new Environment(replicaEnvHome, envConfig);
    }
 @Test
 public void testCleanAfterMinUtilizationChange() {
   open(true /*runCleaner*/);
   writeFiles(4 /*nActive*/, 3 /*nObsolete*/);
   expectNothingToClean();
   final EnvironmentConfig envConfig = env.getConfig();
   envConfig.setConfigParam(EnvironmentConfig.CLEANER_MIN_UTILIZATION, "90");
   env.setMutableConfig(envConfig);
   expectBackgroundCleaning();
   close();
 }
  private EnvironmentConfig makeEnvConfig() {

    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(true);
    DbInternal.disableParameterValidation(envConfig);
    envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(), "10000");
    /* Control cleaning explicitly. */
    envConfig.setConfigParam(EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
    return envConfig;
  }
Example #16
0
 public static EnhancedEnvironment setupCopyEnvironment(File env, boolean readOnly)
     throws DatabaseException {
   EnvironmentConfig envConfig = new EnvironmentConfig();
   envConfig.setAllowCreate(true);
   envConfig.setReadOnly(readOnly);
   try {
     return new EnhancedEnvironment(env, envConfig);
   } catch (IllegalArgumentException iae) {
     throw new IllegalArgumentException(
         "problem with specified environment " + env + "; is it already open?", iae);
   }
 }
 private static void setDurability(
     EnvironmentConfig envConfig, boolean dbTxnNoSync, boolean dbTxnWriteNoSync)
     throws ConfigException {
   if (dbTxnNoSync && dbTxnWriteNoSync) {
     throw new ConfigException(ERR_CONFIG_JEB_DURABILITY_CONFLICT.get());
   }
   if (dbTxnNoSync) {
     envConfig.setDurability(Durability.COMMIT_NO_SYNC);
   } else if (dbTxnWriteNoSync) {
     envConfig.setDurability(Durability.COMMIT_WRITE_NO_SYNC);
   }
 }
Example #18
0
  /**
   * Set an attribute value for the given environment.
   *
   * @param targetEnv The target JE environment. May be null if the environment is not open.
   * @param attribute name/value pair
   */
  public void setAttribute(Environment targetEnv, Attribute attribute)
      throws AttributeNotFoundException, InvalidAttributeValueException {

    if (attribute == null) {
      throw new AttributeNotFoundException("Attribute cannot be null");
    }

    /* Sanity check parameters. */
    String name = attribute.getName();
    Object value = attribute.getValue();

    if (name == null) {
      throw new AttributeNotFoundException("Attribute name cannot be null");
    }

    if (value == null) {
      throw new InvalidAttributeValueException(
          "Attribute value for attribute " + name + " cannot be null");
    }

    try {
      if (name.equals(ATT_SET_READ_ONLY)) {
        openConfig.setReadOnly(((Boolean) value).booleanValue());
      } else if (name.equals(ATT_SET_TRANSACTIONAL)) {
        openConfig.setTransactional(((Boolean) value).booleanValue());
      } else if (name.equals(ATT_SET_SERIALIZABLE)) {
        openConfig.setTxnSerializableIsolation(((Boolean) value).booleanValue());
      } else {
        /* Set the specified attribute if the environment is open. */
        if (targetEnv != null) {

          EnvironmentMutableConfig config = targetEnv.getMutableConfig();

          if (name.equals(ATT_CACHE_SIZE)) {
            config.setCacheSize(((Long) value).longValue());
            targetEnv.setMutableConfig(config);
          } else if (name.equals(ATT_CACHE_PERCENT)) {
            config.setCachePercent(((Integer) value).intValue());
            targetEnv.setMutableConfig(config);
          } else {
            throw new AttributeNotFoundException("attribute " + name + " is not valid.");
          }
        } else {
          throw new AttributeNotFoundException("attribute " + name + " is not valid.");
        }
      }
    } catch (NumberFormatException e) {
      throw new InvalidAttributeValueException("attribute name=" + name);
    } catch (DatabaseException e) {
      throw new InvalidAttributeValueException("attribute name=" + name + e.getMessage());
    }
  }
Example #19
0
  public void setup(File envHome, boolean readOnly) throws DatabaseException {

    // create environment config and environment instance
    myEnvConfig = new EnvironmentConfig();
    myEnvConfig.setReadOnly(readOnly);
    myEnvConfig.setAllowCreate(!readOnly);

    myEnv = new Environment(envHome, myEnvConfig);

    // create class catalog database and instance
    Database myClassCatalogDb = setDatabase("MyClassCatalogDb", readOnly);
    myClassCatalog = new StoredClassCatalog(myClassCatalogDb);
  }
 /**
  * @param dbEnvPath path to db environment
  * @param isReadOnly If true, then all databases opened in this environment must be opened as
  *     read-only. If you are writing a multi-process application, then all but one of your
  *     processes must set this value to true.
  * @param allowCreateNew If true, the database environment is created when it is opened. If false,
  *     environment open fails if the environment does not exist. This property has no meaning if
  *     the database environment already exists.
  * @param cacheSizeInMB
  * @throws DatabaseException
  */
 public BerkeleyDbEnvironment(
     String dbEnvPath, boolean isReadOnly, boolean allowCreateNew, Long cacheSizeInMB)
     throws DatabaseException {
   EnvironmentConfig envConfig = new EnvironmentConfig();
   envConfig.setAllowCreate(allowCreateNew);
   envConfig.setReadOnly(isReadOnly);
   envConfig.setTransactional(false);
   if (cacheSizeInMB != null && cacheSizeInMB != -1) {
     long cacheSizeInByte = cacheSizeInMB * 1024 * 1024;
     envConfig.setCacheSize(cacheSizeInByte);
   }
   environment = new Environment(new File(dbEnvPath), envConfig);
 }
Example #21
0
  public static void main(String[] args) {
    Environment env = null;
    Database db = null;
    EnvironmentConfig envconfig = new EnvironmentConfig();
    envconfig.setAllowCreate(true);
    try {
      env = new Environment(new File("D://bdb"), envconfig);
      DatabaseConfig dbconfig = new DatabaseConfig();
      dbconfig.setAllowCreate(true);
      db = env.openDatabase(null, "dbac.db", dbconfig);
      String key = "mykey";
      DatabaseEntry thekey = new DatabaseEntry();
      thekey.setData(key.getBytes("utf-8"));

      Long value = new Long(123456);
      DatabaseEntry thevalue = new DatabaseEntry();
      EntryBinding myBinging = TupleBinding.getPrimitiveBinding(Long.class);
      myBinging.objectToEntry(value, thevalue);
      // LongBinding myLongBinging=(LongBinding)TupleBinding.getPrimitiveBinding(Long.class);
      // myLongBinging.objectToEntry(value, thevalue);
      db.put(null, thekey, thevalue);
      DatabaseEntry valueEntry = new DatabaseEntry();
      OperationStatus status = db.get(null, thekey, valueEntry, LockMode.DEFAULT);
      if (status == OperationStatus.SUCCESS) {
        // Long number=myLongBinging.entryToObject(valueEntry);
        Long number = (Long) myBinging.entryToObject(valueEntry);
        System.out.println(env.getDatabaseNames());
        System.out.println(number);
      }
    } catch (EnvironmentLockedException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (db != null) {
        try {
          db.close();
        } catch (DatabaseException e) {
          e.printStackTrace();
        }
      }
      if (env != null) {
        try {
          env.cleanLog();
          env.close();
        } catch (DatabaseException e) {
          e.printStackTrace();
        }
      }
    }
  }
  /**
   * Tests that when a file is opened with a lesser version than the current version, a new log file
   * is started for writing new log entries. This is important so that the new header version is
   * written even if no new log file is needed. If the new version were not written, an older
   * version of JE would not recognize that there had been a version change.
   */
  public void testLesserVersionNotUpdated() throws DatabaseException, IOException {

    TestUtils.loadLog(getClass(), Utils.MIN_VERSION_NAME, envHome);
    File logFile = new File(envHome, TestUtils.LOG_FILE_NAME);
    long origFileSize = logFile.length();

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    envConfig.setAllowCreate(false);
    envConfig.setTransactional(true);

    Environment env = new Environment(envHome, envConfig);
    env.sync();
    env.close();

    assertEquals(origFileSize, logFile.length());
  }
  private void createRepEnvInfo(String sleepTime) throws Throwable {

    /*
     * Set a large buffer size and disable the checkpointing, so the
     * data in the buffer can only be flushed by the LogFlushTask.
     */
    EnvironmentConfig envConfig = RepTestUtils.createEnvConfig(Durability.COMMIT_NO_SYNC);
    envConfig.setConfigParam(EnvironmentParams.MAX_MEMORY.getName(), "20000000");
    envConfig.setConfigParam(EnvironmentParams.LOG_MEM_SIZE.getName(), "120000000");
    envConfig.setConfigParam(EnvironmentParams.NUM_LOG_BUFFERS.getName(), "4");
    envConfig.setConfigParam(EnvironmentConfig.ENV_RUN_CHECKPOINTER, "false");

    /* Configure the log flush task. */
    ReplicationConfig repConfig = new ReplicationConfig();
    repConfig.setConfigParam(ReplicationConfig.LOG_FLUSH_TASK_INTERVAL, sleepTime);
    repEnvInfo = RepTestUtils.setupEnvInfos(envRoot, 3, envConfig, repConfig);
  }
  @Test
  public void getEnvironmentConfigTest() throws Exception {
    final EnvironmentConfig expected = new EnvironmentConfig();

    final BJEConfig bjeConfig = new BJEConfig();
    final Field privateConfigField = bjeConfig.getClass().getDeclaredField("envConfig");
    privateConfigField.setAccessible(true);
    privateConfigField.set(bjeConfig, expected);

    final EnvironmentConfig actual = bjeConfig.getEnvironmentConfig();

    // equals() is not overridden in EnvironmentConfig class. So not sure
    // about its behavior.
    // But toString is overridden. It is pretty enough for comparing objects
    // in the present test case.
    assertEquals(actual.toString(), expected.toString());
  }
  private void openGroup() throws IOException {

    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setTransactional(true);
    envConfig.setAllowCreate(true);

    ReplicationConfig repConfig = new ReplicationConfig();
    repConfig.setConfigParam(RepParams.VLSN_LOG_CACHE_SIZE.getName(), "2");

    repEnvInfo = RepTestUtils.setupEnvInfos(envRoot, nNodes, envConfig, repConfig);
    master = RepTestUtils.joinGroup(repEnvInfo);

    StoreConfig config = new StoreConfig();
    config.setAllowCreate(true);
    config.setTransactional(true);
    store = new EntityStore(master, "test", config);
    primaryIndex = store.getPrimaryIndex(Integer.class, AppData.class);
  }
  private static void openEnv() throws DatabaseException {
    System.out.println("opening env");

    // Set up the environment.
    EnvironmentConfig myEnvConfig = new EnvironmentConfig();
    myEnvConfig.setAllowCreate(true);
    myEnvConfig.setTransactional(true);
    // Environment handles are free-threaded in JE,
    // so we do not have to do anything to cause the
    // environment handle to be free-threaded.

    // Set up the database
    DatabaseConfig myDbConfig = new DatabaseConfig();
    myDbConfig.setAllowCreate(true);
    myDbConfig.setTransactional(true);
    myDbConfig.setSortedDuplicates(true);
    // no DatabaseConfig.setThreaded() method available.
    // db handles in java are free-threaded so long as the
    // env is also free-threaded.

    // Open the environment
    myEnv =
        new Environment(
            new File(myEnvPath), // Env home
            myEnvConfig);

    // Open the database. Do not provide a txn handle. This open
    // is autocommitted because DatabaseConfig.setTransactional()
    // is true.
    myDb =
        myEnv.openDatabase(
            null, // txn handle
            dbName, // Database file name
            myDbConfig);

    // Used by the bind API for serializing objects
    // Class database must not support duplicates
    myDbConfig.setSortedDuplicates(false);
    myClassDb =
        myEnv.openDatabase(
            null, // txn handle
            cdbName, // Database file name
            myDbConfig);
  }
Example #27
0
  /** Set up the environment and db. */
  private void initDbs(int nDumps, Hashtable[] dataMaps) throws DatabaseException {

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
    envConfig.setAllowCreate(true);
    env = new Environment(envHome, envConfig);

    /* Make a db and open it. */
    for (int i = 0; i < nDumps; i += 1) {
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setSortedDuplicates(true);
      Database myDb = env.openDatabase(null, dbName + i, dbConfig);
      Cursor cursor = myDb.openCursor(null, null);
      doLargePut(dataMaps[i], cursor, N_KEYS);
      cursor.close();
      myDb.close();
    }
  }
Example #28
0
 private CurrentTransaction(Environment env) {
   this.env = env;
   try {
     EnvironmentConfig config = env.getConfig();
     txnMode = config.getTransactional();
     lockingMode = DbCompat.getInitializeLocking(config);
     if (txnMode || lockingMode) {
       writeLockMode = LockMode.RMW;
     } else {
       writeLockMode = LockMode.DEFAULT;
     }
     cdbMode = DbCompat.getInitializeCDB(config);
     if (cdbMode) {
       localCdbCursors = new ThreadLocal();
     }
   } catch (DatabaseException e) {
     throw new RuntimeExceptionWrapper(e);
   }
 }
  private static void setJEProperties(
      BackendCfg cfg, EnvironmentConfig envConfig, ByteString backendId) {
    for (Map.Entry<String, String> mapEntry : attrMap.entrySet()) {
      String jeProperty = mapEntry.getKey();
      String attrName = mapEntry.getValue();

      String value = getPropertyValue(cfg, attrName, backendId);
      envConfig.setConfigParam(jeProperty, value);
    }
  }
  /** A database in a standalone environment should not be replicated. */
  @Test
  public void testStandalone() throws DatabaseException {

    try {
      EnvironmentConfig envConfig = new EnvironmentConfig();
      envConfig.setAllowCreate(true);
      env = new Environment(envRoot, envConfig);
      DatabaseConfig config = new DatabaseConfig();
      config.setAllowCreate(true);

      validate(config, false /* replicated */);
    } finally {
      if (db != null) {
        db.close();
      }

      if (env != null) {
        env.close();
      }
    }
  }