Skip to content

Instantly share code, notes, and snippets.

@ryankennedy
Created February 10, 2012 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryankennedy/1794000 to your computer and use it in GitHub Desktop.
Save ryankennedy/1794000 to your computer and use it in GitHub Desktop.
testing creating secondary indices in bdb after the primary has already been used
import com.sleepycat.je.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class NewIndexTest {
public static void main(String[] args) throws IOException {
File envHome = new File("bdb-temp-dir");
try {
// Make a temporary home for the BDB-ness
if (!envHome.mkdirs()) {
throw new RuntimeException("KABLOOEY");
}
// Keys...lots of keys
DatabaseEntry foo = new DatabaseEntry("foo".getBytes("us-ascii"));
DatabaseEntry FOO = new DatabaseEntry("FOO".getBytes("us-ascii"));
DatabaseEntry baz = new DatabaseEntry("baz".getBytes("us-ascii"));
DatabaseEntry BAZ = new DatabaseEntry("BAZ".getBytes("us-ascii"));
DatabaseEntry bar = new DatabaseEntry("bar".getBytes("us-ascii"));
// Environment config...yay
EnvironmentConfig config = new EnvironmentConfig();
config.setAllowCreate(true);
config.setTransactional(true);
// DB config...double yay
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
// Open the environment
Environment env = new Environment(envHome, config);
Database db = env.openDatabase(null, "test", dbConfig);
// Write the "bar" key
db.put(null, foo, new DatabaseEntry("bar".getBytes("us-ascii")));
// Close it up
db.close();
env.close();
// Reopen, simulates a deploy
env = new Environment(envHome, config);
db = env.openDatabase(null, "test", dbConfig);
// Set up the secondary index, the key creator indexes the upper case version of the primary key
SecondaryConfig sdbConfig = new SecondaryConfig();
sdbConfig.setAllowCreate(true);
sdbConfig.setTransactional(true);
sdbConfig.setKeyCreator(new SecondaryKeyCreator() {
@Override
public boolean createSecondaryKey(SecondaryDatabase secondary, DatabaseEntry key, DatabaseEntry data, DatabaseEntry result) {
try {
result.setData(new String(key.getData(), "us-ascii").toUpperCase().getBytes("us-ascii"));
return true;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
});
// Open the secondary index
SecondaryDatabase sdb = env.openSecondaryDatabase(null, "foo-secondary", db, sdbConfig);
// Write the "baz" key to the primary, writes the "BAZ" key to the secondary
db.put(null, baz, bar);
DatabaseEntry value = new DatabaseEntry();
// Read "foo" from the primary, should succeed
assert db.get(null, foo, value, LockMode.DEFAULT) == OperationStatus.SUCCESS;
// Read "FOO" from the secondary, should miss
assert sdb.get(null, FOO, value, LockMode.DEFAULT) == OperationStatus.NOTFOUND;
// Read "baz" from the primary, should succeed
assert db.get(null, baz, value, LockMode.DEFAULT) == OperationStatus.SUCCESS;
// Read "BAZ" from the secondary, should succeed
assert sdb.get(null, BAZ, value, LockMode.DEFAULT) == OperationStatus.SUCCESS;
// Overwrite "foo" in the primary, should write "FOO" to the secondary
assert db.put(null, foo, new DatabaseEntry("BURRITO".getBytes("us-ascii"))) == OperationStatus.SUCCESS;
// Read "FOO" from the secondary, should succeed
assert sdb.get(null, FOO, value, LockMode.DEFAULT) == OperationStatus.SUCCESS;
// Close it up
sdb.close();
db.close();
env.close();
} finally {
// Clean it up
if (envHome.exists()) {
deltree(envHome);
}
}
}
/**
* Good 'ole deltree
*/
private static boolean deltree(File envHome) {
if (envHome.isDirectory()) {
for (File file : envHome.listFiles()) {
if (!deltree(file)) {
return false;
}
}
}
return envHome.delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment