// REMOVE INDEX ENTRY FROM CATALOG public void removeInfo(String relation, String attrName, IndexType accessType) throws IOException, Catalogmissparam, Catalogattrnotfound, IndexCatalogException { int recSize; RID rid = null; Scan pscan = null; IndexDesc record = null; if ((relation == null) || (attrName == null)) throw new Catalogmissparam(null, "MISSING_PARAM"); // OPEN SCAN try { pscan = new Scan(this); } catch (Exception e) { throw new IndexCatalogException(e, "scan failed"); } // SCAN FILE while (true) { try { tuple = pscan.getNext(rid); if (tuple == null) throw new Catalogattrnotfound(null, "Catalog: Attribute not Found!"); read_tuple(tuple, record); } catch (Exception e4) { throw new IndexCatalogException(e4, "read_tuple failed"); } if (record.relName.equalsIgnoreCase(relation) == true && record.attrName.equalsIgnoreCase(attrName) == true && (record.accessType == accessType)) { try { deleteRecord(rid); // FOUND - DELETE } catch (Exception e) { throw new IndexCatalogException(e, "deleteRecord() failed"); } break; } } return; };
// RETURN INFO ON AN INDEX public void getInfo(String relation, String attrName, IndexType accessType, IndexDesc record) throws Catalogmissparam, Catalogioerror, Cataloghferror, IOException, Catalogattrnotfound, Exception { int recSize; RID rid = null; Scan pscan = null; if ((relation == null) || (attrName == null)) throw new Catalogmissparam(null, "MISSING_PARAM"); // OPEN SCAN try { pscan = new Scan(this); } catch (IOException e) { System.err.println("Scan" + e); throw new IOException(""); } catch (Exception e1) { System.err.println("Scan" + e1); throw new Exception(""); } // SCAN FILE while (true) { try { tuple = pscan.getNext(rid); if (tuple == null) throw new Catalogattrnotfound(null, "Catalog: Attribute not Found!"); read_tuple(tuple, record); } catch (Exception e4) { throw new IndexCatalogException(e4, "read_tuple failed"); } if (record.relName.equalsIgnoreCase(relation) == true && record.attrName.equalsIgnoreCase(attrName) == true && (accessType == record.accessType)) break; // FOUND } return; };
// GET ALL INDEXES FOR A RELATION // Return indexCnt. public int getRelInfo(String relation, int indexCnt, IndexDesc[] indexes) throws Catalogmissparam, Catalogioerror, Cataloghferror, Catalogindexnotfound, IOException, Catalognomem, Catalogattrnotfound, IndexCatalogException, RelCatalogException, Catalogrelnotfound { RelDesc record = null; int status; int recSize; RID rid = null; Scan pscan = null; int count = 0; if (relation == null) throw new Catalogmissparam(null, "MISSING_PARAM"); try { ExtendedSystemDefs.MINIBASE_RELCAT.getInfo(relation, record); } catch (Catalogioerror e) { System.err.println("Catalog I/O Error!" + e); throw new Catalogioerror(null, ""); } catch (Cataloghferror e1) { System.err.println("Catalog Heapfile Error!" + e1); throw new Cataloghferror(null, ""); } catch (Catalogmissparam e2) { System.err.println("Catalog Missing Param Error!" + e2); throw new Catalogmissparam(null, ""); } catch (Catalogrelnotfound e3) { System.err.println("Catalog: Relation not Found!" + e3); throw new Catalogrelnotfound(null, ""); } // SET INDEX COUNT BY REFERENCE indexCnt = record.indexCnt; if (indexCnt == 0) return indexCnt; // OPEN SCAN try { pscan = new Scan(this); } catch (Exception e) { throw new IndexCatalogException(e, "scan() failed"); } // ALLOCATE INDEX ARRAY indexes = new IndexDesc[indexCnt]; if (indexes == null) throw new Catalognomem(null, "Catalog: No Enough Memory!"); // SCAN THE FILE while (true) { try { tuple = pscan.getNext(rid); if (tuple == null) throw new Catalogindexnotfound(null, "Catalog: Index not Found!"); read_tuple(tuple, indexes[count]); } catch (Exception e4) { throw new IndexCatalogException(e4, " read_tuple() failed"); } if (indexes[count].relName.equalsIgnoreCase(relation) == true) count++; if (count == indexCnt) // IF ALL INDEXES FOUND break; } return indexCnt; };
// ADD INDEX TO A RELATION public void addIndex(String relation, String attrName, IndexType accessType, int buckets) throws IOException, Catalogioerror, Cataloghferror, Catalogmissparam, Catalogattrnotfound, Catalogbadtype, Catalognomem, Catalogindexnotfound, IndexCatalogException, java.lang.Exception { RID rid = null; IndexDesc indexRec = null; AttrDesc attrRec = null; int intKey = 0; float floatKey = (float) 0.0; String charKey = null; int attrCnt = 0; KeyClass key = null; int recSize = 0; Heapfile datafile = null; String indexName = null; Tuple tuple = null; BTreeFile btree = null; Scan pscan = null; AttrType[] typeArray = null; short[] sizeArray = null; // CHECK PARM if ((relation == null) || (attrName == null)) throw new Catalogmissparam(null, "MISSING_PARAM"); // CHECK FOR EXISTING INDEX try { getInfo(relation, attrName, accessType, indexRec); } catch (Catalogioerror e) { System.err.println("Catalog I/O Error!" + e); throw new Catalogioerror(null, ""); } catch (Cataloghferror e1) { System.err.println("Catalog Heapfile Error!" + e1); throw new Cataloghferror(null, ""); } catch (Catalogmissparam e2) { System.err.println("Catalog Missing Param Error!" + e2); throw new Catalogmissparam(null, ""); } // GET ATTRIBUTE INFO try { ExtendedSystemDefs.MINIBASE_ATTRCAT.getInfo(relation, attrName, attrRec); } catch (Exception e2) { throw new IndexCatalogException(e2, "getInfo() failed"); } // Can only index on int's and strings currently if ((attrRec.attrType.attrType != AttrType.attrInteger) && (attrRec.attrType.attrType != AttrType.attrString)) throw new Catalogbadtype(null, "Catalog: BAD TYPE!"); // UPDATE ATTRIBUTE INFO attrRec.indexCnt++; try { ExtendedSystemDefs.MINIBASE_ATTRCAT.removeInfo(relation, attrName); ExtendedSystemDefs.MINIBASE_ATTRCAT.addInfo(attrRec); } catch (Exception e) { throw new IndexCatalogException(e, "add/remove info failed"); } // BUILD INDEX FILE NAME indexName = buildIndexName(relation, attrName, accessType); // ADDED BY BILL KIMMEL - DELETE LATER System.out.println("Index name is " + indexName); // IF BTREE if (accessType.indexType == IndexType.B_Index) { btree = new BTreeFile(indexName, attrRec.attrType.attrType, attrRec.attrLen, 0); } // ADD ENTRY IN INDEXCAT indexRec.relName = new String(relation); indexRec.attrName = new String(attrName); indexRec.accessType = accessType; if (accessType.indexType == IndexType.B_Index) indexRec.order = new TupleOrder(TupleOrder.Ascending); else indexRec.order = new TupleOrder(TupleOrder.Random); indexRec.distinctKeys = DISTINCTKEYS; indexRec.clustered = 0; // 0 means non-clustered!!!! indexRec.indexPages = INDEXPAGES; try { addInfo(indexRec); } catch (Exception e) { throw new IndexCatalogException(e, "addInfo() failed"); } // PREPARE TO SCAN DATA FILE try { datafile = new Heapfile(relation); if (datafile == null) throw new Catalognomem(null, "NO Enough Memory!"); } catch (Exception e) { throw new IndexCatalogException(e, "create heapfile failed"); } try { pscan = datafile.openScan(); } catch (Exception e) { throw new IndexCatalogException(e, "openScan() failed"); } // PREPARE TUPLE try { ExtendedSystemDefs.MINIBASE_ATTRCAT.getTupleStructure( relation, attrCnt, typeArray, sizeArray); } catch (Exception e) { throw new IndexCatalogException(e, "getTupleStructure"); } tuple = new Tuple(Tuple.max_size); if (tuple == null) throw new Catalognomem(null, "Catalog, No Enough Memory!"); try { tuple.setHdr((short) attrCnt, typeArray, sizeArray); } catch (Exception e) { throw new IndexCatalogException(e, "setHdr() failed"); } recSize = tuple.size(); // NOW PROCESS THE HEAPFILE AND INSERT KEY,RID INTO INDEX while (true) { try { tuple = pscan.getNext(rid); if (tuple == null) return; } catch (Exception e) { throw new IndexCatalogException(e, "getNext() failed"); } // PULL OUT THE KEY VALUE FROM HEAPFILE RECORD if (attrRec.attrType.attrType == AttrType.attrInteger) { intKey = tuple.getIntFld(attrRec.attrPos); key = new IntegerKey(intKey); } else if (attrRec.attrType.attrType == AttrType.attrReal) { floatKey = tuple.getFloFld(attrRec.attrPos); key = new IntegerKey((int) floatKey); } else if (attrRec.attrType.attrType == AttrType.attrString) { charKey = new String(tuple.getStrFld(attrRec.attrPos)); key = new StringKey(charKey); } // NOW INSERT RECORD INTO INDEX if (accessType.indexType == IndexType.B_Index) { try { btree.insert(key, rid); } catch (Exception e) { throw new IndexCatalogException(e, "insert failed"); } } } };
// GET ALL INDEXES INLUDING A SPECIFIED ATTRIBUTE public int getAttrIndexes(String relation, String attrName, int indexCnt, IndexDesc[] indexes) throws Catalogmissparam, Catalogioerror, Cataloghferror, IOException, Catalognomem, Catalogindexnotfound, Catalogattrnotfound, IndexCatalogException { AttrDesc record = null; int status; int recSize; RID rid = null; Scan pscan = null; int count = 0; if (relation == null) throw new Catalogmissparam(null, "MISSING_PARAM"); try { ExtendedSystemDefs.MINIBASE_ATTRCAT.getInfo(relation, attrName, record); } catch (Catalogioerror e) { throw e; } catch (Cataloghferror e1) { throw e1; } catch (Catalogmissparam e2) { throw e2; } catch (Catalogattrnotfound e3) { throw e3; } catch (Exception e4) { throw new IndexCatalogException(e4, "getInfo() failed"); } // ASSIGN INDEX COUNT indexCnt = record.indexCnt; if (indexCnt == 0) return 0; // OPEN SCAN try { pscan = new Scan(this); } catch (Exception e) { throw new IndexCatalogException(e, "scan failed"); } // ALLOCATE INDEX ARRAY indexes = new IndexDesc[indexCnt]; if (indexes == null) throw new Catalognomem(null, "Catalog: No Enough Memory!"); // SCAN FILE while (true) { try { tuple = pscan.getNext(rid); if (tuple == null) throw new Catalogindexnotfound(null, "Catalog: Index not Found!"); read_tuple(tuple, indexes[count]); } catch (Exception e4) { throw new IndexCatalogException(e4, "pascan.getNext() failed"); } if (indexes[count].relName.equalsIgnoreCase(relation) == true && indexes[count].attrName.equalsIgnoreCase(attrName) == true) count++; if (count == indexCnt) // if all indexes found break; } return indexCnt; };