Exemple #1
0
 void initNew__wrappee__base(
     Environment env, Locker locker, String databaseName, DatabaseConfig dbConfig)
     throws DatabaseException {
   if (dbConfig.getReadOnly()) {
     throw new DatabaseException(
         "DatabaseConfig.setReadOnly() must be set to false " + "when creating a Database");
   }
   init(env, dbConfig);
   EnvironmentImpl environmentImpl = DbInternal.envGetEnvironmentImpl(envHandle);
   databaseImpl = environmentImpl.createDb(locker, databaseName, dbConfig, this);
   databaseImpl.addReferringHandle(this);
 }
  /* Test mutable and persistent configurations. */
  @Test
  public void testPersistentAndMutableConfigs() throws Exception {

    final String dbName = "foo";

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(true);
    env = create(envHome, envConfig);

    DbConfigManager configMgr = DbInternal.getEnvironmentImpl(env).getConfigManager();
    int defaultNodeMaxEntries = configMgr.getInt(EnvironmentParams.NODE_MAX);
    int defaultNodeDupTreeMaxEntries = configMgr.getInt(EnvironmentParams.NODE_MAX_DUPTREE);

    /* Check the default node max entries setting. */
    assertEquals(defaultNodeMaxEntries, 128);
    assertEquals(defaultNodeDupTreeMaxEntries, 128);

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

    /* Do updates on each persistent and mutable config. */

    /* Check whether BtreeComparator setting is persisted. */
    dbConfig.setOverrideBtreeComparator(true);
    dbConfig.setBtreeComparator(TestComparator.class);
    DatabaseConfig newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getBtreeComparator() instanceof TestComparator);

    /* Check whether DuplicateComparator setting is persisted. */
    dbConfig.setOverrideDuplicateComparator(true);
    dbConfig.setDuplicateComparator(new TestSerialComparator());
    newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getDuplicateComparator() instanceof TestSerialComparator);

    /* Check whether KeyPrefixing setting is persisted. */
    dbConfig.setKeyPrefixing(true);
    newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getKeyPrefixing());

    /* Check whether NodeMaxEntries setting is persisted. */
    dbConfig.setNodeMaxEntries(512);
    newConfig = setAndGetDbConfig(env, dbConfig, dbName);
    assertTrue(newConfig.getNodeMaxEntries() == 512);

    close(env);
  }
Exemple #3
0
  /**
   * Opens a sequence handle, adding the sequence record if appropriate.
   *
   * @throws IllegalArgumentException via Database.openSequence.
   * @throws IllegalStateException via Database.openSequence.
   */
  Sequence(Database db, Transaction txn, DatabaseEntry key, SequenceConfig config)
      throws SequenceNotFoundException, SequenceExistsException {

    if (db.getDatabaseImpl().getSortedDuplicates()) {
      throw new UnsupportedOperationException(
          "Sequences not supported in databases configured for " + "duplicates");
    }

    SequenceConfig useConfig = (config != null) ? config : SequenceConfig.DEFAULT;

    if (useConfig.getRangeMin() >= useConfig.getRangeMax()) {
      throw new IllegalArgumentException("Minimum sequence value must be less than the maximum");
    }

    if (useConfig.getInitialValue() > useConfig.getRangeMax()
        || useConfig.getInitialValue() < useConfig.getRangeMin()) {
      throw new IllegalArgumentException("Initial sequence value is out of range");
    }

    if (useConfig.getRangeMin() > useConfig.getRangeMax() - useConfig.getCacheSize()) {
      throw new IllegalArgumentException("The cache size is larger than the sequence range");
    }

    if (useConfig.getAutoCommitNoSync()) {
      autoCommitConfig = DbInternal.getDefaultTxnConfig(db.getEnvironment()).clone();
      autoCommitConfig.overrideDurability(Durability.COMMIT_NO_SYNC);
    } else {
      /* Use the environment's default transaction config. */
      autoCommitConfig = null;
    }

    this.db = db;
    this.key = copyEntry(key);
    logger = db.getEnvironment().getEnvironmentImpl().getLogger();

    /* Perform an auto-commit transaction to create the sequence. */
    Locker locker = null;
    Cursor cursor = null;
    OperationStatus status = OperationStatus.NOTFOUND;
    try {
      locker =
          LockerFactory.getReadableLocker(
              db.getEnvironment(),
              txn,
              db.isTransactional(),
              false /*retainNonTxnLocks*/,
              false /*readCommitedIsolation*/);

      cursor = new Cursor(db, locker, null);

      boolean sequenceExists = readData(cursor, null);
      boolean isWritableLocker =
          !db.getConfig().getTransactional()
              || (locker.isTransactional()
                  && !DbInternal.getEnvironmentImpl(db.getEnvironment()).isReplicated());

      if (sequenceExists) {
        if (useConfig.getAllowCreate() && useConfig.getExclusiveCreate()) {
          throw new SequenceExistsException(
              "ExclusiveCreate=true and the sequence record " + "already exists.");
        }
      } else {
        if (useConfig.getAllowCreate()) {
          if (!isWritableLocker) {
            if (cursor != null) {
              cursor.close();
            }
            locker.operationEnd(OperationStatus.SUCCESS);

            locker =
                LockerFactory.getWritableLocker(
                    db.getEnvironment(),
                    txn,
                    db.isTransactional(),
                    false,
                    db.getDatabaseImpl().isReplicated(),
                    autoCommitConfig);
            cursor = new Cursor(db, locker, null);
          }

          /* Get the persistent fields from the config. */
          rangeMin = useConfig.getRangeMin();
          rangeMax = useConfig.getRangeMax();
          increment = !useConfig.getDecrement();
          wrapAllowed = useConfig.getWrap();
          storedValue = useConfig.getInitialValue();

          /*
           * To avoid dependence on SerializableIsolation, try
           * putNoOverwrite first.  If it fails, then try to get an
           * existing record.
           */
          status = cursor.putNoOverwrite(key, makeData());

          if (!readData(cursor, null)) {
            /* A retry loop should be performed here. */
            throw new IllegalStateException("Sequence record removed during openSequence.");
          }
          status = OperationStatus.SUCCESS;
        } else {
          throw new SequenceNotFoundException(
              "AllowCreate=false and the sequence record " + "does not exist.");
        }
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
      if (locker != null) {
        locker.operationEnd(status);
      }
    }

    /*
     * cacheLast is initialized such that the cache will be considered
     * empty the first time get() is called.
     */
    cacheSize = useConfig.getCacheSize();
    cacheValue = storedValue;
    cacheLast = increment ? (storedValue - 1) : (storedValue + 1);
  }
  /** Test open and close of an environment. */
  public void testCacheStats() throws DatabaseException {

    EnvironmentConfig envConfig = TestUtils.initEnvConfig();
    envConfig.setTransactional(true);
    envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
    envConfig.setAllowCreate(true);
    env = new Environment(envHome, envConfig);
    EnvironmentStats stat = env.getStats(TestUtils.FAST_STATS);
    env.close();
    env = null;
    assertEquals(0, stat.getNCacheMiss());
    assertEquals(0, stat.getNNotResident());

    // Try to open and close again, now that the environment exists
    envConfig.setAllowCreate(false);
    envConfig.setConfigParam(EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
    env = new Environment(envHome, envConfig);
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    Database db = env.openDatabase(null, "foo", dbConfig);
    db.put(null, new DatabaseEntry(new byte[0]), new DatabaseEntry(new byte[0]));
    Transaction txn = env.beginTransaction(null, null);
    db.put(txn, new DatabaseEntry(new byte[0]), new DatabaseEntry(new byte[0]));
    stat = env.getStats(TestUtils.FAST_STATS);
    MemoryBudget mb = DbInternal.envGetEnvironmentImpl(env).getMemoryBudget();

    assertEquals(mb.getCacheMemoryUsage(), stat.getCacheTotalBytes());
    assertEquals(mb.getLogBufferBudget(), stat.getBufferBytes());
    assertEquals(mb.getTreeMemoryUsage() + mb.getTreeAdminMemoryUsage(), stat.getDataBytes());
    assertEquals(mb.getLockMemoryUsage(), stat.getLockBytes());
    assertEquals(mb.getAdminMemoryUsage(), stat.getAdminBytes());

    assertTrue(stat.getBufferBytes() > 0);
    assertTrue(stat.getDataBytes() > 0);
    assertTrue(stat.getLockBytes() > 0);
    assertTrue(stat.getAdminBytes() > 0);

    assertEquals(
        stat.getCacheTotalBytes(),
        stat.getBufferBytes() + stat.getDataBytes() + stat.getLockBytes() + stat.getAdminBytes());

    assertEquals(12, stat.getNCacheMiss());
    assertEquals(12, stat.getNNotResident());

    /* Test deprecated getCacheDataBytes method. */
    final EnvironmentStats finalStat = stat;
    final long expectCacheDataBytes = mb.getCacheMemoryUsage() - mb.getLogBufferBudget();
    (new Runnable() {
          @Deprecated
          public void run() {
            assertEquals(expectCacheDataBytes, finalStat.getCacheDataBytes());
          }
        })
        .run();

    txn.abort();
    db.close();
    env.close();
    env = null;
  }