changeset 123:495ac60d7f5f

Modified JungleUpdater
author one
date Fri, 27 Dec 2013 20:13:37 +0900
parents cef245f71053
children dacfa7eba841
files src/main/java/alice/jungle/transaction/JungleUpdater.java src/main/java/app/bbs/BullentInBoardJungleManager.java src/main/java/app/bbs/JungleManager.java src/main/java/app/bbs/NetworkJungleBulletinBoard.java src/main/java/app/bbs/codesegment/LogUpdateCodeSegment.java
diffstat 5 files changed, 112 insertions(+), 146 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/alice/jungle/transaction/JungleUpdater.java	Fri Dec 27 19:17:07 2013 +0900
+++ b/src/main/java/alice/jungle/transaction/JungleUpdater.java	Fri Dec 27 20:13:37 2013 +0900
@@ -20,7 +20,9 @@
 		JungleTreeEditor editor = _editor;
 		Either<Error, JungleTreeEditor> either = null;
 		for (TreeOperation op : _log) { 
-			either = _edit(editor, op);
+			NodePath path = op.getNodePath();
+			NodeOperation nodeOp = op.getNodeOperation();
+			either = _edit(editor, path, nodeOp, nodeOp.getPosition());
 			if(either.isA()) {
 				return either;
 			}
@@ -29,13 +31,29 @@
 		return either;
 	}
 	
-	private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
-			TreeOperation op) {
-		NodePath path = op.getNodePath();
-		NodeOperation nodeOp = op.getNodeOperation();
-		int pos = nodeOp.getPosition();
-		Command c = nodeOp.getCommand();
-		String key = "";
+	/* 
+	 * This method decide node position by param. 
+	 */
+	public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,Iterable<TreeOperation> _log, int pos) {
+		JungleTreeEditor editor = _editor;
+		Either<Error, JungleTreeEditor> either = null;
+		for (TreeOperation op : _log) { 
+			NodePath path = op.getNodePath();
+			NodeOperation nodeOp = op.getNodeOperation();
+			either = _edit(editor, path, nodeOp, pos);
+			if(either.isA()) {
+				return either;
+			}
+			editor = either.b();
+		}
+		return either;
+	}
+	
+	
+	private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor, NodePath path, 
+				NodeOperation nodeOp, int pos) {
+			String key = "";
+			Command c = nodeOp.getCommand();
 		switch (c) {
 		case PUT_ATTRIBUTE:
 			key = nodeOp.getKey();
@@ -50,8 +68,6 @@
 			return editor.deleteChildAt(path, 0);
 		}
 		return null;
-	}	
-	
-	
-	
+	}
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/app/bbs/BullentInBoardJungleManager.java	Fri Dec 27 20:13:37 2013 +0900
@@ -0,0 +1,81 @@
+package app.bbs;
+
+import java.nio.ByteBuffer;
+
+import alice.jungle.operations.NetworkTreeOperationLog;
+import alice.jungle.transaction.JungleUpdater;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
+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.Node;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
+
+public class BullentInBoardJungleManager {
+	private static BullentInBoardJungleManager instance = new BullentInBoardJungleManager();
+	private Jungle jungle;
+
+	private BullentInBoardJungleManager() {
+		jungle = new DefaultJungle(null,"default",new DefaultTreeEditor(new DefaultTraverser()));
+	}
+	
+	public static BullentInBoardJungleManager getInstantce() {
+		return instance;
+	}
+	
+	public static void setJungle(Jungle _j) {
+		instance.jungle = _j;
+	}
+	
+	public static Jungle getJungle() {
+		return instance.jungle;
+	}
+	
+	public static JungleTree createNewTree(String name) {
+		return instance.jungle.createNewTree(name);	
+	}
+
+	public static Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) {
+		String treeName = netLog.getTreeName();
+		Jungle jungle = BullentInBoardJungleManager.getJungle(); 
+		if (jungle.getTreeByName(treeName) == null) {
+			if(null == jungle.createNewTree(treeName)){
+				throw new IllegalStateException();
+			}
+		}
+		JungleTree tree = jungle.getTreeByName(treeName);
+		JungleTreeEditor editor = tree.getLocalTreeEditor();
+
+		int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp());
+		Either<Error, JungleTreeEditor> either = JungleUpdater.edit(editor, netLog, pos);
+		if(either.isA()) {
+			throw new IllegalStateException();
+		}
+		editor = either.b();
+		either = editor.success();
+		if(either.isA()) {
+			throw new IllegalStateException();
+		}
+		return either;
+	}
+	
+	private static int calculatePosition(Node node, long newNodeTimeStamp) {
+		int count = 0;
+		long childTimeStamp = 0;
+		for(Node n : node.getChildren()) {
+			ByteBuffer timestamp = n.getAttributes().get("timestamp");
+			if(timestamp == null) {
+				return count;
+			}
+			childTimeStamp = timestamp.getLong();
+			if (newNodeTimeStamp < childTimeStamp) {
+				break;
+			}
+			count++;
+		}
+		return count;
+	}
+}
--- a/src/main/java/app/bbs/JungleManager.java	Fri Dec 27 19:17:07 2013 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,131 +0,0 @@
-package app.bbs;
-
-import java.nio.ByteBuffer;
-import java.util.Iterator;
-
-import alice.jungle.operations.NetworkTreeOperationLog;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
-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.Node;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
-
-public class JungleManager {
-	private static JungleManager instance = new JungleManager();
-	private Jungle jungle;
-	private static int NOT_CHANGE_POSITION = 0;
-
-	private JungleManager() {
-		jungle = new DefaultJungle(null,"default",new DefaultTreeEditor(new DefaultTraverser()));
-	}
-	
-	public static JungleManager getInstantce() {
-		return instance;
-	}
-	
-	public static void setJungle(Jungle _j) {
-		instance.jungle = _j;
-	}
-	
-	public static Jungle getJungle() {
-		return instance.jungle;
-	}
-	
-	public static JungleTree createNewTree(String name) {
-		return instance.jungle.createNewTree(name);	
-	}
-
-	public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) {
-		JungleTreeEditor editor = _editor;
-		Either<Error, JungleTreeEditor> either = null;
-		for (TreeOperation op : _log) {
-			either = _edit(editor, op, pos);
-			if(either.isA()) {
-				return either;
-			}
-			editor = either.b();
-		}
-		return either;
-	}
-	
-	private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
-			TreeOperation op, int _pos) {
-		NodePath path = new DefaultNodePath();
-		NodeOperation nodeOp = op.getNodeOperation();
-		int pos = _pos;
-		if (_pos == NOT_CHANGE_POSITION ) {
-			pos = nodeOp.getPosition();
-		}
-		Command c = nodeOp.getCommand();
-		String key = "";
-		switch (c) {
-		case PUT_ATTRIBUTE:
-			path = op.getNodePath();
-			key = nodeOp.getKey();
-			ByteBuffer value = nodeOp.getValue();
-			return editor.putAttribute(path, key, value);
-		case DELETE_ATTRIBUTE:
-			key = nodeOp.getKey();
-			return editor.deleteAttribute(path, key);
-		case APPEND_CHILD:
-			return editor.addNewChildAt(path, pos);
-		case DELETE_CHILD:
-			return editor.deleteChildAt(path, 0);
-		}
-		return null;
-	}
-	
-	public static Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) {
-		String treeName = netLog.getTreeName();
-		Jungle jungle = JungleManager.getJungle(); 
-		if (jungle.getTreeByName(treeName) == null) {
-			if(null == jungle.createNewTree(treeName)){
-				throw new IllegalStateException();
-			}
-		}
-		JungleTree tree = jungle.getTreeByName(treeName);
-		JungleTreeEditor editor = tree.getLocalTreeEditor();
-
-		//		int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp());
-		int pos = 0;
-		Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, netLog, pos);
-		if(either.isA()) {
-			throw new IllegalStateException();
-		}
-		editor = either.b();
-		either = editor.success();
-		if(either.isA()) {
-			throw new IllegalStateException();
-		}
-		return either;
-	}
-	
-	private static int calculatePosition(Node node, long newNodeTimeStamp) {
-		int count = 0;
-		long childTimeStamp = 0;
-		for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) {
-			Node n = iter.next();
-			if(n.getAttributes().get("timestamp") == null) {
-				return NOT_CHANGE_POSITION;
-			}
-			if(n.getAttributes().get("timestamp") != null) {
-				childTimeStamp = n.getAttributes().get("timestamp").getLong();
-				if (newNodeTimeStamp < childTimeStamp) {
-					break;
-				}
-			}
-			count++;
-		}
-		return count;
-	}
-}
--- a/src/main/java/app/bbs/NetworkJungleBulletinBoard.java	Fri Dec 27 19:17:07 2013 +0900
+++ b/src/main/java/app/bbs/NetworkJungleBulletinBoard.java	Fri Dec 27 20:13:37 2013 +0900
@@ -37,7 +37,7 @@
 	{
 		journal = new PersistentJournal();	
 		jungle = new NetworkDefaultJungle(journal, _uuid,new DefaultTreeEditor(new DefaultTraverser()));	
-		JungleManager.setJungle(jungle);
+		BullentInBoardJungleManager.setJungle(jungle);
 	}
 	
 	public void init() throws IOException {
--- a/src/main/java/app/bbs/codesegment/LogUpdateCodeSegment.java	Fri Dec 27 19:17:07 2013 +0900
+++ b/src/main/java/app/bbs/codesegment/LogUpdateCodeSegment.java	Fri Dec 27 20:13:37 2013 +0900
@@ -8,7 +8,7 @@
 import alice.datasegment.CommandType;
 import alice.datasegment.Receiver;
 import alice.jungle.operations.NetworkTreeOperationLog;
-import app.bbs.JungleManager;
+import app.bbs.BullentInBoardJungleManager;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
@@ -34,7 +34,7 @@
 		@SuppressWarnings("unchecked")
 		List<String> list = clist.asClass(List.class);
 		if (!log.from.equals("local")) {
-			Either<Error, JungleTreeEditor> either = JungleManager.update(netLog);
+			Either<Error, JungleTreeEditor> either = BullentInBoardJungleManager.update(netLog);
 			if(either.isA()) {
 				/* Should throw after new LogUpdateCodeSegment */
 				throw new IllegalStateException();