Browse Source

Refactor skip garbage pack logic into a method

There are multiple places in DfsReader to skip garbage pack if both of
the following conditions satisfied:

* AvoidUnreachable flag is set
* The pack is a garabge pack

Refactor them into a shared private method.

Change-Id: I67d6bb601db55f904437c807c6a3c36f0a723265
Signed-off-by: Zhen Chen <czhen@google.com>
stable-4.7
Zhen Chen 8 years ago
parent
commit
8dd5b644dc
  1. 38
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReader.java

38
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReader.java

@ -165,20 +165,19 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
throws IOException { throws IOException {
if (id.isComplete()) if (id.isComplete())
return Collections.singleton(id.toObjectId()); return Collections.singleton(id.toObjectId());
boolean noGarbage = avoidUnreachable;
HashSet<ObjectId> matches = new HashSet<ObjectId>(4); HashSet<ObjectId> matches = new HashSet<ObjectId>(4);
PackList packList = db.getPackList(); PackList packList = db.getPackList();
resolveImpl(packList, id, noGarbage, matches); resolveImpl(packList, id, matches);
if (matches.size() < MAX_RESOLVE_MATCHES && packList.dirty()) { if (matches.size() < MAX_RESOLVE_MATCHES && packList.dirty()) {
resolveImpl(db.scanPacks(packList), id, noGarbage, matches); resolveImpl(db.scanPacks(packList), id, matches);
} }
return matches; return matches;
} }
private void resolveImpl(PackList packList, AbbreviatedObjectId id, private void resolveImpl(PackList packList, AbbreviatedObjectId id,
boolean noGarbage, HashSet<ObjectId> matches) throws IOException { HashSet<ObjectId> matches) throws IOException {
for (DfsPackFile pack : packList.packs) { for (DfsPackFile pack : packList.packs) {
if (noGarbage && pack.isGarbage()) { if (skipGarbagePack(pack)) {
continue; continue;
} }
pack.resolve(this, matches, id, MAX_RESOLVE_MATCHES); pack.resolve(this, matches, id, MAX_RESOLVE_MATCHES);
@ -192,20 +191,19 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
public boolean has(AnyObjectId objectId) throws IOException { public boolean has(AnyObjectId objectId) throws IOException {
if (last != null && last.hasObject(this, objectId)) if (last != null && last.hasObject(this, objectId))
return true; return true;
boolean noGarbage = avoidUnreachable;
PackList packList = db.getPackList(); PackList packList = db.getPackList();
if (hasImpl(packList, objectId, noGarbage)) { if (hasImpl(packList, objectId)) {
return true; return true;
} else if (packList.dirty()) { } else if (packList.dirty()) {
return hasImpl(db.scanPacks(packList), objectId, noGarbage); return hasImpl(db.scanPacks(packList), objectId);
} }
return false; return false;
} }
private boolean hasImpl(PackList packList, AnyObjectId objectId, private boolean hasImpl(PackList packList, AnyObjectId objectId)
boolean noGarbage) throws IOException { throws IOException {
for (DfsPackFile pack : packList.packs) { for (DfsPackFile pack : packList.packs) {
if (pack == last || (noGarbage && pack.isGarbage())) if (pack == last || skipGarbagePack(pack))
continue; continue;
if (pack.hasObject(this, objectId)) { if (pack.hasObject(this, objectId)) {
last = pack; last = pack;
@ -228,13 +226,12 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
} }
PackList packList = db.getPackList(); PackList packList = db.getPackList();
boolean noGarbage = avoidUnreachable; ldr = openImpl(packList, objectId);
ldr = openImpl(packList, objectId, noGarbage);
if (ldr != null) { if (ldr != null) {
return checkType(ldr, objectId, typeHint); return checkType(ldr, objectId, typeHint);
} }
if (packList.dirty()) { if (packList.dirty()) {
ldr = openImpl(db.scanPacks(packList), objectId, noGarbage); ldr = openImpl(db.scanPacks(packList), objectId);
if (ldr != null) { if (ldr != null) {
return checkType(ldr, objectId, typeHint); return checkType(ldr, objectId, typeHint);
} }
@ -254,10 +251,10 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
return ldr; return ldr;
} }
private ObjectLoader openImpl(PackList packList, AnyObjectId objectId, private ObjectLoader openImpl(PackList packList, AnyObjectId objectId)
boolean noGarbage) throws IOException { throws IOException {
for (DfsPackFile pack : packList.packs) { for (DfsPackFile pack : packList.packs) {
if (pack == last || (noGarbage && pack.isGarbage())) { if (pack == last || skipGarbagePack(pack)) {
continue; continue;
} }
ObjectLoader ldr = pack.get(this, objectId); ObjectLoader ldr = pack.get(this, objectId);
@ -332,7 +329,6 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
} }
int lastIdx = 0; int lastIdx = 0;
DfsPackFile lastPack = packs[lastIdx]; DfsPackFile lastPack = packs[lastIdx];
boolean noGarbage = avoidUnreachable;
OBJECT_SCAN: for (Iterator<T> it = pending.iterator(); it.hasNext();) { OBJECT_SCAN: for (Iterator<T> it = pending.iterator(); it.hasNext();) {
T t = it.next(); T t = it.next();
@ -351,7 +347,7 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
if (i == lastIdx) if (i == lastIdx)
continue; continue;
DfsPackFile pack = packs[i]; DfsPackFile pack = packs[i];
if (noGarbage && pack.isGarbage()) if (skipGarbagePack(pack))
continue; continue;
try { try {
long p = pack.findOffset(this, t); long p = pack.findOffset(this, t);
@ -371,6 +367,10 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
last = lastPack; last = lastPack;
} }
private boolean skipGarbagePack(DfsPackFile pack) {
return avoidUnreachable && pack.isGarbage();
}
@Override @Override
public <T extends ObjectId> AsyncObjectLoaderQueue<T> open( public <T extends ObjectId> AsyncObjectLoaderQueue<T> open(
Iterable<T> objectIds, final boolean reportMissing) { Iterable<T> objectIds, final boolean reportMissing) {

Loading…
Cancel
Save