| 1 |
|
| 2 |
import java.util.Properties; |
| 3 |
import jdbm.RecordManager; |
| 4 |
import jdbm.RecordManagerFactory; |
| 5 |
|
| 6 |
import jdbm.helper.Tuple; |
| 7 |
import jdbm.helper.TupleBrowser; |
| 8 |
import jdbm.helper.StringComparator; |
| 9 |
|
| 10 |
import jdbm.btree.BTree; |
| 11 |
|
| 12 |
/** |
| 13 |
* Famous People example. |
| 14 |
* <p> |
| 15 |
* Demonstrates the use of B+Tree data structure to manage a list of |
| 16 |
* people and their occupation. The example covers insertion, |
| 17 |
* ordered traversal, reverse traversal and range lookup of records. |
| 18 |
* |
| 19 |
* @author <a href="mailto:[email protected]">Alex Boisvert</a> |
| 20 |
*/ |
| 21 |
public class FamousPeople { |
| 22 |
|
| 23 |
static String DATABASE = "people"; |
| 24 |
static String BTREE_NAME = "FamousPeople"; |
| 25 |
|
| 26 |
static String[] people = |
| 27 |
{ "Greenspan, Alan", |
| 28 |
"Williams-Byrd, Julie", |
| 29 |
"Picasso, Pablo", |
| 30 |
"Stallman, Richard", |
| 31 |
"Fort, Paul", |
| 32 |
"S�ndergaard, Ole", |
| 33 |
"Schwarzenegger, Arnold", |
| 34 |
"Dulkinys, Susanna" }; |
| 35 |
|
| 36 |
static String[] occupations = |
| 37 |
{ "Federal Reserve Board Chairman", |
| 38 |
"Engineer", |
| 39 |
"Painter", |
| 40 |
"Programmer", |
| 41 |
"Poet", |
| 42 |
"Typographer", |
| 43 |
"Actor", |
| 44 |
"Designer" }; |
| 45 |
|
| 46 |
static String PREFIX = "S"; |
| 47 |
|
| 48 |
/** |
| 49 |
* Example main entrypoint. |
| 50 |
*/ |
| 51 |
public static void main( String[] args ) { |
| 52 |
RecordManager recman; |
| 53 |
long recid; |
| 54 |
Tuple tuple = new Tuple(); |
| 55 |
TupleBrowser browser; |
| 56 |
BTree tree; |
| 57 |
Properties props; |
| 58 |
|
| 59 |
props = new Properties(); |
| 60 |
|
| 61 |
try { |
| 62 |
// open database and setup an object cache |
| 63 |
recman = RecordManagerFactory.createRecordManager( DATABASE, props ); |
| 64 |
|
| 65 |
// try to reload an existing B+Tree |
| 66 |
recid = recman.getNamedObject( BTREE_NAME ); |
| 67 |
if ( recid != 0 ) { |
| 68 |
tree = BTree.load( recman, recid ); |
| 69 |
System.out.println( "Reloaded existing BTree with " + tree.size() |
| 70 |
+ " famous people." ); |
| 71 |
} else { |
| 72 |
// create a new B+Tree data structure and use a StringComparator |
| 73 |
// to order the records based on people's name. |
| 74 |
tree = BTree.createInstance( recman, new StringComparator() ); |
| 75 |
recman.setNamedObject( BTREE_NAME, tree.getRecid() ); |
| 76 |
System.out.println( "Created a new empty BTree" ); |
| 77 |
} |
| 78 |
|
| 79 |
// insert people with their respective occupation |
| 80 |
System.out.println(); |
| 81 |
for ( int i=0; i<people.length; i++ ) { |
| 82 |
System.out.println( "Insert: " + people[i] ); |
| 83 |
tree.insert( people[ i ], occupations[ i ], false ); |
| 84 |
} |
| 85 |
|
| 86 |
// make the data persistent in the database |
| 87 |
recman.commit(); |
| 88 |
|
| 89 |
// show list of people with their occupation |
| 90 |
System.out.println(); |
| 91 |
System.out.println( "Person Occupation " ); |
| 92 |
System.out.println( "------------------ ------------------" ); |
| 93 |
|
| 94 |
// traverse people in order |
| 95 |
browser = tree.browse(); |
| 96 |
while ( browser.getNext( tuple ) ) { |
| 97 |
print( tuple ); |
| 98 |
} |
| 99 |
|
| 100 |
// traverse people in reverse order |
| 101 |
System.out.println(); |
| 102 |
System.out.println( "Reverse order:" ); |
| 103 |
browser = tree.browse( null ); // position browser at end of the list |
| 104 |
|
| 105 |
while ( browser.getPrevious( tuple ) ) { |
| 106 |
print( tuple ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
// display people whose name start with PREFIX range |
| 112 |
System.out.println(); |
| 113 |
System.out.println( "All people whose name start with '" + PREFIX + "':" ); |
| 114 |
|
| 115 |
browser = tree.browse( PREFIX ); |
| 116 |
while ( browser.getNext( tuple ) ) { |
| 117 |
String key = (String) tuple.getKey(); |
| 118 |
if ( key.startsWith( PREFIX ) ) { |
| 119 |
print( tuple ); |
| 120 |
} else { |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
} catch ( Exception except ) { |
| 126 |
except.printStackTrace(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Print a Tuple containing a ( Person, Occupation ) pair. |
| 133 |
*/ |
| 134 |
static void print( Tuple tuple ) { |
| 135 |
String person = (String) tuple.getKey(); |
| 136 |
String occupation = (String) tuple.getValue(); |
| 137 |
System.out.println( pad( person, 25) + occupation ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* Pad a string with spaces on the right. |
| 143 |
* |
| 144 |
* @param str String to add spaces |
| 145 |
* @param width Width of string after padding |
| 146 |
*/ |
| 147 |
static String pad( String str, int width ) { |
| 148 |
StringBuffer buf = new StringBuffer( str ); |
| 149 |
int space = width-buf.length(); |
| 150 |
while ( space-- > 0 ) { |
| 151 |
buf.append( ' ' ); |
| 152 |
} |
| 153 |
return buf.toString(); |
| 154 |
} |
| 155 |
|
| 156 |
} |