]> source.dussan.org Git - jgit.git/commitdiff
reftable: add OutputStream argument to ReftableWriter constructor 58/148358/10
authorHan-Wen Nienhuys <hanwen@google.com>
Mon, 26 Aug 2019 14:38:39 +0000 (16:38 +0200)
committerHan-Wen Nienhuys <hanwen@google.com>
Wed, 11 Sep 2019 11:01:56 +0000 (13:01 +0200)
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>
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableWriter.java

index f10a1d8127108d2a5d0b31d1d8a62451e8e981eb..6f0ea18d2ddaa5b36d9c676de5222bc603eee0bc 100644 (file)
@@ -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());
                }
index 6459c2763d4fb0528bb40f54b15e1162ff5abae8..2217caa160742e5c1afcf9775a4c19b3ab282f2b 100644 (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 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<>();