8
|
1 package jp.ac.u_ryukyu.ie.cr.bbs.network.bbs;
|
2
|
2
|
|
3
|
9
|
4 import jp.ac.u_ryukyu.ie.cr.bbs.local.browsing.JungleBrowsingBulletinBoard;
|
2
|
5 import jp.ac.u_ryukyu.ie.cr.jungle.Jungle;
|
|
6 import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList;
|
|
7 import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader;
|
8
|
8 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor;
|
|
9 import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree;
|
2
|
10 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
8
|
11 import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error;
|
2
|
12 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.NetworkDefaultJungle;
|
|
13 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.AliceJournal;
|
|
14 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.NetworkJournal;
|
|
15 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal;
|
|
16 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater;
|
|
17
|
|
18 import java.io.File;
|
|
19 import java.io.IOException;
|
|
20 import java.util.concurrent.atomic.AtomicInteger;
|
|
21
|
9
|
22 public class NetworkJungleBulletinBoard extends JungleBrowsingBulletinBoard implements NetworkBulletinBoard {
|
|
23 private final Jungle jungle;
|
2
|
24 private final NetworkJournal journal;
|
|
25 private final String LOG_DIR;
|
|
26 private Boolean persistentFlag;
|
9
|
27 private AtomicInteger requestCounter; //使われてない?
|
2
|
28 private long renewTime;
|
|
29
|
9
|
30 private NetworkJungleBulletinBoard(String _uuid, NetworkJournal journal) {
|
|
31 this(new NetworkDefaultJungle(journal, _uuid), journal);
|
|
32 }
|
|
33
|
|
34 private NetworkJungleBulletinBoard(Jungle jungle, NetworkJournal journal) {
|
|
35 super(jungle);
|
|
36 this.jungle = jungle;
|
|
37 this.journal = journal;
|
2
|
38 BulletinBoardJungleManager.setJungle(jungle);
|
9
|
39 this.persistentFlag = false;
|
|
40 this.requestCounter = BulletinBoardJungleManager.getRequestCounter();
|
|
41 this.LOG_DIR = "./log";
|
|
42 this.renewTime = 0;
|
2
|
43 }
|
|
44
|
|
45 public NetworkJungleBulletinBoard(String _uuid) {
|
|
46 this(_uuid, new AliceJournal());
|
9
|
47 this.jungle.createNewTree("boards");
|
2
|
48 }
|
|
49
|
|
50 public static NetworkBulletinBoard NewPersistentJungle(String _uuid) {
|
|
51 NetworkJungleBulletinBoard board = new NetworkJungleBulletinBoard(_uuid, new PersistentJournal());
|
|
52 board.persistentFlag = true;
|
|
53 return board;
|
|
54 }
|
|
55
|
|
56 public void init() {
|
|
57 if (!persistentFlag) {
|
|
58 return;
|
|
59 }
|
|
60 checkAndCreateLogDirectory();
|
|
61 try {
|
|
62 commitLogRecover();
|
|
63 } catch (IOException e) {
|
|
64 e.printStackTrace();
|
|
65 }
|
|
66 }
|
|
67
|
|
68 public void checkAndCreateLogDirectory() {
|
|
69 File logFile = new File(LOG_DIR);
|
|
70 if (!logFile.exists()) {
|
|
71 logFile.mkdir();
|
|
72 return;
|
|
73 }
|
|
74 if (logFile.isFile()) {
|
|
75 logFile.delete();
|
|
76 logFile.mkdir();
|
|
77 }
|
|
78 }
|
|
79
|
|
80 public void commitLogRecover() throws IOException {
|
|
81 File[] logFiles = new File(LOG_DIR).listFiles();
|
|
82 for (File logFile : logFiles) {
|
|
83 commitLogRecover(logFile);
|
|
84 logFile.delete();
|
|
85 }
|
|
86 if (jungle.getTreeByName("boards") == null) {
|
|
87 jungle.createNewTree("boards");
|
|
88 }
|
|
89 }
|
|
90
|
|
91 private void commitLogRecover(File logFile) throws IOException {
|
|
92 journal.setInputFile(logFile);
|
|
93 ChangeListReader reader = journal.getReader();
|
|
94 if (reader == null)
|
|
95 return;
|
|
96 for (ChangeList chList : reader) {
|
|
97 String treeName = chList.getTreeName();
|
|
98 JungleTree tree = jungle.getTreeByName(treeName);
|
|
99 if (tree == null) {
|
|
100 tree = jungle.createNewTree(treeName);
|
|
101 }
|
8
|
102 JungleTreeEditor editor = tree.getLocalJungleTreeEditor();
|
2
|
103 Either<Error, JungleTreeEditor> either = JungleUpdater.edit(editor, chList);
|
|
104 editor = either.b();
|
|
105 if (either.isA()) {
|
|
106 throw new IOException("Failed commit log recovery");
|
|
107 }
|
|
108 editor.success();
|
|
109 }
|
|
110 }
|
|
111
|
|
112 public long getRenewTime(String _boardName) {
|
|
113 return renewTime;
|
|
114 }
|
|
115
|
|
116 public int getRequestNum() {
|
|
117 return requestCounter.get();
|
|
118 }
|
|
119
|
|
120
|
|
121 }
|