Ejemplo n.º 1
0
  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();
  }
Ejemplo n.º 2
0
  /**
   * Inserts an entity and returns null, or updates it if the primary key already exists and returns
   * the existing entity.
   *
   * <p>If a {@link PrimaryKey#sequence} is used and the primary key field of the given entity is
   * null or zero, this method will assign the next value from the sequence to the primary key field
   * of the given entity.
   *
   * @param txn the transaction used to protect this operation, null to use auto-commit, or null if
   *     the store is non-transactional.
   * @param entity the entity to be inserted or updated.
   * @return the existing entity that was updated, or null if the entity was inserted.
   * @throws DatabaseException the base class for all BDB exceptions.
   */
  public E put(Transaction txn, E entity) throws DatabaseException {

    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry dataEntry = new DatabaseEntry();
    assignKey(entity, keyEntry);

    boolean autoCommit = false;
    Environment env = db.getEnvironment();
    if (transactional && txn == null && DbCompat.getThreadTransaction(env) == null) {
      txn = env.beginTransaction(null, getAutoCommitTransactionConfig());
      autoCommit = true;
    }

    CursorConfig cursorConfig = null;
    if (concurrentDB) {
      cursorConfig = new CursorConfig();
      DbCompat.setWriteCursor(cursorConfig, true);
    }
    boolean failed = true;
    Cursor cursor = db.openCursor(txn, cursorConfig);
    LockMode lockMode = locking ? LockMode.RMW : null;
    try {
      while (true) {
        OperationStatus status = cursor.getSearchKey(keyEntry, dataEntry, lockMode);
        if (status == OperationStatus.SUCCESS) {
          E existing = entityBinding.entryToObject(keyEntry, dataEntry);
          entityBinding.objectToData(entity, dataEntry);
          cursor.put(keyEntry, dataEntry);
          failed = false;
          return existing;
        } else {
          entityBinding.objectToData(entity, dataEntry);
          status = cursor.putNoOverwrite(keyEntry, dataEntry);
          if (status != OperationStatus.KEYEXIST) {
            failed = false;
            return null;
          }
        }
      }
    } finally {
      cursor.close();
      if (autoCommit) {
        if (failed) {
          txn.abort();
        } else {
          txn.commit();
        }
      }
    }
  }
Ejemplo n.º 3
0
  /*
   * void return type since error conditions are propogated
   * via exceptions.
   */
  private void printStocks(Database db) throws DeadlockException, DatabaseException {
    Cursor dbc = db.openCursor(null, null);

    System.out.println("\tSymbol\tPrice");
    System.out.println("\t======\t=====");

    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry();
    OperationStatus ret;
    for (ret = dbc.getFirst(key, data, LockMode.DEFAULT);
        ret == OperationStatus.SUCCESS;
        ret = dbc.getNext(key, data, LockMode.DEFAULT)) {
      String keystr = new String(key.getData(), key.getOffset(), key.getSize());
      String datastr = new String(data.getData(), data.getOffset(), data.getSize());
      System.out.println("\t" + keystr + "\t" + datastr);
    }
    dbc.close();
  }
Ejemplo n.º 4
0
  /**
   * Generic query process
   *
   * @param query contains parsed query for process
   * @param db_name contains database to query
   * @param indices passes the indices that store the result
   * @throws DatabaseException when BerkeleyDB passes errors
   * @throws FileNotFoundException when respective .idx or .txt value not found
   */
  private void queryDB(String query, String db_name, ArrayList<Integer> indices)
      throws DatabaseException, FileNotFoundException {
    OperationStatus oprStatus;
    Database std_db = new Database(db_name, null, null);
    Cursor std_cursor = std_db.openCursor(null, null); // Create new cursor object
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry();

    key.setData(query.getBytes());
    key.setSize(query.length());

    // Returns OperationStatus
    oprStatus = std_cursor.getSearchKey(key, data, LockMode.DEFAULT);
    while (oprStatus == OperationStatus.SUCCESS) {
      String s = new String(data.getData());
      if (!(indices.contains(Integer.parseInt(s)))) {
        indices.add(Integer.parseInt(s));
      }
      oprStatus = std_cursor.getNextDup(key, data, LockMode.DEFAULT);
    }

    std_cursor.close();
    std_db.close();
  }
Ejemplo n.º 5
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
  }
Ejemplo n.º 6
0
  /**
   * Prints the results obtained by BerkeleyDB
   *
   * @throws DatabaseException whenever BerkeleyDB is violateed
   * @throws FileNotFoundException when .idx files not found or .txt files not found.
   * @throws ParseException when ParseDouble returns an error.
   */
  private void printResults() throws DatabaseException, FileNotFoundException, ParseException {

    // System.out.println("Num of indices before pprice rdate constraints: " + indices.size());
    if (indices.isEmpty()) {
      System.out.println("+=-=-=-=-=-=-=-=-=-=-=-=-=+");
      System.out.println("No results matching given query.");
      System.out.println("+=-=-=-=-=-=-=-=-=-=-=-=-=+");
    }

    Integer counter = 0;
    for (Integer index : indices) {

      OperationStatus oprStatus;
      Database std_db = new Database("rw.idx", null, null);
      Cursor std_cursor = std_db.openCursor(null, null); // Create new cursor object
      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();
      Product product = new Product();
      Review review = new Review();

      String searchkey = index.toString().toLowerCase();
      key.setData(searchkey.getBytes());
      key.setSize(searchkey.length());

      // Returns OperationStatus
      oprStatus = std_cursor.getSearchKey(key, data, LockMode.DEFAULT);
      Bill:
      {
        if (oprStatus == OperationStatus.SUCCESS) {
          String s = new String(data.getData());

          load_data(product, review, s);

          /** Filters low priority queue for pprice / rdate processes. */
          GenericStack<String> tmplow = new GenericStack<String>(lowpriorities);
          while (!tmplow.isEmpty()) {
            String subquery = tmplow.pop();

            if (subquery.matches("pprice.*")) {
              Double value =
                  Double.parseDouble(
                      subquery
                          .replace("pprice", "")
                          .replace(">", "")
                          .replace("=", "")
                          .replace("<", ""));

              if (product.getPrice().equals("unknown")) break Bill;
              if (subquery.matches("pprice<.*")
                  && !(Double.parseDouble(product.getPrice()) > value)) continue;
              else if (subquery.matches("pprice=.*")
                  && !(Double.parseDouble(product.getPrice()) == value)) continue;
              else if (subquery.matches("pprice>.*")
                  && !(Double.parseDouble(product.getPrice()) < value)) continue;
              else break Bill;

            } else if (subquery.matches("rdate.*")) {
              String comparator = subquery.substring(5, 6);
              DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
              Date valuedate = df.parse(subquery.substring(6) + " 00:00:00");
              long valuedatedoesntmatataer =
                  (valuedate.getTime() / 1000)
                      - 25200; // delay set by 7hours - timezone difference.
              switch (comparator) {
                case "<":
                  if (!(Long.parseLong(review.getTime()) < valuedatedoesntmatataer)) {
                    break Bill;
                  } else {
                    break;
                  }
                case ">":
                  if (!(Long.parseLong(review.getTime()) > valuedatedoesntmatataer)) {
                    break Bill;
                  } else {
                    break;
                  }
                case "=":
                  if (!(Long.parseLong(review.getTime()) == valuedatedoesntmatataer)) {
                    break Bill;
                  } else {
                    break;
                  }
                default:
                  break Bill;
              }
            }
          }

          // System.out.print(" "+ index +" ");
          product.print();
          review.print();
          counter++;
        }
        std_cursor.close();
        std_db.close();
      }
    }
    System.out.println("+=-=-=-=-=-=-=-=-=-=-=-=-=+");
    System.out.println("Total records found: " + counter);
    System.out.println("+=-=-=-=-=-=-=-=-=-=-=-=-=+");
  }
Ejemplo n.º 7
0
  protected int untypedCount(byte[] k) {
    EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController();
    deactivateController.lock();
    try {
      com.sleepycat.db.DatabaseEntry key = new com.sleepycat.db.DatabaseEntry(k);

      //
      // When we have a custom-comparison function, Berkeley DB returns
      // the key on-disk (when it finds one). We disable this behavior:
      // (ref Oracle SR 5925672.992)
      //
      // In DB > 5.1.x we can not set DB_DBT_PARTIAL in the key Dbt when calling
      // getSearchKey.
      //
      if (com.sleepycat.db.Environment.getVersionMajor() < 5
          || (com.sleepycat.db.Environment.getVersionMajor() == 5
              && com.sleepycat.db.Environment.getVersionMinor() <= 1)) {
        key.setPartial(true);
      }

      com.sleepycat.db.DatabaseEntry value = new com.sleepycat.db.DatabaseEntry();
      //
      // dlen is 0, so we should not retrieve any value
      //
      value.setPartial(true);
      TransactionI transaction = _store.evictor().beforeQuery();
      com.sleepycat.db.Transaction tx = transaction == null ? null : transaction.dbTxn();

      for (; ; ) {
        com.sleepycat.db.Cursor dbc = null;
        try {
          dbc = _db.openCursor(tx, null);
          if (dbc.getSearchKey(key, value, null) == com.sleepycat.db.OperationStatus.SUCCESS) {
            return dbc.count();
          } else {
            return 0;
          }
        } catch (com.sleepycat.db.DeadlockException dx) {
          if (_store.evictor().deadlockWarning()) {
            _store
                .communicator()
                .getLogger()
                .warning(
                    "Deadlock in Freeze.Index.untypedCount while "
                        + "iterating over Db \""
                        + _store.evictor().filename()
                        + "/"
                        + _dbName
                        + "\"");
          }

          if (tx != null) {
            throw new DeadlockException(
                _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), transaction, dx);
          }
          //
          // Otherwise retry
          //
        } catch (com.sleepycat.db.DatabaseException dx) {
          throw new DatabaseException(
              _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), dx);
        } finally {
          if (dbc != null) {
            try {
              dbc.close();
            } catch (com.sleepycat.db.DeadlockException dx) {
              if (tx != null) {
                throw new DeadlockException(
                    _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(),
                    transaction,
                    dx);
              }
            } catch (com.sleepycat.db.DatabaseException dx) {
              //
              // Ignored
              //
            }
          }
        }
      }
    } finally {
      deactivateController.unlock();
    }
  }
Ejemplo n.º 8
0
    //
    // XXX Figure out the appropriate way to pick out IDs.
    //
    int txn() {
      Cursor acurs = null;
      Cursor bcurs = null;
      Cursor hcurs = null;
      Cursor tcurs = null;
      Transaction t = null;

      Defrec rec = new Defrec();
      Histrec hrec = new Histrec();
      int account, branch, teller;

      DatabaseEntry d_dbt = new DatabaseEntry();
      DatabaseEntry d_histdbt = new DatabaseEntry();
      DatabaseEntry k_dbt = new DatabaseEntry();
      DatabaseEntry k_histdbt = new DatabaseEntry();

      account = TpcbExample.this.random_id(TpcbExample.ACCOUNT);
      branch = TpcbExample.this.random_id(TpcbExample.BRANCH);
      teller = TpcbExample.this.random_id(TpcbExample.TELLER);

      // The history key will not actually be retrieved,
      // but it does need to be set to something.
      byte[] hist_key = new byte[4];
      k_histdbt.setData(hist_key);
      k_histdbt.setSize(4 /* == sizeof(int)*/);

      byte[] key_bytes = new byte[4];
      k_dbt.setData(key_bytes);
      k_dbt.setSize(4 /* == sizeof(int)*/);

      d_dbt.setData(rec.data);
      d_dbt.setUserBuffer(rec.length(), true);

      hrec.set_aid(account);
      hrec.set_bid(branch);
      hrec.set_tid(teller);
      hrec.set_amount(10);
      // Request 0 bytes since we're just positioning.
      d_histdbt.setPartial(0, 0, true);

      // START PER-TRANSACTION TIMING.
      //
      // Technically, TPCB requires a limit on response time, you only
      // get to count transactions that complete within 2 seconds.
      // That's not an issue for this sample application -- regardless,
      // here's where the transaction begins.
      try {
        t = dbenv.beginTransaction(null, null);

        acurs = adb.openCursor(t, null);
        bcurs = bdb.openCursor(t, null);
        tcurs = tdb.openCursor(t, null);
        hcurs = hdb.openCursor(t, null);

        // Account record
        k_dbt.setRecordNumber(account);
        if (acurs.getSearchKey(k_dbt, d_dbt, null) != OperationStatus.SUCCESS)
          throw new Exception("acurs get failed");
        rec.set_balance(rec.get_balance() + 10);
        acurs.putCurrent(d_dbt);

        // Branch record
        k_dbt.setRecordNumber(branch);
        if (bcurs.getSearchKey(k_dbt, d_dbt, null) != OperationStatus.SUCCESS)
          throw new Exception("bcurs get failed");
        rec.set_balance(rec.get_balance() + 10);
        bcurs.putCurrent(d_dbt);

        // Teller record
        k_dbt.setRecordNumber(teller);
        if (tcurs.getSearchKey(k_dbt, d_dbt, null) != OperationStatus.SUCCESS)
          throw new Exception("ccurs get failed");
        rec.set_balance(rec.get_balance() + 10);
        tcurs.putCurrent(d_dbt);

        // History record
        d_histdbt.setPartial(0, 0, false);
        d_histdbt.setData(hrec.data);
        d_histdbt.setUserBuffer(hrec.length(), true);
        if (hdb.append(t, k_histdbt, d_histdbt) != OperationStatus.SUCCESS)
          throw new DatabaseException("put failed");

        acurs.close();
        acurs = null;
        bcurs.close();
        bcurs = null;
        tcurs.close();
        tcurs = null;
        hcurs.close();
        hcurs = null;

        // null out t in advance; if the commit fails,
        // we don't want to abort it in the catch clause.
        Transaction tmptxn = t;
        t = null;
        tmptxn.commit();

        // END TIMING
        return (0);
      } catch (Exception e) {
        try {
          if (acurs != null) acurs.close();
          if (bcurs != null) bcurs.close();
          if (tcurs != null) tcurs.close();
          if (hcurs != null) hcurs.close();
          if (t != null) t.abort();
        } catch (DatabaseException dbe) {
          // not much we can do here.
        }

        if (TpcbExample.this.verbose) {
          System.out.println(
              "Transaction A="
                  + String.valueOf(account)
                  + " B="
                  + String.valueOf(branch)
                  + " T="
                  + String.valueOf(teller)
                  + " failed");
          System.out.println("Reason: " + e.toString());
        }
        return (-1);
      }
    }