Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 238:15235a18202a Implementation_of_communication
commandLine insert
author | tatsuki |
---|---|
date | Mon, 15 Feb 2016 00:17:36 +0900 |
parents | 33c93f162389 |
children | 2fc1c4f19c02 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/commandline.java |
diffstat | 1 files changed, 103 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/commandline.java Sun Feb 14 18:04:24 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/commandline.java Mon Feb 15 00:17:36 2016 +0900 @@ -3,15 +3,21 @@ 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.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; +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.xml.reader.XmlReader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.ByteBuffer; import java.util.Iterator; import java.util.LinkedList; @@ -45,9 +51,7 @@ @Override public boolean hasNext() { - if (count < split.length) - return true; - return false; + return count < split.length; } @Override @@ -61,12 +65,86 @@ while (iterator.hasNext()) { switch (iterator.next()) { - case "select": + case "select": //検索 select(iterator); + break; + case "insert"://Nodeの追加 insert into treeName path <Path> key value attribute + insert(iterator); + break; + case "update": + break; } } } + private void insert(Iterator<String> iterator) { + DefaultNodePath path = new DefaultNodePath(); + String treeName = ""; + while (iterator.hasNext()) { + switch (iterator.next()) { + case "into": + if (iterator.hasNext()) + treeName = iterator.next(); + continue; + case "path": + String pathStr = ""; + if (iterator.hasNext()) + pathStr = iterator.next(); + pathStr = pathStr.substring(1, pathStr.length() - 1); // <>を取り取り除く + String[] nums = pathStr.split(","); + for (String num : nums) { + if (num.equals("-1")) + continue; + path = path.add(new Integer(num)); + } + continue; + case "keys": + + } + break; + } + + LinkedList<String> keys = new LinkedList<>(); + while (iterator.hasNext()) { + String str = iterator.next(); + if (str.equals("values")) + break; + keys.add(str);//表示するkeyを表示 + } + + LinkedList<String> values = new LinkedList<>(); + while (iterator.hasNext()) { + String str = iterator.next(); + values.add(str);//表示するkeyを表示 + } + + JungleTree tree = jungle.getTreeByName(treeName); + if (tree == null) + tree = jungle.createNewTree(treeName); + JungleTreeEditor editor = tree.getTreeEditor(); + Iterator<String> keyIterator = keys.iterator(); + Iterator<String> valueIterator = values.iterator(); + NodePath parentPath = path.last().right(); // Nodeの追加 + int num = path.last().left(); + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(parentPath, num); + if (either.isA()) { + System.out.println("faild");// Nodeの追加に失敗したらその場でinsert終了 + return; + } + editor = either.b(); + while (keyIterator.hasNext()) {//値の追加 + String key = keyIterator.next(); + String value = valueIterator.next(); + either = editor.putAttribute(path, key, ByteBuffer.wrap(value.getBytes())); + if (either.isA())//Nodeの追加に失敗したらその場でinsert終了 + return; + editor = either.b(); + } + editor.success(); + if (either.isA())//Nodeの追加に失敗したらその場でinsert終了 + System.out.println("faild"); + } + private void select(Iterator<String> iterator) { //取得するkeyの取得 @@ -82,15 +160,26 @@ LinkedList<String> treeNames = new LinkedList<>(); while (iterator.hasNext()) { String str = iterator.next(); - if (str.equals("where")) - where(iterator, treeNames); + // if (str.equals("where")) + // where(iterator,keys, treeNames); treeNames.add(str);//木の名前を取得 } Iterator<String> treeIterator = treeNames.iterator(); while (treeIterator.hasNext()) { - String treeName = treeIterator.next(); + String[] split = treeIterator.next().split(":"); // TreeName:idで表記されているのでTreename と Idに分割する + String treeName = split[0];//TreeNameの取得 JungleTree tree = jungle.getTreeByName(treeName); + + if (split.length == 2) { + Either<Error, JungleTree> either = jungle.getTreeByName(treeName).getOldTree(new Long(split[1])); + if (either.isA()) { + tree = jungle.getTreeByName(treeName); + } else { + tree = either.b(); + } + } + InterfaceTraverser traverser = tree.getTraverser(true); Iterator<TreeNode> nodeIterator = traverser.find((TreeNode node) -> { //この場合条件がないので探索する木のNodeを全て取得する @@ -99,7 +188,8 @@ while (nodeIterator.hasNext()) { TreeNode node = nodeIterator.next(); - + System.out.println("---------------------------------------------------------------------"); + System.out.println("path = " + tree.getNodePath(node)); Iterator<String> nodeKeys; if (keys.getFirst().equals("*")) nodeKeys = node.getAttributes().getKeys(); @@ -116,11 +206,10 @@ } } - private void where(Iterator<String> iterator, LinkedList<String> treeNames) { - while (iterator.hasNext()) { - String str = iterator.next(); +// private void where(Iterator<String> iterator,LinkedList<String> keys ,LinkedList<String> treeNames) { +// LinkedList<String> conditions = new LinkedList<>(); +// while (iterator.hasNext()) { +// String str = iterator.next(); +// } - } - - } }