[JGIT PATCH 04/13] Make RefDatabase thread-safe
This is necessary to support a thread-safe Repository.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
.../src/org/spearce/jgit/lib/RefDatabase.java | 29 +++++++++++---------
1 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
index 4cf6e08..87f26bf 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
@@ -93,7 +93,7 @@ RefDatabase(final Repository r) {
clearCache();
}
- void clearCache() {
+ synchronized void clearCache() {
looseRefs = new HashMap<String, Ref>();
looseRefsMTime = new HashMap<String, Long>();
packedRefs = new HashMap<String, Ref>();
@@ -139,9 +139,11 @@ RefUpdate newUpdate(final String name) throws IOException {
}
void stored(final String origName, final String name, final ObjectId id, final long time) {
- looseRefs.put(name, new Ref(Ref.Storage.LOOSE, origName, name, id));
- looseRefsMTime.put(name, time);
- setModified();
+ synchronized (this) {
+ looseRefs.put(name, new Ref(Ref.Storage.LOOSE, origName, name, id));
+ looseRefsMTime.put(name, time);
+ setModified();
+ }
db.fireRefsMaybeChanged();
}
@@ -157,11 +159,13 @@ void stored(final String origName, final String name, final ObjectId id, final l
void link(final String name, final String target) throws IOException {
final byte[] content = Constants.encode("ref: " + target + "\n");
lockAndWriteFile(fileForRef(name), content);
- setModified();
+ synchronized (this) {
+ setModified();
+ }
db.fireRefsMaybeChanged();
}
- void setModified() {
+ private void setModified() {
lastRefModification = refModificationCounter++;
}
@@ -210,7 +214,7 @@ Ref readRef(final String partialName) throws IOException {
return avail;
}
- private void readPackedRefs(final Map<String, Ref> avail) {
+ private synchronized void readPackedRefs(final Map<String, Ref> avail) {
refreshPackedRefs();
avail.putAll(packedRefs);
}
@@ -229,7 +233,7 @@ private void readLooseRefs(final Map<String, Ref> avail,
}
}
- private void readOneLooseRef(final Map<String, Ref> avail,
+ private synchronized void readOneLooseRef(final Map<String, Ref> avail,
final String origName, final String refName, final File ent) {
// Unchanged and cached? Don't read it again.
//
@@ -323,8 +327,8 @@ private Ref readRefBasic(final String name, final int depth) throws IOException
return readRefBasic(name, name, depth);
}
- private Ref readRefBasic(final String origName, final String name, final int depth)
- throws IOException {
+ private synchronized Ref readRefBasic(final String origName,
+ final String name, final int depth) throws IOException {
// Prefer loose ref to packed ref as the loose
// file can be more up-to-date than a packed one.
//
@@ -408,7 +412,7 @@ private Ref readRefBasic(final String origName, final String name, final int dep
return ref;
}
- private void refreshPackedRefs() {
+ private synchronized void refreshPackedRefs() {
final long currTime = packedRefsFile.lastModified();
final long currLen = currTime == 0 ? 0 : packedRefsFile.length();
if (currTime == packedRefsLastModified && currLen == packedRefsLength)
@@ -479,7 +483,7 @@ private void lockAndWriteFile(File file, byte[] content) throws IOException {
throw new ObjectWritingException("Unable to write " + name);
}
- void removePackedRef(String name) throws IOException {
+ synchronized void removePackedRef(String name) throws IOException {
packedRefs.remove(name);
writePackedRefs();
}
@@ -508,5 +512,4 @@ private static BufferedReader openReader(final File fileLocation)
return new BufferedReader(new InputStreamReader(new FileInputStream(
fileLocation), Constants.CHARSET));
}
-
}
--
1.6.1.rc4.301.g5497a