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