Example #1
0
  public void populateHistory(Database dbp) {
    Histrec hrec = new Histrec();
    hrec.set_amount(10);

    byte[] arr = new byte[4]; // sizeof(int)
    int i;
    DatabaseEntry kdbt = new DatabaseEntry(arr);
    kdbt.setSize(arr.length);
    DatabaseEntry ddbt = new DatabaseEntry(hrec.data);
    ddbt.setSize(hrec.data.length);

    try {
      for (i = 1; i <= history; i++) {
        kdbt.setRecordNumber(i);

        hrec.set_aid(random_id(ACCOUNT));
        hrec.set_bid(random_id(BRANCH));
        hrec.set_tid(random_id(TELLER));

        dbp.append(null, kdbt, ddbt);
      }
    } catch (DatabaseException dbe) {
      errExit(dbe, "Failure initializing history file");
    }
  }
Example #2
0
 static ObjectRecord unmarshalValue(
     com.sleepycat.db.DatabaseEntry e,
     Ice.Communicator communicator,
     Ice.EncodingVersion encoding,
     boolean keepStats) {
   Ice.InputStream is;
   if (e.getDataNIO() != null) {
     is = new Ice.InputStream(communicator, encoding, e.getDataNIO());
   } else {
     is = new Ice.InputStream(communicator, encoding, e.getData());
   }
   is.setSliceValues(false);
   ObjectRecord rec = new ObjectRecord();
   is.startEncapsulation();
   if (keepStats) {
     rec.ice_readMembers(is);
     is.readPendingValues();
   } else {
     Ice.ObjectHolder holder = new Ice.ObjectHolder();
     is.readValue(holder);
     is.readPendingValues();
     rec.servant = holder.value;
   }
   is.endEncapsulation();
   return rec;
 }
  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();
  }
Example #4
0
  /**
   * Instantiates the query for rscore
   *
   * @param query contains user query
   * @param cmp contains compare structure
   * @param resultIndices 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 queryRScore(String query, ArrayList<Integer> resultIndices, COMPARE cmp)
      throws FileNotFoundException, DatabaseException {
    if (cmp == COMPARE.EQUAL) {
      query = query.replace("rscore=", "").toLowerCase();
      queryDB(query, rscoreIndex, resultIndices);
      return;
    } else if (cmp == COMPARE.LESS) {
      query = query.replace("rscore<", "");
    } else {
      query = query.replace("rscore>", "");
    }

    OperationStatus oprStatus;
    Database std_db = new Database(rscoreIndex, null, null);
    Cursor std_cursor = std_db.openCursor(null, null); // Create new cursor object

    if (cmp == COMPARE.LESS) {
      for (int n = 0; n < Integer.parseInt(query); n++) {
        String searchkey = new String();
        searchkey = n + ".0";
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
        oprStatus = std_cursor.getFirst(key, data, LockMode.DEFAULT);
        key.setData(searchkey.getBytes());
        key.setSize(searchkey.length());
        data = new DatabaseEntry();
        oprStatus = std_cursor.getSearchKey(key, data, LockMode.DEFAULT);
        while (oprStatus == OperationStatus.SUCCESS) {
          String s = new String(data.getData());
          if (!(resultIndices.contains(Integer.parseInt(s)))) {
            resultIndices.add(Integer.parseInt(s));
          }
          oprStatus = std_cursor.getNextDup(key, data, LockMode.DEFAULT);
        }
      }

    } else {
      for (int n = 5; n > Integer.parseInt(query); n--) {
        String searchkey = new String();
        searchkey = n + ".0";
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
        oprStatus = std_cursor.getFirst(key, data, LockMode.DEFAULT);
        key.setData(searchkey.getBytes());
        key.setSize(searchkey.length());
        data = new DatabaseEntry();
        oprStatus = std_cursor.getSearchKey(key, data, LockMode.DEFAULT);
        while (oprStatus == OperationStatus.SUCCESS) {
          String s = new String(data.getData());
          if (!(resultIndices.contains(Integer.parseInt(s)))) {
            resultIndices.add(Integer.parseInt(s));
          }
          oprStatus = std_cursor.getNextDup(key, data, LockMode.DEFAULT);
        }
      }
    }
  }
Example #5
0
  //
  // A DatabaseEntry object won't always have a ByteBuffer. In fact, the entry only
  // keeps a ByteBuffer if it's a direct buffer, otherwise the entry keeps a reference
  // to the buffer's backing array and discards the buffer.
  //
  public static ByteBuffer getBuffer(com.sleepycat.db.DatabaseEntry entry) {
    ByteBuffer b = entry.getDataNIO();
    if (b == null) {
      byte[] arr = entry.getData();
      if (arr != null) {
        b = ByteBuffer.wrap(arr, entry.getOffset(), entry.getSize());
      }
    }

    return b;
  }
Example #6
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);
      }
    }
  }
Example #7
0
 static Ice.Identity unmarshalKey(
     com.sleepycat.db.DatabaseEntry e,
     Ice.Communicator communicator,
     Ice.EncodingVersion encoding) {
   Ice.InputStream is;
   if (e.getDataNIO() != null) {
     is = new Ice.InputStream(communicator, encoding, e.getDataNIO());
   } else {
     is = new Ice.InputStream(communicator, encoding, e.getData());
   }
   Ice.Identity key = new Ice.Identity();
   key.ice_readMembers(is);
   return key;
 }
Example #8
0
  public void populateTable(Database dbp, int start_id, int balance, int nrecs, String msg) {
    Defrec drec = new Defrec();

    DatabaseEntry kdbt = new DatabaseEntry(drec.data);
    kdbt.setSize(4); // sizeof(int)
    DatabaseEntry ddbt = new DatabaseEntry(drec.data);
    ddbt.setSize(drec.data.length); // uses whole array

    try {
      for (int i = 0; i < nrecs; i++) {
        kdbt.setRecordNumber(start_id + (int) i);
        drec.set_balance(balance);
        dbp.putNoOverwrite(null, kdbt, ddbt);
      }
    } catch (DatabaseException dbe) {
      System.err.println("Failure initializing " + msg + " file: " + dbe.toString());
      System.exit(1);
    }
  }
  /* used in freetext index stuff since the row data is not just a pkey. it also has a
   * pos index after it e.g. 42-pkey:pos
   */
  public void moveWithPartialData(DatabaseEntry newkey, DatabaseEntry partial_data)
      throws DatabaseException {
    key = newkey;
    data = partial_data;
    DatabaseEntry original_data = IteratorUtil.cloneDatabaseEntry(partial_data);
    int s = original_data.getSize();

    last_opstat = index_cursor.getSearchBothRange(key, data, LockMode.DEFAULT);

    if (IteratorUtil.compareDatabaseEntries(original_data, 0, s, data, 0, s) != 0)
      last_opstat = OperationStatus.NOTFOUND;

    //	if(isValid())
    //		System.out.println("\t\tMOVE OK "+new String(key.getData())+" |
    // "+LongBinding.entryToLong(newdata));
    //	else
    //		System.out.println("\t\tFAILED MOVE "+new String(key.getData())+" |
    // "+LongBinding.entryToLong(newdata));
  }
 protected T back() {
   if (HGUtils.eq(key.getData(), initialKey.getData())) return null;
   try {
     OperationStatus status = cursor.cursor().getPrev(key, data, LockMode.DEFAULT);
     if (status == OperationStatus.SUCCESS)
       return converter.fromByteArray(data.getData(), data.getOffset(), data.getSize());
     else return null;
   } catch (Throwable t) {
     closeNoException();
     throw new HGException(t);
   }
 }
Example #11
0
  public boolean createSecondaryKey(
      com.sleepycat.db.SecondaryDatabase secondary,
      com.sleepycat.db.DatabaseEntry key,
      com.sleepycat.db.DatabaseEntry value,
      com.sleepycat.db.DatabaseEntry result)
      throws com.sleepycat.db.DatabaseException {
    Ice.Communicator communicator = _store.communicator();
    Ice.EncodingVersion encoding = _store.encoding();
    ObjectRecord rec =
        ObjectStore.unmarshalValue(value, communicator, encoding, _store.keepStats());

    byte[] secondaryKey = marshalKey(rec.servant);
    if (secondaryKey != null) {
      result.setData(secondaryKey);
      result.setSize(secondaryKey.length);
      return true;
    } else {
      //
      // Don't want to index this one
      //
      return false;
    }
  }
Example #12
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();
  }
  // Abstract method that we must implement
  public boolean createSecondaryKey(
      SecondaryDatabase secDb,
      DatabaseEntry keyEntry, // From the primary
      DatabaseEntry dataEntry, // From the primary
      DatabaseEntry resultEntry) // set the key data on this.
      throws DatabaseException {

    if (dataEntry != null) {
      // Convert dataEntry to an Inventory object
      Inventory inventoryItem = (Inventory) theBinding.entryToObject(dataEntry);
      // Get the item name and use that as the key
      String theItem = inventoryItem.getItemName();
      resultEntry.setData(theItem.getBytes());
    }
    return true;
  }
Example #14
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();
  }
Example #15
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 #16
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 #17
0
  /** Increments the record number key at the given slot. */
  private void bumpRecordNumber(int i) {

    DatabaseEntry entry = new DatabaseEntry(keys[i]);
    DbCompat.setRecordNumber(entry, DbCompat.getRecordNumber(entry) + 1);
    keys[i] = entry.getData();
  }
Example #18
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);
      }
    }
 public KeyRangeForwardResultSet(
     BDBTxCursor cursor, DatabaseEntry key, ByteArrayConverter<T> converter) {
   super(cursor, key, converter);
   initialKey = new DatabaseEntry();
   assignData(initialKey, key.getData());
 }
Example #20
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("+=-=-=-=-=-=-=-=-=-=-=-=-=+");
  }
 public void clearEntries(DatabaseEntry key, DatabaseEntry pkey, DatabaseEntry data) {
   key.setData(null);
   pkey.setData(null);
 }
Example #22
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 #23
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();
    }
  }
Example #24
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();
    }
  }
Example #25
0
 /**
  * Utility method for use by bindings to translate a record number integer to a entry buffer.
  *
  * @param recordNumber the record number.
  * @param entry the entry buffer to hold the record number.
  */
 public static void recordNumberToEntry(long recordNumber, DatabaseEntry entry) {
   entry.setData(new byte[4], 0, 4);
   DbCompat.setRecordNumber(entry, (int) recordNumber);
 }
Example #26
0
  public static String rank(Database sqSumdb) throws DatabaseException {

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

    // initialize the ids and distances to be replaced by the loop below
    String firstIDs = "";
    String secondIDs = "";
    String thirdIDs = "";
    double inf = Integer.MAX_VALUE;
    double firstDist = inf;
    double secondDist = inf;
    double thirdDist = inf;
    // initialize containers for rating data for each song before distance calculation
    boolean firstFilled = true;
    boolean secondFilled = true;
    boolean thirdFilled = true;
    while (sqSumCurs.getNext(sqSumKey, sqSumData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
      // get song id
      String id = (new String(sqSumKey.getData()));
      String[] sumN = (new String(sqSumData.getData())).split(",");
      sqSumKey.setData(null);
      sqSumData.setData(null);
      double sum = Integer.parseInt(sumN[0]);
      double N = Integer.parseInt(sumN[1]);
      double dist = Math.sqrt(sum) / N;

      if (dist != inf) {
        if (dist < firstDist || !firstFilled) {
          // set thirdID and thirdDist
          thirdIDs = secondIDs;
          thirdDist = secondDist;
          // set secondID and secondDist
          secondIDs = firstIDs;
          secondDist = firstDist;
          // set firstID and firstDist
          firstIDs = id;
          firstDist = dist;
          if (firstFilled) {
            if (secondFilled) {
              thirdFilled = true;
            }
            secondFilled = true;
          }
          firstFilled = true;
        } else if ((dist < secondDist || !secondFilled) && dist != firstDist) {
          // set thirdID and thirdDist
          thirdIDs = secondIDs;
          thirdDist = secondDist;
          // set secondID and secondDist
          secondIDs = id;
          secondDist = dist;
          if (secondFilled) {
            thirdFilled = true;
          }
          secondFilled = true;
        } else if ((dist < thirdDist || !thirdFilled) && dist != firstDist && dist != secondDist) {
          // set thirdID and thirdDist
          thirdIDs = id;
          thirdDist = dist;
          thirdFilled = true;
        } else if (dist == firstDist) {
          // append fistID
          firstIDs += ", " + id;
        } else if (dist == secondDist) {
          // append secondID
          secondIDs += ", " + id;
        } else if (dist == thirdDist) {
          // append thirdID
          thirdIDs += ", " + id;
        }
      }
    }
    if (firstIDs.equals("")) firstIDs = "<no 1st place>";
    if (secondIDs.equals("")) secondIDs = "<no 2nd place>";
    if (thirdIDs.equals("")) thirdIDs = "<no 3rd place>";

    return firstIDs + ", " + secondIDs + ", " + thirdIDs;
  }
Example #27
0
  @Test
  public void testDraining() throws Exception {
    EnvironmentConfig masterConfig = makeBasicConfig();
    masterConfig.setReplicationLimit(100000000);
    ReplicationManagerSiteConfig site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLocalSite(true);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);

    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLegacy(true);
    masterConfig.addReplicationManagerSite(site);

    Environment master = new Environment(mkdir("master"), masterConfig);
    setTimeouts(master);
    // Prevent connection retries, so that all connections
    // originate from clients
    master.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY, Integer.MAX_VALUE);
    master.replicationManagerStart(2, ReplicationManagerStartPolicy.REP_MASTER);

    DatabaseConfig dc = new DatabaseConfig();
    dc.setTransactional(true);
    dc.setAllowCreate(true);
    dc.setType(DatabaseType.BTREE);
    dc.setPageSize(4096);
    Database db = master.openDatabase(null, "test.db", null, dc);

    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    value.setData(data);

    for (int i = 0; ((BtreeStats) db.getStats(null, null)).getPageCount() < 500; i++) {
      String k = "The record number is: " + i;
      key.setData(k.getBytes());
      db.put(null, key, value);
    }

    // tell fiddler to stop reading once it sees a PAGE message
    Socket s = new Socket("localhost", mgrPort);
    OutputStreamWriter w = new OutputStreamWriter(s.getOutputStream());

    String path1 = "{" + masterPort + "," + clientPort + "}"; // looks like {6000,6001}
    w.write("{init," + path1 + ",page_clog}\r\n");
    w.flush();
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    br.readLine();
    assertEquals("ok", br.readLine());
    // create client
    //
    EnvironmentConfig ec = makeBasicConfig();
    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLocalSite(true);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    Environment client = new Environment(mkdir("client"), ec);
    setTimeouts(client);
    client.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);

    // wait til it gets stuck
    Thread.sleep(5000); // FIXME

    // Do the same for another client, because the master has 2
    // msg processing threads.  (It's no longer possible to
    // configure just 1.)
    String path2 = "{" + masterPort + "," + client2Port + "}";
    w.write("{init," + path2 + ",page_clog}\r\n");
    w.flush();
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    br.readLine();
    assertEquals("ok", br.readLine());

    ec = makeBasicConfig();
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLocalSite(true);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    Environment client2 = new Environment(mkdir("client2"), ec);
    setTimeouts(client2);
    client2.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);

    // wait til it gets stuck
    Thread.sleep(5000);

    // With the connection stuck, the master cannot write out log
    // records for new "live" transactions.  Knowing we didn't
    // write the record, we should not bother waiting for an ack
    // that cannot possibly arrive; so we should simply return
    // quickly.  The duration should be very quick, but anything
    // less than the ack timeout indicates correct behavior (in
    // case this test runs on a slow, overloaded system).
    //
    long startTime = System.currentTimeMillis();
    key.setData("one extra record".getBytes());
    db.put(null, key, value);
    long duration = System.currentTimeMillis() - startTime;
    assertTrue("txn duration: " + duration, duration < 29000);
    System.out.println("txn duration: " + duration);
    db.close();

    // Tell fiddler to close the connections.  That should trigger
    // us to abandon the timeout.  Then create another client and
    // see that it can complete its internal init quickly.  Since
    // we have limited threads at the master, this demonstrates
    // that they were abandoned.
    //
    path1 = "{" + clientPort + "," + masterPort + "}"; // looks like {6001,6000}
    w.write("{" + path1 + ",shutdown}\r\n");
    w.flush();
    assertEquals("ok", br.readLine());
    path2 = "{" + client2Port + "," + masterPort + "}"; // looks like {6001,6000}
    w.write("{" + path2 + ",shutdown}\r\n");
    w.flush();
    assertEquals("ok", br.readLine());

    ec = makeBasicConfig();
    site = new ReplicationManagerSiteConfig("localhost", client3Port);
    site.setLocalSite(true);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", masterPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", clientPort);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);
    site = new ReplicationManagerSiteConfig("localhost", client2Port);
    site.setLegacy(true);
    ec.addReplicationManagerSite(site);

    EventHandler clientMonitor = new EventHandler();
    ec.setEventHandler(clientMonitor);
    Environment client3 = new Environment(mkdir("client3"), ec);
    setTimeouts(client3);
    startTime = System.currentTimeMillis();
    client3.replicationManagerStart(2, ReplicationManagerStartPolicy.REP_CLIENT);
    clientMonitor.await();
    duration = System.currentTimeMillis() - startTime;
    assertTrue("sync duration: " + duration, duration < 20000); // 20 seconds should be plenty

    client3.close();
    master.close();

    w.write("shutdown\r\n");
    w.flush();
    assertEquals("ok", br.readLine());
    s.close();
  }