JDBM3
JDBM3 copied to clipboard
Unable to add an element to the beginning of a list
Consider the following unit test:
import java.util.List;
import org.apache.jdbm.DB; import org.apache.jdbm.DBMaker; import org.junit.Test;
public class JDBMTester {
@Test
public void testAddValueToHead() {
DB db = DBMaker.openFile("tmp2").make();
List<String> list = getList(db, "list2");
list.add(0, "1");
db.commit();
db.close();
db = DBMaker.openFile("tmp2").make();
list = getList(db, "list2");
list.add(0, "0");
db.commit();
db.close();
}
private <K> List<K> getList(DB db, String name) {
List<K> result = db.getLinkedList(name);
if (result == null) {
result = db.createLinkedList(name);
}
return result;
}
}
When adding to the front of the list, need to update the Root's pointer to first. Need to change LinkedList2.java:
421: Root r = getRoot(); 422: r.size++; 423: db.update(rootRecid, r, ROOT_SERIALIZER);
421: Root r = getRoot(); ADD: if (prev == 0) r.first = recid; 422: r.size++; 423: db.update(rootRecid, r, ROOT_SERIALIZER);