Mercurial > hg > Members > nobuyasu > jungle-network
view src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 170:2403b9a4416f
create Delete Attribute Method
author | tatsuki |
---|---|
date | Wed, 30 Jul 2014 10:20:01 +0900 |
parents | 54d68cda84c4 |
children | df063cf6f3b5 |
line wrap: on
line source
package app.bbs; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicInteger; import alice.jungle.core.NetworkDefaultJungle; import alice.jungle.persistent.AliceJournal; import alice.jungle.persistent.NetworkJournal; import alice.jungle.persistent.PersistentJournal; import alice.jungle.transaction.JungleUpdater; import app.bbs.thinks.getAttributeImp; 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.bbs.BoardMessage; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TraversableNodeWrapper; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultEvaluator; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversal; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; import junit.framework.Assert; public class NetworkJungleBulletinBoard implements NetworkBulletinBoard { private final Jungle jungle; private final NetworkJournal journal; private final String LOG_DIR; private Boolean persistentFlag; private AtomicInteger requestCounter; private long renewTime; private NetworkJungleBulletinBoard(String _uuid, NetworkJournal _journal) { journal = _journal; jungle = new NetworkDefaultJungle(journal, _uuid, new DefaultTreeEditor(new DefaultTraverser())); BulletinBoardJungleManager.setJungle(jungle); persistentFlag = false; requestCounter = BulletinBoardJungleManager.getRequestCounter(); LOG_DIR = "./log"; renewTime = 0; } public NetworkJungleBulletinBoard(String _uuid) { this(_uuid, new AliceJournal()); jungle.createNewTree("boards"); } public static NetworkBulletinBoard NewPersistentJungle(String _uuid) { NetworkJungleBulletinBoard board = new NetworkJungleBulletinBoard( _uuid, new PersistentJournal()); board.persistentFlag = true; return board; } public void init() { if (!persistentFlag) { return; } checkAndCreateLogDirectory(); try { commitLogRecover(); } catch (IOException e) { e.printStackTrace(); } } public void checkAndCreateLogDirectory() { File logFile = new File(LOG_DIR); if (!logFile.exists()) { logFile.mkdir(); return; } if (logFile.isFile()) { logFile.delete(); logFile.mkdir(); } } public void commitLogRecover() throws IOException { File[] logFiles = new File(LOG_DIR).listFiles(); for (File logFile : logFiles) { commitLogRecover(logFile); logFile.delete(); } if (jungle.getTreeByName("boards") == null) { jungle.createNewTree("boards"); } } private void commitLogRecover(File logFile) throws IOException { journal.setInputFile(logFile); ChangeListReader reader = journal.getReader(); if (reader == null) return; for (ChangeList chList : reader) { String treeName = chList.getTreeName(); JungleTree tree = jungle.getTreeByName(treeName); if (tree == null) { tree = jungle.createNewTree(treeName); } JungleTreeEditor editor = tree.getLocalTreeEditor(); Either<Error, JungleTreeEditor> either = JungleUpdater.edit(editor, chList); editor = either.b(); if (either.isA()) { throw new IOException("Failed commit log recovery"); } editor.success(); } } public Iterable<String> getBoards() { JungleTree tree = jungle.getTreeByName("boards"); Node node = tree.getRootNode(); Children<Node> chs = node.getChildren(); IterableConverter.Converter<String, Node> converter = new IterableConverter.Converter<String, Node>() { public String conv(Node _b) { ByteBuffer e = _b.getAttributes().get("name"); return new String(e.array()); } }; return new IterableConverter<String, Node>(chs, converter); } public long getRenewTime(String _boardName) { return renewTime; } public void createBoards(final String _name, final String _author, final String _initMessage, final String _editKey) { requestCounter.incrementAndGet(); if (null == jungle.createNewTree(_name)) { throw new IllegalStateException(); } JungleTree tree = jungle.getTreeByName("boards"); 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(); either = editor.putAttribute(root.add(0), "name",ByteBuffer.wrap(_name.getBytes())); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); final long timestamp = System.currentTimeMillis(); ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); either = editor.putAttribute(root.add(0), "timestamp", tBuffer); if (either.isA()) { throw new IllegalStateException(); } either = either.b().success(); if (either.isA()) { throw new IllegalStateException(); } tree = jungle.getTreeByName(_name); editor = tree.getTreeEditor(); either = editor.addNewChildAt(root, 0); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_initMessage.getBytes())).b(); _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); ByteBuffer tBuffer2 = ByteBuffer.allocate(16); tBuffer2.putLong(timestamp); _e = _e.getAttributes().put("timestamp", tBuffer2).b(); return DefaultEither.newB(_e); } }; either = editor.edit(root.add(0), e); if (either.isA()) { throw new IllegalStateException(); } either.b().success(); } public void createFolder(final String _board, final String _author, final String _message, final String _editKey, String _nodeNum) { JungleTree tree = jungle.getTreeByName(_board); if (tree == null) { throw new IllegalStateException(); } DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count, count + 1))); } } catch (Exception _e) { } requestCounter.incrementAndGet(); Either<Error, JungleTreeEditor> either; final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); do { Node node = tree.getRootNode(); DefaultTraverser traverser = new DefaultTraverser(); TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); DefaultEvaluator evaluator = new DefaultEvaluator(path); Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); if (ret.isA()) { Assert.fail(); } Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); TraversableNodeWrapper<Node> target = traversal.destination(); int size = target.getWrapped().getChildren().size(); JungleTreeEditor editor = tree.getTreeEditor(); either = editor.addNewChildAt(path, size); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); _e = _e.getAttributes().put("timestamp", tBuffer).b(); return DefaultEither.newB(_e); } }; path = path.add(size); either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void createBoardMessage(final String _board, final String _author, final String _message, final String _editKey) { requestCounter.incrementAndGet(); JungleTree tree = jungle.getTreeByName(_board); if (tree == null) { throw new IllegalStateException(); } Either<Error, JungleTreeEditor> either; final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); do { Node node = tree.getRootNode(); int size = node.getChildren().size(); DefaultNodePath path = new DefaultNodePath(); JungleTreeEditor editor = tree.getTreeEditor(); either = editor.addNewChildAt(path, size); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); _e = _e.getAttributes().put("timestamp", tBuffer).b(); return DefaultEither.newB(_e); } }; path = path.add(size); either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void editMessage(String _board, String _nodeNum, final String _author, final String _message, final String _editKey) { requestCounter.incrementAndGet(); final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; do { DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); _e = _e.getAttributes().put("timestamp", tBuffer).b(); return DefaultEither.newB(_e); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); renewTime = timestamp; } public void createAttribute(String _board, String _uuid, final String _author, final String _message, final String _editKey) { requestCounter.incrementAndGet(); final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; DefaultNodePath path = new DefaultNodePath(); do { try { for (int count = 0; _uuid.substring(count, count + 1) != null; count++) { if (!_uuid.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_uuid.substring(count, count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { String str = "0"; int count = 0; for (; _e.getAttributes().get("mes" + String.valueOf(count)) != null; count++) { } str = String.valueOf(count); _e = _e.getAttributes().put("mes" + str, ByteBuffer.wrap(_message.getBytes())).b(); _e = _e.getAttributes().put("timestamp" + str, tBuffer).b(); return DefaultEither.newB(_e); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void editAttribute(String boardName, String _path, String id, String _message) { requestCounter.incrementAndGet(); final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(boardName); Either<Error, JungleTreeEditor> either = null; DefaultNodePath path = new DefaultNodePath(); do { try { for (int count = 0; _path.substring(count, count + 1) != null; count++) { if (!_path.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_path.substring(count, count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { // EnableNodeWrapper<T> node = _e.getWrap(); _e = _e.getAttributes().put("mes" + id, ByteBuffer.wrap(_message.getBytes())).b(); _e = _e.getAttributes().put("timestamp" + id, tBuffer).b(); return DefaultEither.newB(_e); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void deleteAttribute(String _board, String _path ,final String id) { requestCounter.incrementAndGet(); final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; DefaultNodePath path = new DefaultNodePath(); do { try { for (int count = 0; _path.substring(count, count + 1) != null; count++) { if (!_path.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_path.substring(count, count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { _e = _e.getAttributes().delete("mes" + id ).b(); _e = _e.getAttributes().delete("timestamp" + id ).b(); int count = Integer.parseInt(id); for (; _e.getAttributes().get("mes" + String.valueOf(count + 1)) != null; count++) { _e = _e.getAttributes().put("mes" + count, _e.getAttributes().get("mes" + String.valueOf(count + 1))).b(); _e = _e.getAttributes().put("timestamp" + count, tBuffer).b(); } _e = _e.getAttributes().delete("mes" + count ).b(); _e = _e.getAttributes().delete("timestamp" + count ).b(); return DefaultEither.newB(_e); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) { requestCounter.incrementAndGet(); final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; do { DefaultNodePath path = new DefaultNodePath(); path = path.add(Integer.parseInt(_uuid)); JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); _e = _e.getAttributes().put("timestamp", tBuffer).b(); return DefaultEither.newB(_e); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); renewTime = timestamp; } public Iterable<BoardMessage> getMessages(String _boardName) { requestCounter.incrementAndGet(); JungleTree tree = jungle.getTreeByName(_boardName); Node node = tree.getRootNode(); Children<Node> chs = node.getChildren(); final AtomicInteger counter = new AtomicInteger(0); IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { public BoardMessage conv(Node _b) { String uuid = Integer.toString(counter.get()); String author = new String(_b.getAttributes().get("author") .array()); String message = new String(_b.getAttributes().get("mes").array()); counter.incrementAndGet(); return new BoardMessageImpl(author, message, uuid); } }; return new IterableConverter<BoardMessage, Node>(chs, converter); } public Iterable<BoardMessage> getFolder(String _boardName, String _nodeNum) { DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); } } catch (Exception _e) { } requestCounter.incrementAndGet(); JungleTree tree = jungle.getTreeByName(_boardName); Node node = tree.getRootNode(); DefaultTraverser traverser = new DefaultTraverser(); TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); DefaultEvaluator evaluator = new DefaultEvaluator(path); Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); if (ret.isA()) { Assert.fail(); } Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); TraversableNodeWrapper<Node> target = traversal.destination(); Children<Node> chs = target.getWrapped().getChildren(); final AtomicInteger counter = new AtomicInteger(0); IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { public BoardMessage conv(Node _b) { String uuid = Integer.toString(counter.get()); String message = new String(_b.getAttributes().get("mes").array()); counter.incrementAndGet(); return new BoardMessageImpl(null, message, uuid); } }; return new IterableConverter<BoardMessage, Node>(chs, converter); } public getAttributeImp getAttribute(String _boardName, String _nodeNum) { DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); } } catch (Exception _e) { } requestCounter.incrementAndGet(); JungleTree tree = jungle.getTreeByName(_boardName); Node node = tree.getRootNode(); DefaultTraverser traverser = new DefaultTraverser(); TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); DefaultEvaluator evaluator = new DefaultEvaluator(path); Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); if (ret.isA()) { Assert.fail(); } Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); TraversableNodeWrapper<Node> target = traversal.destination(); return new getAttributeImp(target.getWrapped()); } public int getRequestNum() { return requestCounter.get(); } private static class BoardMessageImpl implements BoardMessage { private final String author; private final String message; private final String uuid; public BoardMessageImpl(String _author, String _message, String _uuid) { author = _author; message = _message; uuid = _uuid; } public String getAuthor() { return author; } public String getMessage() { return message; } public String getUUID() { return uuid; } } }