Example #1
0
 public Tuple next() throws NoSuchElementException, TransactionAbortedException, DbException {
   // some code goes here
   if (i == null) {
     throw new NoSuchElementException("iterator is closed");
   }
   if (!i.hasNext()) {
     throw new NoSuchElementException("end of seq scan");
   }
   return i.next();
 }
Example #2
0
  /**
   * Create a new TableStats object, that keeps track of statistics on each column of a table
   *
   * @param tableid The table over which to compute statistics
   * @param ioCostPerPage The cost per page of IO. This doesn't differentiate between
   *     sequential-scan IO and disk seeks.
   */
  public TableStats(int tableid, int ioCostPerPage) {
    // For this function, we use the DbFile for the table in question,
    // then scan through its tuples and calculate the values that you
    // to build the histograms.

    // TODO: Fill out the rest of the constructor.
    // Feel free to change anything already written, it's only a guideline

    this.ioCostPerPage = ioCostPerPage;
    DbFile file = Database.getCatalog().getDbFile(tableid);
    tupleDesc = file.getTupleDesc();
    numPages = ((HeapFile) file).numPages();
    numTuples = 0;

    int numFields = tupleDesc.numFields();

    // TODO: what goes here?
    statistics = new ArrayList<Object>();

    for (int i = 0; i < numFields; i++) {
      if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) {
        statistics.add(new IntStatistics(NUM_HIST_BINS));
      } else {
        statistics.add(new StringHistogram(NUM_HIST_BINS));
      }
    }

    final DbFileIterator iter = file.iterator(null);
    try {
      iter.open();

      while (iter.hasNext()) {
        Tuple t = iter.next();
        numTuples++;

        // TODO: and here?
        for (int i = 0; i < numFields; i++) {
          if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) {
            ((IntStatistics) statistics.get(i)).addValue(((IntField) t.getField(i)).getValue());
          } else {
            ((StringHistogram) statistics.get(i))
                .addValue(((StringField) t.getField(i)).getValue());
          }
        }
      }
      iter.close();
    } catch (DbException e) {
      e.printStackTrace();
    } catch (TransactionAbortedException e) {
      e.printStackTrace();
    }
  }
Example #3
0
 public boolean hasNext() throws TransactionAbortedException, DbException {
   // some code goes here
   if (fileIt == null) {
     return false;
   }
   return fileIt.hasNext();
 }
Example #4
0
 public void close() {
   // some code goes here
   if (i != null) {
     i.close();
     i_pos = i;
     i = null;
   }
 }
Example #5
0
 public void open() throws DbException, TransactionAbortedException {
   // some code goes here
   if (i_pos != null) {
     i = i_pos;
   } else {
     Catalog gc = Database.getCatalog();
     HeapFile file = (HeapFile) gc.getDbFile(tableid);
     i = file.iterator(tid);
   }
   i.open();
 }
Example #6
0
 public Tuple next() throws NoSuchElementException, TransactionAbortedException, DbException {
   // some code goes here
   if (fileIt == null) {
     throw new NoSuchElementException("No Such Element");
   }
   Tuple temp = fileIt.next();
   if (temp == null) {
     throw new NoSuchElementException("No Next Element");
   } else {
     return temp;
   }
 }
Example #7
0
  @Test
  public void testIteratorClose() throws Exception {
    // make more than 1 page. Previous closed iterator would start fetching
    // from page 1.
    HeapFile twoPageFile = SystemTestUtil.createRandomHeapFile(2, 520, null, null);

    DbFileIterator it = twoPageFile.iterator(tid);
    it.open();
    assertTrue(it.hasNext());
    it.close();
    try {
      it.next();
      fail("expected exception");
    } catch (NoSuchElementException e) {
    }
    // close twice is harmless
    it.close();
  }
Example #8
0
  @Test
  public void testIteratorBasic() throws Exception {
    HeapFile smallFile = SystemTestUtil.createRandomHeapFile(2, 3, null, null);

    DbFileIterator it = smallFile.iterator(tid);
    // Not open yet
    assertFalse(it.hasNext());
    try {
      it.next();
      fail("expected exception");
    } catch (NoSuchElementException e) {
    }

    it.open();
    int count = 0;
    while (it.hasNext()) {
      System.out.println(count);
      assertNotNull(it.next());
      count += 1;
    }
    assertEquals(3, count);
    it.close();
  }
Example #9
0
 public void rewind() throws DbException, NoSuchElementException, TransactionAbortedException {
   dbiter.rewind();
 }
Example #10
0
 public void close() {
   dbiter.close();
 }
Example #11
0
 public Tuple next() throws NoSuchElementException, TransactionAbortedException, DbException {
   return dbiter.next();
 }
Example #12
0
 public boolean hasNext() throws TransactionAbortedException, DbException {
   return dbiter.hasNext();
 }
Example #13
0
 public void open() throws DbException, TransactionAbortedException {
   dbiter.open();
 }
Example #14
0
  public static void main(String[] args) throws Exception {
    if (args.length < 1 || args.length > 5) {
      System.out.println("Invalid number of arguments.\n" + usage);
      return;
    }

    String confDir = Server.DEFAULT_CONF_DIR;
    String outputDir = DEFAULT_OUTPUT_DIR;
    if (args.length >= 3 && args[1].equals("--conf")) {
      confDir = args[2];
      args = ParallelUtility.removeArg(args, 1);
      args = ParallelUtility.removeArg(args, 1);
    }

    if (args.length >= 3 && args[1].equals("--output")) {
      outputDir = args[2];
      args = ParallelUtility.removeArg(args, 1);
      args = ParallelUtility.removeArg(args, 1);
    }

    Catalog c = Database.getCatalog();

    SocketInfo[] workers = ParallelUtility.loadWorkers(confDir);
    c.loadSchema(args[0]);
    TableStats.computeStatistics();

    File catalogFile = new File(args[0]);

    for (SocketInfo worker : workers) {
      File folder = new File(outputDir + "/" + worker.getHost() + "_" + worker.getPort());
      folder.mkdirs();
      ParallelUtility.copyFileFolder(
          catalogFile, new File(folder.getAbsolutePath() + "/catalog.schema"), true);
    }

    TransactionId fateTid = new TransactionId();

    Iterator<Integer> tableIds = c.tableIdIterator();
    while (tableIds.hasNext()) {
      int tableid = tableIds.next();
      int numTuples = getTotalTuples(tableid);
      HeapFile h = (HeapFile) c.getDatabaseFile(tableid);

      int eachSplitSize = numTuples / workers.length;
      int[] splitSizes = new int[workers.length];
      Arrays.fill(splitSizes, eachSplitSize);
      for (int i = 0; i < numTuples % workers.length; i++) {
        splitSizes[i] += 1;
      }

      DbFileIterator dfi = h.iterator(fateTid);
      dfi.open();

      for (int i = 0; i < workers.length; i++) {
        ArrayList<Tuple> buffer = new ArrayList<Tuple>();
        for (int j = 0; j < splitSizes[i]; j++) {
          dfi.hasNext();
          buffer.add(dfi.next());
        }
        Iterator<TDItem> items = h.getTupleDesc().iterator();
        ArrayList<Type> types = new ArrayList<Type>();
        while (items.hasNext()) types.add(items.next().fieldType);

        writeHeapFile(
            buffer,
            new File(
                outputDir
                    + "/"
                    + workers[i].getHost()
                    + "_"
                    + workers[i].getPort()
                    + "/"
                    + c.getTableName(tableid)
                    + ".dat"),
            BufferPool.getPageSize(),
            types.toArray(new Type[] {}));
      }
    }
  }
Example #15
0
 public void rewind() throws DbException, NoSuchElementException, TransactionAbortedException {
   // some code goes here
   fileIt.close();
   fileIt.open();
 }
Example #16
0
 public void rewind() throws DbException, NoSuchElementException, TransactionAbortedException {
   // some code goes here
   i.rewind();
   i_pos = null;
 }
Example #17
0
 public void open() throws DbException, TransactionAbortedException {
   // some code goes here
   fileIt = Database.getCatalog().getDbFile(tableid).iterator(tid);
   fileIt.open();
 }
Example #18
0
  public static void main(String args[])
      throws DbException, TransactionAbortedException, IOException {
    // convert a file
    if (args[0].equals("convert")) {
      try {
        if (args.length == 3) {
          HeapFileEncoder.convert(
              new File(args[1]),
              new File(args[1].replaceAll(".txt", ".dat")),
              BufferPool.PAGE_SIZE,
              Integer.parseInt(args[2]));
        } else if (args.length == 4) {
          ArrayList<Type> ts = new ArrayList<Type>();
          String[] typeStringAr = args[3].split(",");
          for (String s : typeStringAr) {
            if (s.toLowerCase().equals("int")) ts.add(Type.INT_TYPE);
            else if (s.toLowerCase().equals("string")) ts.add(Type.STRING_TYPE);
            else {
              System.out.println("Unknown type " + s);
              return;
            }
          }
          HeapFileEncoder.convert(
              new File(args[1]),
              new File(args[1].replaceAll(".txt", ".dat")),
              BufferPool.PAGE_SIZE,
              Integer.parseInt(args[2]),
              ts.toArray(new Type[0]));

        } else {
          System.out.println("Unexpected number of arguments to convert ");
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else if (args[0].equals("print")) {
      File tableFile = new File(args[1]);
      int columns = Integer.parseInt(args[2]);
      DbFile table = Utility.openHeapFile(columns, tableFile);
      TransactionId tid = new TransactionId();
      DbFileIterator it = table.iterator(tid);

      if (null == it) {
        System.out.println(
            "Error: method HeapFile.iterator(TransactionId tid) not yet implemented!");
      } else {
        it.open();
        while (it.hasNext()) {
          Tuple t = it.next();
          System.out.println(t);
        }
        it.close();
      }
    } else if (args[0].equals("parser")) {
      // Strip the first argument and call the parser
      String[] newargs = new String[args.length - 1];
      for (int i = 1; i < args.length; ++i) {
        newargs[i - 1] = args[i];
      }

      try {
        // dynamically load Parser -- if it doesn't exist, print error message
        Class<?> c = Class.forName("simpledb.Parser");
        Class<?> s = String[].class;

        java.lang.reflect.Method m = c.getMethod("main", s);
        m.invoke(null, (java.lang.Object) newargs);
      } catch (ClassNotFoundException cne) {
        System.out.println(
            "Class Parser not found -- perhaps you are trying to run the parser as a part of lab1?");
      } catch (Exception e) {
        System.out.println("Error in parser.");
        e.printStackTrace();
      }

    } else {
      System.err.println("Unknown command: " + args[0]);
      System.exit(1);
    }
  }
Example #19
0
 public boolean hasNext() throws TransactionAbortedException, DbException {
   // some code goes here
   return i != null && i.hasNext();
 }