1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.river.outrigger.snaplogstore;
19
20 import org.apache.river.outrigger.OutriggerServerImpl;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Iterator;
27 import java.util.TreeMap;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 class LogFile {
46
47
48
49 protected volatile File baseDir;
50
51
52
53
54
55 protected volatile String baseFile;
56
57
58
59
60 static final String LOG_TYPE = "LogStore";
61
62
63
64
65 protected static final int LOG_VERSION = 3;
66
67
68 protected static final byte BOOT_OP = 1;
69
70 protected static final byte JOINSTATE_OP = 11;
71
72 protected static final byte WRITE_OP = 2;
73
74 protected static final byte TAKE_OP = 3;
75
76 protected static final byte REGISTER_OP = 4;
77
78 protected static final byte RENEW_OP = 5;
79
80 protected static final byte NOTIFIED_OP = 6;
81
82 protected static final byte CANCEL_OP = 7;
83
84 protected static final byte PREPARE_OP = 8;
85
86 protected static final byte COMMIT_OP = 9;
87
88 protected static final byte ABORT_OP = 10;
89
90 protected static final byte UUID_OP = 12;
91
92 protected static final byte BATCH_WRITE_OP = 13;
93
94 protected static final byte BATCH_TAKE_OP = 14;
95
96
97 private static final Logger logger =
98 Logger.getLogger(OutriggerServerImpl.storeLoggerName);
99
100
101
102
103
104
105
106 protected LogFile(File baseDir, String baseFile) {
107 this.baseDir = baseDir;
108 this.baseFile = baseFile;
109 }
110
111
112
113
114
115
116
117
118
119
120 protected LogFile(String basePath) throws IOException {
121 baseDir = new File(basePath);
122 if (baseDir.isDirectory()) {
123 baseFile = "";
124 } else {
125 baseFile = baseDir.getName();
126 String pname = baseDir.getParent();
127 if (pname == null)
128 pname = ".";
129 baseDir = new File(pname);
130 if (baseFile.charAt(baseFile.length() - 1) != '.')
131 baseFile += ".";
132 }
133 }
134
135
136
137
138
139
140
141 int existingLogs(Collection files) {
142 if (logger.isLoggable(Level.FINE)) {
143 logger.log(Level.FINE, "scanning {0} for {1} baseFile",
144 new Object[]{baseDir, baseFile});
145 }
146
147 String[] inDir = baseDir.list();
148 TreeMap found = new TreeMap();
149 int highest = -1;
150
151
152 if (inDir == null)
153 return highest;
154
155 fileLoop:
156 for (int f = 0; f < inDir.length; f++) {
157
158 String name = inDir[f];
159
160 logger.log(Level.FINE, "checking {0}", name);
161
162 if (!name.startsWith(baseFile))
163 continue;
164
165
166 int num;
167 try {
168 num = Integer.parseInt(name.substring(baseFile.length()));
169 if (num > highest)
170 highest = num;
171 } catch (NumberFormatException e) {
172 continue fileLoop;
173 }
174
175 found.put(Integer.valueOf(num), new File(baseDir, name));
176 }
177
178 files.addAll(found.values());
179 if (logger.isLoggable(Level.FINE)) {
180 logger.log(Level.FINE, "returning {0} files",
181 Integer.valueOf(files.size()));
182 Iterator it = files.iterator();
183 while (it.hasNext())
184 logger.log(Level.FINE, it.next().toString());
185 }
186
187 return highest;
188 }
189
190
191
192
193 void destroy() {
194 if (logger.isLoggable(Level.FINE))
195 logger.log(Level.FINE, "destroy");
196
197 ArrayList files = new ArrayList();
198 existingLogs(files);
199 for (int i = 0; i < files.size(); i++) {
200 File log = (File) files.get(i);
201 try {
202 if (!log.delete()) {
203 logger.log(Level.INFO, "Could not delete {0}", log);
204 }
205 } catch (SecurityException e) {
206 if (!log.delete()) {
207 logger.log(Level.INFO,
208 "SecurityException : Could not delete " + log,
209 e);
210 }
211 }
212 }
213 }
214 }