use of org.cojen.tupl.Database in project Tupl by cojen.
the class Shutdown method main.
/**
* @param args a base file path for the database, and an optional cache size
*/
public static void main(String[] args) throws Exception {
var config = new DatabaseConfig().createFilePath(false).baseFilePath(args[0]).eventListener(EventListener.printTo(System.out));
if (args.length > 1) {
config.minCacheSize(Long.parseLong(args[1]));
}
Database db = Database.open(config);
DatabaseStats stats = db.stats();
db.shutdown();
System.out.println(stats);
}
use of org.cojen.tupl.Database in project Tupl by cojen.
the class Verify method main.
/**
* @param args first argument is a base file path for the database, second optional
* argument is the cache size
*/
public static void main(String[] args) throws Exception {
var config = new DatabaseConfig().createFilePath(false).baseFilePath(args[0]).eventListener(EventListener.printTo(System.out));
if (args.length > 1) {
config.minCacheSize(Long.parseLong(args[1]));
}
Database db = Database.open(config);
System.out.println(db.stats());
var v = new Verify();
db.verify(v);
System.out.println(v);
System.exit(v.failed);
}
use of org.cojen.tupl.Database in project Tupl by cojen.
the class DatabaseReplicatorTest method valueWriteRecover.
@Test
public void valueWriteRecover() throws Exception {
// Verifies that a checkpoint in the middle of a value write on the replica can still
// properly recover the registered cursor.
Database[] dbs = startGroup(2, Role.OBSERVER, null);
Database leaderDb = dbs[0];
Database replicaDb = dbs[1];
Index leaderIx = leaderDb.openIndex("test");
var rnd = new Random();
var part1 = new byte[1000];
rnd.nextBytes(part1);
var part2 = new byte[1000];
rnd.nextBytes(part2);
Transaction txn = leaderDb.newTransaction();
byte[] key = "key1".getBytes();
Cursor c = leaderIx.newAccessor(txn, key);
c.valueWrite(0, part1, 0, part1.length);
txn.flush();
// Wait for replica to catch up.
fence(leaderDb, replicaDb);
Index replicaIx = replicaDb.openIndex("test");
assertTrue(replicaIx.exists(Transaction.BOGUS, key));
replicaDb.checkpoint();
replicaDb = closeAndReopen(1);
replicaIx = replicaDb.openIndex("test");
assertTrue(replicaIx.exists(Transaction.BOGUS, key));
// Finish writing and wait for replica to catch up.
c.valueWrite(part1.length, part2, 0, part2.length);
c.close();
txn.commit();
fence(leaderDb, replicaDb);
var expect = new byte[part1.length + part2.length];
System.arraycopy(part1, 0, expect, 0, part1.length);
System.arraycopy(part2, 0, expect, part1.length, part2.length);
fastAssertArrayEquals(expect, leaderIx.load(null, key));
fastAssertArrayEquals(expect, replicaIx.load(null, key));
replicaDb.close();
}
use of org.cojen.tupl.Database in project Tupl by cojen.
the class DatabaseReplicatorTest method emergencyRecovery.
@Test
public void emergencyRecovery() throws Exception {
// Test that database can be opened after the replication files are deleted. Anything
// in them is lost.
Database db = startGroup(1)[0];
Index ix = db.openIndex("test");
ix.store(null, "k1".getBytes(), "v1".getBytes());
db.suspendCheckpoints();
db.checkpoint();
ix.store(null, "k2".getBytes(), "v2".getBytes());
db.close();
File baseFile = mReplBaseFiles[0];
String prefix = baseFile.getName();
baseFile.getParentFile().listFiles(file -> {
String name = file.getName();
if (name.startsWith(prefix) && !name.endsWith(".db")) {
file.delete();
}
return false;
});
db = closeAndReopen(0);
ix = db.openIndex("test");
fastAssertArrayEquals("v1".getBytes(), ix.load(null, "k1".getBytes()));
assertNull(ix.load(null, "k2".getBytes()));
db.close();
}
use of org.cojen.tupl.Database in project Tupl by cojen.
the class DatabaseReplicatorTest method explicitFailover.
@Test
public void explicitFailover() throws Exception {
Database[] dbs = startGroup(2);
Database leaderDb = dbs[0];
Database replicaDb = dbs[1];
Index leaderIx = leaderDb.openIndex("test");
// Wait for replica to catch up.
fence(leaderDb, replicaDb);
Index replicaIx = replicaDb.openIndex("test");
byte[] key = "hello".getBytes();
byte[] value = "world".getBytes();
leaderDb.failover();
try {
leaderIx.store(null, key, value);
fail();
} catch (UnmodifiableReplicaException e) {
}
boolean success = false;
for (int i = 0; i < 10; i++) {
try {
replicaIx.store(null, key, value);
success = true;
break;
} catch (UnmodifiableReplicaException e) {
}
TestUtils.sleep(1000);
}
assertTrue(success);
// Wait for old leader to catch up.
fence(replicaDb, leaderDb);
fastAssertArrayEquals(value, leaderIx.load(null, key));
}
Aggregations