Mercurial > hg > Members > nobuyasu > jungle-network
view src/jungle/test/codesegment/operation/TestPutAttributeCodeSegment.java @ 66:29127ac788a6
move some files
author | one |
---|---|
date | Tue, 20 Aug 2013 17:38:09 +0900 |
parents | bf3dc481cc9b |
children | 87ec5dd0dc27 |
line wrap: on
line source
package jungle.test.codesegment.operation; import java.io.IOException; import java.nio.ByteBuffer; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; 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.TreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; 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 jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jungle.app.bbs.JungleManager; import org.msgpack.type.Value; import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; import alice.jungle.datasegment.store.operations.DefaultTreeOperationContainer; import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer; import alice.jungle.transaction.NetworkDefaultJungleTreeEditor; public class TestPutAttributeCodeSegment extends CodeSegment { Receiver arg1 = ids.create(CommandType.TAKE); public TestPutAttributeCodeSegment() { arg1.setKey("log"); } public void run() { System.out.println("--TestPutAttributeCodeSegment--"); DefaultTreeOperationContainer convertedOpContainer = arg1 .asClass(DefaultTreeOperationContainer.class); TreeOperation convertedOp = null; try { convertedOp = convertedOpContainer.convert(); } catch (IOException e) { e.printStackTrace(); } Jungle jungle = JungleManager.getJungle(); JungleTree tree = jungle.getTreeByName("tree"); JungleTreeEditor editor = tree.getTreeEditor(); DefaultNodePath root = new DefaultNodePath(); Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); editor.success(); NetworkDefaultJungleTreeEditor nEditor = (NetworkDefaultJungleTreeEditor) tree .getTreeEditor(); either = edit(nEditor, convertedOp); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); editor.success(); if (arg1.index >= 10) { new ShowAttribute(); ods.update("local", "show", 1); return; } TestPutAttributeCodeSegment cs = new TestPutAttributeCodeSegment(); DefaultTreeOperation treeOp = cs.getSampleOperation("message"+ arg1.index); DefaultTreeOperationContainer treeOperationContainer = new DefaultTreeOperationContainer(); try { treeOperationContainer.unconvert(treeOp); } catch (IOException e) { e.printStackTrace(); } ods.update("local", "log", treeOperationContainer); } public DefaultTreeOperation getSampleOperation(String message) { /* Create TreeOperation */ String key = "key1"; ByteBuffer b = ByteBuffer.wrap(message.getBytes()); PutAttributeOperation op = new PutAttributeOperation(key, b); DefaultNodePath p = new DefaultNodePath(); p = p.add(0); DefaultTreeOperation treeOp = new DefaultTreeOperation(p, op); return treeOp; } public Either<Error, JungleTreeEditor> edit(JungleTreeEditor editor, TreeOperation op) { NodePath path = op.getNodePath(); NodeOperation nodeOp = op.getNodeOperation(); Command c = nodeOp.getCommand(); String str = ""; String key = ""; switch (c) { case PUT_ATTRIBUTE: key = nodeOp.getKey(); ByteBuffer value = nodeOp.getValue(); if (value.limit() < 100) { str = String.format("key:%s,value:%s", key, new String(value.array())); } else { str = String.format("key:%s,value:%d", key, value.limit()); } return editor.putAttribute(path, key, value); case DELETE_ATTRIBUTE: key = nodeOp.getKey(); str = String.format("key:%s", nodeOp.getKey()); return editor.deleteAttribute(path, key); case APPEND_CHILD: str = String.format("pos:%d", nodeOp.getPosition()); return editor.addNewChildAt(path, 0); case DELETE_CHILD: str = String.format("pos:%d", nodeOp.getPosition()); return editor.deleteChildAt(path, 0); } return null; } }