public void runTest(DatabaseType type) throws DatabaseException, FileNotFoundException {
    int i;
    DatabaseConfig conf = new DatabaseConfig();
    conf.setErrorStream(TestUtils.getErrorStream());
    conf.setErrorPrefix("HashCompareTest");
    conf.setType(type);
    if (type == DatabaseType.HASH) {
      conf.setHashComparator(new HashComparator());
    } else conf.setBtreeComparator(new BtreeComparator());
    conf.setAllowCreate(true);

    Database db = new Database(HASHCOMPARETEST_DBNAME, null, conf);

    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry("world".getBytes());
    for (i = 0; i < 100; i++) {
      key.setData((new String("key" + i)).getBytes());
      db.put(null, key, data);
    }
    i = 0;
    Cursor dbc;
    dbc = db.openCursor(null, CursorConfig.DEFAULT);
    while (dbc.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
      ++i;
    }
    //		System.out.println("retrieved " + i + " entries");
    dbc.close();
    db.close();
  }
  /** Test exclusive creation. */
  @Test
  public void testExclusive() throws Throwable {

    try {
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();

      /*
       * Make sure that the database keeps its own copy of the
       * configuration object.
       */
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setExclusiveCreate(true);

      /* Should succeed and create the database. */
      Database dbA = env.openDatabase(null, "foo", dbConfig);
      dbA.close();

      /* Should not succeed, because the database exists. */
      try {
        env.openDatabase(null, "foo", dbConfig);
        fail("Database already exists");
      } catch (DatabaseException e) {
      }
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
Example #3
0
  public BDBCreator(String tableName, ArrayList<Integer> keyPositions, int secondaryIndex) {
    /* Creates and loads a BDB table with a secondary index on the column defined by*/
    this.tableName = tableName.toLowerCase();
    this.keyPositions = keyPositions;
    this.secIndexPos = secondaryIndex;
    this.tMap = Main.indexTypeMaps.get(this.tableName);
    setEnvironment();

    dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    myDB = myDbEnvironment.openDatabase(null, this.tableName, dbConfig);
    SecondaryConfig secConfig = new SecondaryConfig();
    secConfig.setAllowCreate(true);
    secConfig.setSortedDuplicates(true);

    createSecDB(secConfig);
    setConfig(dbConfig, secConfig);

    loadData();

    if (secDB != null) {
      secDB.close();
    }
    if (myDB != null) {
      myDB.close();
    }

    closeEnvironment();
  }
Example #4
0
  public static Database buildsecondary(Database std) {
    // Parse database loaded
    try {
      io.deleteFile("secondarydb");
      // SecondaryDatabases started
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setType(DatabaseType.HASH);
      dbConfig.setAllowCreate(true);
      dbConfig.setUnsortedDuplicates(true);
      Database secdb = new Database("secondarydb", null, dbConfig);
      // Cursors started
      Cursor stdcursor = std.openCursor(null, null);
      Cursor secdbcursor = secdb.openCursor(null, null);
      // Key and Data started
      DatabaseEntry stdkey = new DatabaseEntry();
      DatabaseEntry stddata = new DatabaseEntry();
      DatabaseEntry seckey = new DatabaseEntry();
      DatabaseEntry secdata = new DatabaseEntry();

      while (stdcursor.getNext(stdkey, stddata, LockMode.DEFAULT)
          == OperationStatus.SUCCESS) { // Writing into secondary db
        String[] key = new String(stdkey.getData()).split(",");
        String data = new String(stddata.getData());
        // DEBUG:
        // System.out.println("key 0:" + key[0] + " key 1:" + key[1] + " data:" + data);
        seckey.setData(key[1].getBytes());
        OperationStatus operation = secdbcursor.getSearchKey(seckey, secdata, LockMode.DEFAULT);

        String b = null;
        while (operation == OperationStatus.SUCCESS) {
          b = new String(secdata.getData());
          secdbcursor.delete();
          operation = secdbcursor.getNextDup(seckey, secdata, LockMode.DEFAULT);
        }
        if (b == null) {
          seckey.setData(key[1].getBytes());
          secdata.setData(("(" + key[0] + "," + data + ")").getBytes());
          secdb.put(null, seckey, secdata);
        }
        if (b != null) {
          secdata.setData(b.concat("(" + key[0] + "," + data + ")").getBytes());
          secdb.put(null, seckey, secdata);
        }
        seckey.setData(null);
        secdata.setData(null);

        stdkey.setData(null);
        stddata.setData(null);
      }
      // io.debugread(secdb);

      return secdb;
    } catch (Exception e) {
      System.out.println("Error creating <user>,<song,rating> secondary table!\n");
      System.out.println(e.getMessage());
    }
    return null; // SHOULD NEVER HAPPEN
  }
Example #5
0
  public static Database setupDatabase(boolean readOnly, String dbName, Bdb bdb)
      throws IOException, DatabaseException {
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setReadOnly(readOnly);
    dbConfig.setAllowCreate(!readOnly);
    dbConfig.setDeferredWrite(!readOnly);

    return bdb.bdbEnv.openDatabase(null, dbName, dbConfig);
  }
Example #6
0
 public WorkQueues(Environment env, String dbName, boolean resumable) throws DatabaseException {
   this.env = env;
   this.resumable = resumable;
   DatabaseConfig dbConfig = new DatabaseConfig();
   dbConfig.setAllowCreate(true);
   dbConfig.setTransactional(resumable);
   dbConfig.setDeferredWrite(!resumable);
   urlsDB = env.openDatabase(null, dbName, dbConfig);
   webURLBinding = new WebURLTupleBinding();
 }
  /**
   * 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;
    }
  }
  /* 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);
  }
Example #9
0
  public BDBCreator(String tableName, ArrayList<Integer> keyPositions) {
    /* Creates and loads a BDB table without a secondary index*/
    this.tableName = tableName.toLowerCase();
    this.keyPositions = keyPositions;
    this.tMap = Main.indexTypeMaps.get(this.tableName);
    setEnvironment();

    dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    myDB = myDbEnvironment.openDatabase(null, this.tableName, dbConfig);

    loadData();

    if (myDB != null) {
      myDB.close();
    }
    closeEnvironment();
  }
  public static void main(String[] args) {

    Environment myEnv;
    Database myDatabase;

    try {
      EnvironmentConfig envConfig = new EnvironmentConfig();
      envConfig.setAllowCreate(true);
      myEnv = new Environment(new File("/Users/broso/testDB"), envConfig);

      // Environment open omitted for clarity
      DatabaseConfig myDbConfig = new DatabaseConfig();
      SecondaryConfig mySecConfig = new SecondaryConfig();
      myDbConfig.setAllowCreate(true);
      mySecConfig.setAllowCreate(true);
      // Duplicates are frequently required for secondary databases.
      mySecConfig.setSortedDuplicates(true);

      // Open the primary

      String dbName = "myPrimaryDatabase";
      Database myDb = myEnv.openDatabase(null, dbName, myDbConfig);

      // Open the secondary.
      // Key creators are described in the next section.
      // AKeyCreator akc = new AKeyCreator();

      // Get a secondary object and set the key creator on it.
      // mySecConfig.setKeyCreator(keyCreator);

      // Perform the actual open
      String secDbName = "mySecondaryDatabase";
      SecondaryDatabase mySecDb = myEnv.openSecondaryDatabase(null, secDbName, myDb, mySecConfig);
    } catch (DatabaseException de) {
      // Exception handling goes here
    }
  }
Example #11
0
  public static Database buildtertiary(Database std) {

    try {
      // configure new database instance
      io.deleteFile("tertiarydb");
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setType(DatabaseType.HASH);
      dbConfig.setAllowCreate(true);
      dbConfig.setUnsortedDuplicates(true);
      Database tdb = new Database("tertiarydb", null, dbConfig);

      // configure cursors and entries
      Cursor stdCurs = std.openCursor(null, null);
      Cursor tCurs = tdb.openCursor(null, null);
      DatabaseEntry stdKey = new DatabaseEntry();
      DatabaseEntry stdData = new DatabaseEntry();
      DatabaseEntry tKey = new DatabaseEntry();
      DatabaseEntry tData = new DatabaseEntry();

      // extract from linearly constructed database to populate <song>,<users> table, making
      // use of songs grouped by id
      String currUsers = "";
      String prevID = "default";
      boolean firstIter = true;
      while (stdCurs.getNext(stdKey, stdData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
        // get rating data from current row
        String[] currIDUser = (new String(stdKey.getData())).split(",");
        String currID = currIDUser[0].trim();
        String currUser = currIDUser[1].trim();
        String currRating = (new String(stdData.getData()));
        stdKey.setData(null);
        stdData.setData(null);
        if (currID.equals(prevID) || firstIter) {
          // concatenate new username with current string
          currUsers += "(" + currUser + "," + currRating + ")";
        } else if (!firstIter) {
          // insert completed <usernames> into table under key <song id>
          tKey.setData(prevID.getBytes());
          tData.setData(currUsers.substring(0, currUsers.length()).getBytes());
          tCurs.put(tKey, tData);
          tKey.setData(null);
          tData.setData(null);
          // DEBUG:
          // System.out.println(prevID+","+currUsers.substring(0, currUsers.length()-1));
          // start the new <usernames> for the next song (in currID)
          currUsers = "(" + currUser + "," + currRating + ")";
        }
        prevID = currID;
        firstIter = false;
      }
      // repeat iteration for last song
      tKey.setData(prevID.getBytes());
      tData.setData(currUsers.substring(0, currUsers.length()).getBytes());
      tCurs.put(tKey, tData);
      tKey.setData(null);
      tData.setData(null);

      // DEBUG:
      // io.debugread(tdb);
      tCurs.close();

      return tdb;

    } catch (Exception e) {
      System.out.println(" error creating <song>,<users> tertiary table\n");
      System.out.println(e.getMessage());
    }
    return null; // should never happen
  }
Example #12
0
  public static String top3(String line, Database secdb, Database tdb)
      throws DatabaseException, FileNotFoundException {
    // configure sqSum db for keeping track of cumulative squaresums
    io.deleteFile("sqSumdb");
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setType(DatabaseType.HASH);
    dbConfig.setAllowCreate(true);
    dbConfig.setUnsortedDuplicates(true);
    Database sqSumdb = new Database("sqSumdb", null, dbConfig);

    Cursor sqSumCurs = sqSumdb.openCursor(null, null);
    DatabaseEntry sqSumKey = new DatabaseEntry();
    DatabaseEntry sqSumData = new DatabaseEntry();

    Cursor secCurs = secdb.openCursor(null, null);
    Cursor tCurs = tdb.openCursor(null, null);
    DatabaseEntry secKey = new DatabaseEntry();
    DatabaseEntry secData = new DatabaseEntry();
    DatabaseEntry tKey = new DatabaseEntry();
    DatabaseEntry tData = new DatabaseEntry();

    // get input song users and ratings
    tKey.setData(line.getBytes());
    if (tCurs.getSearchKey(tKey, tData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
      // for each user in input song, get songs and ratings
      HashMap<String, String> iRatings = parseBrace(new String(tData.getData()));
      for (String user : iRatings.keySet()) {
        // get current user's rating for input song
        int inputRating = Integer.parseInt(iRatings.get(user));
        // get songs and ratings for user
        secKey.setData(null);
        secData.setData(null);
        secKey.setData(user.getBytes());
        if (secCurs.getSearchKey(secKey, secData, null) == OperationStatus.SUCCESS) {
          // for each song rated by user
          HashMap<String, String> currSongs = parseBrace(new String(secData.getData()));
          for (String song : currSongs.keySet()) {
            // input song should not be compared to itself, otherwise get the users and ratings for
            // it
            if (song.equals(line)) continue;
            // current user's rating for song under inspection
            int songRating = Integer.parseInt(currSongs.get(song));
            sqSumKey.setData(null);
            sqSumData.setData(null);
            sqSumKey.setData(song.getBytes());
            if (sqSumCurs.getSearchKey(sqSumKey, sqSumData, null) == OperationStatus.SUCCESS) {
              // if song is already in sqSum,N db, add to it
              String[] sumN = (new String(sqSumData.getData())).split(",");

              // add the new square sum to the previous and increment N
              int sum = Integer.parseInt(sumN[0]);
              int N = Integer.parseInt(sumN[1]);
              sum += (inputRating - songRating) * (inputRating - songRating);
              N += 1;
              String encoded = Integer.toString(sum) + "," + Integer.toString(N);

              // remove old entry for current song and replace it with the new one
              sqSumCurs.delete();
              sqSumKey.setData(null);
              sqSumData.setData(null);
              sqSumKey.setData(song.getBytes());
              sqSumData.setData(encoded.getBytes());
              sqSumdb.put(null, sqSumKey, sqSumData);

            } else {
              // if song in not already in sqSum,N db, create it
              int sum = (inputRating - songRating) * (inputRating - songRating);
              int N = 1;
              String encoded = Integer.toString(sum) + "," + Integer.toString(N);
              sqSumKey.setData(null);
              sqSumData.setData(null);
              sqSumKey.setData(song.getBytes());
              sqSumData.setData(encoded.getBytes());
              sqSumdb.put(null, sqSumKey, sqSumData);
            }
          }
        } else {
          System.out.println("ERROR:  user " + user + " not in secondary database.");
        }
      }
    } else {
      return line + " has not been rated by any user.";
    }
    // DEBUG
    // io.debugread(sqSumdb);

    // calculate distance for each song in, inserting into the top 3 spots as we iterate through
    String ranked = rank(sqSumdb);
    return line + ", " + ranked;
  }
Example #13
0
  public int doloop() throws DatabaseException {
    Database db = null;

    for (; ; ) {
      if (db == null) {
        DatabaseConfig dbconf = new DatabaseConfig();
        dbconf.setType(DatabaseType.BTREE);
        if (dbenv.getIsMaster()) {
          /*
           * Open database allowing create only if this is a master
           * database.  A client database uses polling to attempt
           * to open the database without allowing create until
           * it is successful.
           *
           * This polling logic for allowing create can be
           * simplified under some circumstances.  For example, if
           * the application can be sure a database is already
           * there, it would never need to open it allowing create.
           */
          dbconf.setAllowCreate(true);
        }
        dbconf.setTransactional(true);

        try {
          db = dbenv.openDatabase(null, RepConfig.progname, null, dbconf);
        } catch (java.io.FileNotFoundException e) {
          System.err.println("no stock database available yet.");
          if (db != null) {
            db.close(true);
            db = null;
          }
          try {
            Thread.sleep(RepConfig.SLEEPTIME);
          } catch (InterruptedException ie) {
          }
          continue;
        }
      }

      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

      /* Listen for input, and add it to the database. */
      System.out.print("QUOTESERVER");
      if (!dbenv.getIsMaster()) System.out.print("(read-only)");
      System.out.print("> ");
      System.out.flush();
      String nextline = null;
      try {
        nextline = stdin.readLine();
      } catch (IOException ioe) {
        System.err.println("Unable to get data from stdin");
        break;
      }
      String[] words = nextline.split("\\s");

      /* A blank line causes the DB to be dumped to stdout. */
      if (words.length == 0 || (words.length == 1 && words[0].length() == 0)) {
        try {
          if (dbenv.getInClientSync())
            System.err.println("Cannot read data during client initialization - please try again.");
          else printStocks(db);
        } catch (DeadlockException de) {
          continue;
        } catch (DatabaseException e) {
          /*
           * This could be DB_REP_HANDLE_DEAD, which
           * should close the database and continue.
           */
          System.err.println("Got db exception reading replication" + "DB: " + e);
          System.err.println(
              "Expected if it was due to a dead "
                  + "replication handle, otherwise an unexpected error.");
          db.close(true); /* Close no sync. */
          db = null;
          continue;
        }
        continue;
      }

      if (words.length == 1
          && (words[0].compareToIgnoreCase("quit") == 0
              || words[0].compareToIgnoreCase("exit") == 0)) {
        dbenv.setAppFinished(true);
        break;
      } else if (words.length != 2) {
        System.err.println("Format: TICKER VALUE");
        continue;
      }

      if (!dbenv.getIsMaster()) {
        System.err.println("Can't update client.");
        continue;
      }

      DatabaseEntry key = new DatabaseEntry(words[0].getBytes());
      DatabaseEntry data = new DatabaseEntry(words[1].getBytes());

      db.put(null, key, data);
    }
    if (db != null) db.close(true);
    return 0;
  }
  /*
   * Test that changing the Btree comparator really writes it to disk.
   */
  @Test
  public void testConfigOverrideUpdateSR15743() 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.setOverrideBtreeComparator(false);
      dbConfigA.setBtreeComparator(TestComparator.class);
      dbConfigA.setAllowCreate(true);
      Database dbA = env.openDatabase(null, "foo", dbConfigA);

      /* Change the original dbConfig */
      dbConfigA.setBtreeComparator(TestComparator2.class);
      DatabaseConfig getConfig1 = dbA.getConfig();
      assertEquals(TestComparator.class, getConfig1.getBtreeComparator().getClass());

      /*
       * Change the retrieved config, ought to have no effect on what the
       * Database is storing.
       */
      getConfig1.setBtreeComparator(TestComparator2.class);
      DatabaseConfig getConfig2 = dbA.getConfig();
      assertEquals(TestComparator.class, getConfig2.getBtreeComparator().getClass());

      dbA.close();
      close(env);

      /* Ensure new comparator is written to disk. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      /* Change the comparator. */
      dbConfigA.setOverrideBtreeComparator(true);
      dbConfigA.setBtreeComparator(TestComparator2.class);
      dbA = env.openDatabase(null, "foo", dbConfigA);

      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator2.class, getConfig2.getBtreeComparator().getClass());

      dbA.close();
      close(env);

      /* Read it back during recovery to ensure it was written. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      dbA = env.openDatabase(null, "foo", dbConfigA);
      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator2.class, getConfig2.getBtreeComparator().getClass());

      /* Create a root for the tree. */
      dbA.put(null, new DatabaseEntry(new byte[1]), new DatabaseEntry(new byte[1]));

      dbA.close();
      close(env);

      /* Change it to a third one when there is a root present. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      /* Change the comparator. */
      dbConfigA.setOverrideBtreeComparator(true);
      dbConfigA.setBtreeComparator(TestComparator3.class);
      dbA = env.openDatabase(null, "foo", dbConfigA);
      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator3.class, getConfig2.getBtreeComparator().getClass());
      dbA.close();
      close(env);

      /* Read it back during recovery to ensure it was written. */
      envConfig = TestUtils.initEnvConfig();
      env = create(envHome, envConfig);

      dbConfigA = new DatabaseConfig();
      dbA = env.openDatabase(null, "foo", dbConfigA);
      getConfig2 = dbA.getConfig();
      assertEquals(TestComparator3.class, getConfig2.getBtreeComparator().getClass());
      dbA.close();
      close(env);

    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
  @Test
  public void testOpenReadOnly() throws Throwable {

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

      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();

      Transaction txn = env.beginTransaction(null, null);
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setTransactional(true);
      dbConfig.setAllowCreate(true);
      Database myDb = env.openDatabase(txn, "testDB2", dbConfig);

      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      try {
        myDb.put(txn, key, data);
      } catch (DatabaseException DBE) {
        fail("unexpected DatabaseException during put");
      }

      txn.commit();
      myDb.close();

      dbConfig = new DatabaseConfig();
      dbConfig.setTransactional(true);
      dbConfig.setReadOnly(true);
      txn = env.beginTransaction(null, null);
      myDb = env.openDatabase(txn, "testDB2", dbConfig);
      assertTrue(myDb.isTransactional());
      assertTrue(myDb.getConfig().getTransactional());

      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      try {
        myDb.put(txn, key, data);
        fail("expected UnsupportedOperationException " + "because open RDONLY");
      } catch (UnsupportedOperationException expected) {
      }

      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      assertEquals(OperationStatus.SUCCESS, myDb.get(txn, key, data, LockMode.DEFAULT));

      Cursor cursor = myDb.openCursor(txn, null);

      assertEquals(OperationStatus.SUCCESS, cursor.getFirst(key, data, LockMode.DEFAULT));

      try {
        cursor.delete();
        fail("expected Exception from delete on RD_ONLY db");
      } catch (UnsupportedOperationException e) {
      }

      key.setData(TestUtils.getTestArray(1));
      data.setData(TestUtils.getTestArray(1));
      try {
        myDb.put(txn, key, data);
        fail("expected UnsupportedOperationException because open RDONLY");
      } catch (UnsupportedOperationException expected) {
      }

      cursor.close();
      txn.commit();
      myDb.close();

      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
  @Test
  public void testIsTransactional() throws Throwable {

    try {
      /* Open environment in transactional mode.*/
      EnvironmentConfig envConfig = TestUtils.initEnvConfig();
      envConfig.setTransactional(true);
      envConfig.setAllowCreate(true);
      env = create(envHome, envConfig);

      /* Create a db, open transactionally with implied auto-commit. */
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setTransactional(true);
      Database myDb = env.openDatabase(null, "testDB", dbConfig);
      assertTrue(myDb.isTransactional());
      assertTrue(myDb.getConfig().getTransactional());
      myDb.close();

      /* Open an existing db, can open it non-transactionally. */
      dbConfig.setTransactional(false);
      myDb = env.openDatabase(null, "testDB", null);
      assertFalse(myDb.isTransactional());
      assertFalse(myDb.getConfig().getTransactional());
      myDb.close();

      /* Open another db, pass an explicit transaction. */
      dbConfig.setTransactional(true);
      Transaction txn = env.beginTransaction(null, null);
      myDb = env.openDatabase(txn, "testDB2", dbConfig);
      assertTrue(myDb.isTransactional());
      assertTrue(myDb.getConfig().getTransactional());

      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();
      key.setData(TestUtils.getTestArray(0));
      data.setData(TestUtils.getTestArray(0));
      try {
        myDb.put(null, key, data);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException, implied autocommit");
      }

      key.setData(TestUtils.getTestArray(1));
      data.setData(TestUtils.getTestArray(1));
      try {
        myDb.put(txn, key, data);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException with txn passed");
      }

      try {
        myDb.get(txn, key, data, LockMode.DEFAULT);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException with txn passed");
      }

      txn.commit();

      try {
        myDb.get(null, key, data, LockMode.DEFAULT);
      } catch (DatabaseException DBE) {
        fail("didn't expect DatabaseException because no txn passed");
      }

      myDb.close();

      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }
  /**
   * Test that any conflicts between configuration object settings and the underlying impl object
   * are detected.
   */
  @Test
  public void testConfigConflict() throws Throwable {

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

      /*
       * Test conflicts of duplicate allowed configuration.
       */

      /* 1a. Create allowing duplicates. */
      DatabaseConfig firstConfig = new DatabaseConfig();
      firstConfig.setAllowCreate(true);
      firstConfig.setSortedDuplicates(true);
      Database firstHandle = env.openDatabase(null, "fooDups", firstConfig);
      /* 1b. Try to open w/no duplicates. */
      DatabaseConfig secondConfig = new DatabaseConfig();
      secondConfig.setSortedDuplicates(false);
      try {
        env.openDatabase(null, "fooDups", secondConfig);
        fail("Conflict in duplicates allowed should be detected.");
      } catch (IllegalArgumentException expected) {
      }

      firstHandle.close();
      env.removeDatabase(null, "fooDups");

      /* 2a. Create dis-allowing duplicates. */
      firstConfig.setSortedDuplicates(false);
      firstConfig.setKeyPrefixing(false);
      firstHandle = env.openDatabase(null, "fooDups", firstConfig);
      /* 2b. Try to open w/duplicates. */
      secondConfig.setSortedDuplicates(true);
      try {
        env.openDatabase(null, "fooDups", secondConfig);
        fail("Conflict in duplicates allowed should be detected.");
      } catch (IllegalArgumentException expected) {
      }
      firstHandle.close();

      /*
       * Test conflicts of read only. If the environment is read/write
       * we should be able to open handles in read only or read/write
       * mode. If the environment is readonly, the database handles
       * must also be read only.
       */
      DatabaseConfig readOnlyConfig = new DatabaseConfig();
      readOnlyConfig.setReadOnly(true);
      Database roHandle = env.openDatabase(null, "fooDups", readOnlyConfig);
      roHandle.close();

      /* Open the environment in read only mode. */
      close(env);
      envConfig = TestUtils.initEnvConfig();
      envConfig.setReadOnly(true);
      env = create(envHome, envConfig);

      /* Open a readOnly database handle, should succeed */
      roHandle = env.openDatabase(null, "fooDups", readOnlyConfig);
      roHandle.close();

      /* Open a read/write database handle, should not succeed. */
      try {
        env.openDatabase(null, "fooDups", null);
        fail("Should not be able to open read/write");
      } catch (IllegalArgumentException expected) {
      }
      close(env);

      /*
       * Check comparator changes.
       */
      /* 1a. Open w/a null comparator */
      env = create(envHome, null);
      firstConfig = new DatabaseConfig();
      firstConfig.setAllowCreate(true);
      firstHandle = env.openDatabase(null, "fooComparator", firstConfig);
      DatabaseConfig firstRetrievedConfig = firstHandle.getConfig();
      assertEquals(null, firstRetrievedConfig.getBtreeComparator());
      assertEquals(null, firstRetrievedConfig.getDuplicateComparator());

      /*
       * 1b. Open a db w/a different comparator, shouldn't take effect
       * because override is not set.
       */
      secondConfig = new DatabaseConfig();
      Comparator btreeComparator = new TestComparator();
      Comparator dupComparator = new TestComparator();
      secondConfig.setBtreeComparator((Class<Comparator<byte[]>>) btreeComparator.getClass());
      secondConfig.setDuplicateComparator((Class<Comparator<byte[]>>) dupComparator.getClass());
      Database secondHandle = env.openDatabase(null, "fooComparator", secondConfig);
      DatabaseConfig retrievedConfig = secondHandle.getConfig();
      assertEquals(null, retrievedConfig.getBtreeComparator());
      assertEquals(null, retrievedConfig.getDuplicateComparator());
      secondHandle.close();

      /* Same as above but with a serialized comparator. */
      secondConfig = new DatabaseConfig();
      btreeComparator = new TestSerialComparator();
      dupComparator = new TestSerialComparator();
      secondConfig.setBtreeComparator(btreeComparator);
      secondConfig.setDuplicateComparator(dupComparator);
      secondHandle = env.openDatabase(null, "fooComparator", secondConfig);
      retrievedConfig = secondHandle.getConfig();
      assertEquals(null, retrievedConfig.getBtreeComparator());
      assertEquals(null, retrievedConfig.getDuplicateComparator());
      secondHandle.close();

      /*
       * Test that update DatabaseConfig while there are open handles
       * should throw exceptions.
       */
      secondConfig.setOverrideBtreeComparator(true);
      secondConfig.setOverrideDuplicateComparator(true);
      btreeComparator = new TestComparator();
      dupComparator = new TestComparator();
      secondConfig.setBtreeComparator((Class<Comparator<byte[]>>) btreeComparator.getClass());
      secondConfig.setDuplicateComparator((Class<Comparator<byte[]>>) dupComparator.getClass());
      try {
        secondHandle = env.openDatabase(null, "fooComparator", secondConfig);
        fail("Expect exceptions here");
      } catch (IllegalStateException e) {
        /* Expected exception. */
      } catch (Exception e) {
        fail("Unexpected exception: " + e.getMessage());
      }
      secondHandle.close();

      /*
       * Open a new database handle without DatabaseConfig changes should
       * be valid.
       */
      try {
        secondHandle = env.openDatabase(null, "fooComparator", firstConfig);
      } catch (Exception e) {
        fail("Unexpected exception: " + e.getMessage());
      }

      secondHandle.close();
      firstHandle.close();
      close(env);
    } catch (Throwable t) {
      t.printStackTrace();
      close(env);
      throw t;
    }
  }
  /** 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;
  }
Example #19
0
  //
  // Initialize the database to the number of accounts, branches,
  // history records, and tellers given to the constructor.
  //
  public void populate() {
    Database dbp = null;

    int err;
    int balance, idnum;
    int end_anum, end_bnum, end_tnum;
    int start_anum, start_bnum, start_tnum;
    int h_nelem;

    idnum = BEGID;
    balance = 500000;

    h_nelem = accounts;

    try {
      DatabaseConfig config = new DatabaseConfig();
      config.setType(DatabaseType.HASH);
      config.setHashNumElements(h_nelem);
      config.setAllowCreate(true);
      dbp = dbenv.openDatabase(null, "account", null, config);
    } catch (Exception e1) {
      // can be DatabaseException or FileNotFoundException
      errExit(e1, "Open of account file failed");
    }

    start_anum = idnum;
    populateTable(dbp, idnum, balance, h_nelem, "account");
    idnum += h_nelem;
    end_anum = idnum - 1;
    try {
      dbp.close();
    } catch (DatabaseException e2) {
      errExit(e2, "Account file close failed");
    }

    if (verbose)
      System.out.println(
          "Populated accounts: " + String.valueOf(start_anum) + " - " + String.valueOf(end_anum));

    //
    // Since the number of branches is very small, we want to use very
    // small pages and only 1 key per page.  This is the poor-man's way
    // of getting key locking instead of page locking.
    //
    h_nelem = (int) branches;

    try {
      DatabaseConfig config = new DatabaseConfig();
      config.setType(DatabaseType.HASH);
      config.setHashNumElements(h_nelem);
      config.setHashFillFactor(1);
      config.setPageSize(512);
      config.setAllowCreate(true);
      dbp = dbenv.openDatabase(null, "branch", null, config);
    } catch (Exception e3) {
      // can be DatabaseException or FileNotFoundException
      errExit(e3, "Branch file create failed");
    }

    start_bnum = idnum;
    populateTable(dbp, idnum, balance, h_nelem, "branch");
    idnum += h_nelem;
    end_bnum = idnum - 1;

    try {
      dbp.close();
    } catch (DatabaseException dbe4) {
      errExit(dbe4, "Close of branch file failed");
    }

    if (verbose)
      System.out.println(
          "Populated branches: " + String.valueOf(start_bnum) + " - " + String.valueOf(end_bnum));

    //
    // In the case of tellers, we also want small pages, but we'll let
    // the fill factor dynamically adjust itself.
    //
    h_nelem = (int) tellers;

    try {
      DatabaseConfig config = new DatabaseConfig();
      config.setType(DatabaseType.HASH);
      config.setHashNumElements(h_nelem);
      config.setHashFillFactor(0);
      config.setPageSize(512);
      config.setAllowCreate(true);
      dbp = dbenv.openDatabase(null, "teller", null, config);
    } catch (Exception e5) {
      // can be DatabaseException or FileNotFoundException
      errExit(e5, "Teller file create failed");
    }

    start_tnum = idnum;
    populateTable(dbp, idnum, balance, h_nelem, "teller");
    idnum += h_nelem;
    end_tnum = idnum - 1;

    try {
      dbp.close();
    } catch (DatabaseException e6) {
      errExit(e6, "Close of teller file failed");
    }

    if (verbose)
      System.out.println(
          "Populated tellers: " + String.valueOf(start_tnum) + " - " + String.valueOf(end_tnum));

    try {
      DatabaseConfig config = new DatabaseConfig();
      config.setType(DatabaseType.RECNO);
      config.setRecordLength(HISTORY_LEN);
      config.setAllowCreate(true);
      dbp = dbenv.openDatabase(null, "history", null, config);
    } catch (Exception e7) {
      // can be DatabaseException or FileNotFoundException
      errExit(e7, "Create of history file failed");
    }

    populateHistory(dbp);

    try {
      dbp.close();
    } catch (DatabaseException e8) {
      errExit(e8, "Close of history file failed");
    }
  }