1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997,2008 Oracle. All rights reserved.
*
* $Id: SequenceExample.java,v 1.10 2008/01/07 14:28:41 cwl Exp $
*/
package je;
import java.io.File;
import java.io.IOException;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Sequence;
import com.sleepycat.je.SequenceConfig;
public class SequenceExample {
private static final int EXIT_SUCCESS = 0;
private static final int EXIT_FAILURE = 1;
private static final String DB_NAME = "sequence.db";
private static final String KEY_NAME = "my_sequence";
public SequenceExample() {
}
public static void usage() {
System.out.println("usage: java " +
"je.SequenceExample " +
"<dbEnvHomeDirectory>");
System.exit(EXIT_FAILURE);
}
public static void main(String[] argv) {
if (argv.length != 1) {
usage();
return;
}
File envHomeDirectory = new File(argv[0]);
try {
SequenceExample app = new SequenceExample();
app.run(envHomeDirectory);
} catch (Exception e) {
e.printStackTrace();
System.exit(EXIT_FAILURE);
}
System.exit(EXIT_SUCCESS);
}
public void run(File envHomeDirectory)
throws DatabaseException, IOException {
/* Create the environment object. */
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
Environment env = new Environment(envHomeDirectory, envConfig);
/* Create the database object. */
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
Database db = env.openDatabase(null, DB_NAME, dbConfig);
/* Create the sequence oject. */
SequenceConfig config = new SequenceConfig();
config.setAllowCreate(true);
DatabaseEntry key =
new DatabaseEntry(KEY_NAME.getBytes("UTF-8"));
Sequence seq = db.openSequence(null, key, config);
/* Allocate a few sequence numbers. */
for (int i = 0; i < 10; i++) {
long seqnum = seq.get(null, 1);
System.out.println("Got sequence number: " + seqnum);
}
/* Close all. */
seq.close();
db.close();
env.close();
}
}
|