PackWriter: write the PackReverseIndex file

PackWriter offers the ability to write out the pack file and its various
index files, except for the newly introduced file-based reverse index.

Now that PackReverseIndexWriter can write reverse index files,
PackWriter#writeReverseIndex will write one for a pack if the
corresponding config flag PackConfig#writeReverseIndex is on.

Change-Id: Ib75dd2bbfb9ee9366d5aacb46700d8cf8af4823a
Signed-off-by: Anna Papitto <annapapitto@google.com>
This commit is contained in:
Anna Papitto 2023-05-02 13:45:05 -07:00
parent 140a8b365a
commit ce88e62edc
5 changed files with 117 additions and 3 deletions

View File

@ -541,6 +541,39 @@ public void testWriteObjectSizeIndex_noDeltas() throws Exception {
assertEquals(18787, objSizeIdx.getSize(idx.findPosition(knownBlob2)));
}
@Test
public void testWriteReverseIndexConfig() {
assertFalse(config.isWriteReverseIndex());
config.setWriteReverseIndex(true);
assertTrue(config.isWriteReverseIndex());
}
@Test
public void testWriteReverseIndexOff() throws Exception {
config.setWriteReverseIndex(false);
writer = new PackWriter(config, db.newObjectReader());
ByteArrayOutputStream reverseIndexOutput = new ByteArrayOutputStream();
writer.writeReverseIndex(reverseIndexOutput);
assertEquals(0, reverseIndexOutput.size());
}
@Test
public void testWriteReverseIndexOn() throws Exception {
config.setWriteReverseIndex(true);
writeVerifyPack4(false);
ByteArrayOutputStream reverseIndexOutput = new ByteArrayOutputStream();
int headerBytes = 12;
int bodyBytes = 12;
int footerBytes = 40;
writer.writeReverseIndex(reverseIndexOutput);
assertTrue(reverseIndexOutput.size() == headerBytes + bodyBytes
+ footerBytes);
}
@Test
public void testExclude() throws Exception {
// TestRepository closes repo

View File

@ -577,6 +577,7 @@ private void packGarbage(ProgressMonitor pm) throws IOException {
cfg.setReuseObjects(true);
cfg.setDeltaCompress(false);
cfg.setBuildBitmaps(false);
cfg.setWriteReverseIndex(false);
try (PackWriter pw = new PackWriter(cfg, ctx);
RevWalk pool = new RevWalk(ctx)) {

View File

@ -62,6 +62,7 @@
import org.eclipse.jgit.internal.storage.file.PackBitmapIndexWriterV1;
import org.eclipse.jgit.internal.storage.file.PackIndexWriter;
import org.eclipse.jgit.internal.storage.file.PackObjectSizeIndexWriter;
import org.eclipse.jgit.internal.storage.file.PackReverseIndexWriter;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.AsyncObjectSizeQueue;
import org.eclipse.jgit.lib.BatchingProgressMonitor;
@ -1136,6 +1137,38 @@ public void writeObjectSizeIndex(OutputStream objIdxStream)
stats.timeWriting += System.currentTimeMillis() - writeStart;
}
/**
* Whether the writer will write a reverse index file. The configuration
* flag must be on and the writer must be able to write corresponding
* forward index.
*
* @return whether the writer will write a reverse index file
*/
public boolean isReverseIndexEnabled() {
// Only write the reverse index if the writer is configured to and the
// forward index that it would correspond to will be written.
return config.isWriteReverseIndex() && !isIndexDisabled();
}
/**
* Write the pack's reverse index file to the output stream.
*
* @param stream
* where to write the file contents to
* @throws IOException
* if writing to the stream fails
*/
public void writeReverseIndex(OutputStream stream) throws IOException {
if (!isReverseIndexEnabled()) {
return;
}
long writeStart = System.currentTimeMillis();
PackReverseIndexWriter writer = PackReverseIndexWriter
.createWriter(stream);
writer.write(sortByName(), packcsum);
stats.timeWriting += System.currentTimeMillis() - writeStart;
}
/**
* Create a bitmap index file to match the pack file just written.
* <p>

View File

@ -744,6 +744,13 @@ public final class ConfigConstants {
*/
public static final String CONFIG_KEY_BITMAP_RECENT_COMMIT_COUNT = "bitmaprecentcommitspan";
/**
* The "pack.writeReverseIndex" key
*
* @since 6.6
*/
public static final String CONFIG_KEY_WRITE_REVERSE_INDEX = "writeReverseIndex";
/**
* The "pack.buildBitmaps" key
* @since 5.8

View File

@ -27,7 +27,10 @@
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_DELTA_COMPRESSION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_DEPTH;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_INDEXVERSION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_MIN_BYTES_OBJ_SIZE_INDEX;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_MIN_SIZE_PREVENT_RACYPACK;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PRESERVE_OLD_PACKS;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PRUNE_PRESERVED;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REUSE_DELTAS;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REUSE_OBJECTS;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_SEARCH_FOR_REUSE_TIMEOUT;
@ -36,10 +39,8 @@
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WAIT_PREVENT_RACYPACK;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WINDOW;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WINDOW_MEMORY;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_MIN_BYTES_OBJ_SIZE_INDEX;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WRITE_REVERSE_INDEX;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_PACK_SECTION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PRESERVE_OLD_PACKS;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PRUNE_PRESERVED;
import java.time.Duration;
import java.util.concurrent.Executor;
@ -161,6 +162,14 @@ public class PackConfig {
*/
public static final int DEFAULT_INDEX_VERSION = 2;
/**
* Default value of the write reverse index option: {@value}
*
* @see #setWriteReverseIndex(boolean)
* @since 6.6
*/
public static final boolean DEFAULT_WRITE_REVERSE_INDEX = false;
/**
* Default value of the build bitmaps option: {@value}
*
@ -292,6 +301,8 @@ public class PackConfig {
private int indexVersion = DEFAULT_INDEX_VERSION;
private boolean writeReverseIndex = DEFAULT_WRITE_REVERSE_INDEX;
private boolean buildBitmaps = DEFAULT_BUILD_BITMAPS;
private int bitmapContiguousCommitCount = DEFAULT_BITMAP_CONTIGUOUS_COMMIT_COUNT;
@ -373,6 +384,7 @@ public PackConfig(PackConfig cfg) {
this.threads = cfg.threads;
this.executor = cfg.executor;
this.indexVersion = cfg.indexVersion;
this.writeReverseIndex = cfg.writeReverseIndex;
this.buildBitmaps = cfg.buildBitmaps;
this.bitmapContiguousCommitCount = cfg.bitmapContiguousCommitCount;
this.bitmapRecentCommitCount = cfg.bitmapRecentCommitCount;
@ -973,6 +985,31 @@ public void setIndexVersion(int version) {
indexVersion = version;
}
/**
* True if the writer should write reverse index files.
*
* Default setting: {@value #DEFAULT_WRITE_REVERSE_INDEX}
*
* @return whether the writer should write reverse index files
* @since 6.6
*/
public boolean isWriteReverseIndex() {
return writeReverseIndex;
}
/**
* Set whether the writer will write reverse index files.
*
* Default setting: {@value #DEFAULT_WRITE_REVERSE_INDEX}
*
* @param writeReverseIndex
* whether the writer should write reverse index files
* @since 6.6
*/
public void setWriteReverseIndex(boolean writeReverseIndex) {
this.writeReverseIndex = writeReverseIndex;
}
/**
* True if writer is allowed to build bitmaps for indexes.
*
@ -1286,6 +1323,8 @@ public void fromConfig(Config rc) {
setSinglePack(rc.getBoolean(CONFIG_PACK_SECTION,
CONFIG_KEY_SINGLE_PACK,
getSinglePack()));
setWriteReverseIndex(rc.getBoolean(CONFIG_PACK_SECTION,
CONFIG_KEY_WRITE_REVERSE_INDEX, isWriteReverseIndex()));
setBuildBitmaps(rc.getBoolean(CONFIG_PACK_SECTION,
CONFIG_KEY_BUILD_BITMAPS, isBuildBitmaps()));
setBitmapContiguousCommitCount(rc.getInt(CONFIG_PACK_SECTION,
@ -1347,6 +1386,7 @@ public String toString() {
b.append(", reuseDeltas=").append(isReuseDeltas()); //$NON-NLS-1$
b.append(", reuseObjects=").append(isReuseObjects()); //$NON-NLS-1$
b.append(", deltaCompress=").append(isDeltaCompress()); //$NON-NLS-1$
b.append(", writeReverseIndex=").append(isWriteReverseIndex()); // $NON-NLS-1$
b.append(", buildBitmaps=").append(isBuildBitmaps()); //$NON-NLS-1$
b.append(", bitmapContiguousCommitCount=") //$NON-NLS-1$
.append(getBitmapContiguousCommitCount());