Skip to content

Commit 6c0a4b5

Browse files
committed
style (line wraps), add explanatory comments
1 parent 17d95a6 commit 6c0a4b5

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

src/org/opensolaris/opengrok/history/FileHistoryCache.java

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@
4646
import org.opensolaris.opengrok.OpenGrokLogger;
4747
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
4848

49+
/*
50+
* Class representing file based storage of per source file history.
51+
*/
4952
class FileHistoryCache implements HistoryCache {
5053
private final Object lock = new Object();
5154

5255
static class FilePersistenceDelegate extends PersistenceDelegate {
5356
@Override
5457
protected Expression instantiate(Object oldInstance, Encoder out) {
5558
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()});
5761
}
5862
}
5963

@@ -95,7 +99,8 @@ private static File getCachedFile(File file) throws HistoryException {
9599
sb.append(add);
96100
sb.append(".gz");
97101
} 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);
99104
}
100105

101106
return new File(sb.toString());
@@ -112,6 +117,13 @@ private static History readCache(File file) throws IOException {
112117
}
113118
}
114119

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+
*/
115127
private void storeFile(History history, File file) throws HistoryException {
116128

117129
File cache = getCachedFile(file);
@@ -135,8 +147,10 @@ private void storeFile(History history, File file) throws HistoryException {
135147
output = File.createTempFile("oghist", null, dir);
136148
try (FileOutputStream out = new FileOutputStream(output);
137149
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());
140154
e.writeObject(history);
141155
}
142156
} catch (IOException ioe) {
@@ -145,20 +159,32 @@ private void storeFile(History history, File file) throws HistoryException {
145159
synchronized (lock) {
146160
if (!cache.delete() && cache.exists()) {
147161
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");
149164
}
150165
throw new HistoryException(
151166
"Cachefile exists, and I could not delete it.");
152167
}
153168
if (!output.renameTo(cache)) {
154169
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");
156172
}
157173
throw new HistoryException("Failed to rename cache tmpfile.");
158174
}
159175
}
160176
}
161177

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+
*/
162188
@Override
163189
public void store(History history, Repository repository)
164190
throws HistoryException {
@@ -171,14 +197,24 @@ public void store(History history, Repository repository)
171197
HashMap<String, List<HistoryEntry>> map =
172198
new HashMap<String, List<HistoryEntry>>();
173199

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+
*/
174207
for (HistoryEntry e : history.getHistoryEntries()) {
175208
for (String s : e.getFiles()) {
176209
List<HistoryEntry> list = map.get(s);
177210
if (list == null) {
178211
list = new ArrayList<HistoryEntry>();
179212
map.put(s, list);
180213
}
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+
*/
182218
if (env.isTagsEnabled() && repository.hasFileBasedTags()) {
183219
list.add(new HistoryEntry(e));
184220
} else {
@@ -187,6 +223,11 @@ public void store(History history, Repository repository)
187223
}
188224
}
189225

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+
*/
190231
File root = RuntimeEnvironment.getInstance().getSourceRootFile();
191232
for (Map.Entry<String, List<HistoryEntry>> e : map.entrySet()) {
192233
for (HistoryEntry ent : e.getValue()) {
@@ -279,9 +320,11 @@ public boolean hasCacheForDirectory(File directory, Repository repository)
279320
File dir = env.getDataRootFile();
280321
dir = new File(dir, "historycache");
281322
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));
283325
} 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);
285328
}
286329
return dir.exists();
287330
}

0 commit comments

Comments
 (0)