* Example main entrypoint.
*/
public static void main( String[] args ) {
RecordManager recman;
long recid;
Tuple tuple = new Tuple();
TupleBrowser browser;
BTree tree;
Properties props;
props = new Properties();
try {
// open database and setup an object cache
recman = RecordManagerFactory.createRecordManager( DATABASE, props );
// try to reload an existing B+Tree
recid = recman.getNamedObject( BTREE_NAME );
if ( recid != 0 ) {
tree = BTree.load( recman, recid );
System.out.println( "Reloaded existing BTree with " + tree.size()
+ " famous people." );
} else {
// create a new B+Tree data structure and use a StringComparator
// to order the records based on people's name.
tree = BTree.createInstance( recman, new StringComparator() );
recman.setNamedObject( BTREE_NAME, tree.getRecid() );
System.out.println( "Created a new empty BTree" );
}
// insert people with their respective occupation
System.out.println();
for ( int i=0; i<people.length; i++ ) {
System.out.println( "Insert: " + people[i] );
tree.insert( people[ i ], occupations[ i ], false );
}
// make the data persistent in the database
recman.commit();
// show list of people with their occupation
System.out.println();
System.out.println( "Person Occupation " );
System.out.println( "------------------ ------------------" );
// traverse people in order
browser = tree.browse();
while ( browser.getNext( tuple ) ) {
print( tuple );
}
// traverse people in reverse order
System.out.println();
System.out.println( "Reverse order:" );
browser = tree.browse( null ); // position browser at end of the list
while ( browser.getPrevious( tuple ) ) {
print( tuple );
}
// display people whose name start with PREFIX range
System.out.println();
System.out.println( "All people whose name start with '" + PREFIX + "':" );
browser = tree.browse( PREFIX );
while ( browser.getNext( tuple ) ) {
String key = (String) tuple.getKey();
if ( key.startsWith( PREFIX ) ) {
print( tuple );
} else {
break;
}