changeset 241:5a301161e36a Implementation_of_communication

change commandline sql -> nosql
author tatsuki
date Mon, 15 Feb 2016 14:04:51 +0900
parents 648df7d29d13
children 30c0ca7d36a5
files src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/commandline.java
diffstat 1 files changed, 134 insertions(+), 151 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/commandline.java	Mon Feb 15 00:47:19 2016 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/commandline/commandline.java	Mon Feb 15 14:04:51 2016 +0900
@@ -23,6 +23,10 @@
 import java.util.Iterator;
 import java.util.LinkedList;
 
+/**
+ * 就活でjungleを見せるために作ったもの
+ * それ以降アップデートする予定はない
+ */
 
 public class commandline {
     Jungle jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser()));
@@ -43,8 +47,7 @@
     }
 
     private void parse(String sql) {
-        Command cmd = Command.none;
-        String[] split = sql.split(" ");
+        String[] split = sql.split("\\.");
         Iterator<String> iterator = new Iterator<String>() {
             private int count = 0;
 
@@ -61,88 +64,80 @@
             }
         };
 
+        String treeName = "";
+        if (iterator.hasNext())
+            treeName = iterator.next();
 
-        while (iterator.hasNext()) {
-            switch (iterator.next()) {
-                case "select": //検索 select keyName from treeName
-                    select(iterator);
+        String[] cmd = new String[2];
+        if (iterator.hasNext()) {
+            String str = iterator.next();
+            cmd = str.split("\\(");
+        }
+
+        if (cmd[1] != null) {
+            switch (cmd[0]) {
+                case "find": //検索  treeName.find(検索するkey 例 id) 絞込はまだ treeNameは:revisionで過去のTreeにアクセス可能
+                    String condition = cmd[1].replace(")", "").replace(" ", "");  // )を取り除く
+                    find(treeName, condition, iterator);
                     break;
-                case "insert"://Nodeの追加 insert into treeName path <Path> keys key values attribute
-                    insert(iterator);
+                case "insert"://Nodeの追加 treeName.insert(<path>,key:value,key:value………)
+                    String value = cmd[1].replace(")", "").replace(" ", "");  // )を取り除く
+                    insert(treeName, value, iterator);
                     break;
-                case "update"://attribute等のupdate treeName path <Path> keys key values attribute
-                    update(iterator);
+                case "insertAttribute"://attribute等のupdate treeName.insertAttribute(path,key:value, ………) treeName path <Path> keys key values attribute
+                    value = cmd[1].replace(")", "").replace(" ", "");  // )を取り除く
+                    update(treeName, value, iterator);
                     break;
-                case "inport"://attribute等のupdate treeName path <Path> keys key values attribute
-                    inport(iterator); //xmlをinportする inport treeName xmlまでの絶対path
+                case "import"://attribute等のupdate treeName path <Path> keys key values attribute
+                    String url = cmd[1].replace(")", "").replace(" ", ""); // )を取り除く
+                    dataImport(treeName, url); //xmlをimportする treename.import(url)
                     break;
             }
         }
     }
 
-    private void inport(Iterator<String> iterator) {
+    private void dataImport(String treeName, String url) {
         //テストxmlの読み込み 後で消す
-        String treeName = "";
-        String path = "";
-        if (iterator.hasNext())
-            treeName = iterator.next();
-        if (iterator.hasNext())
-            path = iterator.next();
-        if (treeName.equals("") || path.equals("")) {
+        if (treeName.equals("") || url.equals("")) {
             System.out.println("faild");
             return;
         }
         JungleTree tree = jungle.createNewTree(treeName);
         if (tree == null)
             tree = jungle.getTreeByName(treeName);
-        new XmlReader().start(path, tree);
+        new XmlReader().start(url + ".xml", tree);
     }
 
-    private void update(Iterator<String> iterator) {
+    private void update(String treeName, String args, Iterator<String> iterator)  {
         {
-            DefaultNodePath path = new DefaultNodePath();
-            String treeName = "";
-            if (iterator.hasNext())
-                treeName = iterator.next();
-            else {
-                System.out.println("faild");
+            String[] split = args.split(">");//,で分割するとpathがおかしくなるので>で分割することでpathとkey:valueの組み合わせに分ける
+            if (split.length < 2) {
                 return;
             }
-            while (iterator.hasNext()) {
-                switch (iterator.next()) {
-                    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 " ":
-                        continue;
-                    case "keys":
-
-                }
-                break;
+            DefaultNodePath path = new DefaultNodePath();
+            String pathStr = "";
+            pathStr = split[0]; // pathを取得する
+            pathStr = pathStr.substring(1, pathStr.length()); // <を取り取り除く
+            String[] nums = pathStr.split(",");
+            for (String num : nums) {
+                if (num.equals("-1"))
+                    continue;
+                path = path.add(new Integer(num));
             }
 
+            split = split[1].split(",");//ここで後ろのkey:valueの組み合わせを,で分割する
+            LinkedList<String> values = new LinkedList<>();
             LinkedList<String> keys = new LinkedList<>();
-            while (iterator.hasNext()) {
-                String str = iterator.next();
-                if (str.equals("values"))
-                    break;
-                keys.add(str);//表示するkeyを表示
+            for (String s : split) {
+                String[] pair = s.split(":");
+                if (pair.length != 2)
+                    continue;
+                String k = pair[0];
+                String v = pair[1];
+                keys.add(k);//表示するkeyを表示
+                values.add(v);
             }
 
-            LinkedList<String> values = new LinkedList<>();
-            while (iterator.hasNext()) {
-                String str = iterator.next();
-                values.add(str);//表示するkeyを表示
-            }
 
             JungleTree tree = jungle.getTreeByName(treeName);
             if (tree == null)
@@ -155,57 +150,43 @@
                 String key = keyIterator.next();
                 String value = valueIterator.next();
                 either = editor.putAttribute(path, key, ByteBuffer.wrap(value.getBytes()));
-                if (either.isA())//Nodeの追加に失敗したらその場でinsert終了
+                if (either.isA())//aattributeの追加に失敗したらその場でupdate終了
                     return;
                 editor = either.b();
             }
             editor.success();
-            if (either.isA())//Nodeの追加に失敗したらその場でinsert終了
+            if (either.isA())//commitに失敗したらその場でinsert終了
                 System.out.println("faild");
         }
     }
 
-    private void insert(Iterator<String> iterator) {
+    private void insert(String treeName, String args, Iterator<String> iterator) {
+        String[] split = args.split(">");//,で分割するとpathがおかしくなるので>で分割することでpathとkey:valueの組み合わせに分ける
+        if (split.length < 2) {
+            return;
+        }
         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 " ":
-                    continue;
-                case "keys":
-
-            }
-            break;
+        String pathStr = "";
+        pathStr = split[0]; // pathを取得する
+        pathStr = pathStr.substring(1, pathStr.length()); // <を取り取り除く
+        String[] nums = pathStr.split(",");
+        for (String num : nums) {
+            if (num.equals("-1"))
+                continue;
+            path = path.add(new Integer(num));
         }
 
+        split = split[1].split(",");//ここで後ろのkey:valueの組み合わせを,で分割する
+        LinkedList<String> values = new LinkedList<>();
         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を表示
+        for (String s : split) {
+            String[] pair = s.split(":");
+            if (pair.length != 2)
+                continue;
+            String k = pair[0];
+            String v = pair[1];
+            keys.add(k);//表示するkeyを表示
+            values.add(v);
         }
 
         JungleTree tree = jungle.getTreeByName(treeName);
@@ -236,64 +217,66 @@
     }
 
 
-    private void select(Iterator<String> iterator) {
+    private void find(String treeName, String condition, Iterator<String> iterator) { //conditionには条件が入るが今はkeyのみしか入らない。
         //取得するkeyの取得
-        LinkedList<String> keys = new LinkedList<>();
-        while (iterator.hasNext()) {
-            String str = iterator.next();
-            if (str.equals("from"))
-                break;
-            keys.add(str);//表示するkeyを表示
+        String[] keys = condition.split(",");
+        String[] split = treeName.split(":"); // TreeName:idで表記されているのでTreename と Idに分割する
+        treeName = split[0];//TreeNameの取得
+        JungleTree tree = jungle.getTreeByName(treeName);
+        if (tree == null) {
+            System.out.println("tree no found");
+            return;
+        }
+
+        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を全て取得する
+            return true;
+        });
+
+        while (nodeIterator.hasNext()) {
+            TreeNode node = nodeIterator.next();
+            System.out.println("---------------------------------------------------------------------");
+            System.out.println("path = " + tree.getNodePath(node));
+            Iterator<String> nodeKeys;
+            if (keys[0].equals(""))
+                nodeKeys = node.getAttributes().getKeys();
+            else {
+                nodeKeys = new Iterator<String>() {
+                    private int count = 0;
+
+                    @Override
+                    public boolean hasNext() {
+                        return count < keys.length;
+                    }
+
+                    @Override
+                    public String next() {
+                        String tmp = keys[count];
+                        count++;
+                        return tmp;
+                    }
+                };
+                ;
+            }
+            while (nodeKeys.hasNext()) {
+                String key = nodeKeys.next();
+                String value = node.getAttributes().getString(key);
+                if (value != null)
+                    System.out.println(key + "=" + value + ",");
+            }
         }
 
 
-        LinkedList<String> treeNames = new LinkedList<>();
-        while (iterator.hasNext()) {
-            String str = iterator.next();
-            //  if (str.equals("where"))
-            //      where(iterator,keys, treeNames);
-            treeNames.add(str);//木の名前を取得
-        }
-
-        Iterator<String> treeIterator = treeNames.iterator();
-        while (treeIterator.hasNext()) {
-            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を全て取得する
-                return true;
-            });
-
-            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();
-                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> keys ,LinkedList<String> treeNames) {