Mercurial > hg > Database > jungle-network
changeset 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 | a6fa10e42601 |
children | 0ea565624ddf |
files | src/main/java/alice/jungle/persistent/AliceJournal.java src/main/java/alice/jungle/persistent/NetworkJournal.java src/main/java/alice/jungle/persistent/PersistentChangeListReader.java src/main/java/alice/jungle/persistent/PersistentChangeListWriter.java src/main/java/alice/jungle/persistent/PersistentJournal.java src/main/java/alice/jungle/persistent/SingletonMessageFromAlice.java src/main/java/app/bbs/BulletinBoardJungleManager.java src/main/java/app/bbs/NetworkJungleBulletinBoard.java src/main/java/app/bbs/PersistentJungleBulletinBoard.java src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java |
diffstat | 10 files changed, 128 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/alice/jungle/persistent/AliceJournal.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/alice/jungle/persistent/AliceJournal.java Sun Jan 12 06:18:37 2014 +0900 @@ -1,14 +1,15 @@ package alice.jungle.persistent; +import java.io.File; +import java.io.FileNotFoundException; import java.util.Iterator; 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.persistent.ChangeListWriter; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Journal; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Result; -public class AliceJournal implements Journal { +public class AliceJournal implements NetworkJournal { private static final AliceChangeListWriter ALICE_WRITER = new AliceChangeListWriter(); private static final AliceChangeListReader ALICE_READER = new AliceChangeListReader(); @@ -79,6 +80,15 @@ } } + + @Override + public void setInputFile(File file) throws FileNotFoundException { + + } + @Override + public void setOutputFile(File file) throws FileNotFoundException { + + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/jungle/persistent/NetworkJournal.java Sun Jan 12 06:18:37 2014 +0900 @@ -0,0 +1,14 @@ +package alice.jungle.persistent; + +import java.io.File; +import java.io.FileNotFoundException; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Journal; + +public interface NetworkJournal extends Journal{ + + public void setInputFile(File file) throws FileNotFoundException; + public void setOutputFile(File file) throws FileNotFoundException; + + +}
--- a/src/main/java/alice/jungle/persistent/PersistentChangeListReader.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/alice/jungle/persistent/PersistentChangeListReader.java Sun Jan 12 06:18:37 2014 +0900 @@ -14,7 +14,7 @@ public class PersistentChangeListReader implements ChangeListReader { private InputStream in; - MessagePack msgpack = PersistentJournal.getMsgPackInstance(); + MessagePack msgpack = SingletonMessageFromAlice.getInstance(); public PersistentChangeListReader(InputStream _in) { in = _in;
--- a/src/main/java/alice/jungle/persistent/PersistentChangeListWriter.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/alice/jungle/persistent/PersistentChangeListWriter.java Sun Jan 12 06:18:37 2014 +0900 @@ -13,7 +13,7 @@ public class PersistentChangeListWriter implements ChangeListWriter { - MessagePack msgpack = PersistentJournal.getMsgPackInstance(); + MessagePack msgpack = SingletonMessageFromAlice.getInstance(); OutputStream out; public PersistentChangeListWriter(OutputStream _out) {
--- a/src/main/java/alice/jungle/persistent/PersistentJournal.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/alice/jungle/persistent/PersistentJournal.java Sun Jan 12 06:18:37 2014 +0900 @@ -8,24 +8,17 @@ import java.io.InputStream; import java.io.OutputStream; -import org.msgpack.MessagePack; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Journal; -public class PersistentJournal implements Journal { +public class PersistentJournal implements NetworkJournal { private ChangeListWriter WRITER; private ChangeListReader READER; private OutputStream out; private InputStream in; - private static MessagePack msgpack; - - static { - msgpack = new MessagePack(); - } - + public PersistentJournal() { } @@ -51,7 +44,7 @@ } catch (FileNotFoundException e) { e.printStackTrace(); } - PersistentChangeListWriter writer = new PersistentChangeListWriter(outStream); + ChangeListWriter writer = new PersistentChangeListWriter(outStream); return writer; } @@ -81,15 +74,8 @@ return in; } - public static MessagePack getMsgPackInstance() { - return msgpack; - } - public void close() throws IOException { out.close(); in.close(); } - - - }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/jungle/persistent/SingletonMessageFromAlice.java Sun Jan 12 06:18:37 2014 +0900 @@ -0,0 +1,12 @@ +package alice.jungle.persistent; + +import org.msgpack.MessagePack; + +import alice.codesegment.SingletonMessage; + +public class SingletonMessageFromAlice { + + public static MessagePack getInstance(){ + return SingletonMessage.getInstance(); + } +}
--- a/src/main/java/app/bbs/BulletinBoardJungleManager.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/app/bbs/BulletinBoardJungleManager.java Sun Jan 12 06:18:37 2014 +0900 @@ -74,7 +74,7 @@ if(timestamp == null) { return count; } - childTimeStamp = timestamp.getLong(); + childTimeStamp = timestamp.getLong(0); if (newNodeTimeStamp < childTimeStamp) { break; }
--- a/src/main/java/app/bbs/NetworkJungleBulletinBoard.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/app/bbs/NetworkJungleBulletinBoard.java Sun Jan 12 06:18:37 2014 +0900 @@ -1,17 +1,24 @@ package app.bbs; +import java.io.File; +import java.io.IOException; import java.nio.ByteBuffer; import java.util.Date; 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 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.trasnformer.EditableNode; @@ -25,16 +32,85 @@ public class NetworkJungleBulletinBoard implements NetworkBulletinBoard { private final Jungle jungle; - + private final NetworkJournal journal; + private final String LOG_DIR; + private Boolean persistentFlag; + + private NetworkJungleBulletinBoard(String _uuid, NetworkJournal _journal) + { + journal = _journal; + jungle = new NetworkDefaultJungle(journal, _uuid,new DefaultTreeEditor(new DefaultTraverser())); + BulletinBoardJungleManager.setJungle(jungle); + persistentFlag = false; + LOG_DIR = "./log"; + } + public NetworkJungleBulletinBoard(String _uuid) { - jungle = new NetworkDefaultJungle(new AliceJournal(), _uuid,new DefaultTreeEditor(new DefaultTraverser())); + this(_uuid, new AliceJournal()); jungle.createNewTree("boards"); - BulletinBoardJungleManager.setJungle(jungle); + } + + 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()
--- a/src/main/java/app/bbs/PersistentJungleBulletinBoard.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/app/bbs/PersistentJungleBulletinBoard.java Sun Jan 12 06:18:37 2014 +0900 @@ -25,16 +25,16 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; import alice.jungle.core.NetworkDefaultJungle; +import alice.jungle.persistent.NetworkJournal; import alice.jungle.persistent.PersistentJournal; import alice.jungle.transaction.JungleUpdater; public class PersistentJungleBulletinBoard implements NetworkBulletinBoard { private final Jungle jungle; - private final PersistentJournal journal; + private final NetworkJournal journal; private final String LOG_DIR = "./log"; - public PersistentJungleBulletinBoard(String _uuid) throws FileNotFoundException { journal = new PersistentJournal();
--- a/src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java Sat Jan 11 09:33:45 2014 +0900 +++ b/src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java Sun Jan 12 06:18:37 2014 +0900 @@ -56,12 +56,8 @@ NetworkBulletinBoard cassaBBS = null; if(persistentFlag) { System.out.println("log loading..."); - try { - cassaBBS = new PersistentJungleBulletinBoard(name); - cassaBBS.init(); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } + cassaBBS = NetworkJungleBulletinBoard.NewPersistentJungle(name); + cassaBBS.init(); } else { cassaBBS = new NetworkJungleBulletinBoard(name); cassaBBS.init();