view src/jungle/test/datasegment/store/operations/DefaultNodeOperationContainer.java @ 2:20498c88a70d

add Container
author one
date Sun, 09 Jun 2013 13:53:16 +0900
parents
children 3770d2be3e73
line wrap: on
line source

package jungle.test.datasegment.store.operations;

import java.io.IOException;
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.AppendChildAtOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteAttributeOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteChildAtOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.PutAttributeOperation;

import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.msgpack.template.OrdinalEnumTemplate;
import org.msgpack.template.Template;
import org.msgpack.type.Value;

@Message
public class DefaultNodeOperationContainer {

	public int pos;
	public String key;
	public Value value;
	public Value commandValue;
	
	
	public DefaultNodeOperationContainer() {
		
	}

	public void unconvert(NodeOperation op) throws IOException {
		MessagePack msgpack = new MessagePack();
		pos = op.getPosition();
		key = op.getKey();
		value = null;
		if (op.getValue() != null) {
			ByteBuffer b = op.getValue();
			Value v = msgpack.unconvert(b);
			value = v;
		}
		Command c = op.getCommand();
		msgpack.register(c.getClass(), new OrdinalEnumTemplate(c.getClass()));
		Value cValue = msgpack.unconvert(c);
		commandValue = cValue;
	}
	
	public NodeOperation convert() throws IOException{
		MessagePack msgpack = new MessagePack();
		Command c = msgpack.convert(commandValue, Command.class);
		ByteBuffer b = null;
		if (value != null) {
			b = msgpack.convert(value, ByteBuffer.class);
		}
		if (c == Command.PUT_ATTRIBUTE) {
			return new PutAttributeOperation(key, b);
		} else if (c == Command.APPEND_CHILD) {
			return new AppendChildAtOperation(pos);
		} else if (c == Command.DELETE_CHILD) {
			return new DeleteChildAtOperation(pos);
		} else if (c == Command.DELETE_ATTRIBUTE){
			return new DeleteAttributeOperation(key);
		}
		return null;
	}
	
	
}