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"); } }
/** * 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); } } } }
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); } }
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; } }
/** * 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(); }
/** * 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("+=-=-=-=-=-=-=-=-=-=-=-=-=+"); }
// // 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); } }