reftable: add OutputStream argument to ReftableWriter constructor

This lets us write reftables generically with functions that take
just ReftableWriter argument

Change-Id: I7285951f62f9bd4c78e8f0de194c077d51fa4e51
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
This commit is contained in:
Han-Wen Nienhuys 2019-08-26 16:38:39 +02:00
parent 90efbd216f
commit 5c390cf9de
2 changed files with 38 additions and 10 deletions

View File

@ -765,12 +765,10 @@ private void writeReftable(DfsPackDescription pack, Collection<Ref> refs)
throws IOException {
try (DfsOutputStream out = objdb.writeFile(pack, REFTABLE)) {
ReftableConfig cfg = configureReftable(reftableConfig, out);
ReftableWriter writer = new ReftableWriter(cfg)
ReftableWriter writer = new ReftableWriter(cfg, out)
.setMinUpdateIndex(reftableInitialMinUpdateIndex)
.setMaxUpdateIndex(reftableInitialMaxUpdateIndex)
.begin(out)
.sortAndWriteRefs(refs)
.finish();
.setMaxUpdateIndex(reftableInitialMaxUpdateIndex).begin()
.sortAndWriteRefs(refs).finish();
pack.addFileExt(REFTABLE);
pack.setReftableStats(writer.getStats());
}

View File

@ -108,6 +108,7 @@ public class ReftableWriter {
private long minUpdateIndex;
private long maxUpdateIndex;
private OutputStream outputStream;
private ReftableOutputStream out;
private ObjectIdSubclassMap<RefList> obj2ref;
@ -136,7 +137,20 @@ public ReftableWriter() {
* 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.
*/
public ReftableWriter(ReftableConfig cfg, @Nullable OutputStream os) {
config = cfg;
outputStream = os;
}
/**
@ -183,16 +197,32 @@ public ReftableWriter setMaxUpdateIndex(long max) {
}
/**
* Begin writing the reftable.
* 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}
* @throws java.io.IOException
* if reftable header cannot be written.
*/
public ReftableWriter begin(OutputStream os) throws IOException {
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.
*
* @return {@code this}
*/
public ReftableWriter begin() {
if (out != null) {
throw new IllegalStateException("begin() called twice.");//$NON-NLS-1$
}
refBlockSize = config.getRefBlockSize();
logBlockSize = config.getLogBlockSize();
restartInterval = config.getRestartInterval();
@ -212,7 +242,7 @@ public ReftableWriter begin(OutputStream os) throws IOException {
restartInterval = refBlockSize < (60 << 10) ? 16 : 64;
}
out = new ReftableOutputStream(os, refBlockSize, alignBlocks);
out = new ReftableOutputStream(outputStream, refBlockSize, alignBlocks);
refs = new Section(REF_BLOCK_TYPE);
if (indexObjects) {
obj2ref = new ObjectIdSubclassMap<>();