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.
(int tableid, int ioCostPerPage)
| 96 | * sequential-scan IO and disk seeks. |
| 97 | */ |
| 98 | public TableStats(int tableid, int ioCostPerPage) { |
| 99 | // For this function, you'll have to get the |
| 100 | // DbFile for the table in question, |
| 101 | // then scan through its tuples and calculate |
| 102 | // the values that you need. |
| 103 | // You should try to do this reasonably efficiently, but you don't |
| 104 | // necessarily have to (for example) do everything |
| 105 | // in a single scan of the table. |
| 106 | // some code goes here |
| 107 | this.tableId = tableid; |
| 108 | this.ioCostPerPage = ioCostPerPage; |
| 109 | HeapFile heapFile = (HeapFile) catalog.getDatabaseFile(tableid); |
| 110 | this.tupleDesc = heapFile.getTupleDesc(); |
| 111 | this.numPages = heapFile.numPages(); |
| 112 | this.dbFileIterator = heapFile.iterator(new TransactionId()); |
| 113 | |
| 114 | this.max = new int[tupleDesc.numFields()]; |
| 115 | this.min = new int[tupleDesc.numFields()]; |
| 116 | this.intHistograms = new IntHistogram[tupleDesc.numFields()]; |
| 117 | Arrays.fill(min,Integer.MAX_VALUE); |
| 118 | Arrays.fill(max,Integer.MIN_VALUE); |
| 119 | |
| 120 | try { |
| 121 | this.dbFileIterator.open(); |
| 122 | while(dbFileIterator.hasNext()){ |
| 123 | this.total++; |
| 124 | Tuple tuple = dbFileIterator.next(); |
| 125 | for(int i=0;i<max.length;i++){ |
| 126 | Type fieldType = tuple.getField(i).getType(); |
| 127 | if(fieldType.equals(Type.INT_TYPE)){ |
| 128 | IntField field = (IntField)tuple.getField(i); |
| 129 | int value = field.getValue(); |
| 130 | if(value>max[i]){ |
| 131 | max[i] = value; |
| 132 | } |
| 133 | if(value<min[i]){ |
| 134 | min[i] =value; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } catch (DbException e) { |
| 140 | e.printStackTrace(); |
| 141 | } catch (TransactionAbortedException e) { |
| 142 | e.printStackTrace(); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | for(int i=0;i<tupleDesc.numFields();i++){ |
| 147 | Type fieldType = tupleDesc.getFieldType(i); |
| 148 | if(fieldType.equals(Type.STRING_TYPE)){ |
| 149 | continue; |
| 150 | } |
| 151 | this.intHistograms[i] =new IntHistogram(100,min[i],max[i]); |
| 152 | try { |
| 153 | this.dbFileIterator.rewind(); |
| 154 | while(dbFileIterator.hasNext()){ |
| 155 | Tuple tuple = dbFileIterator.next(); |
nothing calls this directly
no test coverage detected