}
private void writeAndRead()
throws Exception {
CurrentTransaction txn = CurrentTransaction.getInstance(env);
if (txn != null) {
txn.beginTransaction(null);
}
MarshalledObject o1 = new MarshalledObject("data1", "pk1", "ik1", "");
assertNull(storeMap1.put(null, o1));
assertEquals(o1, storeMap1.get("pk1"));
assertEquals(o1, indexMap1.get("ik1"));
MarshalledObject o2 = new MarshalledObject("data2", "pk2", "", "pk1");
assertNull(storeMap2.put(null, o2));
assertEquals(o2, storeMap2.get("pk2"));
assertEquals(o2, indexMap2.get("pk1"));
if (txn != null) {
txn.commitTransaction();
txn.beginTransaction(null);
}
/*
* store1 contains o1 with primary key "pk1" and index key "ik1".
*
* store2 contains o2 with primary key "pk2" and foreign key "pk1",
* which is the primary key of store1.
*/
if (onDelete == ForeignKeyDeleteAction.ABORT) {
/* Test that we abort trying to delete a referenced key. */
try {
storeMap1.remove("pk1");
fail();
} catch (RuntimeExceptionWrapper expected) {
assertTrue(expected.getCause() instanceof DatabaseException);
if (txn != null) {
txn.abortTransaction();
txn.beginTransaction(null);
}
}
/* Test that we can put a record into store2 with a null foreign
* key value. */
o2 = new MarshalledObject("data2", "pk2", "", "");
assertNotNull(storeMap2.put(null, o2));
assertEquals(o2, storeMap2.get("pk2"));
/* The index2 record should have been deleted since the key was set
* to null above. */
assertNull(indexMap2.get("pk1"));
/* Test that now we can delete the record in store1, since it is no
* longer referenced. */
assertNotNull(storeMap1.remove("pk1"));
assertNull(storeMap1.get("pk1"));
assertNull(indexMap1.get("ik1"));
} else if (onDelete == ForeignKeyDeleteAction.NULLIFY) {
/* Delete the referenced key. */
assertNotNull(storeMap1.remove("pk1"));
assertNull(storeMap1.get("pk1"));
assertNull(indexMap1.get("ik1"));
/* The store2 record should still exist, but should have an empty
* secondary key since it was nullified. */
o2 = (MarshalledObject) storeMap2.get("pk2");
assertNotNull(o2);
assertEquals("data2", o2.getData());
assertEquals("pk2", o2.getPrimaryKey());
assertEquals("", o2.getIndexKey1());
assertEquals("", o2.getIndexKey2());
} else if (onDelete == ForeignKeyDeleteAction.CASCADE) {
/* Delete the referenced key. */
assertNotNull(storeMap1.remove("pk1"));
assertNull(storeMap1.get("pk1"));
assertNull(indexMap1.get("ik1"));
/* The store2 record should have deleted also. */
assertNull(storeMap2.get("pk2"));
assertNull(indexMap2.get("pk1"));
} else {
throw new IllegalStateException();
}
/*
* Test that a foreign key value may not be used that is not present
* in the foreign store. "pk2" is not in store1 in this case.
*/
assertNull(storeMap1.get("pk2"));
MarshalledObject o3 = new MarshalledObject("data3", "pk3", "", "pk2");
try {
storeMap2.put(null, o3);
fail();
} catch (RuntimeExceptionWrapper expected) {
assertTrue(expected.getCause() instanceof DatabaseException);
}
if (txn != null) {
txn.commitTransaction();
}
}