Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 142:ef183969bf31
add get oldTree
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/DefaultJungle.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/DefaultJungle.java Thu Nov 13 22:04:14 2014 +0900 @@ -37,7 +37,6 @@ JungleTree t = j.createNewTree("fuga"); JungleTreeEditor e1 = t.getTreeEditor(); - //JungleTreeEditor e2 = t.getTreeEditor(); DefaultNodePath root = new DefaultNodePath();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/DefaultJungleTree.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/DefaultJungleTree.java Thu Nov 13 22:04:14 2014 +0900 @@ -1,12 +1,8 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle; -import java.util.concurrent.atomic.AtomicReference; - -import org.omg.CORBA.TCKind; - import fj.data.List; +import fj.data.Option; import fj.data.TreeMap; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; @@ -16,85 +12,109 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.AtomicReservableReference; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTransactionManager; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeContext; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TreeContext; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.GetOldTreeError; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexManager; +public class DefaultJungleTree implements JungleTree { + private final AtomicReservableReference<TreeContext> repository; + private final String uuid; + private final ChangeListWriter writer; + private final TreeEditor editor; -public class DefaultJungleTree implements JungleTree -{ - private final AtomicReservableReference<TreeContext> repository; - private final String uuid; - private final ChangeListWriter writer; - private final TreeEditor editor; - - public DefaultJungleTree(TreeContext tc,String uuid,ChangeListWriter writer,TreeEditor editor) - { - this.repository = new AtomicReservableReference<TreeContext>(tc); - this.uuid = uuid; - this.writer = writer; - this.editor = editor; - } + public DefaultJungleTree(TreeContext tc, String uuid, ChangeListWriter writer, TreeEditor editor) { + this.repository = new AtomicReservableReference<TreeContext>(tc); + this.uuid = uuid; + this.writer = writer; + this.editor = editor; + } - @Override - public JungleTreeEditor getTreeEditor() - { - TreeContext tc = repository.get(); - DefaultTransactionManager txManager = new DefaultTransactionManager(writer,tc,repository,uuid); - TreeNode root = tc.getTreeNode(); - TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = getIndex(); - return new DefaultJungleTreeEditor(root,txManager,editor,index); - } - - @Override - public IndexJungleTreeEditor getIndexTreeEditor() - { - TreeContext tc = repository.get(); - DefaultTransactionManager txManager = new DefaultTransactionManager(writer,tc,repository,uuid); - TreeNode root = tc.getTreeNode(); - TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = getIndex(); - return new IndexJungleTreeEditor(root,txManager,editor,newIndex); - } - + @Override + public JungleTreeEditor getTreeEditor() { + TreeContext tc = repository.get(); + DefaultTransactionManager txManager = new DefaultTransactionManager(writer, tc, repository, uuid); + TreeNode root = tc.getTreeNode(); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = getIndex(); + return new DefaultJungleTreeEditor(root, txManager, editor, index); + } + + @Override + public IndexJungleTreeEditor getIndexTreeEditor() { + TreeContext tc = repository.get(); + DefaultTransactionManager txManager = new DefaultTransactionManager(writer, tc, repository, uuid); + TreeNode root = tc.getTreeNode(); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = getIndex(); + return new IndexJungleTreeEditor(root, txManager, editor, newIndex); + } + + @Override + public JungleTreeEditor getLocalTreeEditor() { + return getTreeEditor(); + } + + @Override + public TreeNode getRootNode() { + TreeContext tc = repository.get(); + ChangeSet cs = tc.getChangeSet(); + return cs.getRoot(); + } - @Override - public JungleTreeEditor getLocalTreeEditor() - { - return getTreeEditor(); - } - - @Override - public TreeNode getRootNode() - { - TreeContext tc = repository.get(); - ChangeSet cs = tc.getChangeSet(); - return cs.getRoot(); - } + @Override + public InterfaceTraverser getTraverser() { + AtomicReservableReference<TreeContext>.Reservation reservation = repository.getReservation(); + IndexManager indexManager = new IndexManager(reservation); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = getIndex(); + if (index != null) + return new InterfaceTraverser(getRootNode(), index, indexManager); + return new InterfaceTraverser(getRootNode(), indexManager); + } + + @Override + public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() { + TreeContext tc = repository.get(); + ChangeSet cs = tc.getChangeSet(); + return cs.getIndex(); + } + + @Override + public Iterable<TreeOperation> getLog() { + TreeContext tc = repository.get(); + ChangeSet cs = tc.getChangeSet(); + return cs.getOperations(); + } - @Override - public InterfaceTraverser getTraverser() { - AtomicReservableReference<TreeContext>.Reservation reservation = repository.getReservation(); - IndexManager indexManager = new IndexManager(reservation); - TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = getIndex(); - if (index != null) - return new InterfaceTraverser(getRootNode(), index, indexManager); - return new InterfaceTraverser(getRootNode(),indexManager); - } - - @Override - public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() { - TreeContext tc = repository.get(); - ChangeSet cs = tc.getChangeSet(); - return cs.getIndex(); - } - - @Override - public Iterable<TreeOperation> getLog() { - TreeContext tc = repository.get(); - ChangeSet cs = tc.getChangeSet(); - return cs.getOperations(); - } - + @Override + public long revision() { + TreeContext tc = repository.get(); + ChangeSet cs = tc.getChangeSet(); + return cs.revision(); + } + + @Override + public Either<Error, JungleTree> getOldTree(long revision) { + TreeContext tc = repository.get(); + ChangeSet cs = tc.getChangeSet(); + + for (;cs.revision() != revision;) { + cs = cs.prev(); + if (cs == null) + return DefaultEither.newA(GetOldTreeError.OLD_TREE_NOT_FOUND); + } + + TreeNode root = cs.getRoot(); + + TreeContext oldTc = new DefaultTreeContext(root, cs); + String oldTreeUuid = uuid + revision; + JungleTree oldTree = new DefaultJungleTree(oldTc,oldTreeUuid,writer,editor); + return DefaultEither.newB(oldTree); + } + + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/JungleTree.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/JungleTree.java Thu Nov 13 22:04:14 2014 +0900 @@ -3,13 +3,13 @@ import fj.data.List; import fj.data.TreeMap; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; public interface JungleTree @@ -21,4 +21,6 @@ public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex(); public IndexJungleTreeEditor getIndexTreeEditor(); public Iterable<TreeOperation> getLog(); + public long revision(); + public Either<Error, JungleTree> getOldTree(long revision); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/ChangeSet.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/ChangeSet.java Thu Nov 13 22:04:14 2014 +0900 @@ -6,6 +6,9 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TreeContext; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; public interface ChangeSet
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/NulIterator.java Thu Nov 13 22:04:14 2014 +0900 @@ -0,0 +1,19 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store; + +import java.util.Iterator; + +public class NulIterator<T> implements Iterator<T> { + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return false; + } + + @Override + public T next() { + // TODO Auto-generated method stub + return null; + } + +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/AtomicReservableReference.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/AtomicReservableReference.java Thu Nov 13 22:04:14 2014 +0900 @@ -3,6 +3,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import sun.security.action.GetBooleanAction; + public class AtomicReservableReference<V> { public static void main(String _args[]) @@ -56,6 +58,7 @@ return r.get(); } + public class Reservation { public final Reservation expect; @@ -73,11 +76,11 @@ ref.set(object); } - public V get() - { + public V get(){ return ref.get(); } + public void confirm() { if(flagConfirmed.compareAndSet(false,true)){
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultChangeSet.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultChangeSet.java Thu Nov 13 22:04:14 2014 +0900 @@ -29,6 +29,7 @@ this.revision = _revision; this.index = index; } + @Override public TreeNode getRoot()
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeChildren.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeChildren.java Thu Nov 13 22:04:14 2014 +0900 @@ -111,10 +111,12 @@ @Override public Either<Error,TreeNode> at(int _pos) { - TreeNode Node = children.index(_pos); - if(Node == null){ - return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); - } + System.out.println(children.length()); + if(children.length() < _pos + 1){ + return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); + } + + TreeNode Node = children.index(_pos); return DefaultEither.newB(Node); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/util/GetOldTreeError.java Thu Nov 13 22:04:14 2014 +0900 @@ -0,0 +1,5 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util; + +public class GetOldTreeError implements Error { + public static final Error OLD_TREE_NOT_FOUND = new DefaultError(); +}
--- a/src/test/java/DefaultJungleTreeTest.java Tue Nov 11 18:57:52 2014 +0900 +++ b/src/test/java/DefaultJungleTreeTest.java Thu Nov 13 22:04:14 2014 +0900 @@ -8,7 +8,6 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Result; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import junit.framework.Assert; @@ -26,12 +25,12 @@ public static final String key = "KEY"; public static final ByteBuffer value = ByteBuffer.wrap(key.getBytes()); - public <T extends TreeNode>void testSampleTest() + public void testSampleTest() { Jungle j = instance(); j.createNewTree("tree"); JungleTree tree = j.getTreeByName("tree"); - + Assert.assertEquals(0,tree.revision()); JungleTreeEditor editor1 = tree.getTreeEditor(); JungleTreeEditor editor2 = tree.getTreeEditor(); @@ -48,7 +47,7 @@ TreeNode node = tree.getRootNode(); ByteBuffer v = node.getAttributes().get(key); Assert.assertEquals(0,v.compareTo(value)); - + Assert.assertEquals(1,tree.revision()); // editor2 must fail transction either = editor2.putAttribute(root,key,value); if(either.isA()){
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/GetOldTreeTest.java Thu Nov 13 22:04:14 2014 +0900 @@ -0,0 +1,45 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core; + +import java.nio.ByteBuffer; + +import org.junit.Test; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; +import junit.framework.Assert; + +public class GetOldTreeTest { + + @Test + public void getOldTreeTest() { + Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTreeEditor(new DefaultTraverser())); + jungle.createNewTree("tree"); + JungleTree tree = jungle.getTreeByName("tree"); + JungleTreeEditor editor = tree.getTreeEditor(); + DefaultNodePath path = new DefaultNodePath(); + + for (int num = 0; num < 10; num++) { + JungleTreeEditor addChildEditor = editor.addNewChildAt(path, num).b(); + JungleTreeEditor putAttributeEditor = addChildEditor.putAttribute(path.add(num), "test", ByteBuffer.wrap("tatsuki".getBytes())).b(); + Either<Error, JungleTreeEditor> successEither = putAttributeEditor.success(); + Assert.assertFalse(successEither.isA()); + editor = successEither.b(); + } + System.out.println(tree.revision()); + TreeNode root = tree.getRootNode(); + Assert.assertTrue(root.getChildren().at(9).isB()); + + JungleTree oldTree = tree.getOldTree(9).b(); + System.out.println(oldTree.revision()); + TreeNode oldRoot = oldTree.getRootNode(); + Assert.assertTrue(oldRoot.getChildren().at(9).isA()); + } +}