Example #1
0
 protected void finalize() {
   try {
     delete();
   } catch (Exception e) {
     System.err.println("Exception during finalization: " + e);
     e.printStackTrace(System.err);
   }
 }
Example #2
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 #3
0
 public static void errExit(Exception err, String s) {
   System.err.print(progname + ": ");
   if (s != null) {
     System.err.print(s + ": ");
   }
   System.err.println(err.toString());
   System.exit(1);
 }
Example #4
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 #5
0
  public static void start(Database std, String filename) {

    // build hash tables
    long starttime;
    long performance = 0; // For measuring performance
    Database secDB = null;
    Database tDB = null;
    try {
      starttime = System.currentTimeMillis();
      System.out.print("Building <user>,<songs,ratings> secondary table...");
      secDB = buildsecondary(std);
      performance = System.currentTimeMillis() - starttime;
      System.out.println(" Complete! " + performance + "ms");

    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    try {
      starttime = System.currentTimeMillis();
      System.out.print("Building <song>,<users> tertiary table...");
      tDB = buildtertiary(std);
      performance = System.currentTimeMillis() - starttime;
      System.out.println(" Complete! " + performance + "ms");

    } catch (Exception e) {
      System.out.println("Error building secondary tables.");
      System.out.println(e.getMessage());
    }

    // QUERIES //
    // use hash tables to query fast!
    PrintStream writer = io.writer("indexedanswers.txt", false);
    BufferedReader qreader = io.reader(filename);
    String line, send;
    long max = 0;
    long min = Integer.MAX_VALUE;
    long total = 0;
    long count = 0;
    long avg = 0;
    try {

      while ((line = qreader.readLine()) != null) {
        line = line.trim();
        if (line.equals("")) continue;
        if (!line.equals("")) {
          System.out.print("Indexed... ");
          starttime = System.currentTimeMillis();
          send = top3(line, secDB, tDB);
          if (send != null) writer.println(send);
          else System.out.println("Error from ranking search function");
          performance = System.currentTimeMillis() - starttime;
          System.out.println(" Complete! " + performance + "ms");
        }
        if (performance > max) max = performance;
        if (performance < min) min = performance;
        total += performance;
        count += 1;
      }
      avg = total / count;
    } catch (IOException ioe) {
      System.out.println(ioe.getMessage());
    } catch (Exception e) {
      System.out.println("error querying for top 3 songs");
      System.out.println(e.getMessage());
    }

    System.out.println(
        "Total querying time: "
            + Long.toString(total)
            + " ms (average: "
            + Long.toString(avg)
            + " ms/query). Fastest query: "
            + Long.toString(min)
            + " ms.  Slowest query: "
            + Long.toString(max)
            + " ms.");
    writer.close();
  }
Example #6
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);
      }
    }