46
46
import org .opensolaris .opengrok .OpenGrokLogger ;
47
47
import org .opensolaris .opengrok .configuration .RuntimeEnvironment ;
48
48
49
+ /*
50
+ * Class representing file based storage of per source file history.
51
+ */
49
52
class FileHistoryCache implements HistoryCache {
50
53
private final Object lock = new Object ();
51
54
52
55
static class FilePersistenceDelegate extends PersistenceDelegate {
53
56
@ Override
54
57
protected Expression instantiate (Object oldInstance , Encoder out ) {
55
58
File f = (File )oldInstance ;
56
- return new Expression (oldInstance , f .getClass (), "new" , new Object [] {f .toString ()});
59
+ return new Expression (oldInstance , f .getClass (), "new" ,
60
+ new Object [] {f .toString ()});
57
61
}
58
62
}
59
63
@@ -95,7 +99,8 @@ private static File getCachedFile(File file) throws HistoryException {
95
99
sb .append (add );
96
100
sb .append (".gz" );
97
101
} catch (IOException e ) {
98
- throw new HistoryException ("Failed to get path relative to source root for " + file , e );
102
+ throw new HistoryException ("Failed to get path relative to " +
103
+ "source root for " + file , e );
99
104
}
100
105
101
106
return new File (sb .toString ());
@@ -112,6 +117,13 @@ private static History readCache(File file) throws IOException {
112
117
}
113
118
}
114
119
120
+ /**
121
+ * Store history object (encoded as XML and compressed with gzip) in a file.
122
+ *
123
+ * @param history history object to store
124
+ * @param file file to store the history object into
125
+ * @throws HistoryException
126
+ */
115
127
private void storeFile (History history , File file ) throws HistoryException {
116
128
117
129
File cache = getCachedFile (file );
@@ -135,8 +147,10 @@ private void storeFile(History history, File file) throws HistoryException {
135
147
output = File .createTempFile ("oghist" , null , dir );
136
148
try (FileOutputStream out = new FileOutputStream (output );
137
149
XMLEncoder e = new XMLEncoder (
138
- new BufferedOutputStream (new GZIPOutputStream (out )))) {
139
- e .setPersistenceDelegate (File .class , new FilePersistenceDelegate ());
150
+ new BufferedOutputStream (
151
+ new GZIPOutputStream (out )))) {
152
+ e .setPersistenceDelegate (File .class ,
153
+ new FilePersistenceDelegate ());
140
154
e .writeObject (history );
141
155
}
142
156
} catch (IOException ioe ) {
@@ -145,20 +159,32 @@ private void storeFile(History history, File file) throws HistoryException {
145
159
synchronized (lock ) {
146
160
if (!cache .delete () && cache .exists ()) {
147
161
if (!output .delete ()) {
148
- OpenGrokLogger .getLogger ().log (Level .WARNING , "Failed to remove temporary history cache file" );
162
+ OpenGrokLogger .getLogger ().log (Level .WARNING ,
163
+ "Failed to remove temporary history cache file" );
149
164
}
150
165
throw new HistoryException (
151
166
"Cachefile exists, and I could not delete it." );
152
167
}
153
168
if (!output .renameTo (cache )) {
154
169
if (!output .delete ()) {
155
- OpenGrokLogger .getLogger ().log (Level .WARNING , "Failed to remove temporary history cache file" );
170
+ OpenGrokLogger .getLogger ().log (Level .WARNING ,
171
+ "Failed to remove temporary history cache file" );
156
172
}
157
173
throw new HistoryException ("Failed to rename cache tmpfile." );
158
174
}
159
175
}
160
176
}
161
177
178
+ /**
179
+ * Store history for the whole repository in directory hierarchy resembling
180
+ * the original repository structure. History of individual files will be
181
+ * stored under this hierarchy, each file containing history of
182
+ * corresponding source file.
183
+ *
184
+ * @param history history object to process into per-file histories
185
+ * @param repository repository object
186
+ * @throws HistoryException
187
+ */
162
188
@ Override
163
189
public void store (History history , Repository repository )
164
190
throws HistoryException {
@@ -171,14 +197,24 @@ public void store(History history, Repository repository)
171
197
HashMap <String , List <HistoryEntry >> map =
172
198
new HashMap <String , List <HistoryEntry >>();
173
199
200
+ /*
201
+ * Go through all history entries for this repository (acquired through
202
+ * history/log command executed for top-level directory of the repo
203
+ * and parsed into HistoryEntry structures) and create hash map which
204
+ * maps file names into list of HistoryEntry structures corresponding
205
+ * to changesets in which the file was modified.
206
+ */
174
207
for (HistoryEntry e : history .getHistoryEntries ()) {
175
208
for (String s : e .getFiles ()) {
176
209
List <HistoryEntry > list = map .get (s );
177
210
if (list == null ) {
178
211
list = new ArrayList <HistoryEntry >();
179
212
map .put (s , list );
180
213
}
181
- // We need to do deep copy in order to have different tags per each commit
214
+ /*
215
+ * We need to do deep copy in order to have different tags
216
+ * per each commit.
217
+ */
182
218
if (env .isTagsEnabled () && repository .hasFileBasedTags ()) {
183
219
list .add (new HistoryEntry (e ));
184
220
} else {
@@ -187,6 +223,11 @@ public void store(History history, Repository repository)
187
223
}
188
224
}
189
225
226
+ /*
227
+ * Now traverse the list of files from the hash map built above
228
+ * and for each file store its history (saved in the value of the
229
+ * hash map entry for the file) in a file.
230
+ */
190
231
File root = RuntimeEnvironment .getInstance ().getSourceRootFile ();
191
232
for (Map .Entry <String , List <HistoryEntry >> e : map .entrySet ()) {
192
233
for (HistoryEntry ent : e .getValue ()) {
@@ -279,9 +320,11 @@ public boolean hasCacheForDirectory(File directory, Repository repository)
279
320
File dir = env .getDataRootFile ();
280
321
dir = new File (dir , "historycache" );
281
322
try {
282
- dir = new File (dir , env .getPathRelativeToSourceRoot (new File (repos .getDirectoryName ()), 0 ));
323
+ dir = new File (dir , env .getPathRelativeToSourceRoot (
324
+ new File (repos .getDirectoryName ()), 0 ));
283
325
} catch (IOException e ) {
284
- throw new HistoryException ("Could not resolve " +repos .getDirectoryName ()+" relative to source root" , e );
326
+ throw new HistoryException ("Could not resolve " +
327
+ repos .getDirectoryName ()+" relative to source root" , e );
285
328
}
286
329
return dir .exists ();
287
330
}
0 commit comments