Mercurial > hg > Members > nobuyasu > jungle-network
view src/jungle/test/codesegment/practice/TestPutAttributeCodeSegment.java @ 20:506388c02b4b
fix using edit method
author | one |
---|---|
date | Sun, 30 Jun 2013 04:11:23 +0900 |
parents | 08e283ede7c9 |
children | 2a378b94cc55 |
line wrap: on
line source
package jungle.test.codesegment.practice; 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.test.datasegment.store.operations.DefaultTreeOperationContainer; import jungle.test.datasegment.store.operations.DefaultTreeOperationLogContainer; import jungle.test.transaction.NetworkDefaultJungleTreeEditor; import org.msgpack.type.Value; import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; public class TestPutAttributeCodeSegment extends CodeSegment { Receiver arg1 = ids.create(CommandType.TAKE); public TestPutAttributeCodeSegment() { arg1.setKey("log"); } public void run() { System.out.println("--TestPutAttributeCodeSegment--"); System.out.println("type = " + arg1.type); System.out.println("index = " + arg1.index); System.out.println("data = " + arg1.getVal()); System.out.println(((Value)arg1.getVal()).getType()); DefaultTreeOperationContainer convertedOpContainer = arg1.asClass(DefaultTreeOperationContainer.class); TreeOperation convertedOp = null; try { convertedOp = convertedOpContainer.convert(); } catch (IOException e) { e.printStackTrace(); } Jungle jungle = new JungleManager().getJungle(); JungleTree tree = jungle.getTreeByName("tree"); // NetworkDefaultJungleTreeEditor editor = (NetworkDefaultJungleTreeEditor) tree.getTreeEditor(); // edit(editor, convertedOp); JungleTreeEditor editor = tree.getTreeEditor(); editor.success(); if (arg1.index >= 10) { new ShowAttribute(); ods.update("local","show",1); } else { 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 void 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()); } editor.putAttribute(path, key, value); break; case DELETE_ATTRIBUTE: key = nodeOp.getKey(); str = String.format("key:%s", nodeOp.getKey()); editor.deleteAttribute(path, key); break; case APPEND_CHILD: str = String.format("pos:%d", nodeOp.getPosition()); editor.addNewChildAt(path, 0); break; case DELETE_CHILD: str = String.format("pos:%d", nodeOp.getPosition()); editor.deleteChildAt(path, 0); break; } System.out.println(String.format("[%s:%s]", c, str)); System.out.println("path:"); for (int i: path ) { System.out.println(i); } } }