view src/alice/jungle/datasegment/store/operations/DefaultTreeOperationLogContainer.java @ 31:190f6a3bdab2

rename some packages
author one
date Mon, 01 Jul 2013 20:34:03 +0900
parents src/jungle/test/datasegment/store/operations/DefaultTreeOperationLogContainer.java@e968224ad0ce
children afccf06c4063
line wrap: on
line source

package alice.jungle.datasegment.store.operations;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.List;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DefaultTreeOperation;
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 jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;

import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.msgpack.template.ListTemplate;
import org.msgpack.template.ValueTemplate;
import org.msgpack.type.Value;

@Message
public class DefaultTreeOperationLogContainer {

	Value logValue;
	
	public static void main(String[] args) throws IOException {
		String key = "hoge";
		ByteBuffer b = ByteBuffer.wrap("messagepack value".getBytes());
		PutAttributeOperation putOp = new PutAttributeOperation(key, b);
		DefaultNodePath nodePath = new DefaultNodePath();
		nodePath = nodePath.add(1).add(2).add(3);
		TreeOperation op = new DefaultTreeOperation(nodePath, putOp);
		List<TreeOperation> list = new LinkedList<TreeOperation>();
		list.add(op);
		list.add(op);
		list.add(op);
		DefaultTreeOperationLog log = new DefaultTreeOperationLog(list, list.size());
		
		DefaultTreeOperationLogContainer logContainer = new DefaultTreeOperationLogContainer();
		logContainer.unconvert(log);

		MessagePack msgpack = new MessagePack();
		Value logContainerValue = msgpack.unconvert(logContainer);
		DefaultTreeOperationLogContainer convertedLogContainer = msgpack.convert(logContainerValue,
																	DefaultTreeOperationLogContainer.class);
		
		TreeOperationLog convertedLog = convertedLogContainer.convert();
		for (TreeOperation treeOp : convertedLog) {
			NodePath path = treeOp.getNodePath();
			NodeOperation nodeOp = treeOp.getNodeOperation();
			Command c = nodeOp.getCommand();
			String str = "";
			switch (c) {
			case PUT_ATTRIBUTE:
				String k = nodeOp.getKey();
				ByteBuffer value = nodeOp.getValue();
				if (value.limit() < 100) {
					str = String.format("key:%s,value:%s", k,
							new String(value.array()));
				} else {
					str = String.format("key:%s,value:%d", k, value.limit());
				}
				break;
			case DELETE_ATTRIBUTE:
				str = String.format("key:%s", nodeOp.getKey());
				break;
			case APPEND_CHILD:
				str = String.format("pos:%d", nodeOp.getPosition());
				break;
			case DELETE_CHILD:
				str = String.format("pos:%d", nodeOp.getPosition());
				break;
			}
			System.out.println(String.format("[%s:%s:%s]", c, path, str));
			for (int i: path ) {
				System.out.println(i);
			}
			
		}
		
		
	}
	
	public DefaultTreeOperationLogContainer() {
		
	}
	
	public void unconvert(TreeOperationLog _log) throws IOException {
		MessagePack msgpack = new MessagePack();
		List<Value> list = new LinkedList<Value>();
		for(TreeOperation op : _log) {
			NodeOperation nOp = op.getNodeOperation();
			NodePath nodePath = op.getNodePath();
			DefaultTreeOperation treeOp = new DefaultTreeOperation(nodePath, nOp);
			DefaultTreeOperationContainer container = new DefaultTreeOperationContainer();
			container.unconvert(treeOp);
			Value v = msgpack.unconvert(container);
			list.add(v);
		}
		/* */
		msgpack.register(List.class, new ListTemplate(ValueTemplate.getInstance()));
		Value listValue = msgpack.unconvert(list);
		logValue = listValue;
	}
	
	public DefaultTreeOperationLog convert() throws IOException {
		MessagePack msgpack = new MessagePack();
		msgpack.register(List.class, new ListTemplate(ValueTemplate.getInstance()));
		List<Value> listValue = msgpack.convert(logValue, List.class);
		List<TreeOperation> logList = new LinkedList<TreeOperation>();
		for(Value v: listValue) {
			DefaultTreeOperationContainer container = msgpack.convert(v, DefaultTreeOperationContainer.class);
			logList.add(container.convert());
		}
		DefaultTreeOperationLog log = new DefaultTreeOperationLog(logList, logList.size());
		return log;
	}
	
	
	
}