Use file extension with DfsPackDescription get/set file size.

Previously the size getters and setters had explicit methods for index
and pack. Update the api to be based on the file extension. This will
make it possible to support other extensions in the future, such as
the forthcoming bitmap extensions.

Change-Id: Iab9d4abe0af65b2fc71ad71ef1db0feb6b3b5c58
This commit is contained in:
Colby Ranger 2013-01-15 14:24:30 -08:00
parent 5dcc8693d7
commit 510a605546
6 changed files with 31 additions and 40 deletions

View File

@ -45,6 +45,8 @@
import static org.eclipse.jgit.storage.dfs.DfsObjDatabase.PackSource.GC; import static org.eclipse.jgit.storage.dfs.DfsObjDatabase.PackSource.GC;
import static org.eclipse.jgit.storage.dfs.DfsObjDatabase.PackSource.UNREACHABLE_GARBAGE; import static org.eclipse.jgit.storage.dfs.DfsObjDatabase.PackSource.UNREACHABLE_GARBAGE;
import static org.eclipse.jgit.storage.pack.PackConstants.PACK_EXT;
import static org.eclipse.jgit.storage.pack.PackConstants.PACK_INDEX_EXT;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -65,7 +67,6 @@
import org.eclipse.jgit.storage.dfs.DfsObjDatabase.PackSource; import org.eclipse.jgit.storage.dfs.DfsObjDatabase.PackSource;
import org.eclipse.jgit.storage.file.PackIndex; import org.eclipse.jgit.storage.file.PackIndex;
import org.eclipse.jgit.storage.pack.PackConfig; import org.eclipse.jgit.storage.pack.PackConfig;
import org.eclipse.jgit.storage.pack.PackConstants;
import org.eclipse.jgit.storage.pack.PackWriter; import org.eclipse.jgit.storage.pack.PackWriter;
import org.eclipse.jgit.util.io.CountingOutputStream; import org.eclipse.jgit.util.io.CountingOutputStream;
@ -319,25 +320,25 @@ private DfsPackDescription writePack(PackSource source, PackWriter pw,
DfsPackDescription pack = repo.getObjectDatabase().newPack(source); DfsPackDescription pack = repo.getObjectDatabase().newPack(source);
newPackDesc.add(pack); newPackDesc.add(pack);
out = objdb.writeFile(pack, PackConstants.PACK_EXT); out = objdb.writeFile(pack, PACK_EXT);
try { try {
pw.writePack(pm, pm, out); pw.writePack(pm, pm, out);
} finally { } finally {
out.close(); out.close();
} }
out = objdb.writeFile(pack, PackConstants.PACK_INDEX_EXT); out = objdb.writeFile(pack, PACK_INDEX_EXT);
try { try {
CountingOutputStream cnt = new CountingOutputStream(out); CountingOutputStream cnt = new CountingOutputStream(out);
pw.writeIndex(cnt); pw.writeIndex(cnt);
pack.setIndexSize(cnt.getCount()); pack.setFileSize(PACK_INDEX_EXT, cnt.getCount());
} finally { } finally {
out.close(); out.close();
} }
PackWriter.Statistics stats = pw.getStatistics(); PackWriter.Statistics stats = pw.getStatistics();
pack.setPackStats(stats); pack.setPackStats(stats);
pack.setPackSize(stats.getTotalBytes()); pack.setFileSize(PACK_EXT, stats.getTotalBytes());
pack.setObjectCount(stats.getTotalObjects()); pack.setObjectCount(stats.getTotalObjects());
pack.setDeltaCount(stats.getTotalDeltas()); pack.setDeltaCount(stats.getTotalDeltas());
objectsPacked += stats.getTotalObjects(); objectsPacked += stats.getTotalObjects();

View File

@ -153,7 +153,7 @@ public void flush() throws IOException {
throw new IOException(); throw new IOException();
byte[] packHash = packOut.writePackFooter(); byte[] packHash = packOut.writePackFooter();
packDsc.setPackSize(packOut.getCount()); packDsc.setFileSize(PACK_EXT, packOut.getCount());
packOut.close(); packOut.close();
packOut = null; packOut = null;
@ -260,7 +260,7 @@ PackIndex writePackIndex(DfsPackDescription pack, byte[] packHash,
buf.writeTo(cnt, null); buf.writeTo(cnt, null);
else else
index(cnt, packHash, list); index(cnt, packHash, list);
pack.setIndexSize(cnt.getCount()); pack.setFileSize(PACK_INDEX_EXT, cnt.getCount());
} finally { } finally {
os.close(); os.close();
} }

View File

@ -139,7 +139,7 @@ public DfsPackCompactor autoAdd() throws IOException {
DfsObjDatabase objdb = repo.getObjectDatabase(); DfsObjDatabase objdb = repo.getObjectDatabase();
for (DfsPackFile pack : objdb.getPacks()) { for (DfsPackFile pack : objdb.getPacks()) {
DfsPackDescription d = pack.getPackDescription(); DfsPackDescription d = pack.getPackDescription();
if (d.getPackSize() < autoAddSize) if (d.getFileSize(PACK_EXT) < autoAddSize)
add(pack); add(pack);
} }
return this; return this;
@ -290,7 +290,7 @@ private void writePack(DfsObjDatabase objdb, DfsPackDescription pack,
CountingOutputStream cnt = new CountingOutputStream(out); CountingOutputStream cnt = new CountingOutputStream(out);
pw.writePack(pm, pm, cnt); pw.writePack(pm, pm, cnt);
pack.setObjectCount(pw.getObjectCount()); pack.setObjectCount(pw.getObjectCount());
pack.setPackSize(cnt.getCount()); pack.setFileSize(PACK_EXT, cnt.getCount());
} finally { } finally {
out.close(); out.close();
} }
@ -302,7 +302,7 @@ private void writeIndex(DfsObjDatabase objdb, DfsPackDescription pack,
try { try {
CountingOutputStream cnt = new CountingOutputStream(out); CountingOutputStream cnt = new CountingOutputStream(out);
pw.writeIndex(cnt); pw.writeIndex(cnt);
pack.setIndexSize(cnt.getCount()); pack.setFileSize(PACK_INDEX_EXT, cnt.getCount());
} finally { } finally {
out.close(); out.close();
} }

View File

@ -43,6 +43,8 @@
package org.eclipse.jgit.storage.dfs; package org.eclipse.jgit.storage.dfs;
import java.util.HashMap;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
@ -67,9 +69,7 @@ public class DfsPackDescription implements Comparable<DfsPackDescription> {
private long lastModified; private long lastModified;
private long packSize; private Map<String, Long> sizeMap;
private long indexSize;
private long objectCount; private long objectCount;
@ -98,6 +98,7 @@ public DfsPackDescription(DfsRepositoryDescription repoDesc, String name) {
this.repoDesc = repoDesc; this.repoDesc = repoDesc;
int dot = name.lastIndexOf('.'); int dot = name.lastIndexOf('.');
this.packName = (dot < 0) ? name : name.substring(0, dot); this.packName = (dot < 0) ? name : name.substring(0, dot);
this.sizeMap = new HashMap<String, Long>(5);
} }
/** @return description of the repository. */ /** @return description of the repository. */
@ -144,39 +145,27 @@ public DfsPackDescription setLastModified(long timeMillis) {
return this; return this;
} }
/** @return size of the pack, in bytes. If 0 the pack size is not yet known. */
public long getPackSize() {
return packSize;
}
/** /**
* @param ext
* the file extension.
* @param bytes * @param bytes
* size of the pack in bytes. If 0 the size is not known and will * size of the file in bytes. If 0 the file is not known and will
* be determined on first read. * be determined on first read.
* @return {@code this} * @return {@code this}
*/ */
public DfsPackDescription setPackSize(long bytes) { public DfsPackDescription setFileSize(String ext, long bytes) {
packSize = Math.max(0, bytes); sizeMap.put(ext, Long.valueOf(Math.max(0, bytes)));
return this; return this;
} }
/** /**
* @return size of the index, in bytes. If 0 the index size is not yet * @param ext
* known. * the file extension.
* @return size of the file, in bytes. If 0 the file size is not yet known.
*/ */
public long getIndexSize() { public long getFileSize(String ext) {
return indexSize; Long size = sizeMap.get(ext);
} return size == null ? 0 : size.longValue();
/**
* @param bytes
* size of the index in bytes. If 0 the size is not known and
* will be determined on first read.
* @return {@code this}
*/
public DfsPackDescription setIndexSize(long bytes) {
indexSize = Math.max(0, bytes);
return this;
} }
/** /**

View File

@ -165,7 +165,7 @@ public final class DfsPackFile {
this.packDesc = desc; this.packDesc = desc;
this.key = key; this.key = key;
length = desc.getPackSize(); length = desc.getFileSize(PACK_EXT);
if (length <= 0) if (length <= 0)
length = -1; length = -1;
} }

View File

@ -43,6 +43,8 @@
package org.eclipse.jgit.storage.dfs; package org.eclipse.jgit.storage.dfs;
import static org.eclipse.jgit.storage.pack.PackConstants.PACK_EXT;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -58,7 +60,6 @@
import org.eclipse.jgit.lib.ProgressMonitor; import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.storage.file.PackIndex; import org.eclipse.jgit.storage.file.PackIndex;
import org.eclipse.jgit.storage.file.PackLock; import org.eclipse.jgit.storage.file.PackLock;
import org.eclipse.jgit.storage.pack.PackConstants;
import org.eclipse.jgit.transport.PackParser; import org.eclipse.jgit.transport.PackParser;
import org.eclipse.jgit.transport.PackedObjectInfo; import org.eclipse.jgit.transport.PackedObjectInfo;
@ -147,7 +148,7 @@ public PackLock parse(ProgressMonitor receiving, ProgressMonitor resolving)
out = null; out = null;
currBuf = null; currBuf = null;
readBlock = null; readBlock = null;
packDsc.setPackSize(packEnd); packDsc.setFileSize(PACK_EXT, packEnd);
writePackIndex(); writePackIndex();
objdb.commitPack(Collections.singletonList(packDsc), null); objdb.commitPack(Collections.singletonList(packDsc), null);
@ -206,7 +207,7 @@ protected void onPackHeader(long objectCount) throws IOException {
packDsc = objdb.newPack(DfsObjDatabase.PackSource.RECEIVE); packDsc = objdb.newPack(DfsObjDatabase.PackSource.RECEIVE);
packKey = new DfsPackKey(); packKey = new DfsPackKey();
out = objdb.writeFile(packDsc, PackConstants.PACK_EXT); out = objdb.writeFile(packDsc, PACK_EXT);
int size = out.blockSize(); int size = out.blockSize();
if (size <= 0) if (size <= 0)
size = blockCache.getBlockSize(); size = blockCache.getBlockSize();