Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 49:e3a507fd57a4
modified TreeEditor and JungleTreeEditor (not done yet)
author | Shoshi TAMAKI |
---|---|
date | Fri, 08 Feb 2013 01:22:26 +0900 |
parents | 962cf4e208a1 |
children | 0a3bff2d9c89 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/JungleTreeEditor.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TreeEditor.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/Logger.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/OperationLog.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultJungleTreeEditor.java src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java |
diffstat | 7 files changed, 64 insertions(+), 169 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/JungleTreeEditor.java Fri Feb 08 00:32:38 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/JungleTreeEditor.java Fri Feb 08 01:22:26 2013 +0900 @@ -1,10 +1,23 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Transaction; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; +import java.nio.ByteBuffer; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; -public interface JungleTreeEditor<T extends JungleTreeEditor<T>> extends TreeEditor<T> , Transaction<T> +public interface JungleTreeEditor { + public Node getRoot(); + + public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path,int _pos); + public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path,int _pos); + public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value); + public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path,String _key); + public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor); + + public Either<Error,JungleTreeEditor> success(); public String getID(); public String getRevision(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TreeEditor.java Fri Feb 08 00:32:38 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TreeEditor.java Fri Feb 08 01:22:26 2013 +0900 @@ -1,15 +1,11 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store; -import java.nio.ByteBuffer; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; -public interface TreeEditor<T extends TreeEditor<T>> +public interface TreeEditor { - public Either<Error,T> appendChild(NodePath _path,int _pos); - public Either<Error,T> deleteChild(NodePath _path,int _pos); - public Either<Error,T> putAttribute(NodePath _path,String _key,ByteBuffer _value); - public Either<Error,T> deleteAttribute(NodePath _path,String _key); - public Either<Error,T> edit(NodePath _path,NodeEditor _transformer); + public <T extends ClonableNode<T>> Either<Error,T> edit(T _root,NodePath _path,NodeEditor _transformer); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Fri Feb 08 00:32:38 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Fri Feb 08 01:22:26 2013 +0900 @@ -1,14 +1,9 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; -import java.nio.ByteBuffer; import fj.data.List; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteAttribute; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteChildAt; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultEvaluator; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Direction; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversal; @@ -17,52 +12,20 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; -public class DefaultTreeEditor<T extends ClonableNode<T>> - implements TreeEditor<DefaultTreeEditor<T>> +public class DefaultTreeEditor implements TreeEditor { - private final T root; private final Traverser traverser; - public DefaultTreeEditor(T _root,Traverser _traverser) + public DefaultTreeEditor(Traverser _traverser) { - root = _root; traverser = _traverser; } - public T getRootNode() - { - return root; - } - @Override - public Either<Error,DefaultTreeEditor<T>> appendChild(NodePath _path,int _pos) - { - return edit(_path,new AppendChildAt(_pos)); - } - - @Override - public Either<Error, DefaultTreeEditor<T>> deleteChild(NodePath _path,int _pos) - { - return edit(_path,new DeleteChildAt(_pos)); - } - - @Override - public Either<Error, DefaultTreeEditor<T>> putAttribute(NodePath _path, String _key,ByteBuffer _value) - { - return edit(_path,new PutAttribute(_key,_value)); - } - - @Override - public Either<Error, DefaultTreeEditor<T>> deleteAttribute(NodePath _path, String _key) - { - return edit(_path,new DeleteAttribute(_key)); - } - - @Override - public Either<Error,DefaultTreeEditor<T>> edit(NodePath _path,NodeEditor _editor) + public <T extends ClonableNode<T>> Either<Error,T> edit(T _root,NodePath _path,NodeEditor _editor) { DefaultEvaluator e = new DefaultEvaluator(_path); - TraversableNodeWrapper<T> wrap = new TraversableNodeWrapper<T>(root); + TraversableNodeWrapper<T> wrap = new TraversableNodeWrapper<T>(_root); Either<Error, Traversal<TraversableNodeWrapper<T>>> either = traverser.traverse(wrap,e); if(either.isA()){ @@ -70,12 +33,12 @@ } Traversal<TraversableNodeWrapper<T>> t = either.b(); - Either<Error,DefaultTreeEditor<T>> ret = clone(t,_editor); + Either<Error,T> ret = clone(t,_editor); return ret; } - private Either<Error,DefaultTreeEditor<T>> clone(Traversal<TraversableNodeWrapper<T>> _t,NodeEditor _editor) + private <T extends ClonableNode<T>> Either<Error,T> clone(Traversal<TraversableNodeWrapper<T>> _t,NodeEditor _editor) { // copying nodes from bottom to root @@ -112,7 +75,6 @@ } T newRoot = child; - DefaultTreeEditor<T> newEditor = new DefaultTreeEditor<T>(newRoot,traverser); - return DefaultEither.newB(newEditor); + return DefaultEither.newB(newRoot); } } \ No newline at end of file
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/Logger.java Fri Feb 08 00:32:38 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; - -public interface Logger extends Iterable<Operation> -{ - public Logger add(Operation _op); -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/OperationLog.java Fri Feb 08 01:22:26 2013 +0900 @@ -0,0 +1,8 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; + +public interface OperationLog extends Iterable<Operation> +{ + public OperationLog add(Operation _op); +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultJungleTreeEditor.java Fri Feb 08 00:32:38 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultJungleTreeEditor.java Fri Feb 08 01:22:26 2013 +0900 @@ -5,30 +5,31 @@ 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.NodePath; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.Logger; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traverser; 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; -public class DefaultJungleTreeEditor implements JungleTreeEditor<DefaultJungleTreeEditor> +public class DefaultJungleTreeEditor<T extends ClonableNode<T>> implements JungleTreeEditor { private final TransactionManager txManager; - private final DefaultNode root; - private final DefaultTreeEditor<LoggingNode<ClonableDefaultNode>> editor; + private final T root; + private final DefaultTreeEditor editor; private final Traverser traverser; + private final OperationLog log; - public DefaultJungleTreeEditor(DefaultNode _root,TransactionManager _txManager,Traverser _traverser) + public DefaultJungleTreeEditor(T _root,TransactionManager _txManager,Traverser _traverser) { root = _root; txManager = _txManager; - ClonableDefaultNode node = new ClonableDefaultNode(root); - LoggingNode<ClonableDefaultNode> loggingNode = new LoggingNode<ClonableDefaultNode>(node); - editor = new DefaultTreeEditor<LoggingNode<ClonableDefaultNode>>(loggingNode,_traverser); + editor = new DefaultTreeEditor(_traverser); traverser = _traverser; } @@ -41,9 +42,10 @@ } @Override - public Either<Error,DefaultJungleTreeEditor> appendChild(NodePath _path, int _pos) + public Either<Error,JungleTreeEditor> appendChild(NodePath _path, int _pos) { - Either<Error,DefaultTreeEditor<LoggingNode<ClonableDefaultNode>>> either = editor.appendChild(_path,_pos); + AppendChildAt appendChildAt = new AppendChildAt(_pos); + Either<Error,T> either = editor.edit(root,_path,appendChildAt); if(either.isA()){ return DefaultEither.newA(either.a()); }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java Fri Feb 08 00:32:38 2013 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java Fri Feb 08 01:22:26 2013 +0900 @@ -1,12 +1,11 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.impl.treeeditor; import java.nio.ByteBuffer; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNode; 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.TraversableNodeWrapper; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.tests.util.TestUtil; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.ClonableDefaultNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultEvaluator; @@ -19,36 +18,38 @@ public class DefaultTreeEditorTest extends TestCase { - public DefaultTreeEditor<ClonableDefaultNode> instance() + public DefaultTreeEditor instance() { DefaultTraverser traverser = new DefaultTraverser(); - ClonableDefaultNode root = new ClonableDefaultNode(new DefaultNode()); - return new DefaultTreeEditor<ClonableDefaultNode>(root,traverser); + return new DefaultTreeEditor(traverser); } public void testEdittingDoesNotEffectToOtherTree() { ClonableDefaultNode root = TestUtil.createMockTree(3); - DefaultTreeEditor<ClonableDefaultNode> editor = new DefaultTreeEditor<ClonableDefaultNode>(root,new DefaultTraverser()); + DefaultTreeEditor editor = new DefaultTreeEditor(new DefaultTraverser()); DefaultNodePath path = new DefaultNodePath().add(0).add(2); - ClonableDefaultNode oldRoot = editor.getRootNode(); + ClonableDefaultNode oldRoot = root; - DefaultTreeEditor<ClonableDefaultNode> currentEditor = editor; + DefaultTreeEditor currentEditor = editor; String key = "path"; + + ClonableDefaultNode currentRoot = root; for(DefaultNodePath part : path.inits()){ ByteBuffer value = ByteBuffer.wrap(part.toString().getBytes()); - Either<Error, DefaultTreeEditor<ClonableDefaultNode>> either = currentEditor.putAttribute(part,key,value); + PutAttribute putAttribute = new PutAttribute(key,value); + Either<Error,ClonableDefaultNode> either = currentEditor.edit(currentRoot,part,putAttribute); if(either.isA()){ Assert.fail(); } - currentEditor = either.b(); + currentRoot = either.b(); } - ClonableDefaultNode newRoot = currentEditor.getRootNode(); + ClonableDefaultNode newRoot = currentRoot; + DefaultTraverser traverser = new DefaultTraverser(); - DefaultTraverser traverser = new DefaultTraverser(); for(DefaultNodePath part : path.inits()){ Either<Error,Traversal<TraversableNodeWrapper<ClonableDefaultNode>>> either = traverser.traverse(new TraversableNodeWrapper<ClonableDefaultNode>(newRoot),new DefaultEvaluator(part)); if(either.isA()){ @@ -74,96 +75,17 @@ } - public void testAppendChild() + public void testEdit() { - DefaultTreeEditor<ClonableDefaultNode> instance = instance(); - DefaultNodePath path = new DefaultNodePath(); // root - - Either<Error, DefaultTreeEditor<ClonableDefaultNode>> either = instance.appendChild(path,0); - if(either.isA()){ - Assert.fail(); - } - instance = either.b(); - - ClonableDefaultNode node = instance.getRootNode(); - Assert.assertEquals(1,node.getChildren().size()); - } - - public void testDeleteChild() - { - DefaultTreeEditor<ClonableDefaultNode> instance = instance(); + DefaultTreeEditor instance = instance(); + ClonableDefaultNode node = new ClonableDefaultNode(); DefaultNodePath path = new DefaultNodePath(); - Either<Error,DefaultTreeEditor<ClonableDefaultNode>> either = instance.appendChild(path,0); - if(either.isA()){ - Assert.fail(); - } - instance = either.b(); - ClonableDefaultNode node = instance.getRootNode(); - Assert.assertEquals(1,node.getChildren().size()); - - either = instance.deleteChild(path,0); + Either<Error,ClonableDefaultNode> either = instance.edit(node,path,new AppendChildAt(0)); if(either.isA()){ Assert.fail(); } - instance = either.b(); - node = instance.getRootNode(); - Assert.assertEquals(0,node.getChildren().size()); - } - - public DefaultTreeEditor<ClonableDefaultNode> testPutAttribute() - { - DefaultTreeEditor<ClonableDefaultNode> instance = instance(); - DefaultNodePath path = new DefaultNodePath(); - - ClonableDefaultNode old = instance.getRootNode(); - - String key = "KEY"; - ByteBuffer value = ByteBuffer.wrap(key.getBytes()); - - Either<Error,DefaultTreeEditor<ClonableDefaultNode>> either = instance.putAttribute(path,key,value); - if(either.isA()){ - Assert.fail(); - } - instance = either.b(); - - ClonableDefaultNode root = instance.getRootNode(); - ByteBuffer actual = root.getAttributes().get(key); - - Assert.assertEquals(0,actual.compareTo(value)); - - return instance; - } - - public void testDeleteAttribute() - { - DefaultTreeEditor<ClonableDefaultNode> instance = testPutAttribute(); - DefaultNodePath path = new DefaultNodePath(); - - String key = "KEY"; - - Either<Error, DefaultTreeEditor<ClonableDefaultNode>> either = instance.deleteAttribute(path, key); - if(either.isA()){ - Assert.fail(); - } - instance = either.b(); - - ByteBuffer actual = instance.getRootNode().getAttributes().get(key); - Assert.assertNull(actual); - } - - public void testEdit() - { - DefaultTreeEditor<ClonableDefaultNode> instance = instance(); - DefaultNodePath path = new DefaultNodePath(); - - Either<Error, DefaultTreeEditor<ClonableDefaultNode>> either = instance.edit(path,new AppendChildAt(0)); - if(either.isA()){ - Assert.fail(); - } - instance = either.b(); - - ClonableDefaultNode node = instance.getRootNode(); - Assert.assertEquals(1,node.getChildren().size()); + ClonableDefaultNode newRoot = either.b(); + Assert.assertEquals(1,newRoot.getChildren().size()); } }