Browse Source

Pack: Replace extensions bitset with bitmapIdx PackFile

The only extension that was ever consulted from the bitmap was the
bitmap index. We can simplify the Pack code as well as the code of
all the callers if we focus on just that usage.

Change-Id: I799ddfdee93142af67ce5081d14a430d36aa4c15
Signed-off-by: Nasser Grainawi <quic_nasserg@quicinc.com>
tags/v5.11.0.202103091610-r
Nasser Grainawi 3 years ago
parent
commit
7fbff35887

+ 1
- 1
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackTest.java View File

new PackIndexWriterV1(f).write(list, footer); new PackIndexWriterV1(f).write(list, footer);
} }


Pack pack = new Pack(packName, PackExt.INDEX.getBit());
Pack pack = new Pack(packName, null);
try { try {
pack.get(wc, b); pack.get(wc, b);
fail("expected LargeObjectException.ExceedsByteArrayLimit"); fail("expected LargeObjectException.ExceedsByteArrayLimit");

+ 21
- 22
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java View File

package org.eclipse.jgit.internal.storage.file; package org.eclipse.jgit.internal.storage.file;


import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK; import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;


import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;


import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.pack.ObjectToPack; import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.eclipse.jgit.internal.storage.pack.PackWriter; import org.eclipse.jgit.internal.storage.pack.PackWriter;
import org.eclipse.jgit.lib.AbbreviatedObjectId; import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
* Add a single existing pack to the list of available pack files. * Add a single existing pack to the list of available pack files.
*/ */
@Override @Override
public Pack openPack(File pack)
throws IOException {
final String p = pack.getName();
if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, pack));
// The pack and index are assumed to exist. The existence of other
// extensions needs to be explicitly checked.
//
int extensions = PACK.getBit() | INDEX.getBit();
final String base = p.substring(0, p.length() - 4);
for (PackExt ext : PackExt.values()) {
if ((extensions & ext.getBit()) == 0) {
final String name = base + ext.getExtension();
if (new File(pack.getParentFile(), name).exists())
extensions |= ext.getBit();
}
}
Pack res = new Pack(pack, extensions);
public Pack openPack(File pack) throws IOException {
PackFile pf;
try {
pf = new PackFile(pack);
} catch (IllegalArgumentException e) {
throw new IOException(
MessageFormat.format(JGitText.get().notAValidPack, pack),
e);
}
String p = pf.getName();
// TODO(nasserg): See if PackFile can do these checks instead
if (p.length() != 50 || !p.startsWith("pack-") //$NON-NLS-1$
|| !pf.getPackExt().equals(PACK)) {
throw new IOException(
MessageFormat.format(JGitText.get().notAValidPack, pack));
}
PackFile bitmapIdx = pf.create(BITMAP_INDEX);
Pack res = new Pack(pack, bitmapIdx.exists() ? bitmapIdx : null);
packed.insert(res); packed.insert(res);
return res; return res;
} }

+ 17
- 21
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java View File



package org.eclipse.jgit.internal.storage.file; package org.eclipse.jgit.internal.storage.file;


import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX; import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.KEEP; import static org.eclipse.jgit.internal.storage.pack.PackExt.KEEP;


import java.util.zip.DataFormatException; import java.util.zip.DataFormatException;
import java.util.zip.Inflater; import java.util.zip.Inflater;


import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.LargeObjectException; import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.pack.BinaryDelta; import org.eclipse.jgit.internal.storage.pack.BinaryDelta;
import org.eclipse.jgit.internal.storage.pack.ObjectToPack; import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.eclipse.jgit.internal.storage.pack.PackOutputStream; import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
import org.eclipse.jgit.lib.AbbreviatedObjectId; import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;


private final PackFile packFile; private final PackFile packFile;


private final int extensions;

private PackFile keepFile; private PackFile keepFile;


final int hash; final int hash;


private volatile Exception invalidatingCause; private volatile Exception invalidatingCause;


private boolean invalidBitmap;
@Nullable
private PackFile bitmapIdxFile;


private AtomicInteger transientErrorCount = new AtomicInteger(); private AtomicInteger transientErrorCount = new AtomicInteger();


* *
* @param packFile * @param packFile
* path of the <code>.pack</code> file holding the data. * path of the <code>.pack</code> file holding the data.
* @param extensions
* additional pack file extensions with the same base as the pack
* @param bitmapIdxFile
* existing bitmap index file with the same base as the pack
*/ */
public Pack(File packFile, int extensions) {
public Pack(File packFile, @Nullable PackFile bitmapIdxFile) {
this.packFile = new PackFile(packFile); this.packFile = new PackFile(packFile);
this.fileSnapshot = PackFileSnapshot.save(packFile); this.fileSnapshot = PackFileSnapshot.save(packFile);
this.packLastModified = fileSnapshot.lastModifiedInstant(); this.packLastModified = fileSnapshot.lastModifiedInstant();
this.extensions = extensions;
this.bitmapIdxFile = bitmapIdxFile;


// Multiply by 31 here so we can more directly combine with another // Multiply by 31 here so we can more directly combine with another
// value in WindowCache.hash(), without doing the multiply there. // value in WindowCache.hash(), without doing the multiply there.
} }


synchronized PackBitmapIndex getBitmapIndex() throws IOException { synchronized PackBitmapIndex getBitmapIndex() throws IOException {
if (invalid || invalidBitmap)
if (invalid || bitmapIdxFile == null) {
return null; return null;
if (bitmapIdx == null && hasExt(BITMAP_INDEX)) {
}
if (bitmapIdx == null) {
final PackBitmapIndex idx; final PackBitmapIndex idx;
try { try {
idx = PackBitmapIndex.open(packFile.create(BITMAP_INDEX), idx(),
idx = PackBitmapIndex.open(bitmapIdxFile, idx(),
getReverseIdx()); getReverseIdx());
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// Once upon a time this bitmap file existed. Now it // Once upon a time this bitmap file existed. Now it
// has been removed. Most likely an external gc has // has been removed. Most likely an external gc has
// removed this packfile and the bitmap // removed this packfile and the bitmap
invalidBitmap = true;
return null;
bitmapIdxFile = null;
return null;
} }


// At this point, idx() will have set packChecksum. // At this point, idx() will have set packChecksum.
if (Arrays.equals(packChecksum, idx.packChecksum))
if (Arrays.equals(packChecksum, idx.packChecksum)) {
bitmapIdx = idx; bitmapIdx = idx;
else
invalidBitmap = true;
} else {
bitmapIdxFile = null;
}
} }
return bitmapIdx; return bitmapIdx;
} }
} }
} }


private boolean hasExt(PackExt ext) {
return (extensions & ext.getBit()) != 0;
}

@SuppressWarnings("nls") @SuppressWarnings("nls")
@Override @Override
public String toString() { public String toString() {

+ 1
- 4
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackDirectory.java View File

continue; continue;
} }


list.add(new Pack(packFile,
packFilesByExt.containsKey(BITMAP_INDEX)
? BITMAP_INDEX.getBit()
: 0));
list.add(new Pack(packFile, packFilesByExt.get(BITMAP_INDEX)));
foundNew = true; foundNew = true;
} }



Loading…
Cancel
Save