public static void main(String arr[]) {
    ParseQuery pq = new ParseQuery();
    // DBSystem.readConfig("/tmp/config.txt");
    String configFilePath = arr[0];
    DBSystem.readConfig(configFilePath);
    // String query = "Select distinct * from countries where id=123 and code like 'jkl' group by
    // continent having code like 'antartic' order by name";
    String inputFile = arr[1];
    try {
      BufferedReader br = new BufferedReader(new FileReader(inputFile));
      String line;
      while ((line = br.readLine()) != null) {
        pq.queryType(line);
      }
    } catch (FileNotFoundException e) {
      System.out.println("Input file not found");
      // e.printStackTrace();
    } catch (IOException e) {
      System.out.println("I/O Exception");
      // e.printStackTrace();
    }

    //        /String query = "create table rtyui(name varchar, age int, rollno int)";

  }
  public void createCommand(String query) {

    int isValid = validateAndDisplayCreateCommandParameters(query);
    PrintWriter pow = null;
    if (isValid == 1) {
      // System.out.println(DBSystem.PATH_FOR_DATA);
      try {
        // code to Create the tablename.data and tablename.csv file.
        File fdataFile = new File(DBSystem.PATH_FOR_DATA + "/" + tab_name + ".data");
        new File(DBSystem.PATH_FOR_DATA + "/" + tab_name + ".csv").createNewFile();
        // Add the details in the .data file
        pow = new PrintWriter(new FileWriter(fdataFile));
        pow.write(sb.toString());
        pow.flush();
        // Add the details in the config.txt.
        pow = new PrintWriter(new FileWriter(DBSystem.CONFIG_FILE_PATH, true));
        pow.write("BEGIN\n");
        pow.write(tab_name + "\n");
        String[] ar = sb.toString().split(",");
        for (int j = 0; j < ar.length; j++) {
          String[] at = ar[j].split(":");
          pow.write(at[0]);
          pow.write(",");
          pow.write(at[1] + "\n");
        }
        pow.write("END\n");
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        pow.close();
      }
    } else {
      System.out.println("Query Invalid");
    }
    DBSystem.readConfig(DBSystem.CONFIG_FILE_PATH);
  }
Exemple #3
0
  // Sorts Both the file , individually
  // irrespective of their size.
  public static void initialize(
      String table1, String table1_join_col, String table2, String table2_join_col) {

    // System.out.println(DBSystem.PATH_FOR_DATA);
    // System.out.println(table1);

    tab1 = table1;
    tab2 = table2;

    for (Table t : DBSystem.tableList) {

      if (t.getName().equalsIgnoreCase(table1)) {
        table1JoinColIndex = t.getColumnNum().get(table1_join_col);
        table1JoinColType = t.getColumnData().get(table1_join_col);
        table1Lines = t.getLines();
        t1 = t;
      } else if (t.getName().equalsIgnoreCase(table2)) {
        table2JoinColIndex = t.getColumnNum().get(table2_join_col);
        table2JoinColType = t.getColumnData().get(table2_join_col);
        table2Lines = t.getLines();
        t2 = t;
      }
    }
    // System.out.println(table1 + " - " + table1_join_col + " - " + table1JoinColIndex + " - " +
    // table1JoinColType);
    // System.out.println(table2 + " - " + table2_join_col + " - " + table2JoinColIndex + " - " +
    // table2JoinColType);

    if (!table1JoinColType.equalsIgnoreCase(table2JoinColType)) {
      System.out.println("Both the tables must have similar join types");
      System.exit(1);
    }

    int numberOfcolumnsForJoin = 2; // For Joining Two tables

    // Sort the columns used for join
    for (int k = 0; k < numberOfcolumnsForJoin; k++) {
      String tab = null;
      String tabColType = null;
      int tabColIndex = 0;
      int tLines = 0;

      if (k == 0) {
        // Sorting table 1.
        tab = table1;
        tabColType = table1JoinColType;
        tabColIndex = table1JoinColIndex;
        tLines = table1Lines;
      } else if (k == 1) {
        // Sorting table 2
        tab = table2;
        tabColType = table2JoinColType;
        tabColIndex = table2JoinColIndex;
        tLines = table2Lines;
      }

      Comparator<String> cmp = new SortingComparator(tabColIndex, tabColType);
      PriorityQueue<String> priorityQueue = new PriorityQueue<String>(10, cmp);

      int remainingBufferSize = MAX_MEM_TO_USE;
      int fileCount = 1;
      String line;
      int inOutFlag = 0;

      for (int i = 0; i < tLines; i++) {
        line = DBSystem.getRecord(tab, i);

        // System.out.println(line);
        // String cols [] = line.split(",");

        remainingBufferSize -= line.getBytes().length;
        if (remainingBufferSize >= 0) {
          priorityQueue.add(line);
          inOutFlag = 1;
        } else {
          // write to file
          FileWriter fileWriter = null;
          // System.out.println("Written");
          try {
            // String fileName = setBlockFile(fileCount,table1);
            // System.out.println(fileName);
            fileWriter = new FileWriter(new File(setBlockFile(fileCount, tab)));
            int siz = priorityQueue.size();
            // System.out.println(siz);
            for (int j = 0; j < siz; j++) {
              fileWriter.write(priorityQueue.poll().toString());
              // System.out.println(priorityQueue.poll().toString());
              fileWriter.write("\n");
            }
            fileCount++;
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
            try {
              fileWriter.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          // set remainingSize back to MAX_MEM_TO_USE
          remainingBufferSize = MAX_MEM_TO_USE;
          // current read line
          priorityQueue.add(line);
          remainingBufferSize -= line.getBytes().length;
          inOutFlag = 0;
        }
      }

      if (inOutFlag == 1) {
        FileWriter fw = null;

        try {
          fw = new FileWriter(new File(setBlockFile(fileCount, tab)));
          int psiz = priorityQueue.size();
          for (int j = 0; j < psiz; j++) {
            fw.write(priorityQueue.poll().toString());
            // System.out.println(priorityQueue.poll().toString());
            fw.write("\n");
          }

        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            fw.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }

      if (k == 0) table1_tempfileCount = fileCount;
      if (k == 1) table2_tempfileCount = fileCount;
    }

    // Begin the merging process for both the files.

    // System.out.println(table1_tempfileCount);
    // System.out.println(table2_tempfileCount);

    // Merging Process
    SortedMergeJoin sortedMergeJoin =
        new SortedMergeJoin(
            table1,
            table1_tempfileCount,
            table1JoinColIndex,
            table1JoinColType,
            table2,
            table2_tempfileCount,
            table2JoinColIndex,
            table2JoinColType);
    sortedMergeJoin.execute();
  }