/** * Reads the schema from a file and creates the appropriate tables in the database. * * @param catalogFile */ public void loadSchema(String catalogFile) { String line = ""; String baseFolder = new File(catalogFile).getParent(); try { BufferedReader br = new BufferedReader(new FileReader(new File(catalogFile))); while ((line = br.readLine()) != null) { // assume line is of the format name (field type, field type, ...) String name = line.substring(0, line.indexOf("(")).trim(); // System.out.println("TABLE NAME: " + name); String fields = line.substring(line.indexOf("(") + 1, line.indexOf(")")).trim(); String[] els = fields.split(","); ArrayList<String> names = new ArrayList<String>(); ArrayList<Type> types = new ArrayList<Type>(); String primaryKey = ""; for (String e : els) { String[] els2 = e.trim().split(" "); names.add(els2[0].trim()); if (els2[1].trim().toLowerCase().equals("int")) types.add(Type.INT_TYPE); else if (els2[1].trim().toLowerCase().equals("string")) types.add(Type.STRING_TYPE); else { System.out.println("Unknown type " + els2[1]); System.exit(0); } if (els2.length == 3) { if (els2[2].trim().equals("pk")) primaryKey = els2[0].trim(); else { System.out.println("Unknown annotation " + els2[2]); System.exit(0); } } } Type[] typeAr = types.toArray(new Type[0]); String[] namesAr = names.toArray(new String[0]); TupleDesc t = new TupleDesc(typeAr, namesAr); HeapFile tabHf = new HeapFile(new File(baseFolder + "/" + name + ".dat"), t); addTable(tabHf, name, primaryKey); System.out.println("Added table : " + name + " with schema " + t); } } catch (IOException e) { e.printStackTrace(); System.exit(0); } catch (IndexOutOfBoundsException e) { System.out.println("Invalid catalog entry : " + line); System.exit(0); } }
/** * Add a new table to the catalog. This table has tuples formatted using the specified TupleDesc * and its 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 */ public void addTable(DbFile file) { addTable(file, (UUID.randomUUID()).toString()); }
public void addTable(DbFile file, String name) { addTable(file, name, ""); }