From 5c390cf9ded63a83e762932adddcdf309ee4316c Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Mon, 26 Aug 2019 16:38:39 +0200 Subject: [PATCH] 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 --- .../storage/dfs/DfsGarbageCollector.java | 8 ++-- .../storage/reftable/ReftableWriter.java | 40 ++++++++++++++++--- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java index f10a1d8127..6f0ea18d2d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java @@ -765,12 +765,10 @@ public class DfsGarbageCollector { 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()); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableWriter.java index 6459c2763d..2217caa160 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableWriter.java @@ -108,6 +108,7 @@ public class ReftableWriter { private long minUpdateIndex; private long maxUpdateIndex; + private OutputStream outputStream; private ReftableOutputStream out; private ObjectIdSubclassMap obj2ref; @@ -136,7 +137,20 @@ public class 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 class ReftableWriter { } /** - * 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 class ReftableWriter { 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<>(); -- 2.39.5