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());
}
private long minUpdateIndex;
private long maxUpdateIndex;
+ private OutputStream outputStream;
private ReftableOutputStream out;
private ObjectIdSubclassMap<RefList> obj2ref;
* 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;
}
/**
}
/**
- * 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();
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<>();