Пример #1
0
  /**
   * Parses the query and splits it into three query stacks for correct processing order.
   *
   * @param query used to parse.
   */
  private void loadSubqueryPriorityQ(Query query) {
    if (!query.isValid()) return; // TODO: throw an exception.

    for (String subquery : query.getQuery().split(" ")) {
      if (subquery.matches("(?i:r:.*)") || subquery.matches("(?i:p:.*)")) {
        highpriorities.push(subquery);
      } else if (subquery.matches("(?i:pprice.*)") || subquery.matches("(?i:rdate.*)")) {
        lowpriorities.push(subquery);
      } else if (subquery.matches("(?i:rscore.*)")) {
        rscorepriorities.push(subquery);
      } else highpriorities.push(subquery);
    }
  }
Пример #2
0
  /**
   * Instantiates the processing hierarchy for rscore priorities
   *
   * @param has_high_priority depending on whether or not a high priority condition was prev.
   *     entered.
   * @throws DatabaseException when BerkeleyDB passes errors
   * @throws FileNotFoundException when respective .idx or .txt value not found
   */
  private void processRScorePriority(boolean has_high_priority)
      throws FileNotFoundException, DatabaseException {
    boolean first = true;
    while (!rscorepriorities.isEmpty()) {
      ArrayList<Integer> tempKeys = new ArrayList<Integer>();
      String subquery = rscorepriorities.pop();
      COMPARE cmp = COMPARE.EQUAL;
      if (subquery.matches("rscore<.*")) cmp = COMPARE.LESS;
      else if (subquery.matches("rscore=.*")) cmp = COMPARE.EQUAL;
      else if (subquery.matches("rscore>.*")) cmp = COMPARE.GREATER;

      queryRScore(subquery, tempKeys, cmp);

      if (!has_high_priority && first) {
        indices = tempKeys;
        first = false;
      } else {

        Iterator<Integer> iter = indices.iterator();
        while (iter.hasNext()) if (!tempKeys.contains(iter.next())) iter.remove();
      }
    }
  }
Пример #3
0
  /**
   * Instantiates the processing hierarchy for high priorities
   *
   * @throws DatabaseException when BerkeleyDB passes errors
   * @throws FileNotFoundException when respective .idx or .txt value not found
   * @return conditional depending on whether the stack is empty at startup
   */
  private boolean processHighPriorities() throws DatabaseException, FileNotFoundException {
    if (highpriorities.isEmpty()) return false;

    for (int i = 0; !highpriorities.isEmpty(); i++) {
      String subquery = highpriorities.pop();
      if (subquery.matches(".*%")) {
        subquery = subquery.replace("%", "");
        IndexGen shell = new IndexGen();
        ArrayList<Integer> next_result_indices = new ArrayList<Integer>();

        String check_file = "";

        if (subquery.matches("r:[^%]*")) {
          check_file += "'rterms.txt'";
          subquery = subquery.replace("r:", "");
        } else if (subquery.matches("p:[^%]*")) {
          check_file += "'pterms.txt'";
          subquery = subquery.replace("p:", "");
        } else {
          check_file += "'rterms.txt' 'pterms.txt'";
        }
        for (String match :
            shell
                .executeCommand(
                    "grep -h \"\\b"
                        + subquery.toLowerCase().replace("%", "")
                        + "[[:alpha:]]*\" "
                        + check_file
                        + " | sort -t, -k 2,2n -u | grep -o \"\\b"
                        + subquery.toLowerCase().replace("%", "")
                        + "[[:alpha:]]*\" | sort | uniq")
                .split("\n")) {
          queryPTerms(match, next_result_indices);
          queryRTerms(match, next_result_indices);
        }

        if (i == 0) indices = next_result_indices;
        else {
          Iterator<Integer> iter = indices.iterator();
          while (iter.hasNext()) if (!next_result_indices.contains(iter.next())) iter.remove();
        }

      } else if (subquery.matches("r:[^%]*")) {
        ArrayList<Integer> next_result_indices = new ArrayList<Integer>();
        queryRTerms(subquery, next_result_indices);
        if (i == 0) indices = next_result_indices;
        else {
          Iterator<Integer> iter = indices.iterator();
          while (iter.hasNext()) if (!next_result_indices.contains(iter.next())) iter.remove();
        }

      } else if (subquery.matches("p:[^%]*")) {
        ArrayList<Integer> next_result_indices = new ArrayList<Integer>();
        queryPTerms(subquery, next_result_indices);
        if (i == 0) indices = next_result_indices;
        else {
          Iterator<Integer> iter = indices.iterator();
          while (iter.hasNext()) if (!next_result_indices.contains(iter.next())) iter.remove();
        }
      } else {
        ArrayList<Integer> next_result_indices = new ArrayList<Integer>();
        queryPTerms(subquery, next_result_indices);
        queryRTerms(subquery, next_result_indices);
        if (i == 0) indices = next_result_indices;
        else {
          Iterator<Integer> iter = indices.iterator();
          while (iter.hasNext()) if (!next_result_indices.contains(iter.next())) iter.remove();
        }
      }
    }
    return true;
  }
Пример #4
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("+=-=-=-=-=-=-=-=-=-=-=-=-=+");
  }