81
|
1 package alice.jungle.persistence;
|
|
2
|
83
|
3 import java.io.EOFException;
|
|
4 import java.io.File;
|
|
5 import java.io.FileInputStream;
|
|
6 import java.io.FileNotFoundException;
|
|
7 import java.io.FileOutputStream;
|
|
8 import java.io.IOException;
|
|
9 import java.io.InputStream;
|
|
10 import java.io.OutputStream;
|
|
11 import java.util.Iterator;
|
|
12
|
|
13 import org.msgpack.MessagePack;
|
|
14
|
|
15 import alice.jungle.operations.NetworkTreeOperationLog;
|
|
16
|
81
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader;
|
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter;
|
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Journal;
|
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Result;
|
84
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet;
|
83
|
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
81
|
24
|
|
25 public class NetworkJournal implements Journal {
|
|
26
|
|
27 private static final NetworkChangeListWriter WRITER = new NetworkChangeListWriter();
|
|
28 private static final NetworkChangeListReader READER = new NetworkChangeListReader();
|
83
|
29 private static MessagePack msgpack = new MessagePack();
|
|
30 private static OutputStream out = null;
|
|
31 private static InputStream in = null;
|
|
32
|
|
33 public NetworkJournal() {
|
|
34
|
|
35 }
|
|
36
|
|
37 public NetworkJournal(File file) throws FileNotFoundException {
|
|
38 out = new FileOutputStream(file, true);
|
|
39 in = new FileInputStream(file);
|
|
40 }
|
|
41
|
81
|
42 @Override
|
|
43 public ChangeListReader getReader() {
|
|
44 return READER;
|
|
45 }
|
|
46 @Override
|
|
47 public ChangeListWriter getWriter() {
|
|
48 return WRITER;
|
|
49 }
|
|
50
|
83
|
51 public void close() throws IOException {
|
|
52 out.close();
|
|
53 in.close();
|
|
54 }
|
|
55
|
|
56 public void setOutputStream(OutputStream _out) {
|
|
57 out = _out;
|
|
58 }
|
|
59
|
|
60 public OutputStream getOutputStream() {
|
|
61 return out;
|
|
62 }
|
|
63
|
|
64 public void setInputStream(InputStream _in) {
|
|
65 in = _in;
|
|
66 }
|
|
67
|
|
68 public InputStream getInputStream() {
|
|
69 return in;
|
|
70 }
|
|
71
|
81
|
72 private static class NetworkChangeListWriter implements ChangeListWriter
|
|
73 {
|
|
74 @Override
|
|
75 public Result write(ChangeList _operations)
|
|
76 {
|
83
|
77 NetworkTreeOperationLog log = new NetworkTreeOperationLog(_operations);
|
|
78 try {
|
|
79 msgpack.write(out, log);
|
|
80 out.flush();
|
|
81 } catch (IOException e) {
|
|
82 return null;
|
|
83 }
|
81
|
84 return Result.SUCCESS;
|
|
85 }
|
84
|
86
|
|
87 public Result write(ChangeSet cs)
|
|
88 {
|
|
89 NetworkTreeOperationLog log = new NetworkTreeOperationLog(cs.uuid(),cs.getChangeList());
|
|
90 try {
|
|
91 msgpack.write(out, log);
|
|
92 out.flush();
|
|
93 } catch (IOException e) {
|
|
94 return null;
|
|
95 }
|
|
96 return Result.SUCCESS;
|
|
97 }
|
|
98
|
81
|
99 }
|
|
100
|
84
|
101 public static MessagePack getMessagePack() {
|
83
|
102 return msgpack;
|
|
103 }
|
|
104
|
81
|
105 private static class NetworkChangeListReader implements ChangeListReader
|
|
106 {
|
83
|
107
|
|
108
|
81
|
109 @Override
|
|
110 public ChangeListReader newReader()
|
|
111 {
|
|
112 return this;
|
|
113 }
|
|
114
|
|
115 @Override
|
83
|
116 public ChangeList read()
|
81
|
117 {
|
83
|
118 try {
|
|
119 final NetworkTreeOperationLog readLog = msgpack.read(in, NetworkTreeOperationLog.class);
|
|
120 ChangeList cl = new ChangeList() {
|
|
121 @Override
|
|
122 public Iterator<TreeOperation> iterator() {
|
|
123 return readLog.iterator();
|
|
124 }
|
|
125 };
|
|
126 return cl;
|
|
127 } catch (EOFException e){
|
|
128 // e.printStackTrace();
|
|
129 } catch (IOException e) {
|
|
130 // e.printStackTrace();
|
|
131 }
|
81
|
132 return null;
|
|
133 }
|
|
134
|
|
135 }
|
|
136 }
|