view src/jungle/test/bbs/JungleManager.java @ 56:ccfe9b5e8f11

bug LogUpdateCodeSegment
author one
date Sun, 14 Jul 2013 21:26:49 +0900
parents bf3dc481cc9b
children 8a532ca5df80
line wrap: on
line source

package jungle.test.bbs;

import java.nio.ByteBuffer;

import alice.jungle.core.NetworkDefaultJungle;
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.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 jm = new JungleManager();
	private Jungle jungle;

	private JungleManager() {

	}
	
	public static void setJungle(Jungle _j) {
		jm.jungle = _j;
	}
	
	public static Jungle getJungle() {
		return jm.jungle;
	}
	
	public static JungleTree createNewTree(String name) {
		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) { 
			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) {
		DefaultNodePath path = new DefaultNodePath();
		NodeOperation nodeOp = op.getNodeOperation();
		Command c = nodeOp.getCommand();
		String key = "";
		switch (c) {
		case PUT_ATTRIBUTE:
			path = path.add(pos);
			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;
	}

	
}