view src/alice/jungle/operations/NetworkNodeOperation.java @ 79:5b9708d9febc

Modified NetworkTreeOperationLog NetworkNodeOperation
author one
date Wed, 16 Oct 2013 19:25:02 +0900
parents 0055d917c796
children b3ccefdf2b43
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.operations.NodeOperation;
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.EditableNode;
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;

import alice.jungle.datasegment.store.transformer.NetworkAppendChildAt;

@Message
public class NetworkNodeOperation implements NodeOperation{

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

	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 1;
		case APPEND_CHILD:
			return 2;
		case DELETE_CHILD:
			return 3;
		case DELETE_ATTRIBUTE:
			return 4;
		default:
			break;
		}
		return 0;
	}
	
	public static Command getCommand(int num) {
		switch(num) {
		case 1:
			return Command.PUT_ATTRIBUTE;
		case 2:
			return Command.APPEND_CHILD;
		case 3:
			return Command.DELETE_CHILD;
		case 4:
			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 <T extends EditableNode<T>> Either<Error, T> invoke(T _target) {
		switch(getCommand(commandType)) {
		case PUT_ATTRIBUTE:
			PutAttribute putAttribute = new PutAttribute(key,value);
			return putAttribute.edit(_target);
		case APPEND_CHILD:
			NetworkAppendChildAt appendChildAt = new NetworkAppendChildAt(pos);
			return appendChildAt.edit(_target);
		case DELETE_CHILD:
			DeleteChildAt deleteChildAt = new DeleteChildAt(pos);
			return deleteChildAt.edit(_target);
		case DELETE_ATTRIBUTE:
			DeleteAttribute deleteAttribute = new DeleteAttribute(key);
			return deleteAttribute.edit(_target);
		default:
			break;
		}
		return null;
	}

}