view src/main/java/alice/jungle/operations/NetworkNodeOperation.java @ 180:2828205bdc3a

fit JungleCore
author tatsuki
date Sat, 13 Sep 2014 12:54:02 +0900
parents 6f104ab4eb81
children 89c15aa2bc6d
line wrap: on
line source

package alice.jungle.operations;

import java.nio.ByteBuffer;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteAttribute;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteChildAt;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;

import org.msgpack.annotation.Message;

@Message
public class NetworkNodeOperation implements NodeOperation{

	public int pos;
	public String key;
	public ByteBuffer value;
	public int commandType;

	public final static int NUM_PUT_ATTRIBUTE = 1;
	public final static int NUM_APPEND_CHILD = 2;
	public final static int NUM_DELETE_CHILD = 3;
	public final static int NUM_DELETE_ATTRIBUTE = 4;
	
	public NetworkNodeOperation() {
		pos = -2;
		key = null;
		value = null;
		commandType = 0;
	}
	
	public NetworkNodeOperation(NodeOperation _op) {
		pos = _op.getPosition();
		key = _op.getKey();
		value = _op.getValue();
		commandType = getCommandType(_op.getCommand());
	}
	
	public static int getCommandType(Command c) {
		switch(c) {
		case PUT_ATTRIBUTE:
			return NUM_PUT_ATTRIBUTE;
		case APPEND_CHILD:
			return NUM_APPEND_CHILD;
		case DELETE_CHILD:
			return NUM_DELETE_CHILD;
		case DELETE_ATTRIBUTE:
			return NUM_DELETE_ATTRIBUTE;
		default:
			break;
		}
		return 0;
	}
	
	public static Command getCommand(int num) {
		switch(num) {
		case NUM_PUT_ATTRIBUTE:
			return Command.PUT_ATTRIBUTE;
		case NUM_APPEND_CHILD:
 			return Command.APPEND_CHILD;
		case NUM_DELETE_CHILD:
			return Command.DELETE_CHILD;
		case NUM_DELETE_ATTRIBUTE:
			return Command.DELETE_ATTRIBUTE;
		default:
			break;
		}
		return null;
	}
	
	public Command getCommand() {
		return getCommand(commandType);
	}

	public int getPosition() {
		return pos;
	}
	
	public String getKey() {
		return key;
	}
	
	public ByteBuffer getValue() {
		return value;
	}

	@Override
	public Either<Error, TreeNode> invoke(TreeNode _target) {
		switch(getCommand(commandType)) {
		case PUT_ATTRIBUTE:
			return _target.getAttributes().put(key, value);
		case APPEND_CHILD:
			return _target.getChildren().addNewChildAt(pos);
		case DELETE_CHILD:
			return _target.getChildren().deleteChildAt(pos);
		case DELETE_ATTRIBUTE:
			return _target.getAttributes().delete(key);
		default:
			break;
		}
		return null;
	}

}