Beispiel #1
0
  /** See if this new handle's configuration is compatible with the pre-existing database. */
  private void validateConfigAgainstExistingDb(DatabaseConfig config, DatabaseImpl databaseImpl)
      throws DatabaseException {

    /*
     * The allowDuplicates property is persistent and immutable.  It does
     * not need to be specified if the useExistingConfig property is set.
     */
    if (!config.getUseExistingConfig()) {
      if (databaseImpl.getSortedDuplicates() != config.getSortedDuplicates()) {
        throw new DatabaseException(
            "You can't open a Database with a duplicatesAllowed "
                + "configuration of "
                + config.getSortedDuplicates()
                + " if the underlying database was created with a "
                + "duplicatesAllowedSetting of "
                + databaseImpl.getSortedDuplicates()
                + ".");
      }
    }

    /*
     * The transactional property is kept constant while any handles are
     * open, and set when the first handle is opened.  It does not need to
     * be specified if the useExistingConfig property is set.
     */
    if (databaseImpl.hasOpenHandles()) {
      if (!config.getUseExistingConfig()) {
        if (config.getTransactional() != databaseImpl.isTransactional()) {
          throw new DatabaseException(
              "You can't open a Database with a transactional "
                  + "configuration of "
                  + config.getTransactional()
                  + " if the underlying database was created with a "
                  + "transactional configuration of "
                  + databaseImpl.isTransactional()
                  + ".");
        }
      }
    } else {
      databaseImpl.setTransactional(config.getTransactional());
    }

    /*
     * Only re-set the comparators if the override is allowed.
     */
    if (config.getOverrideBtreeComparator()) {
      databaseImpl.setBtreeComparator(config.getBtreeComparator());
    }

    if (config.getOverrideDuplicateComparator()) {
      databaseImpl.setDuplicateComparator(config.getDuplicateComparator());
    }
  }
  /**
   * Test that we can retrieve a database configuration and that it clones its configuration
   * appropriately.
   */
  @Test
  public void testConfig() throws Throwable {

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

      /*
       * Make sure that the database keeps its own copy of the
       * configuration object.
       */
      DatabaseConfig dbConfigA = new DatabaseConfig();
      dbConfigA.setAllowCreate(true);
      Database dbA = env.openDatabase(null, "foo", dbConfigA);

      /* Change the original dbConfig */
      dbConfigA.setAllowCreate(false);
      DatabaseConfig getConfig1 = dbA.getConfig();
      assertEquals(true, getConfig1.getAllowCreate());
      assertEquals(false, getConfig1.getSortedDuplicates());

      /*
       * Change the retrieved config, ought to have no effect on what the
       * Database is storing.
       */
      getConfig1.setSortedDuplicates(true);
      DatabaseConfig getConfig2 = dbA.getConfig();
      assertEquals(false, getConfig2.getSortedDuplicates());

      dbA.close();
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
Beispiel #3
0
 private void validateConfigAgainstExistingDb__wrappee__base(
     DatabaseConfig config, DatabaseImpl databaseImpl) throws DatabaseException {
   if (!config.getUseExistingConfig()) {
     if (databaseImpl.getSortedDuplicates() != config.getSortedDuplicates()) {
       throw new DatabaseException(
           "You can't open a Database with a duplicatesAllowed "
               + "configuration of "
               + config.getSortedDuplicates()
               + " if the underlying database was created with a "
               + "duplicatesAllowedSetting of "
               + databaseImpl.getSortedDuplicates()
               + ".");
     }
   }
   if (databaseImpl.hasOpenHandles()) {
     if (!config.getUseExistingConfig()) {
       if (config.getTransactional() != databaseImpl.isTransactional()) {
         throw new DatabaseException(
             "You can't open a Database with a transactional "
                 + "configuration of "
                 + config.getTransactional()
                 + " if the underlying database was created with a "
                 + "transactional configuration of "
                 + databaseImpl.isTransactional()
                 + ".");
       }
     }
   } else {
     databaseImpl.setTransactional(config.getTransactional());
   }
   if (config.getOverrideBtreeComparator()) {
     databaseImpl.setBtreeComparator(config.getBtreeComparator());
   }
   if (config.getOverrideDuplicateComparator()) {
     databaseImpl.setDuplicateComparator(config.getDuplicateComparator());
   }
 }