Test that scanning the BTree for predicates does not read all the pages
()
| 209 | |
| 210 | /** Test that scanning the BTree for predicates does not read all the pages */ |
| 211 | @Test public void testReadPage() throws Exception { |
| 212 | // Create the table |
| 213 | final int LEAF_PAGES = 30; |
| 214 | |
| 215 | List<List<Integer>> tuples = new ArrayList<>(); |
| 216 | int keyField = 0; |
| 217 | BTreeFile f = BTreeUtility.createBTreeFile(2, LEAF_PAGES*502, null, tuples, keyField); |
| 218 | tuples.sort(new TupleComparator(keyField)); |
| 219 | TupleDesc td = Utility.getTupleDesc(2); |
| 220 | InstrumentedBTreeFile table = new InstrumentedBTreeFile(f.getFile(), keyField, td); |
| 221 | Database.getCatalog().addTable(table, SystemTestUtil.getUUID()); |
| 222 | |
| 223 | // EQUALS |
| 224 | TransactionId tid = new TransactionId(); |
| 225 | List<List<Integer>> tuplesFiltered = new ArrayList<>(); |
| 226 | IndexPredicate ipred = new IndexPredicate(Op.EQUALS, new IntField(r.nextInt(LEAF_PAGES*502))); |
| 227 | Iterator<List<Integer>> it = tuples.iterator(); |
| 228 | while(it.hasNext()) { |
| 229 | List<Integer> tup = it.next(); |
| 230 | if(tup.get(keyField) == ((IntField) ipred.getField()).getValue()) { |
| 231 | tuplesFiltered.add(tup); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | Database.resetBufferPool(BufferPool.DEFAULT_PAGES); |
| 236 | table.readCount = 0; |
| 237 | BTreeScan scan = new BTreeScan(tid, f.getId(), "table", ipred); |
| 238 | SystemTestUtil.matchTuples(scan, tuplesFiltered); |
| 239 | // root pointer page + root + leaf page (possibly 2 leaf pages) |
| 240 | assertTrue(table.readCount == 3 || table.readCount == 4); |
| 241 | |
| 242 | // LESS_THAN |
| 243 | tuplesFiltered.clear(); |
| 244 | ipred = new IndexPredicate(Op.LESS_THAN, new IntField(r.nextInt(LEAF_PAGES*502))); |
| 245 | it = tuples.iterator(); |
| 246 | while(it.hasNext()) { |
| 247 | List<Integer> tup = it.next(); |
| 248 | if(tup.get(keyField) < ((IntField) ipred.getField()).getValue()) { |
| 249 | tuplesFiltered.add(tup); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | Database.resetBufferPool(BufferPool.DEFAULT_PAGES); |
| 254 | table.readCount = 0; |
| 255 | scan = new BTreeScan(tid, f.getId(), "table", ipred); |
| 256 | SystemTestUtil.matchTuples(scan, tuplesFiltered); |
| 257 | // root pointer page + root + leaf pages |
| 258 | int leafPageCount = tuplesFiltered.size()/502; |
| 259 | if(leafPageCount < LEAF_PAGES) |
| 260 | leafPageCount++; // +1 for next key locking |
| 261 | assertEquals(leafPageCount + 2, table.readCount); |
| 262 | |
| 263 | // GREATER_THAN |
| 264 | tuplesFiltered.clear(); |
| 265 | ipred = new IndexPredicate(Op.GREATER_THAN_OR_EQ, new IntField(r.nextInt(LEAF_PAGES*502))); |
| 266 | it = tuples.iterator(); |
| 267 | while(it.hasNext()) { |
| 268 | List<Integer> tup = it.next(); |
nothing calls this directly
no test coverage detected