Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 237:33c93f162389 Implementation_of_communication
add getNodePath and commandLine
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungleTree.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungleTree.java Sun Feb 14 18:04:24 2016 +0900 @@ -1,21 +1,24 @@ package jp.ac.u_ryukyu.ie.cr.jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; +import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; -import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTransactionManager; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.jungle.util.GetOldTreeError; -import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; -import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap; +import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; public class DefaultJungleTree implements JungleTree { @@ -92,7 +95,7 @@ } @Override - public Either<jp.ac.u_ryukyu.ie.cr.jungle.util.Error, TreeNode> getNodeOfPath(NodePath path) { + public Either<Error, TreeNode> getNodeOfPath(NodePath path) { TreeNode node = repository.get().getRoot(); for (int num : path) { if (num == -1) @@ -106,6 +109,27 @@ } @Override + public NodePath getNodePath(TreeNode node) { + ParentIndex parentIndex = getParentIndex(); + NodePath path = new DefaultNodePath().pop().right(); + Optional<TreeNode> parentOp = parentIndex.get(node); + while(parentOp.isPresent()) { + TreeNode parent = parentOp.get(); + TreeNodeChildren children = parent.getChildren(); + int num = 0; + for (TreeNode child : children) { + if (child == node) + break; + num++; + } + path = path.addHead(num); + node = parent; + parentOp = parentIndex.get(node); + } + return path.addHead(-1); + } + + @Override public void setBufferSize(int _bufferSize) { // not use }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTree.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTree.java Sun Feb 14 18:04:24 2016 +0900 @@ -30,4 +30,6 @@ public Either<Error, TreeNode> getNodeOfPath(NodePath path); public void setBufferSize(int _bufferSize); + + public NodePath getNodePath(TreeNode node); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/Command.java Sun Feb 14 18:04:24 2016 +0900 @@ -0,0 +1,6 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.commandline; + + +public enum Command { + select,delete,none +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/commandline.java Sun Feb 14 18:04:24 2016 +0900 @@ -0,0 +1,126 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.commandline; + +import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle; +import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.xml.reader.XmlReader; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Iterator; +import java.util.LinkedList; + + +public class commandline { + Jungle jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser())); + + public void start() throws IOException { + //テストxmlの読み込み 後で消す + JungleTree tree = jungle.createNewTree("XMLTEST"); + new XmlReader().start("/Users/e115731/workspace/jungle-core/xml/drink.xml", tree); + System.out.println("jungle 対話モード"); + while (true) { + System.out.println("入力受付中"); + + InputStreamReader isr = new InputStreamReader(System.in); + BufferedReader br = new BufferedReader(isr); + String str = br.readLine(); + if (str.equals("exit")) + break; + parse(str); + } + System.out.println("exit"); + } + + private void parse(String sql) { + Command cmd = Command.none; + String[] split = sql.split(" "); + Iterator<String> iterator = new Iterator<String>() { + private int count = 0; + + @Override + public boolean hasNext() { + if (count < split.length) + return true; + return false; + } + + @Override + public String next() { + String tmp = split[count]; + count++; + return tmp; + } + }; + + + while (iterator.hasNext()) { + switch (iterator.next()) { + case "select": + select(iterator); + } + } + } + + + private void select(Iterator<String> iterator) { + //取得するkeyの取得 + LinkedList<String> keys = new LinkedList<>(); + while (iterator.hasNext()) { + String str = iterator.next(); + if (str.equals("from")) + break; + keys.add(str);//表示するkeyを表示 + } + + + LinkedList<String> treeNames = new LinkedList<>(); + while (iterator.hasNext()) { + String str = iterator.next(); + if (str.equals("where")) + where(iterator, treeNames); + treeNames.add(str);//木の名前を取得 + } + + Iterator<String> treeIterator = treeNames.iterator(); + while (treeIterator.hasNext()) { + String treeName = treeIterator.next(); + JungleTree tree = jungle.getTreeByName(treeName); + InterfaceTraverser traverser = tree.getTraverser(true); + + Iterator<TreeNode> nodeIterator = traverser.find((TreeNode node) -> { //この場合条件がないので探索する木のNodeを全て取得する + return true; + }); + + while (nodeIterator.hasNext()) { + TreeNode node = nodeIterator.next(); + + Iterator<String> nodeKeys; + if (keys.getFirst().equals("*")) + nodeKeys = node.getAttributes().getKeys(); + else + nodeKeys = keys.iterator(); + while (nodeKeys.hasNext()) { + String key = nodeKeys.next(); + String value = node.getAttributes().getString(key); + if (value != null) + System.out.println(key + "=" + value + ","); + } + } + + } + } + + private void where(Iterator<String> iterator, LinkedList<String> treeNames) { + while (iterator.hasNext()) { + String str = iterator.next(); + + } + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/main.java Sun Feb 14 18:04:24 2016 +0900 @@ -0,0 +1,10 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.commandline; + +import java.io.IOException; + +public class main { + public static void main(String args[]) throws IOException { + commandline cmd = new commandline(); + cmd.start(); + } +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/List.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/List.java Sun Feb 14 18:04:24 2016 +0900 @@ -132,7 +132,7 @@ @Override public String toString() { String pathString = "<"; - Iterator<T> iterator = reverseIterator(); + Iterator<T> iterator = iterator(); while (true) { pathString += iterator.next(); if (iterator.hasNext())
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/treemap/TreeMap.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/treemap/TreeMap.java Sun Feb 14 18:04:24 2016 +0900 @@ -72,7 +72,7 @@ @Override public boolean hasNext() { - return currentNode != null; + return currentNode != null && currentNode.isNotEmpty(); } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/NodePath.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/NodePath.java Sun Feb 14 18:04:24 2016 +0900 @@ -9,4 +9,5 @@ public NodePath tail(); public int size(); public Pair<Integer,NodePath> last(); + public NodePath addHead(int pos); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/impl/DefaultNodePath.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/impl/DefaultNodePath.java Sun Feb 14 18:04:24 2016 +0900 @@ -36,6 +36,12 @@ } @Override + public NodePath addHead(int pos) { + List<Integer> newPath = path.add(0,pos); + return new DefaultNodePath(newPath); + } + + @Override public Pair<Integer, NodePath> pop() { Integer head = path.head(); List<Integer> tail = path.deleteHead(); @@ -44,8 +50,8 @@ @Override public Pair<Integer, NodePath> last() { - Integer last = path.head(); - List<Integer> list = path.deleteHead(); + Integer last = path.tail(); + List<Integer> list = path.deleteLast(); return new Pair<>(last, new DefaultNodePath(list)); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/ParentIndex.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/ParentIndex.java Sun Feb 14 18:04:24 2016 +0900 @@ -1,10 +1,11 @@ package jp.ac.u_ryukyu.ie.cr.jungle.store.index; +import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren; -import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap; import java.util.Iterator; +import java.util.Optional; public class ParentIndex { @@ -18,8 +19,8 @@ return parentIndex.isEmpty(); } - public TreeNode get(TreeNode child) { - return parentIndex.get(child).get(); + public Optional<TreeNode> get(TreeNode child) { + return parentIndex.get(child); } public ParentIndex set(TreeNode parent ,TreeNode child) {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/traverser/InterfaceTraverser.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/traverser/InterfaceTraverser.java Sun Feb 14 18:04:24 2016 +0900 @@ -199,6 +199,9 @@ return null; } + public Iterator<TreeNode> find(final Query query) { + return find(query,null,null); + } public Iterator<TreeNode> find(final Query query, final String key, String searchValue) { @@ -236,7 +239,10 @@ TreeNode currentPair = matchNode; for (; nodeIterator.hasNext(); ) { matchNode = nextmatch(nodeIterator.next(), query); + if (matchNode != null) + return currentPair; } + matchNode = null; return currentPair; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/xml/reader/ReadXmlHandler.java Sun Feb 14 18:04:24 2016 +0900 @@ -0,0 +1,111 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.xml.reader; + +import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Pair; +import org.xml.sax.Attributes; +import org.xml.sax.helpers.DefaultHandler; + +import java.nio.ByteBuffer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ReadXmlHandler extends DefaultHandler { + private JungleTree tree; + private JungleTreeEditor editor; + private NodePath path; + private String elementName; + + public ReadXmlHandler(JungleTree tree) { + this.tree = tree; + this.editor = tree.getTreeEditor(); + this.path = new DefaultNodePath().add(-1); + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) { + Pair<Integer, NodePath> nodePair = path.last(); + path = nodePair.right(); + int num = nodePair.left() + 1; + + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, num); + if (either.isA()) { + // error + } + this.editor = either.b(); + + path = path.add(num); + + if (qName != "") { + elementName = qName; + Either<Error, JungleTreeEditor> newEither = editor.putAttribute(path, "element", + ByteBuffer.wrap(qName.getBytes())); + if (newEither.isA()) { + // error + } + this.editor = newEither.b(); + } + + if (attributes.getLength() != 0) { + for (int count = 0; attributes.getLength() > count; count++) { + String key = attributes.getLocalName(count); + String value = attributes.getValue(count); + ByteBuffer bValue = ByteBuffer.wrap(value.getBytes()); + Either<Error, JungleTreeEditor> newEither = editor.putAttribute(path, key, bValue); + if (newEither.isA()) { + // error + } + this.editor = newEither.b(); + } + } + path = path.add(-1); + } + + @Override + public void characters(char[] ch, int start, int length) { + String str = new String(ch, start, length); + Pattern pattern = Pattern.compile("\n"); + Matcher macher = pattern.matcher(str); + + if (!macher.find()) { + + String[] splitStrs = str.split(" "); + for (String splitStr : splitStrs) { + + Pair<Integer, NodePath> nodePair = path.last(); + NodePath onePath = nodePair.right(); + Either<Error, JungleTreeEditor> newEither = editor.putAttribute(onePath,elementName, + ByteBuffer.wrap(splitStr.getBytes())); + if (newEither.isA()) { + // error + } + this.editor = newEither.b(); + + } + } + } + + @Override + public void endElement(String namespaceURI, String localName, String qName) { + path = path.tail(); + } + + + + @Override + public void endDocument() { + Either<Error, JungleTreeEditor> either = editor.success(); + if (either.isA()) { + // error + } + } + + public JungleTree getTree() { + return tree; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/xml/reader/xmlReader.java Sun Feb 14 18:04:24 2016 +0900 @@ -0,0 +1,34 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.xml.reader; + +import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle; +import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.FileInputStream; + + +public class XmlReader { + public static void main(String args[]) { + String xmlPath = args[0]; + Jungle jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser())); + JungleTree tree = jungle.createNewTree("XMLTEST"); + new XmlReader().start(xmlPath,tree); + } + + public JungleTree start(String xmlPath, JungleTree tree) { + try { + SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); + SAXParser saxParser = saxParserFactory.newSAXParser(); + ReadXmlHandler readXmlHandler = new ReadXmlHandler(tree); + saxParser.parse(new FileInputStream(xmlPath), readXmlHandler); + return readXmlHandler.getTree(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } +} \ No newline at end of file
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/bbs/codesegment/StartBBSCodeSegment.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/bbs/codesegment/StartBBSCodeSegment.java Sun Feb 14 18:04:24 2016 +0900 @@ -7,7 +7,6 @@ import jp.ac.u_ryukyu.ie.cr.jungleNetwork.bbs.NetworkBulletinBoard; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.bbs.NetworkJungleBulletinBoard; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.bbs.RequestNumCheckServlet; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.bbs.ShowMessageWithTimeStampServlet; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -72,8 +71,8 @@ Servlet createBoard = new CreateBoardServlet(cassaBBS); Servlet editBoardMessage = new EditMessageServlet(cassaBBS); Servlet index = new ShowBoardsServlet(cassaBBS,createBoardPath,showBoardMessagePath); - // Servlet board = new ShowBoardMessageServlet(cassaBBS,createBoardMessagePath,editMessagePath); - Servlet board = new ShowMessageWithTimeStampServlet(cassaBBS,createBoardMessagePath,thp); + Servlet board = new ShowBoardMessageServlet(cassaBBS,createBoardMessagePath,editMessagePath); + //Servlet board = new ShowMessageWithTimeStampServlet(cassaBBS,createBoardMessagePath,thp); ServletHandler context = new ServletHandler(); context.addServletWithMapping(new ServletHolder(createBoardMessage),createBoardMessagePath);
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkNodePath.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkNodePath.java Sun Feb 14 18:04:24 2016 +0900 @@ -10,72 +10,80 @@ @Message public class NetworkNodePath implements NodePath { - LinkedList<Integer> path; + LinkedList<Integer> path; - public NetworkNodePath() { - path = new LinkedList<Integer>(); - path.add(-1); - } + public NetworkNodePath() { + path = new LinkedList<Integer>(); + path.add(-1); + } - public NetworkNodePath(NodePath _p) { - path = new LinkedList<Integer>(); - for (Integer pos : _p) { - path.add(pos); + public NetworkNodePath(NodePath _p) { + path = new LinkedList<Integer>(); + for (Integer pos : _p) { + path.add(pos); + } } - } + + private NetworkNodePath(LinkedList<Integer> _path) { + path = _path; + } - private NetworkNodePath(LinkedList<Integer> _path) { - path = _path; - } + @Override + public Iterator<Integer> iterator() { + return path.iterator(); + } - @Override - public Iterator<Integer> iterator() { - return path.iterator(); - } + @Override + public NetworkNodePath add(int _pos) { + LinkedList<Integer> newPath = copyPath(); + newPath.add(_pos); + return new NetworkNodePath(newPath); + } - @Override - public NetworkNodePath add(int _pos) { - LinkedList<Integer> newPath = copyPath(); - newPath.add(_pos); - return new NetworkNodePath(newPath); - } + @Override + public NodePath addHead(int pos) { + return null; //Linked Listを後で直す + // List<Integer> newPath = path.add(0, pos); + // return new DefaultNodePath(newPath); + } + - @Override - public Pair<Integer, NodePath> pop() { - LinkedList<Integer> cPath = copyPath(); - int e = cPath.getFirst(); - cPath.remove(); - return new Pair<Integer, NodePath>(e, new NetworkNodePath(cPath)); - } + @Override + public Pair<Integer, NodePath> pop() { + LinkedList<Integer> cPath = copyPath(); + int e = cPath.getFirst(); + cPath.remove(); + return new Pair<Integer, NodePath>(e, new NetworkNodePath(cPath)); + } - @Override - public int size() { - return path.size(); - } - - private LinkedList<Integer> copyPath() { - LinkedList<Integer> newPath = new LinkedList<Integer>(); - for (Integer i : path) { - newPath.add(i); + @Override + public int size() { + return path.size(); } - return newPath; - } - @Override - public String toString() { - return path.toString(); - } + private LinkedList<Integer> copyPath() { + LinkedList<Integer> newPath = new LinkedList<Integer>(); + for (Integer i : path) { + newPath.add(i); + } + return newPath; + } - @Override - public NodePath tail() { - path.removeLast(); - return new NetworkNodePath(path); - } + @Override + public String toString() { + return path.toString(); + } - @Override - public Pair<Integer, NodePath> last() { - Integer num = path.removeLast(); - return new Pair<Integer, NodePath>(num, new NetworkNodePath(path)); - } + @Override + public NodePath tail() { + path.removeLast(); + return new NetworkNodePath(path); + } + + @Override + public Pair<Integer, NodePath> last() { + Integer num = path.removeLast(); + return new Pair<Integer, NodePath>(num, new NetworkNodePath(path)); + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungleTree.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungleTree.java Sun Feb 14 18:04:24 2016 +0900 @@ -11,7 +11,9 @@ import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren; import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; @@ -19,6 +21,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.jungle.util.GetOldTreeError; +import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; public class PersistentJungleTree implements JungleTree { @@ -118,4 +121,24 @@ bufferSize = _bufferSize; } + @Override + public NodePath getNodePath(TreeNode node) { + ParentIndex parentIndex = getParentIndex(); + DefaultNodePath path = new DefaultNodePath(); + Optional<TreeNode> parentOp = parentIndex.get(node); + while(parentOp.isPresent()) { + TreeNode parent = parentOp.get(); + TreeNodeChildren children = parent.getChildren(); + int num = 0; + for (TreeNode child : children) { + if (child == node) + break; + num++; + } + path = path.add(num); + node = parent; + parentOp = parentIndex.get(node); + } + return path; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/SingletonMessageFromAlice.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/SingletonMessageFromAlice.java Sun Feb 14 18:04:24 2016 +0900 @@ -1,11 +1,10 @@ package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent; -import alice.codesegment.SingletonMessage; import org.msgpack.MessagePack; public class SingletonMessageFromAlice { public static MessagePack getInstance(){ - return SingletonMessage.getInstance(); + return new MessagePack(); } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTree.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTree.java Sun Feb 14 18:04:24 2016 +0900 @@ -10,7 +10,9 @@ import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren; import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; @@ -18,6 +20,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.jungle.util.GetOldTreeError; +import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; public class NetworkDefaultJungleTree implements JungleTree { @@ -123,5 +126,24 @@ bufferSize = _bufferSize; } - + @Override + public NodePath getNodePath(TreeNode node) { + ParentIndex parentIndex = getParentIndex(); + DefaultNodePath path = new DefaultNodePath(); + Optional<TreeNode> parentOp = parentIndex.get(node); + while(parentOp.isPresent()) { + TreeNode parent = parentOp.get(); + TreeNodeChildren children = parent.getChildren(); + int num = 0; + for (TreeNode child : children) { + if (child == node) + break; + num++; + } + path = path.add(num); + node = parent; + parentOp = parentIndex.get(node); + } + return path; + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/impl/node/GetNodePath.java Sun Feb 14 18:04:24 2016 +0900 @@ -0,0 +1,80 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.impl.node; + + +import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle; +import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.util.*; +import junit.framework.Assert; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.Iterator; + +public class GetNodePath { + @Test + public void getNodePathTest(){ + Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTreeEditor(new DefaultTraverser())); + jungle.createNewTree("tree"); + JungleTree tree = jungle.getTreeByName("tree"); + JungleTreeEditor editor = tree.getTreeEditor(); + + editor = createTree(editor,0,3,new DefaultNodePath()); + + editor.success(); + InterfaceTraverser traverser = tree.getTraverser(true); + + Iterator<TreeNode> iterator = traverser.find((TreeNode node) -> { // no index find + String value = node.getAttributes().getString(key); + if (value == null) + return false; + if (value.equals("<-1,2,0>")) + return true; + return false; + }); + + Assert.assertTrue(iterator.hasNext()); + TreeNode node = iterator.next(); + NodePath path = tree.getNodePath(node); + String pathtring = path.toString(); + Assert.assertEquals("<-1,2,0>",pathtring); + } + + public static String key = "KEY"; + public static String indexKey = "INDEXKEY"; + public static DefaultTreeNode factory = new DefaultTreeNode(); + + public JungleTreeEditor createTree(JungleTreeEditor editor, int _curY, int _maxHeight, NodePath path) { + + if (_curY == _maxHeight) { + return editor; + } + for (int i = 0; i < 3; i++) { + + Either<jp.ac.u_ryukyu.ie.cr.jungle.util.Error, JungleTreeEditor> either = editor.addNewChildAt(path, i); + if (either.isA()) + Assert.fail(); + editor = either.b(); + String value = path.add(i).toString(); + either = editor.putAttribute(path.add(i), key, ByteBuffer.wrap(value.getBytes())); + if (either.isA()) + Assert.fail(); + editor = either.b(); + String value2 = value + "+ index"; + either = editor.putAttribute(path.add(i), indexKey, ByteBuffer.wrap(value2.getBytes())); + if (either.isA()) + Assert.fail(); + editor = either.b(); + editor = createTree(editor, _curY + 1, _maxHeight, path.add(i)); + } + return editor; + } +}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/index/parentIndexTest.java Sun Jan 24 20:16:56 2016 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/index/parentIndexTest.java Sun Feb 14 18:04:24 2016 +0900 @@ -41,7 +41,7 @@ for (int count = 99; count >= 0; count--) { String attribute = node.getAttributes().getString("KEY"); Assert.assertEquals(attribute, "data" + count); - node = parentIndex.get(node); + node = parentIndex.get(node).get(); } } }