Beispiel #1
0
  boolean dbHasObject(Ice.Identity ident, TransactionI transaction) {
    com.sleepycat.db.Transaction tx = null;

    if (transaction != null) {
      tx = transaction.dbTxn();
      if (tx == null) {
        throw new DatabaseException(_evictor.errorPrefix() + "inactive transaction");
      }
    }

    com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding);

    //
    // Keep 0 length since we're not interested in the data
    //
    com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry();
    dbValue.setPartial(true);

    for (; ; ) {
      try {
        com.sleepycat.db.OperationStatus err = _db.get(tx, dbKey, dbValue, null);

        if (err == com.sleepycat.db.OperationStatus.SUCCESS) {
          return true;
        } else if (err == com.sleepycat.db.OperationStatus.NOTFOUND) {
          return false;
        } else {
          throw new DatabaseException();
        }
      } catch (com.sleepycat.db.DeadlockException dx) {
        if (_evictor.deadlockWarning()) {
          _communicator
              .getLogger()
              .warning(
                  "Deadlock in Freeze.ObjectStore.dhHasObject while reading "
                      + "Db \""
                      + _evictor.filename()
                      + "/"
                      + _dbName
                      + "\"");
        }

        if (tx != null) {
          throw new DeadlockException(
              _evictor.errorPrefix() + "Db.get: " + dx.getMessage(), transaction, dx);
        }
        //
        // Otherwise try again
        //
      } catch (com.sleepycat.db.DatabaseException dx) {
        throw new DatabaseException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), dx);
      }
    }
  }
Beispiel #2
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();
    }
  }
Beispiel #3
0
  protected Ice.Identity[] untypedFindFirst(byte[] k, int firstN) {
    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 pkey = new com.sleepycat.db.DatabaseEntry();
      com.sleepycat.db.DatabaseEntry value = new com.sleepycat.db.DatabaseEntry();
      //
      // dlen is 0, so we should not retrieve any value
      //
      value.setPartial(true);

      Ice.Communicator communicator = _store.communicator();
      Ice.EncodingVersion encoding = _store.encoding();

      TransactionI transaction = _store.evictor().beforeQuery();
      com.sleepycat.db.Transaction tx = transaction == null ? null : transaction.dbTxn();

      java.util.List<Ice.Identity> identities;

      for (; ; ) {
        com.sleepycat.db.SecondaryCursor dbc = null;
        identities = new java.util.ArrayList<Ice.Identity>();

        try {
          //
          // Move to the first record
          //
          dbc = _db.openSecondaryCursor(tx, null);
          boolean first = true;

          boolean found;

          do {
            com.sleepycat.db.OperationStatus status;
            if (first) {
              status = dbc.getSearchKey(key, pkey, value, null);
            } else {
              status = dbc.getNextDup(key, pkey, value, null);
            }

            found = status == com.sleepycat.db.OperationStatus.SUCCESS;

            if (found) {
              Ice.Identity ident = ObjectStore.unmarshalKey(pkey, communicator, encoding);
              identities.add(ident);
              first = false;
            }
          } while ((firstN <= 0 || identities.size() < firstN) && found);

          break; // for(;;)
        } catch (com.sleepycat.db.DeadlockException dx) {
          if (_store.evictor().deadlockWarning()) {
            communicator
                .getLogger()
                .warning(
                    "Deadlock in Freeze.Index.untypedFindFirst 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
              //
            }
          }
        }
      }

      if (identities.size() != 0) {
        Ice.Identity[] result = new Ice.Identity[identities.size()];
        return identities.toArray(result);
      } else {
        return new Ice.Identity[0];
      }
    } finally {
      deactivateController.unlock();
    }
  }
    //
    // 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);
      }
    }