]> source.dussan.org Git - jgit.git/commitdiff
reftable: pass OutputStream at construction time 62/149762/6
authorHan-Wen Nienhuys <hanwen@google.com>
Wed, 18 Sep 2019 13:18:51 +0000 (15:18 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 19 Sep 2019 23:48:11 +0000 (01:48 +0200)
This makes the intended use of the classes more clear. It also
simplifies generic functions that write reftables: they only need a
ReftableWriter as argument, as the stream is carried within the
ReftableWriter.

Change-Id: Idbb06f89ae33100f0c0b562cc38e5b3b026d5181
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/WriteReftable.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollectorTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableCompactorTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackCompactor.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/ReftableBatchRefUpdate.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableCompactor.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableWriter.java

index 6cbc1b082bb7868ffb598ee06332755f8026bdb2..0320ed1174e42eb67b4d035514937ebc012966d6 100644 (file)
@@ -128,9 +128,9 @@ class WriteReftable extends TextBuiltin {
                                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,
index 5a5ae1d7a34fb6920f2220322de790064214c583..cfc275a7dec0f245c1a8bf7217eb1c2936811f0f 100644 (file)
@@ -723,7 +723,7 @@ public class DfsGarbageCollectorTest {
 
                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);
@@ -755,7 +755,7 @@ public class DfsGarbageCollectorTest {
 
                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);
@@ -795,10 +795,10 @@ public class DfsGarbageCollectorTest {
                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);
@@ -877,10 +877,10 @@ public class DfsGarbageCollectorTest {
                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);
@@ -929,10 +929,10 @@ public class DfsGarbageCollectorTest {
                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);
index 11d6439931c733a950f75796aea74c055f369f0f..dd42aa0b381018aa9f3941d7c8923766708347b0 100644 (file)
@@ -297,10 +297,10 @@ public class MergedReftableTest {
        @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();
@@ -337,13 +337,13 @@ public class MergedReftableTest {
                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);
@@ -461,10 +461,10 @@ public class MergedReftableTest {
        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();
index 1ea73097fe5bc111ff0e7f2fb54fe717767c788b..9a7f2409517c3a1d95ed13d15b19d6223a24d3c6 100644 (file)
@@ -67,9 +67,10 @@ public class ReftableCompactorTest {
 
        @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());
@@ -81,10 +82,10 @@ public class ReftableCompactorTest {
        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();
@@ -92,10 +93,11 @@ public class ReftableCompactorTest {
                }
 
                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();
@@ -116,10 +118,10 @@ public class ReftableCompactorTest {
        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();
@@ -128,10 +130,10 @@ public class ReftableCompactorTest {
 
                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();
@@ -139,10 +141,11 @@ public class ReftableCompactorTest {
                }
 
                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();
@@ -163,10 +166,10 @@ public class ReftableCompactorTest {
        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));
@@ -176,10 +179,10 @@ public class ReftableCompactorTest {
 
                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();
@@ -187,10 +190,11 @@ public class ReftableCompactorTest {
                }
 
                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();
@@ -216,10 +220,10 @@ public class ReftableCompactorTest {
        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();
@@ -228,10 +232,10 @@ public class ReftableCompactorTest {
 
                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();
@@ -239,11 +243,12 @@ public class ReftableCompactorTest {
                }
 
                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();
@@ -261,10 +266,10 @@ public class ReftableCompactorTest {
        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();
@@ -273,10 +278,10 @@ public class ReftableCompactorTest {
 
                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();
@@ -284,11 +289,12 @@ public class ReftableCompactorTest {
                }
 
                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();
index a45548d1e7e23946f6bad73e56122f354f220727..1c04733f630747ffb6ece3214be30cfbfeecf955 100644 (file)
@@ -137,9 +137,9 @@ public class ReftableTest {
                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());
@@ -163,9 +163,9 @@ public class ReftableTest {
 
                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();
@@ -424,10 +424,10 @@ public class ReftableTest {
        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(
@@ -438,10 +438,10 @@ public class ReftableTest {
 
        @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";
 
@@ -454,10 +454,10 @@ public class ReftableTest {
 
        @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";
 
@@ -476,10 +476,10 @@ public class ReftableTest {
                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);
@@ -531,8 +531,8 @@ public class ReftableTest {
                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);
@@ -573,11 +573,11 @@ public class ReftableTest {
                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.
@@ -611,10 +611,10 @@ public class ReftableTest {
                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);
@@ -654,10 +654,10 @@ public class ReftableTest {
                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");
 
@@ -678,10 +678,10 @@ public class ReftableTest {
                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();
@@ -730,8 +730,8 @@ public class ReftableTest {
                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++) {
@@ -850,7 +850,7 @@ public class ReftableTest {
                        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");
@@ -935,8 +935,8 @@ public class ReftableTest {
 
        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();
index 0d139c1b7755cdc3130373e4122d78cff73cd3a7..3e71d079b5261967c650871587cc4b820ea9752c 100644 (file)
@@ -744,11 +744,15 @@ public class DfsGarbageCollector {
                        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());
                }
        }
 
@@ -773,14 +777,4 @@ public class DfsGarbageCollector {
                        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());
-               }
-       }
 }
index 8cf66b1598b9b92b8b0d9191a179284793af3723..724975bb2a8294f06fecf26522cfcac369b150f9 100644 (file)
@@ -311,12 +311,16 @@ public class DfsPackCompactor {
                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());
                }
        }
 
@@ -497,16 +501,6 @@ public class DfsPackCompactor {
                }
        }
 
-       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();
index 090c8b3cdebf9a9f72058e38de18129e05141804..364052bf932d573003f466f537255aaa8e6a89bf 100644 (file)
@@ -296,9 +296,9 @@ public class ReftableBatchRefUpdate extends BatchRefUpdate {
        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);
                }
@@ -434,11 +434,11 @@ public class ReftableBatchRefUpdate extends BatchRefUpdate {
                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();
        }
 
index f5685c6ca76c0e9cb06efcbb1f4114c722346f1d..860d044262a7371c8d982d4fc312939a6c969eb0 100644 (file)
@@ -72,9 +72,9 @@ public abstract class Reftable implements AutoCloseable {
                        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()));
index c4e8f69fa48eb401014d662e85940eddab48d32b..2effda4bcb9e17f9290df7395996b5cb70940288 100644 (file)
@@ -68,7 +68,7 @@ import org.eclipse.jgit.lib.ReflogEntry;
  * {@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;
@@ -79,6 +79,17 @@ public class ReftableCompactor {
        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.
         *
@@ -225,19 +236,16 @@ public class ReftableCompactor {
        /**
         * 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();
index 2217caa160742e5c1afcf9775a4c19b3ab282f2b..0bb0a24d58e6377debc43e41f31c5f7d68dca8ac 100644 (file)
@@ -123,32 +123,25 @@ public class ReftableWriter {
 
        /**
         * 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;
        }
@@ -196,22 +189,6 @@ public class ReftableWriter {
                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.