jankotek / JDBM3

Embedded Key Value Java Database
367 stars 94 forks source link

Unable to add an element to the beginning of a list #97

Open michail-prusakov opened 12 years ago

michail-prusakov commented 12 years ago

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;
}

}

filipperich commented 11 years ago

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);