diff src/jungle/test/bbs/JungleManager.java @ 59:8a532ca5df80

refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
author one
date Mon, 15 Jul 2013 10:59:53 +0900
parents ccfe9b5e8f11
children ebf42371454b
line wrap: on
line diff
--- a/src/jungle/test/bbs/JungleManager.java	Mon Jul 15 10:13:34 2013 +0900
+++ b/src/jungle/test/bbs/JungleManager.java	Mon Jul 15 10:59:53 2013 +0900
@@ -1,20 +1,27 @@
 package jungle.test.bbs;
 
+import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.Iterator;
 
 import alice.jungle.core.NetworkDefaultJungle;
+import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
 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.DefaultTreeOperationLog;
 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.DefaultEither;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
 
@@ -38,45 +45,7 @@
 		return jm.jungle.createNewTree(name);		
 	}
 
-	
-	public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log) {
-		JungleTreeEditor editor = _editor;
-		Either<Error, JungleTreeEditor> either = null;
-		for (TreeOperation op : _log) { 
-			either = _edit(editor, op);
-			if(either.isA()) {
-				return either;
-			}
-			editor = either.b();
-		}
-		return either;
-	}
-	
-	private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
-			TreeOperation op) {
-		NodePath path = op.getNodePath();
-		NodeOperation nodeOp = op.getNodeOperation();
-		Command c = nodeOp.getCommand();
-		String key = "";
-		switch (c) {
-		case PUT_ATTRIBUTE:
-			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, 0);
-		case DELETE_CHILD:
-			return editor.deleteChildAt(path, 0);
-		}
-		return null;
-	}
-	
-	
 	public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) {
-		System.out.println("--path editor--");
 		JungleTreeEditor editor = _editor;
 		Either<Error, JungleTreeEditor> either = null;
 		for (TreeOperation op : _log) { 
@@ -111,6 +80,53 @@
 		}
 		return null;
 	}
-
 	
+	public static Either<Error, JungleTreeEditor> update(DefaultTreeOperationLogContainer container) {
+		DefaultTreeOperationLog log = null;
+		try {
+			log = container.convert();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		String treeName = container.getTreeName();
+		if (JungleManager.getJungle().getTreeByName(treeName) == null) {
+			if(null == JungleManager.getJungle().createNewTree(treeName)){
+				throw new IllegalStateException();
+			}
+		}
+		JungleTree tree = JungleManager.getJungle().getTreeByName(treeName);
+		JungleTreeEditor editor = tree.getTreeEditor();
+		int pos = checkTimeStamp(tree.getRootNode(), container.getTimeStamp(), container.getPosition());
+		Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, log, pos);
+		if(either.isA()) {
+			throw new IllegalStateException();
+		}
+		editor = either.b();
+		either = editor.success();
+		if(either.isA()) {
+			throw new IllegalStateException();			
+		}
+		return either;
+	}
+	
+	
+	
+	private static int checkTimeStamp(Node node, long newNodeTimeStamp, int containerPosition) {
+		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 containerPosition;
+			}
+			if(n.getAttributes().get("timestamp") != null) {
+				childTimeStamp = n.getAttributes().get("timestamp").getLong();
+				if (newNodeTimeStamp < childTimeStamp) {
+					break;
+				}
+			}
+			count++;
+		}
+		return count;
+	}
 }