Mercurial > hg > Members > nobuyasu > jungle-network
view src/main/java/app/bbs/BulletinBoardJungleManager.java @ 135:2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 12 Jan 2014 06:18:37 +0900 |
parents | 00fcb468de27 |
children | 07106bbc6c17 |
line wrap: on
line source
package app.bbs; import java.nio.ByteBuffer; import alice.jungle.operations.NetworkTreeOperationLog; import alice.jungle.transaction.JungleUpdater; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; 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.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; public class BulletinBoardJungleManager { private static BulletinBoardJungleManager instance = new BulletinBoardJungleManager(); private Jungle jungle; private BulletinBoardJungleManager() { jungle = new DefaultJungle(null,"default",new DefaultTreeEditor(new DefaultTraverser())); } public static BulletinBoardJungleManager getInstantce() { return instance; } public static void setJungle(Jungle _j) { instance.jungle = _j; } public static Jungle getJungle() { return instance.jungle; } public static JungleTree createNewTree(String name) { return instance.jungle.createNewTree(name); } public static Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) { String treeName = netLog.getTreeName(); Jungle jungle = BulletinBoardJungleManager.getJungle(); if (jungle.getTreeByName(treeName) == null) { if(null == jungle.createNewTree(treeName)){ throw new IllegalStateException(); } } Either<Error, JungleTreeEditor> either = null; do { JungleTree tree = jungle.getTreeByName(treeName); JungleTreeEditor editor = tree.getLocalTreeEditor(); /* * Merge. */ int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp()); either = JungleUpdater.edit(editor, netLog, pos); if(either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); }while(either.isA()); return either; } private static int calculatePosition(Node node, long newNodeTimeStamp) { int count = 0; long childTimeStamp = 0; for(Node n : node.getChildren()) { ByteBuffer timestamp = n.getAttributes().get("timestamp"); if(timestamp == null) { return count; } childTimeStamp = timestamp.getLong(0); if (newNodeTimeStamp < childTimeStamp) { break; } count++; } return count; } }