diff src/test/java/alice/jungle/log/example/FindMatrixTest.java @ 180:4493dd7692ce

add FindMatrixTest
author tatsuki
date Mon, 15 Sep 2014 14:04:22 +0900
parents
children bf08205d5c5f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/alice/jungle/log/example/FindMatrixTest.java	Mon Sep 15 14:04:22 2014 +0900
@@ -0,0 +1,107 @@
+package alice.jungle.log.example;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
+import fj.Ord;
+import fj.data.List;
+import fj.data.TreeMap;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
+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.core.Children;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeAttributes;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultJungleTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.InterfaceTraverser;
+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.Query;
+import junit.framework.TestCase;
+import alice.jungle.operations.NetworkNodePath;
+import alice.jungle.persistent.PersistentJournal;
+import alice.jungle.persistent.PersistentJungle;
+import alice.jungle.transaction.JungleUpdater;
+import app.bbs.NetworkBulletinBoard;
+import app.bbs.NetworkJungleBulletinBoard;
+
+public class FindMatrixTest extends TestCase {
+	
+	public class SearchQuery implements Query{
+		
+		private String key;
+		private String searchAttribute;
+		
+		public SearchQuery (String key, String searchAttribute){
+			this.key = key;
+			this.searchAttribute = searchAttribute;
+		}
+		
+		@Override
+		public boolean condition(TreeNode _node) {
+			ByteBuffer attribute = _node.getAttributes().get(key);
+			if(attribute != null){
+				byte[] byteAttribute = attribute.array();
+				String str = new String(byteAttribute);
+				System.out.println(str);
+				return str.equals(searchAttribute);
+			}
+			return false;
+		}
+
+		@Override
+		public List<Pair<String, String>> indexCondition() {
+			// TODO Auto-generated method stub
+			return null;
+		}
+		
+	};
+	
+	public static void main(String[] args) throws IOException {
+		PersistentJournal journal1 = new PersistentJournal();
+		journal1.setInputFile(new File("./log/1410589309338.log"));
+		journal1.setOutputFile(new File("./log/test.log"));
+		Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTreeEditor(new DefaultTraverser()));
+		ChangeListReader reader = journal1.getReader();
+		int roopCount = 0;
+		String treeName = null;
+		for (ChangeList chList : reader) {
+			roopCount++;
+			treeName = chList.getTreeName();
+			JungleTree tree2 = jungle2.getTreeByName(treeName);
+			if(tree2 == null) {
+				tree2 = jungle2.createNewTree(treeName);
+			}
+			JungleTreeEditor editor2 = tree2.getTreeEditor();
+			Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList);
+			assertFalse(either2.isA());
+			editor2 = either2.b();
+			editor2.success();
+		}
+		
+		
+		JungleTree tree2 = jungle2.getTreeByName(treeName);
+		TreeNode root = tree2.getRootNode();
+		TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = TreeMap.empty(Ord.stringOrd);
+	    DefaultTraverser traverser = new DefaultTraverser();
+	    DefaultTreeEditor treeEditor = new DefaultTreeEditor(traverser);
+	    JungleTreeEditor editor = new DefaultJungleTreeEditor(root,null,treeEditor,null);
+		InterfaceTraverser ifTraverser = new  InterfaceTraverser(root,index,editor);
+		String key = "mes";
+		String attribute = "株式会社フタコ放送";
+		
+		Query query = new FindMatrixTest().new SearchQuery(key, attribute);
+		Iterator<Pair<TreeNode, NodePath>> searchNode = ifTraverser.find(query);
+		TreeNode node = searchNode.next().left();
+		byte[] nodeAttibute = node.getAttributes().get(key).array();
+		System.out.println(new String(nodeAttibute));
+	}
+}