view src/jungle/test/datasegment/store/operations/DefaultNodeOperationContainer.java @ 3:3770d2be3e73

modified DfaultNodePathContainer
author one
date Mon, 10 Jun 2013 01:18:45 +0900
parents 20498c88a70d
children
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.type.Value;

@Message
public class DefaultNodeOperationContainer {

	public int pos;
	public String key;
	public Value value;
	public Value commandValue;
	
	public static void main(String[] args) throws IOException {
		String key = "hoge";
		ByteBuffer b = ByteBuffer.wrap("messagepack value".getBytes());
		PutAttributeOperation op = new PutAttributeOperation(key, b);
		DefaultNodeOperationContainer container = new DefaultNodeOperationContainer();
		container.unconvert(op);
		NodeOperation convertedOp = container.convert(); 
		System.out.println("pos : "+convertedOp.getPosition());
		System.out.println("Command : "+convertedOp.getCommand());
		System.out.println("key : "+convertedOp.getKey());
		System.out.println("value : "+new String(convertedOp.getValue().array()));
		
	}
	
	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();
			byte[] bytes = b.array();
			Value v = msgpack.unconvert(bytes);
			value = v;
		}
		Command c = op.getCommand();
		msgpack.register(Command.class, new OrdinalEnumTemplate(Command.class));
		Value cValue = msgpack.unconvert(c);
		commandValue = cValue;
	}
	
	public NodeOperation convert() throws IOException{
		MessagePack msgpack = new MessagePack();
		msgpack.register(Command.class, new OrdinalEnumTemplate(Command.class));
		Command c = msgpack.convert(commandValue, Command.class);
		ByteBuffer b = null;
		if (value != null) {
			b = ByteBuffer.wrap(msgpack.convert(value, byte[].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;
	}
	
}