@@ -441,39 +441,71 @@ class BerkeleyDBLeafPage extends BerkeleyDBPage {
441
441
442
442
/**
443
443
* Right after the header of a b-tree leaf page there is a
444
- * table with 16bit words, each data item has an entry in this
444
+ * table with 16bit words, each item has an entry in this
445
445
* table. They are the offsets of the actual data (relative to
446
- * the start of the page). Given a page buffer of a b-tree
447
- * page and an item index this function will look up the table
448
- * and return the offset (relative to page start) of this item.
446
+ * the start of the page). Given an item index this function
447
+ * will look up the table and return the offset (relative to
448
+ * page start) of this item. Each row of data (key + value)
449
+ * is actually made up of 2 consecutive items, the key and
450
+ * the value. This method does not need to care whether its
451
+ * a key or a value, it just takes an index and returns the
452
+ * offset where to find the data.
449
453
*/
450
454
private def getItemOffset (int index ) {
451
455
val lookup_table_offs = SIZE_LEAF_HEADER + 2 * index
452
456
b. getShort(lookup_table_offs). bitwiseAnd(0xffff )
453
457
}
454
458
459
+ /**
460
+ * read bytes from the specified offset and copy them
461
+ * into a new ByteBuffer. The returned ByteBuffer will
462
+ * be configured for little endian reading.
463
+ */
455
464
private def getBytes (int offset , int size ){
456
465
val a = newByteArrayOfSize(size)
457
466
b. position(offset)
458
467
b. get(a)
459
468
ByteBuffer . wrap(a). order(ByteOrder . LITTLE_ENDIAN )
460
469
}
461
470
471
+ /**
472
+ * get the type of the item with the index,
473
+ * FIXME: see db_page.h for type constants
474
+ */
462
475
def getItemType (int index ){
463
476
val offset = getItemOffset(index)
464
477
b. get(offset + 2 )
465
478
}
466
479
480
+ /**
481
+ * get the item data at the index, return it in
482
+ * a newly allocated ByteBuffer. The Buffer will
483
+ * be configured as little endian. "Item" can be
484
+ * either a key or a value, its up to higher
485
+ * levels to know what it actually is, as far as
486
+ * this method is concerned its just a bunch of
487
+ * bytes addressed by an index number.
488
+ */
467
489
def getItemData (int index ){
468
490
val offset = getItemOffset(index)
469
491
val size = b. getShort(offset). bitwiseAnd(0xffff )
470
492
getBytes(offset + 3 , size)
471
493
}
472
494
495
+ /**
496
+ * page number of next leaf page in this tree
497
+ * or 0 if this is the last leaf page.
498
+ */
473
499
def getNextLeafPage () throws IOException {
474
500
new BerkeleyDBLeafPage (raf, nextPgno, pgsize)
475
501
}
476
502
503
+ /**
504
+ * Get count of items on this page. This is twice
505
+ * the number of key/value pairs because key and
506
+ * value are separate items. They are stored in
507
+ * alternating sequence: {k1, v1, k2, v2, .., kn, vn}
508
+ */
477
509
def getEntryCount () {
478
510
b. getShort(20 )
479
511
}
0 commit comments