package org.eclipse.jgit.internal.storage.dfs;
-import java.lang.ref.SoftReference;
/**
* Caches recently used objects for {@link DfsReader}.
}
private int maxByteCount;
+ private int curByteCount;
- private final Slot[] table;
-
- private Slot lruHead;
+ private final Entry[] table;
- private Slot lruTail;
-
- private int curByteCount;
+ private Entry lruHead;
+ private Entry lruTail;
DeltaBaseCache(DfsReader reader) {
this(reader.getOptions().getDeltaBaseCacheLimit());
DeltaBaseCache(int maxBytes) {
maxByteCount = maxBytes;
- table = new Slot[1 << TABLE_BITS];
+ table = new Entry[1 << TABLE_BITS];
}
Entry get(DfsPackKey key, long position) {
- Slot e = table[hash(position)];
+ Entry e = table[hash(position)];
for (; e != null; e = e.tableNext) {
if (e.offset == position && key.equals(e.pack)) {
- Entry buf = e.data.get();
- if (buf != null) {
- moveToHead(e);
- return buf;
- }
- return null;
+ moveToHead(e);
+ return e;
}
}
return null;
releaseMemory();
int tableIdx = hash(offset);
- Slot e = new Slot(key, offset, data.length);
- e.data = new SoftReference<Entry>(new Entry(data, objectType));
+ Entry e = new Entry(key, offset, objectType, data);
e.tableNext = table[tableIdx];
table[tableIdx] = e;
lruPushHead(e);
private void releaseMemory() {
while (curByteCount > maxByteCount && lruTail != null) {
- Slot e = lruTail;
- curByteCount -= e.size;
+ Entry e = lruTail;
+ curByteCount -= e.data.length;
lruRemove(e);
removeFromTable(e);
}
}
- private void removeFromTable(Slot e) {
+ private void removeFromTable(Entry e) {
int tableIdx = hash(e.offset);
- Slot p = table[tableIdx];
+ Entry p = table[tableIdx];
if (p == e) {
table[tableIdx] = e.tableNext;
e.pack, Long.valueOf(e.offset)));
}
- private void moveToHead(final Slot e) {
+ private void moveToHead(Entry e) {
if (e != lruHead) {
lruRemove(e);
lruPushHead(e);
}
}
- private void lruRemove(final Slot e) {
- Slot p = e.lruPrev;
- Slot n = e.lruNext;
+ private void lruRemove(Entry e) {
+ Entry p = e.lruPrev;
+ Entry n = e.lruNext;
if (p != null) {
p.lruNext = n;
}
}
- private void lruPushHead(final Slot e) {
- Slot n = lruHead;
+ private void lruPushHead(Entry e) {
+ Entry n = lruHead;
e.lruNext = n;
if (n != null)
n.lruPrev = e;
int getMemoryUsedByLruChainForTest() {
int r = 0;
- for (Slot e = lruHead; e != null; e = e.lruNext) {
- r += e.size;
+ for (Entry e = lruHead; e != null; e = e.lruNext) {
+ r += e.data.length;
}
return r;
}
int getMemoryUsedByTableForTest() {
int r = 0;
for (int i = 0; i < table.length; i++) {
- for (Slot e = table[i]; e != null; e = e.tableNext) {
- r += e.size;
+ for (Entry e = table[i]; e != null; e = e.tableNext) {
+ r += e.data.length;
}
}
return r;
}
static class Entry {
- final byte[] data;
-
- final int type;
-
- Entry(final byte[] aData, final int aType) {
- data = aData;
- type = aType;
- }
- }
-
- private static class Slot {
final DfsPackKey pack;
-
final long offset;
+ final int type;
+ final byte[] data;
- final int size;
-
- Slot tableNext;
-
- Slot lruPrev;
-
- Slot lruNext;
-
- SoftReference<Entry> data;
+ Entry tableNext;
+ Entry lruPrev;
+ Entry lruNext;
- Slot(DfsPackKey key, long offset, int size) {
+ Entry(DfsPackKey key, long offset, int type, byte[] data) {
this.pack = key;
this.offset = offset;
- this.size = size;
+ this.type = type;
+ this.data = data;
}
}
}