Mercurial > hg > Database > jungle-network
view src/jungle/test/bbs/codesegment/ChildLogCheckCodeSegment.java @ 58:4851344e120e
fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
author | one |
---|---|
date | Mon, 15 Jul 2013 10:13:34 +0900 |
parents | 1bfd13f831ea |
children | 8a532ca5df80 |
line wrap: on
line source
package jungle.test.bbs.codesegment; import java.io.IOException; import java.util.Iterator; 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.core.Node; 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.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError; 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.bbs.JungleManager; import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; import alice.jungle.codesegment.HashLogUpdateCodeSegment; import alice.jungle.datasegment.HashSetDataSegment; import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer; public class ChildLogCheckCodeSegment extends CodeSegment { Receiver host = ids.create(CommandType.PEEK); Receiver childLog = ids.create(CommandType.TAKE); Receiver hashLog = ids.create(CommandType.PEEK); public ChildLogCheckCodeSegment() { host.setKey("host"); childLog.setKey("local","childLog"); hashLog.setKey("hashLog"); } public ChildLogCheckCodeSegment(int index) { host.setKey("host"); childLog.setKey("local","childLog", index); hashLog.setKey("hashLog"); } public void run() { System.out.println("--ChildLogCheckCodeSegment--"); int index = childLog.index; HashSetDataSegment ds = hashLog.asClass(HashSetDataSegment.class); String hostName = host.asString(); DefaultTreeOperationLogContainer container = childLog.asClass(DefaultTreeOperationLogContainer.class); DefaultTreeOperationLog log = null; try { log = container.convert(); } catch (IOException e) { e.printStackTrace(); } String treeName = container.getTreeName(); if (JungleManager.getJungle().getTreeByName(treeName) == null) { if(null == JungleManager.getJungle().createNewTree(treeName)){ throw new IllegalStateException(); } } JungleTree tree = JungleManager.getJungle().getTreeByName(treeName); JungleTreeEditor editor = tree.getTreeEditor(); Either<DefaultError, Integer> e = checkTimeStamp(tree.getRootNode(), container.getTimeStamp()); Either<Error, JungleTreeEditor> either; if(e.isA()) { either = JungleManager.edit(editor, log, container.getPosition()); } else { either = JungleManager.edit(editor, log, e.b()); } if(either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); if(either.isA()) { throw new IllegalStateException(); } ds.hash.add(container.getHashLogString()); ods.update("hashLog", ds); ods.put("log", container); new ChildLogCheckCodeSegment(index); if(!hostName.equals("node0")) { ods.put("parent", "childLog", container); } } private DefaultEither<DefaultError, Integer> checkTimeStamp(Node node, long newNodeTimeStamp) { int count = 0; long childTimeStamp = 0; for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) { Node n = iter.next(); if(n.getAttributes().get("timestamp") == null) { return DefaultEither.newA(new DefaultError()); } if(n.getAttributes().get("timestamp") != null) { childTimeStamp = n.getAttributes().get("timestamp").getLong(); if (newNodeTimeStamp < childTimeStamp) { break; } } count++; } return DefaultEither.newB(count); } }