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.FileDescriptor;
24 import java.io.IOException;
25 import java.io.ObjectOutputStream;
26 import java.io.RandomAccessFile;
27 import java.util.ArrayList;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.jini.space.InternalSpaceException;
32
33
34
35
36
37 class SnapshotFile extends LogFile {
38 private volatile RandomAccessFile snapshotFile = null;
39 private volatile String fileName = null;
40 private volatile String previousFilename = null;
41 private volatile ObjectOutputStream out;
42 private volatile int suffix;
43
44
45 private static final Logger logger =
46 Logger.getLogger(OutriggerServerImpl.storeLoggerName);
47
48
49
50 SnapshotFile(String basePath, File[] recover) throws IOException {
51 super(basePath);
52 ArrayList snapshots = new ArrayList();
53 suffix = existingLogs(snapshots);
54
55
56
57 if (snapshots.size() > 2)
58 throw new InternalSpaceException("More than two snapshot files "+
59 "are present");
60
61
62
63 if (snapshots.size() == 2) {
64 File file = (File)snapshots.get(1);
65 file.delete();
66
67
68
69
70
71 if (suffix == 1) {
72 suffix = 0;
73 recover[0] = null;
74 } else {
75 recover[0] = (File)(snapshots.get(0));
76 previousFilename = recover[0].getName();
77 }
78 } else if (snapshots.size() == 1) {
79 File file = (File)(snapshots.get(0));
80 previousFilename = file.getName();
81
82
83
84
85
86 if (suffix == 0)
87 recover[0] = null;
88 else
89 recover[0] = file;
90
91 } else {
92
93
94 next();
95 commit();
96 recover[0] = null;
97 }
98 }
99
100
101
102
103 ObjectOutputStream next() throws IOException {
104
105 suffix++;
106 fileName = baseFile + suffix;
107 snapshotFile = new RandomAccessFile(baseDir.getPath() + File.separator +
108 fileName, "rw");
109 out = new ObjectOutputStream(new LogOutputStream(snapshotFile));
110 return out;
111 }
112
113 void commit() throws IOException {
114 if (snapshotFile != null) {
115 try {
116 close();
117 } catch (IOException ignore) { }
118 }
119
120
121 if (previousFilename != null) {
122 File file = new File(baseDir, previousFilename);
123 file.delete();
124 }
125 previousFilename = fileName;
126 }
127
128
129
130
131 synchronized void close() throws IOException {
132 if (snapshotFile != null) {
133 try {
134 out.close();
135 snapshotFile.close();
136 } finally {
137 snapshotFile = null;
138 }
139 }
140 }
141
142
143
144
145
146 void destroy() {
147 try {
148 close();
149 } catch (Throwable t) {
150
151 }
152 super.destroy();
153 }
154 }