Mercurial > hg > Database > jungle-network
view src/alice/jungle/datasegment/store/operations/DefaultTreeOperationLogContainer.java @ 52:61b2de3f7730
add HashSetDataSegment and HashLogUpdateCodeSegment.
fix bugs
author | one |
---|---|
date | Sat, 13 Jul 2013 17:10:14 +0900 |
parents | a89c3539bff2 |
children | f47a02368099 |
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; String treeName = ""; String uuid = ""; String updaterName = ""; String revision = ""; 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); logContainer.setUUID("uuid"); logContainer.setTreeName("treeName"); MessagePack msgpack = new MessagePack(); Value logContainerValue = msgpack.unconvert(logContainer); DefaultTreeOperationLogContainer convertedLogContainer = msgpack.convert(logContainerValue, DefaultTreeOperationLogContainer.class); TreeOperationLog convertedLog = convertedLogContainer.convert(); System.out.println("uuid = "+ convertedLogContainer.getUUID()); System.out.println("treeName = "+ convertedLogContainer.getTreeName()); 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() { logValue = null; treeName = ""; uuid = ""; } public void setTreeName(String _treeName) { treeName = _treeName; } public String getTreeName() { return treeName; } public void setUUID(String _uuid) { uuid = _uuid; } public String getUUID() { return uuid; } public void setUpdaterName(String _updaterName) { updaterName = _updaterName; } public String getServerName() { return updaterName; } public void setRevision(String _revision) { revision = _revision; } public String getRevision() { return revision; } public void unconvert(Iterable<TreeOperation> _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); } 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; } public String getHashLogString() { return updaterName + revision; } }