Пример #1
0
 /**
  * Return the id of the table with a specified name,
  *
  * @throws NoSuchElementException if the table doesn't exist
  */
 public int getTableId(String name) throws NoSuchElementException {
   // some code goes here
   DbFile tableID = tableFile.get(name);
   if (tableID == null) throw new NoSuchElementException();
   // System.out.println(tableID.getId());
   else return tableID.getId();
 }
Пример #2
0
 /**
  * Returns the DbFile that can be used to read the contents of the specified table.
  *
  * @param tableid The id of the table, as specified by the DbFile.getId() function passed to
  *     addTable
  */
 public DbFile getDbFile(int tableid) throws NoSuchElementException {
   // some code goes here
   String fileName = fileId.get(tableid);
   if (fileName.equals(null)) throw new NoSuchElementException();
   DbFile retFile = tableFile.get(fileName);
   if (retFile.equals(null)) throw new NoSuchElementException();
   else return retFile;
 }
Пример #3
0
 /**
  * Returns the tuple descriptor (schema) of the specified table
  *
  * @param tableid The id of the table, as specified by the DbFile.getId() function passed to
  *     addTable
  * @throws NoSuchElementException if the table doesn't exist
  */
 public TupleDesc getTupleDesc(int tableid) throws NoSuchElementException {
   // some code goes here
   String fileName = fileId.get(tableid);
   if (fileName.equals(null)) throw new NoSuchElementException();
   DbFile retTD = tableFile.get(fileName);
   if (retTD.equals(null)) throw new NoSuchElementException();
   // System.out.println(retTD);
   else return retTD.getTupleDesc();
 }
Пример #4
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();
    }
  }
Пример #5
0
 /**
  * Retrieve the specified page with the associated permissions. Will acquire a lock and may block
  * if that lock is held by another transaction.
  *
  * <p>The retrieved page should be looked up in the buffer pool. If it is present, it should be
  * returned. If it is not present, it should be added to the buffer pool and returned. If there is
  * insufficient space in the buffer pool, an page should be evicted and the new page should be
  * added in its place.
  *
  * @param tid the ID of the transaction requesting the page
  * @param pid the ID of the requested page
  * @param perm the requested permissions on the page
  * @throws DbException
  * @throws TransactionAbortedException
  */
 public Page getPage(TransactionId tid, PageId pid, Permissions perm)
     throws DbException, TransactionAbortedException {
   lockManager.acquireLock(tid, pid, perm);
   if (pageIdToPages.containsKey(pid)) {
     return pageIdToPages.get(pid);
   }
   if (currentPages.get() == maxPages) {
     evictPage();
   }
   int tableId = pid.getTableId();
   Catalog catalog = Database.getCatalog();
   DbFile dbFile = catalog.getDatabaseFile(tableId);
   Page page = dbFile.readPage(pid);
   pageIdToPages.put(pid, page);
   currentPages.incrementAndGet();
   return page;
 }
Пример #6
0
 /**
  * Add a new table to the catalog. This table's contents are stored in the specified DbFile.
  *
  * @param file the contents of the table to add; file.getId() is the identfier of this
  *     file/tupledesc param for the calls getTupleDesc and getFile
  * @param name the name of the table -- may be an empty string. May not be null. If a name
  * @param pkeyField the name of the primary key field conflict exists, use the last table to be
  *     added as the table for a given name.
  */
 public void addTable(DbFile file, String name, String pkeyField) throws IllegalArgumentException {
   // some code goes here
   if (file == null || name == null) {
     throw new IllegalArgumentException();
   } else {
     // String[] tableAdd = {name, pkeyField};
     tableFile.put(name, file);
     tablepkey.put(name, pkeyField);
     fileId.put(file.getId(), name);
   }
 }
Пример #7
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);
    }
  }