Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 124:75ba2f2d6ea3
searchQueryTest add index
author | one |
---|---|
date | Tue, 07 Oct 2014 22:45:52 +0900 |
parents | ef1173d93a45 |
children | a0c4a4b8ad10 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/DefaultJungleTree.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/InterfaceTraverser.java src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/query/SearchQueryTest.java |
diffstat | 3 files changed, 139 insertions(+), 58 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/DefaultJungleTree.java Mon Oct 06 22:47:37 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/DefaultJungleTree.java Tue Oct 07 22:45:52 2014 +0900 @@ -69,7 +69,10 @@ public InterfaceTraverser getTraverser() { TreeContext tc = repository.get(); ChangeSet cs = tc.getChangeSet(); - return new InterfaceTraverser(getRootNode(), cs.getIndex(), getTreeEditor()); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = cs.getIndex(); + if (index != null) + return new InterfaceTraverser(getRootNode(), index, getTreeEditor()); + return new InterfaceTraverser(getRootNode(),getTreeEditor()); } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/InterfaceTraverser.java Mon Oct 06 22:47:37 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/InterfaceTraverser.java Tue Oct 07 22:45:52 2014 +0900 @@ -4,6 +4,7 @@ import fj.Ord; import fj.data.List; +import fj.data.Option; import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; @@ -12,38 +13,43 @@ 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; +import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIndexIterator; import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator; import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.Query; import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.SearchQuery; import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.UpdateQuery; public class InterfaceTraverser { - + InterfaceTraverser traverser; TreeNode node; TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index; JungleTreeEditor editor; public InterfaceTraverser(TreeNode _root, JungleTreeEditor editor) { - this.node = _root; - this.index = TreeMap.empty(Ord.stringOrd); - this.editor = editor; + this.node = _root; + this.index = TreeMap.empty(Ord.stringOrd); + this.editor = editor; } - - public InterfaceTraverser(TreeNode _root, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index, JungleTreeEditor editor) { + + public InterfaceTraverser( + TreeNode _root, + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index, + JungleTreeEditor editor) { this.node = _root; this.index = index; this.editor = editor; } - public InterfaceTraverser getTraverser(JungleTree tree) { - return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(), tree.getTreeEditor()); + return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(), + tree.getTreeEditor()); } - public void set(TreeNode root){ + public void set(TreeNode root) { this.node = root; } + /* * public IteratorPathNode traverse(TreeNode _node ,NodePath _path ,int * _pos){ @@ -65,56 +71,109 @@ * public List<Pair<NodePath,TreeNode>> distinct(String _key ,String... * _attribute){ return null; } */ - + public JungleTreeEditor update(final Query query) { - /*Iterator<Pair<TreeNode, NodePath>> findNode = find(query); - //do { - for (; findNode.hasNext();) { - Either<Error, JungleTreeEditor> either = editor.putAttribute(findNode.next().right(), "KEY", query.getUpdateAttribute()); - if (either.isA()) - ;// wait delay write - editor = either.b(); - } - //} while (editor.success().isA()); -*/ + /* + * Iterator<Pair<TreeNode, NodePath>> findNode = find(query); //do { for + * (; findNode.hasNext();) { Either<Error, JungleTreeEditor> either = + * editor.putAttribute(findNode.next().right(), "KEY", + * query.getUpdateAttribute()); if (either.isA()) ;// wait delay write + * editor = either.b(); } //} while (editor.success().isA()); + */ return editor; } - public Iterator<Pair<TreeNode, NodePath>> find(final Query query) { - final PathNodeIterator itNode = new PathNodeIterator(node); + public Iterator<Pair<TreeNode, NodePath>> find(final Query query, String key, String searchValue) { - return new Iterator<Pair<TreeNode, NodePath>>() { + if (!index.get(key).isNone()) { + + TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = this.index.get(key).some(); + Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(searchValue); + + if (opList.isNone()) + return null; - private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode); + final List<Pair<TreeNode, NodePath>> list = opList.some(); + return list.iterator(); + + } else { + + final PathNodeIterator itNode = new PathNodeIterator(node); + return new Iterator<Pair<TreeNode, NodePath>>() { + + private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode); + + private Pair<TreeNode, NodePath> nextmatch( + PathNodeIterator itNode) { - private Pair<TreeNode, NodePath> nextmatch(PathNodeIterator itNode) { - for (; itNode.hasNext();) { - Pair<TreeNode, NodePath> pathNode = itNode.next(); - if (query.condition(pathNode.left())) - return pathNode; - } - return null; - } + for (; itNode.hasNext();) { + Pair<TreeNode, NodePath> pathNode = itNode.next(); + String value = pathNode.left().getAttributes().getString(key); + Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key); + System.out.println("value = "+ value); + + if (value != null) { + if (innerIndexOp.isNone()) { + + TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap.empty(Ord.stringOrd); + List<Pair<TreeNode, NodePath>> list = List.nil(); + list = list.cons(pathNode); + innerIndex = innerIndex.set(value, list); + index = index.set(key, innerIndex); + + } else { + + TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some(); + innerIndex.set(value, null); + Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(value); + + if (opList.isNone()) { - @Override - public boolean hasNext() { - return matchPair != null; - } + List<Pair<TreeNode, NodePath>> list = List.nil(); + list = list.cons(pathNode); + innerIndex = innerIndex.set(value, list); + + } else { + + List<Pair<TreeNode, NodePath>> list = opList.some(); + list = list.cons(pathNode); + innerIndex = innerIndex.set(value, list); + + } + index = index.set(key, innerIndex); + + } + } + + if (query.condition(pathNode.left())) + return pathNode; + } + return null; + } - @Override - public Pair<TreeNode, NodePath> next() { - Pair<TreeNode, NodePath> currentPair = matchPair; - matchPair = nextmatch(itNode); - return currentPair; - } + @Override + public boolean hasNext() { + if (matchPair == null) { +// index = itNode.getIndex(); + return false; + } + return true; + } - @Override - public void remove() { - // TODO Auto-generated method stub + @Override + public Pair<TreeNode, NodePath> next() { + Pair<TreeNode, NodePath> currentPair = matchPair; + matchPair = nextmatch(itNode); + return currentPair; + } - } + @Override + public void remove() { + // TODO Auto-generated method stub - }; + } + }; + } } }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/query/SearchQueryTest.java Mon Oct 06 22:47:37 2014 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/query/SearchQueryTest.java Tue Oct 07 22:45:52 2014 +0900 @@ -19,6 +19,7 @@ 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; +import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIndexIterator; import org.junit.Assert; import org.junit.Test; @@ -31,28 +32,46 @@ jungle.createNewTree("tree"); JungleTree tree = jungle.getTreeByName("tree"); createTree(tree); - Iterator<Pair<TreeNode, NodePath>> searchNode = search(tree ,"<-1,0,1>",key); - while (searchNode.hasNext()){ - Assert.assertTrue(compare(searchNode.next().left(),"<-1,0,1>")); - } - } - - public Iterator<Pair<TreeNode, NodePath>> search(JungleTree tree ,String searchAttribute,String key){ InterfaceTraverser ifTraverser = tree.getTraverser(); - return ifTraverser.find( + Iterator<Pair<TreeNode, NodePath>> searchNode = ifTraverser.find( (TreeNode node) -> { ByteBuffer attribute = node.getAttributes().get(key); if(attribute != null){ byte[] byteAttribute = attribute.array(); String str = new String(byteAttribute); System.out.println("attribute = " + str); - return str.equals(searchAttribute); + return str.equals("<-1,0,1>"); } return false; } - ); + ,key,"<-1,0,1>"); + + while (searchNode.hasNext()){ + Assert.assertTrue(compare(searchNode.next().left(),"<-1,0,1>")); + } + + searchNode = ifTraverser.find( + (TreeNode node) -> { + ByteBuffer attribute = node.getAttributes().get(key); + if(attribute != null){ + byte[] byteAttribute = attribute.array(); + String str = new String(byteAttribute); + System.out.println("attribute = " + str); + return str.equals("<-1,0,0>"); + } + return false; + } + ,key,"<-1,0,0>"); + + while (searchNode.hasNext()){ + Pair<TreeNode, NodePath> pathNode = searchNode.next(); + System.out.println("last = "+pathNode.left().getAttributes().getString(key)); + Assert.assertTrue(compare(pathNode.left(),"<-1,0,0>")); + System.out.println(pathNode.left().getAttributes().getString(key)); + } } + public boolean compare(TreeNode compareNode, String compareAttribute) { String labName = compareNode.getAttributes().getString(key); if (labName.equals(compareAttribute))