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();
 }
Beispiel #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();
    }
  }
Beispiel #3
0
 public boolean hasNext() throws TransactionAbortedException, DbException {
   // some code goes here
   if (fileIt == null) {
     return false;
   }
   return fileIt.hasNext();
 }
 public void close() {
   // some code goes here
   if (i != null) {
     i.close();
     i_pos = i;
     i = null;
   }
 }
 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();
 }
Beispiel #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;
   }
 }
  @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();
  }
  @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();
  }
Beispiel #9
0
 public void rewind() throws DbException, NoSuchElementException, TransactionAbortedException {
   dbiter.rewind();
 }
Beispiel #10
0
 public void close() {
   dbiter.close();
 }
Beispiel #11
0
 public Tuple next() throws NoSuchElementException, TransactionAbortedException, DbException {
   return dbiter.next();
 }
Beispiel #12
0
 public boolean hasNext() throws TransactionAbortedException, DbException {
   return dbiter.hasNext();
 }
Beispiel #13
0
 public void open() throws DbException, TransactionAbortedException {
   dbiter.open();
 }
Beispiel #14
0
 public void rewind() throws DbException, NoSuchElementException, TransactionAbortedException {
   // some code goes here
   fileIt.close();
   fileIt.open();
 }
Beispiel #15
0
 public void rewind() throws DbException, NoSuchElementException, TransactionAbortedException {
   // some code goes here
   i.rewind();
   i_pos = null;
 }
Beispiel #16
0
 public void open() throws DbException, TransactionAbortedException {
   // some code goes here
   fileIt = Database.getCatalog().getDbFile(tableid).iterator(tid);
   fileIt.open();
 }
  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);
    }
  }
Beispiel #18
0
 public boolean hasNext() throws TransactionAbortedException, DbException {
   // some code goes here
   return i != null && i.hasNext();
 }