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(); }
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 }
/* * 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(); }
public List<WebURL> get(int max) throws DatabaseException { synchronized (mutex) { int matches = 0; List<WebURL> results = new ArrayList<WebURL>(max); Cursor cursor = null; OperationStatus result; DatabaseEntry key = new DatabaseEntry(); DatabaseEntry value = new DatabaseEntry(); Transaction txn; if (resumable) { txn = env.beginTransaction(null, null); } else { txn = null; } try { cursor = urlsDB.openCursor(txn, null); result = cursor.getFirst(key, value, null); while (matches < max && result == OperationStatus.SUCCESS) { if (value.getData().length > 0) { results.add(webURLBinding.entryToObject(value)); matches++; } result = cursor.getNext(key, value, null); } } catch (DatabaseException e) { if (txn != null) { txn.abort(); txn = null; } throw e; } finally { if (cursor != null) { cursor.close(); } if (txn != null) { txn.commit(); } } return results; } }
public void delete(int count) throws DatabaseException { synchronized (mutex) { int matches = 0; Cursor cursor = null; OperationStatus result; DatabaseEntry key = new DatabaseEntry(); DatabaseEntry value = new DatabaseEntry(); Transaction txn; if (resumable) { txn = env.beginTransaction(null, null); } else { txn = null; } try { cursor = urlsDB.openCursor(txn, null); result = cursor.getFirst(key, value, null); while (matches < count && result == OperationStatus.SUCCESS) { cursor.delete(); matches++; result = cursor.getNext(key, value, null); } } catch (DatabaseException e) { if (txn != null) { txn.abort(); txn = null; } throw e; } finally { if (cursor != null) { cursor.close(); } if (txn != null) { txn.commit(); } } } }
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; }
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 }