view src/main/java/alice/jungle/transaction/JungleUpdater.java @ 192:3202a2a427b1

bug fix
author tatsuki
date Tue, 20 Jan 2015 09:30:59 +0900
parents 495ac60d7f5f
children
line wrap: on
line source

package alice.jungle.transaction;

import java.nio.ByteBuffer;

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.operations.NodeOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;

public class JungleUpdater {

	public JungleUpdater() {
		
	}
	
	public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,Iterable<TreeOperation> _log) {
		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, nodeOp.getPosition());
			if(either.isA()) {
				return either;
			}
			editor = either.b();
		}
		return either;
	}
	
	/* 
	 * 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();
			System.out.println("path = " + path.toString());
		switch (c) {
		case PUT_ATTRIBUTE:
			key = nodeOp.getKey();
			ByteBuffer value = nodeOp.getValue();
			System.out.println("key = " + key + " : " + "value = " + new String(value.array()));
			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;
	}

}