cfg.setMaxIndexLevels(indexLevels);
}
- ReftableWriter w = new ReftableWriter(cfg);
+ ReftableWriter w = new ReftableWriter(cfg, os);
w.setMinUpdateIndex(min(logs)).setMaxUpdateIndex(max(logs));
- w.begin(os);
+ w.begin();
w.sortAndWriteRefs(refs);
for (LogEntry e : logs) {
w.writeLog(e.ref, e.updateIndex, e.who,
DfsPackDescription t1 = odb.newPack(INSERT);
try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) {
- new ReftableWriter().begin(out).finish();
+ new ReftableWriter(out).begin().finish();
t1.addFileExt(REFTABLE);
}
odb.commitPack(Collections.singleton(t1), null);
DfsPackDescription t1 = odb.newPack(INSERT);
try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) {
- new ReftableWriter().begin(out).finish();
+ new ReftableWriter(out).begin().finish();
t1.addFileExt(REFTABLE);
}
odb.commitPack(Collections.singleton(t1), null);
Ref next = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE,
"refs/heads/next", commit0.copy());
try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) {
- ReftableWriter w = new ReftableWriter();
+ ReftableWriter w = new ReftableWriter(out);
w.setMinUpdateIndex(42);
w.setMaxUpdateIndex(42);
- w.begin(out);
+ w.begin();
w.sortAndWriteRefs(Collections.singleton(next));
w.finish();
t1.addFileExt(REFTABLE);
Ref newNext = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, NEXT,
commit1);
try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) {
- ReftableWriter w = new ReftableWriter();
- w.setMinUpdateIndex(1);
- w.setMaxUpdateIndex(1);
- w.begin(out);
+ ReftableWriter w = new ReftableWriter(out)
+ .setMinUpdateIndex(1)
+ .setMaxUpdateIndex(1)
+ .begin();
w.writeRef(newNext, 1);
w.finish();
t1.addFileExt(REFTABLE);
Ref newNext = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, NEXT,
commit1);
try (DfsOutputStream out = odb.writeFile(t1, REFTABLE)) {
- ReftableWriter w = new ReftableWriter();
- w.setMinUpdateIndex(1);
- w.setMaxUpdateIndex(1);
- w.begin(out);
+ ReftableWriter w = new ReftableWriter(out)
+ .setMinUpdateIndex(1)
+ .setMaxUpdateIndex(1)
+ .begin();
w.writeRef(newNext, 1);
w.finish();
t1.addFileExt(REFTABLE);
@Test
public void missedUpdate() throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(buf)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(3)
- .begin(buf);
+ .begin();
writer.writeRef(ref("refs/heads/a", 1), 1);
writer.writeRef(ref("refs/heads/c", 3), 3);
writer.finish();
List<Ref> delta2 = Arrays.asList(delete("refs/heads/next"));
List<Ref> delta3 = Arrays.asList(ref("refs/heads/master", 8));
- ReftableCompactor compactor = new ReftableCompactor();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ReftableCompactor compactor = new ReftableCompactor(out);
compactor.addAll(Arrays.asList(
read(write(delta1)),
read(write(delta2)),
read(write(delta3))));
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- compactor.compact(out);
+ compactor.compact();
byte[] table = out.toByteArray();
ReftableReader reader = read(table);
private byte[] write(Collection<Ref> refs, long updateIndex)
throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- new ReftableWriter()
+ new ReftableWriter(buffer)
.setMinUpdateIndex(updateIndex)
.setMaxUpdateIndex(updateIndex)
- .begin(buffer)
+ .begin()
.sortAndWriteRefs(refs)
.finish();
return buffer.toByteArray();
@Test
public void noTables() throws IOException {
- ReftableCompactor compactor = new ReftableCompactor();
+ ReftableCompactor compactor;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
- compactor.compact(out);
+ compactor = new ReftableCompactor(out);
+ compactor.compact();
}
Stats stats = compactor.getStats();
assertEquals(0, stats.minUpdateIndex());
public void oneTable() throws IOException {
byte[] inTab;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(0)
.setMaxUpdateIndex(0)
- .begin(inBuf);
+ .begin();
writer.writeRef(ref(MASTER, 1));
writer.finish();
}
byte[] outTab;
- ReftableCompactor compactor = new ReftableCompactor();
+ ReftableCompactor compactor;
try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
+ compactor = new ReftableCompactor(outBuf);
compactor.tryAddFirst(read(inTab));
- compactor.compact(outBuf);
+ compactor.compact();
outTab = outBuf.toByteArray();
}
Stats stats = compactor.getStats();
public void twoTablesOneRef() throws IOException {
byte[] inTab1;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(0)
.setMaxUpdateIndex(0)
- .begin(inBuf);
+ .begin();
writer.writeRef(ref(MASTER, 1));
writer.finish();
byte[] inTab2;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(inBuf);
+ .begin();
writer.writeRef(ref(MASTER, 2));
writer.finish();
}
byte[] outTab;
- ReftableCompactor compactor = new ReftableCompactor();
+ ReftableCompactor compactor;
try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
+ compactor = new ReftableCompactor(outBuf);
compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
- compactor.compact(outBuf);
+ compactor.compact();
outTab = outBuf.toByteArray();
}
Stats stats = compactor.getStats();
public void twoTablesTwoRefs() throws IOException {
byte[] inTab1;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(0)
.setMaxUpdateIndex(0)
- .begin(inBuf);
+ .begin();
writer.writeRef(ref(MASTER, 1));
writer.writeRef(ref(NEXT, 2));
byte[] inTab2;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(inBuf);
+ .begin();
writer.writeRef(ref(MASTER, 3));
writer.finish();
}
byte[] outTab;
- ReftableCompactor compactor = new ReftableCompactor();
+ ReftableCompactor compactor;
try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
+ compactor = new ReftableCompactor(outBuf);
compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
- compactor.compact(outBuf);
+ compactor.compact();
outTab = outBuf.toByteArray();
}
Stats stats = compactor.getStats();
public void twoTablesIncludeOneDelete() throws IOException {
byte[] inTab1;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(0)
.setMaxUpdateIndex(0)
- .begin(inBuf);
+ .begin();
writer.writeRef(ref(MASTER, 1));
writer.finish();
byte[] inTab2;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(inBuf);
+ .begin();
writer.writeRef(tombstone(MASTER));
writer.finish();
}
byte[] outTab;
- ReftableCompactor compactor = new ReftableCompactor();
+ ReftableCompactor compactor;
try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
+ compactor = new ReftableCompactor(outBuf);
compactor.setIncludeDeletes(true);
compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
- compactor.compact(outBuf);
+ compactor.compact();
outTab = outBuf.toByteArray();
}
Stats stats = compactor.getStats();
public void twoTablesNotIncludeOneDelete() throws IOException {
byte[] inTab1;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(0)
.setMaxUpdateIndex(0)
- .begin(inBuf);
+ .begin();
writer.writeRef(ref(MASTER, 1));
writer.finish();
byte[] inTab2;
try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(inBuf)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(inBuf);
+ .begin();
writer.writeRef(tombstone(MASTER));
writer.finish();
}
byte[] outTab;
- ReftableCompactor compactor = new ReftableCompactor();
+ ReftableCompactor compactor;
try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
+ compactor = new ReftableCompactor(outBuf);
compactor.setIncludeDeletes(false);
compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
- compactor.compact(outBuf);
+ compactor.compact();
outTab = outBuf.toByteArray();
}
Stats stats = compactor.getStats();
byte[] table;
ReftableConfig cfg = new ReftableConfig();
cfg.setIndexObjects(false);
- ReftableWriter writer = new ReftableWriter().setConfig(cfg);
try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
- writer.begin(buf);
+ ReftableWriter writer = new ReftableWriter(buf).setConfig(cfg);
+ writer.begin();
assertEquals(92, writer.estimateTotalBytes());
writer.writeRef(exp);
assertEquals(expBytes, writer.estimateTotalBytes());
int expBytes = 147860;
byte[] table;
- ReftableWriter writer = new ReftableWriter().setConfig(cfg);
try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
- writer.begin(buf);
+ ReftableWriter writer = new ReftableWriter(buf).setConfig(cfg);
+ writer.begin();
writer.sortAndWriteRefs(refs);
assertEquals(expBytes, writer.estimateTotalBytes());
writer.finish();
public void invalidRefWriteOrder() throws IOException {
Ref master = ref(MASTER, 1);
Ref next = ref(NEXT, 2);
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream())
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(new ByteArrayOutputStream());
+ .begin();
writer.writeRef(next);
IllegalArgumentException e = assertThrows(
@Test
public void invalidReflogWriteOrderUpdateIndex() throws IOException {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream())
.setMinUpdateIndex(1)
.setMaxUpdateIndex(2)
- .begin(new ByteArrayOutputStream());
+ .begin();
PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
String msg = "test";
@Test
public void invalidReflogWriteOrderName() throws IOException {
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream())
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(new ByteArrayOutputStream());
+ .begin();
PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
String msg = "test";
String msg = "test";
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(buffer)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(buffer);
+ .begin();
writer.writeRef(master);
writer.writeRef(next);
Ref next = ref(NEXT, 2);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter().setMinUpdateIndex(1)
- .setMaxUpdateIndex(1).begin(buffer);
+ ReftableWriter writer = new ReftableWriter(buffer).setMinUpdateIndex(1)
+ .setMaxUpdateIndex(1).begin();
writer.writeRef(master);
writer.writeRef(next);
cfg.setRefBlockSize(1024);
cfg.setLogBlockSize(1024);
cfg.setAlignBlocks(true);
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(buffer)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
.setConfig(cfg)
- .begin(buffer);
+ .begin();
PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
// Fill out the 1st ref block.
String msgNext = "test next";
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(buffer)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(buffer);
+ .begin();
writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg);
writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(2), msgNext);
PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(buffer)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(buffer);
+ .begin();
writer.writeLog("branchname", 1, who, ObjectId.zeroId(), id(1), "branchname");
String msg = "test";
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter()
+ ReftableWriter writer = new ReftableWriter(buffer)
.setMinUpdateIndex(1)
.setMaxUpdateIndex(1)
- .begin(buffer);
+ .begin();
writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg);
writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(2), msg);
writer.finish();
cfg.setLogBlockSize(2048);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter(cfg);
- writer.setMinUpdateIndex(1).setMaxUpdateIndex(1).begin(buffer);
+ ReftableWriter writer = new ReftableWriter(cfg, buffer);
+ writer.setMinUpdateIndex(1).setMaxUpdateIndex(1).begin();
List<Ref> refs = new ArrayList<>();
for (int i = 1; i <= 5670; i++) {
cfg.setRefBlockSize(64);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ReftableWriter writer = new ReftableWriter(cfg).begin(buffer);
+ ReftableWriter writer = new ReftableWriter(cfg, buffer).begin();
writer.writeRef(ref("refs/heads/i-am-not-a-teapot", 1));
writer.finish();
fail("expected BlockSizeTooSmallException");
private byte[] write(Collection<Ref> refs) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- stats = new ReftableWriter()
- .begin(buffer)
+ stats = new ReftableWriter(buffer)
+ .begin()
.sortAndWriteRefs(refs)
.finish()
.getStats();
return;
}
- try (DfsReftableStack stack = DfsReftableStack.open(ctx, reftablesBefore)) {
- ReftableCompactor compact = new ReftableCompactor();
+ try (DfsReftableStack stack = DfsReftableStack.open(ctx, reftablesBefore);
+ DfsOutputStream out = objdb.writeFile(pack, REFTABLE)) {
+ ReftableCompactor compact = new ReftableCompactor(out);
compact.addAll(stack.readers());
compact.setIncludeDeletes(includeDeletes);
- compactReftable(pack, compact);
+ compact.setConfig(configureReftable(reftableConfig, out));
+ compact.compact();
+ pack.addFileExt(REFTABLE);
+ pack.setReftableStats(compact.getStats());
}
}
pack.setReftableStats(writer.getStats());
}
}
-
- private void compactReftable(DfsPackDescription pack,
- ReftableCompactor compact) throws IOException {
- try (DfsOutputStream out = objdb.writeFile(pack, REFTABLE)) {
- compact.setConfig(configureReftable(reftableConfig, out));
- compact.compact(out);
- pack.addFileExt(REFTABLE);
- pack.setReftableStats(compact.getStats());
- }
- }
}
DfsObjDatabase objdb = repo.getObjectDatabase();
Collections.sort(srcReftables, objdb.reftableComparator());
- try (DfsReftableStack stack = DfsReftableStack.open(ctx, srcReftables)) {
+ try (DfsReftableStack stack = DfsReftableStack.open(ctx, srcReftables);
+ DfsOutputStream out = objdb.writeFile(outDesc, REFTABLE)) {
initOutDesc(objdb);
- ReftableCompactor compact = new ReftableCompactor();
+ ReftableCompactor compact = new ReftableCompactor(out);
compact.addAll(stack.readers());
compact.setIncludeDeletes(true);
- writeReftable(objdb, outDesc, compact);
+ compact.setConfig(configureReftable(reftableConfig, out));
+ compact.compact();
+ outDesc.addFileExt(REFTABLE);
+ outDesc.setReftableStats(compact.getStats());
}
}
}
}
- private void writeReftable(DfsObjDatabase objdb, DfsPackDescription pack,
- ReftableCompactor compact) throws IOException {
- try (DfsOutputStream out = objdb.writeFile(pack, REFTABLE)) {
- compact.setConfig(configureReftable(reftableConfig, out));
- compact.compact(out);
- pack.addFileExt(REFTABLE);
- pack.setReftableStats(compact.getStats());
- }
- }
-
static ReftableConfig configureReftable(ReftableConfig cfg,
DfsOutputStream out) {
int bs = out.blockSize();
private ReftableWriter.Stats write(OutputStream os, ReftableConfig cfg,
long updateIndex, List<Ref> newRefs, List<ReceiveCommand> pending)
throws IOException {
- ReftableWriter writer = new ReftableWriter(cfg)
+ ReftableWriter writer = new ReftableWriter(cfg, os)
.setMinUpdateIndex(updateIndex).setMaxUpdateIndex(updateIndex)
- .begin(os).sortAndWriteRefs(newRefs);
+ .begin().sortAndWriteRefs(newRefs);
if (!isRefLogDisabled()) {
writeLog(writer, updateIndex, pending);
}
tables.add(last);
tables.add(new ReftableReader(BlockSource.from(newTable)));
- ReftableCompactor compactor = new ReftableCompactor();
+ ReftableCompactor compactor = new ReftableCompactor(out);
compactor.setConfig(cfg);
compactor.setIncludeDeletes(true);
compactor.addAll(tables);
- compactor.compact(out);
+ compactor.compact();
return compactor.getStats();
}
cfg.setIndexObjects(false);
cfg.setAlignBlocks(false);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
- new ReftableWriter()
+ new ReftableWriter(buf)
.setConfig(cfg)
- .begin(buf)
+ .begin()
.sortAndWriteRefs(refs)
.finish();
return new ReftableReader(BlockSource.from(buf.toByteArray()));
* {@code setOldestReflogTimeMillis(Long.MAX_VALUE)}.
*/
public class ReftableCompactor {
- private final ReftableWriter writer = new ReftableWriter();
+ private final ReftableWriter writer;
private final ArrayDeque<Reftable> tables = new ArrayDeque<>();
private long compactBytesLimit;
private long oldestReflogTimeMillis;
private Stats stats;
+ /**
+ * Creates a new compactor.
+ *
+ * @param out
+ * stream to write the compacted tables to. Caller is responsible
+ * for closing {@code out}.
+ */
+ public ReftableCompactor(OutputStream out) {
+ writer = new ReftableWriter(out);
+ }
+
/**
* Set configuration for the reftable.
*
/**
* Write a compaction to {@code out}.
*
- * @param out
- * stream to write the compacted tables to. Caller is responsible
- * for closing {@code out}.
* @throws java.io.IOException
* if tables cannot be read, or cannot be written.
*/
- public void compact(OutputStream out) throws IOException {
+ public void compact() throws IOException {
MergedReftable mr = new MergedReftable(new ArrayList<>(tables));
mr.setIncludeDeletes(includeDeletes);
writer.setMinUpdateIndex(Math.max(minUpdateIndex, 0));
writer.setMaxUpdateIndex(maxUpdateIndex);
- writer.begin(out);
+ writer.begin();
mergeRefs(mr);
mergeLogs(mr);
writer.finish();
/**
* Initialize a writer with a default configuration.
+ *
+ * @param os
+ * output stream.
*/
- public ReftableWriter() {
- this(new ReftableConfig());
+ public ReftableWriter(OutputStream os) {
+ this(new ReftableConfig(), os);
lastRef = null;
lastLog = null;
}
- /**
- * Initialize a writer with a specific configuration.
- *
- * @param cfg
- * configuration for the writer.
- */
- public ReftableWriter(ReftableConfig cfg) {
- this(cfg, null);
- }
-
/**
* Initialize a writer with a configuration.
*
* @param cfg
* configuration for the writer
* @param os
- * output stream. Do not supply a stream to begin() on this writer.
+ * output stream.
*/
- public ReftableWriter(ReftableConfig cfg, @Nullable OutputStream os) {
+ public ReftableWriter(ReftableConfig cfg, OutputStream os) {
config = cfg;
outputStream = os;
}
return this;
}
- /**
- * Begin writing the reftable. Should be called only once.
- *
- * @param os
- * stream to write the table to. Caller is responsible for
- * closing the stream after invoking {@link #finish()}.
- * @return {@code this}
- */
- public ReftableWriter begin(OutputStream os) {
- if (outputStream != null) {
- throw new IllegalStateException("begin() called twice.");//$NON-NLS-1$
- }
- outputStream = os;
- return begin();
- }
-
/**
* Begin writing the reftable. Should be called only once. Call this
* if a stream was passed to the constructor.