@Override
 public synchronized void putDecision(Long instance, Decision decision) {
   keyBinding.objectToEntry(instance, key);
   dataBinding.objectToEntry(decision, data);
   OperationStatus status = db.put(null, key, data);
   if (logger.isDebugEnabled()) {
     logger.debug("DB put " + decision + " " + status.name());
   }
 }
 @Override
 public synchronized void putBallot(Long instance, int ballot) {
   keyBinding.objectToEntry(instance, key);
   ballotBinding.objectToEntry(ballot, ballot_data);
   OperationStatus status = db.put(null, key, ballot_data);
   if (logger.isDebugEnabled()) {
     logger.debug("DB put ballot " + ballot + " for instance " + instance + " " + status.name());
   }
 }
    public boolean createSecondaryKey(
        SecondaryDatabase secondaryDb,
        DatabaseEntry keyEntry,
        DatabaseEntry dataEntry,
        DatabaseEntry resultEntry) {

      /*
       * Convert the data entry to a MyData object, extract the secondary
       * key value from it, and then convert it to the resulting
       * secondary key entry.
       */
      MyData data = dataBinding.entryToObject(dataEntry);
      String key = data.getMessage();
      if (key != null) {
        secKeyBinding.objectToEntry(key, resultEntry);
        return true;
      } else {

        /*
         * The message property of MyData is optional, so if it is null
         * then return false to prevent it from being indexed.  Note
         * that if a required key is missing or an error occurs, an
         * exception should be thrown by this method.
         */
        return false;
      }
    }
Beispiel #4
0
  public boolean delete(Transaction txn, K key) throws DatabaseException {

    DatabaseEntry keyEntry = new DatabaseEntry();
    keyBinding.objectToEntry(key, keyEntry);

    OperationStatus status = db.delete(txn, keyEntry);
    return (status == OperationStatus.SUCCESS);
  }
Beispiel #5
0
  public boolean contains(Transaction txn, K key, LockMode lockMode) throws DatabaseException {

    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry dataEntry = NO_RETURN_ENTRY;
    keyBinding.objectToEntry(key, keyEntry);

    OperationStatus status = db.get(txn, keyEntry, dataEntry, lockMode);
    return (status == OperationStatus.SUCCESS);
  }
 @Override
 public synchronized boolean containsDecision(Long instance) {
   boolean found = false;
   keyBinding.objectToEntry(instance, key);
   OperationStatus status = db.get(null, key, data, LockMode.DEFAULT);
   if (status == OperationStatus.SUCCESS) {
     found = true;
   }
   if (logger.isDebugEnabled()) {
     logger.debug("DB contains " + instance + " " + found + " (" + status.name() + ")");
   }
   return found;
 }
 @Override
 public synchronized Decision getDecision(Long instance) {
   keyBinding.objectToEntry(instance, key);
   Decision decision = null;
   OperationStatus status = db.get(null, key, data, LockMode.DEFAULT);
   if (status == OperationStatus.SUCCESS) {
     decision = dataBinding.entryToObject(data);
   }
   if (logger.isDebugEnabled()) {
     logger.debug("DB get " + decision + " " + status.name());
   }
   return decision;
 }
 @Override
 public synchronized int getBallot(Long instance) {
   keyBinding.objectToEntry(instance, key);
   Integer ballot = null;
   OperationStatus status = db.get(null, key, ballot_data, LockMode.DEFAULT);
   if (status == OperationStatus.SUCCESS) {
     ballot = ballotBinding.entryToObject(ballot_data);
   }
   if (logger.isDebugEnabled()) {
     logger.debug("DB get ballot " + ballot + " for instance " + instance + " " + status.name());
   }
   return ballot.intValue();
 }
Beispiel #9
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();
        }
      }
    }
  }
Beispiel #10
0
  private <V> EntityCursor<V> cursor(
      Transaction txn,
      K fromKey,
      boolean fromInclusive,
      K toKey,
      boolean toInclusive,
      ValueAdapter<V> adapter,
      CursorConfig config)
      throws DatabaseException {

    DatabaseEntry fromEntry = null;
    if (fromKey != null) {
      fromEntry = new DatabaseEntry();
      keyBinding.objectToEntry(fromKey, fromEntry);
    }
    DatabaseEntry toEntry = null;
    if (toKey != null) {
      toEntry = new DatabaseEntry();
      keyBinding.objectToEntry(toKey, toEntry);
    }
    KeyRange range = emptyRange.subRange(fromEntry, fromInclusive, toEntry, toInclusive);
    return cursor(txn, range, adapter, config);
  }
  /**
   * Put the given CrawlURI in at the appropriate place.
   *
   * @param curi
   * @throws DatabaseException
   */
  public void put(CrawlURI curi, boolean overwriteIfPresent) throws DatabaseException {
    DatabaseEntry insertKey = (DatabaseEntry) curi.getHolderKey();
    if (insertKey == null) {
      insertKey = calculateInsertKey(curi);
      curi.setHolderKey(insertKey);
    }
    DatabaseEntry value = new DatabaseEntry();
    crawlUriBinding.objectToEntry(curi, value);
    // Output tally on avg. size if level is FINE or greater.
    if (LOGGER.isLoggable(Level.FINE)) {
      tallyAverageEntrySize(curi, value);
    }
    OperationStatus status;
    if (overwriteIfPresent) {
      status = pendingUrisDB.put(null, insertKey, value);
    } else {
      status = pendingUrisDB.putNoOverwrite(null, insertKey, value);
    }

    if (status != OperationStatus.SUCCESS) {
      LOGGER.log(
          Level.SEVERE, "URI enqueueing failed; " + status + " " + curi, new RuntimeException());
    }
  }
  /** Insert or retrieve data */
  public void run() throws DatabaseException {
    /* Create a new, transactional database environment */
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setTransactional(true);
    envConfig.setAllowCreate(true);
    Environment exampleEnv = new Environment(envDir, envConfig);

    /* Make a database within that environment */
    Transaction txn = exampleEnv.beginTransaction(null, null);
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    dbConfig.setSortedDuplicates(true);
    Database exampleDb = exampleEnv.openDatabase(txn, "bindingsDb", dbConfig);

    /*
     * In our example, the database record is composed of an integer
     * key and and instance of the MyData class as data.
     *
     * A class catalog database is needed for storing class descriptions
     * for the serial binding used below.  This avoids storing class
     * descriptions redundantly in each record.
     */
    DatabaseConfig catalogConfig = new DatabaseConfig();
    catalogConfig.setTransactional(true);
    catalogConfig.setAllowCreate(true);
    Database catalogDb = exampleEnv.openDatabase(txn, "catalogDb", catalogConfig);
    StoredClassCatalog catalog = new StoredClassCatalog(catalogDb);

    /*
     * Create a serial binding for MyData data objects.  Serial bindings
     * can be used to store any Serializable object.
     */
    EntryBinding<MyData> dataBinding = new SerialBinding<MyData>(catalog, MyData.class);

    txn.commit();

    /*
     * Further below we'll use a tuple binding (IntegerBinding
     * specifically) for integer keys.  Tuples, unlike serialized Java
     * objects, have a well defined sort order.
     */

    /* DatabaseEntry represents the key and data of each record */
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry dataEntry = new DatabaseEntry();

    if (doInsert) {

      /* put some data in */
      for (int i = offset; i < numRecords + offset; i++) {

        StringBuilder stars = new StringBuilder();
        for (int j = 0; j < i; j++) {
          stars.append('*');
        }
        MyData data = new MyData(i, stars.toString());

        IntegerBinding.intToEntry(i, keyEntry);
        dataBinding.objectToEntry(data, dataEntry);

        txn = exampleEnv.beginTransaction(null, null);
        OperationStatus status = exampleDb.put(txn, keyEntry, dataEntry);

        /*
         * Note that put will throw a DatabaseException when
         * error conditions are found such as deadlock.
         * However, the status return conveys a variety of
         * information. For example, the put might succeed,
         * or it might not succeed if the record exists
         * and duplicates were not
         */
        if (status != OperationStatus.SUCCESS) {
          throw new RuntimeException("Data insertion got status " + status);
        }
        txn.commit();
      }
    } else {

      /* retrieve the data */
      Cursor cursor = exampleDb.openCursor(null, null);

      while (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {

        int key = IntegerBinding.entryToInt(keyEntry);
        MyData data = dataBinding.entryToObject(dataEntry);

        System.out.println("key=" + key + " data=" + data);
      }
      cursor.close();
    }

    catalogDb.close();
    exampleDb.close();
    exampleEnv.close();
  }
  /** Insert or retrieve data. */
  public void run() throws DatabaseException {

    /* Create a new, transactional database environment. */
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setTransactional(true);
    envConfig.setAllowCreate(true);
    Environment exampleEnv = new Environment(envDir, envConfig);

    /*
     * Make a database within that environment. Because this will be used
     * as a primary database, it must not allow duplicates. The primary key
     * of a primary database must be unique.
     */
    Transaction txn = exampleEnv.beginTransaction(null, null);
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    Database exampleDb = exampleEnv.openDatabase(txn, "bindingsDb", dbConfig);

    /*
     * In our example, the database record is composed of an integer key
     * and and instance of the MyData class as data.
     *
     * A class catalog database is needed for storing class descriptions
     * for the serial binding used below.  This avoids storing class
     * descriptions redundantly in each record.
     */
    DatabaseConfig catalogConfig = new DatabaseConfig();
    catalogConfig.setTransactional(true);
    catalogConfig.setAllowCreate(true);
    Database catalogDb = exampleEnv.openDatabase(txn, "catalogDb", catalogConfig);
    StoredClassCatalog catalog = new StoredClassCatalog(catalogDb);

    /*
     * Create a serial binding for MyData data objects.  Serial
     * bindings can be used to store any Serializable object.
     */
    EntryBinding<MyData> dataBinding = new SerialBinding<MyData>(catalog, MyData.class);

    /*
     * Further below we'll use a tuple binding (IntegerBinding
     * specifically) for integer keys.  Tuples, unlike serialized
     * Java objects, have a well defined sort order.
     */

    /*
     * Define a String tuple binding for a secondary key.  The
     * secondary key is the msg field of the MyData object.
     */
    EntryBinding<String> secKeyBinding = TupleBinding.getPrimitiveBinding(String.class);

    /*
     * Open a secondary database to allow accessing the primary
     * database by the secondary key value.
     */
    SecondaryConfig secConfig = new SecondaryConfig();
    secConfig.setTransactional(true);
    secConfig.setAllowCreate(true);
    secConfig.setSortedDuplicates(true);
    secConfig.setKeyCreator(new MyKeyCreator(secKeyBinding, dataBinding));
    SecondaryDatabase exampleSecDb =
        exampleEnv.openSecondaryDatabase(txn, "bindingsSecDb", exampleDb, secConfig);
    txn.commit();

    /* DatabaseEntry represents the key and data of each record. */
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry dataEntry = new DatabaseEntry();

    if (doInsert) {

      /*
       * Put some data in.  Note that the primary database is always used
       * to add data.  Adding or changing data in the secondary database
       * is not allowed; however, deleting through the secondary database
       * is allowed.
       */
      for (int i = offset; i < numRecords + offset; i++) {
        txn = exampleEnv.beginTransaction(null, null);
        StringBuffer stars = new StringBuffer();
        for (int j = 0; j < i; j++) {
          stars.append('*');
        }
        MyData data = new MyData(i, stars.toString());

        IntegerBinding.intToEntry(i, keyEntry);
        dataBinding.objectToEntry(data, dataEntry);

        OperationStatus status = exampleDb.put(txn, keyEntry, dataEntry);

        /*
         * Note that put will throw a DatabaseException when error
         * conditions are found such as deadlock.  However, the status
         * return conveys a variety of information. For example, the
         * put might succeed, or it might not succeed if the record
         * exists and duplicates were not
         */
        if (status != OperationStatus.SUCCESS) {
          throw new RuntimeException("Data insertion got status " + status);
        }
        txn.commit();
      }
    } else {

      /*
       * Retrieve the data by secondary key by opening a cursor on the
       * secondary database.  The key parameter for a secondary cursor is
       * always the secondary key, but the data parameter is always the
       * data of the primary database.  You can cast the cursor to a
       * SecondaryCursor and use additional method signatures for
       * retrieving the primary key also.  Or you can call
       * openSecondaryCursor() to avoid casting.
       */
      txn = exampleEnv.beginTransaction(null, null);
      Cursor cursor = exampleSecDb.openCursor(txn, null);

      while (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {

        String key = secKeyBinding.entryToObject(keyEntry);
        MyData data = dataBinding.entryToObject(dataEntry);

        System.out.println("key=" + key + " data=" + data);
      }
      cursor.close();
      txn.commit();
    }

    /*
     * Always close secondary databases before closing their associated
     * primary database.
     */
    catalogDb.close();
    exampleSecDb.close();
    exampleDb.close();
    exampleEnv.close();
  }