Skip to content
Browse files

Fixed wrong size in BTreeMap after rollback, issue #89

  • Loading branch information...
1 parent f7de667 commit 6b2d997804a454828a8b9b237d876c86ae89de3c @jankotek committed
View
2 src/main/java/org/apache/jdbm/BTreeMap.java
@@ -31,7 +31,7 @@
*/
class BTreeMap<K, V> extends AbstractMap<K, V> implements ConcurrentNavigableMap<K, V> {
- protected final BTree<K, V> tree;
+ protected BTree<K, V> tree;
protected final K fromKey;
View
23 src/main/java/org/apache/jdbm/DBAbstract.java
@@ -564,4 +564,27 @@ public void run(){
}
}
+
+ synchronized public void rollback() {
+ try {
+ for(WeakReference<Object> o:collections.values()){
+ Object c = o.get();
+ if(c != null && c instanceof BTreeMap){
+ //reload tree
+ BTreeMap m = (BTreeMap) c;
+ m.tree = fetch(m.tree.getRecid());
+ }
+ if(c != null && c instanceof BTreeSet){
+ //reload tree
+ BTreeSet m = (BTreeSet) c;
+ m.map.tree = fetch(m.map.tree.getRecid());
+ }
+
+
+ }
+ } catch (IOException e) {
+ throw new IOError(e);
+ }
+
+ }
}
View
2 src/main/java/org/apache/jdbm/DBStore.java
@@ -463,6 +463,8 @@ public synchronized void rollback() {
_physMgr.rollback();
_logicMgr.rollback();
_pageman.rollback();
+
+ super.rollback();
} catch (IOException e) {
throw new IOError(e);
}
View
91 src/test/java/org/apache/jdbm/RollbackTest.java
@@ -0,0 +1,91 @@
+package org.apache.jdbm;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RollbackTest extends TestCaseWithTestFile{
+
+ public void test_treemap() throws IOException {
+ DB db = newDBCache();
+ Map<Integer, String> map = db.createTreeMap("collectionName");
+
+ map.put(1, "one");
+ map.put(2, "two");
+
+ assertEquals(2, map.size());
+ db.commit(); //persist changes into disk
+
+ map.put(3, "three");
+ assertEquals(3, map.size());
+ db.rollback(); //revert recent changes
+ assertEquals(2, map.size());
+ }
+
+ public void test_hashmap() throws IOException {
+ DB db = newDBCache();
+ Map<Integer, String> map = db.createHashMap("collectionName");
+
+ map.put(1, "one");
+ map.put(2, "two");
+
+ assertEquals(2, map.size());
+ db.commit(); //persist changes into disk
+
+ map.put(3, "three");
+ assertEquals(3, map.size());
+ db.rollback(); //revert recent changes
+ assertEquals(2, map.size());
+ }
+
+ public void test_treeset() throws IOException {
+ DB db = newDBCache();
+ Set<Integer> c = db.createTreeSet("collectionName");
+
+ c.add(1);
+ c.add(2);
+
+ assertEquals(2, c.size());
+ db.commit(); //persist changes into disk
+
+ c.add(3);
+ assertEquals(3, c.size());
+ db.rollback(); //revert recent changes
+ assertEquals(2, c.size());
+ }
+
+
+ public void test_hashset() throws IOException {
+ DB db = newDBCache();
+ Set<Integer> c = db.createHashSet("collectionName");
+
+ c.add(1);
+ c.add(2);
+
+ assertEquals(2, c.size());
+ db.commit(); //persist changes into disk
+
+ c.add(3);
+ assertEquals(3, c.size());
+ db.rollback(); //revert recent changes
+ assertEquals(2, c.size());
+ }
+
+ public void test_linkedlist() throws IOException {
+ DB db = newDBCache();
+ List<Integer> c = db.createLinkedList("collectionName");
+
+ c.add(1);
+ c.add(2);
+
+ assertEquals(2, c.size());
+ db.commit(); //persist changes into disk
+
+ c.add(3);
+ assertEquals(3, c.size());
+ db.rollback(); //revert recent changes
+ assertEquals(2, c.size());
+ }
+
+}

0 comments on commit 6b2d997

Please sign in to comment.
Something went wrong with that request. Please try again.