view src/alice/jungle/datasegment/store/container/DefaultNodeOperationContainer.java @ 75:87ec5dd0dc27

Rename from alice.jungle.datasegment.store.operation to alice.jungle.datasegment.store.container
author one
date Tue, 15 Oct 2013 14:43:29 +0900
parents src/alice/jungle/datasegment/store/operations/DefaultNodeOperationContainer.java@85bc7416ae02
children
line wrap: on
line source

package alice.jungle.datasegment.store.container;

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;

import alice.codesegment.SingletonMessage;

@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();
		MessagePack msgpack = SingletonMessage.getInstance();
		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();
		MessagePack msgpack = SingletonMessage.getInstance();
		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;
	}
	
}