Browse Source

reftable: move AutoCloseable to ReftableReader

MergedReftable is not used as an AutoCloseable, because closing tables
is currently handled by DfsReftableStack#close.

Encode that a MergedReftable is a list of ReftableReaders. The previous
code suggested that we could form nested trees of MergedReftables,
which is not how we use reftables.

Change-Id: Icbe2fee8a5a12373f45fc5f97d8b1a2b14231c96
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
next
Han-Wen Nienhuys 5 years ago
parent
commit
a358d0c53b
  1. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java
  2. 10
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableStack.java
  3. 8
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/ReftableBatchRefUpdate.java
  4. 18
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/MergedReftable.java
  5. 6
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java
  6. 12
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableCompactor.java
  7. 2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReader.java

2
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java

@ -414,7 +414,7 @@ public class MergedReftableTest {
} }
private static MergedReftable merge(byte[]... table) { private static MergedReftable merge(byte[]... table) {
List<Reftable> stack = new ArrayList<>(table.length); List<ReftableReader> stack = new ArrayList<>(table.length);
for (byte[] b : table) { for (byte[] b : table) {
stack.add(read(b)); stack.add(read(b));
} }

10
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableStack.java

@ -48,11 +48,11 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.eclipse.jgit.internal.storage.reftable.Reftable; import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
/** /**
* Tracks multiple open * Tracks multiple open
* {@link org.eclipse.jgit.internal.storage.reftable.Reftable} instances. * {@link org.eclipse.jgit.internal.storage.reftable.ReftableReader} instances.
*/ */
public class DfsReftableStack implements AutoCloseable { public class DfsReftableStack implements AutoCloseable {
/** /**
@ -86,7 +86,7 @@ public class DfsReftableStack implements AutoCloseable {
} }
private final List<DfsReftable> files; private final List<DfsReftable> files;
private final List<Reftable> tables; private final List<ReftableReader> tables;
private DfsReftableStack(int tableCnt) { private DfsReftableStack(int tableCnt) {
this.files = new ArrayList<>(tableCnt); this.files = new ArrayList<>(tableCnt);
@ -109,14 +109,14 @@ public class DfsReftableStack implements AutoCloseable {
* @return unmodifiable list of tables, in the same order the files were * @return unmodifiable list of tables, in the same order the files were
* passed to {@link #open(DfsReader, List)}. * passed to {@link #open(DfsReader, List)}.
*/ */
public List<Reftable> readers() { public List<ReftableReader> readers() {
return Collections.unmodifiableList(tables); return Collections.unmodifiableList(tables);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void close() { public void close() {
for (Reftable t : tables) { for (ReftableReader t : tables) {
try { try {
t.close(); t.close();
} catch (IOException e) { } catch (IOException e) {

8
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/ReftableBatchRefUpdate.java

@ -406,7 +406,7 @@ public class ReftableBatchRefUpdate extends BatchRefUpdate {
private boolean canCompactTopOfStack(ReftableConfig cfg) private boolean canCompactTopOfStack(ReftableConfig cfg)
throws IOException { throws IOException {
DfsReftableStack stack = refdb.stack(); DfsReftableStack stack = refdb.stack();
List<Reftable> readers = stack.readers(); List<ReftableReader> readers = stack.readers();
if (readers.isEmpty()) { if (readers.isEmpty()) {
return false; return false;
} }
@ -427,10 +427,10 @@ public class ReftableBatchRefUpdate extends BatchRefUpdate {
private ReftableWriter.Stats compactTopOfStack(OutputStream out, private ReftableWriter.Stats compactTopOfStack(OutputStream out,
ReftableConfig cfg, byte[] newTable) throws IOException { ReftableConfig cfg, byte[] newTable) throws IOException {
List<Reftable> stack = refdb.stack().readers(); List<ReftableReader> stack = refdb.stack().readers();
Reftable last = stack.get(stack.size() - 1); ReftableReader last = stack.get(stack.size() - 1);
List<Reftable> tables = new ArrayList<>(2); List<ReftableReader> tables = new ArrayList<>(2);
tables.add(last); tables.add(last);
tables.add(new ReftableReader(BlockSource.from(newTable))); tables.add(new ReftableReader(BlockSource.from(newTable)));

18
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/MergedReftable.java

@ -67,13 +67,11 @@ import org.eclipse.jgit.lib.ReflogEntry;
* A {@code MergedReftable} is not thread-safe. * A {@code MergedReftable} is not thread-safe.
*/ */
public class MergedReftable extends Reftable { public class MergedReftable extends Reftable {
private final Reftable[] tables; private final ReftableReader[] tables;
/** /**
* Initialize a merged table reader. * Initialize a merged table reader.
* <p> * <p>
* The tables in {@code tableStack} will be closed when this
* {@code MergedReftable} is closed.
* *
* @param tableStack * @param tableStack
* stack of tables to read from. The base of the stack is at * stack of tables to read from. The base of the stack is at
@ -81,12 +79,12 @@ public class MergedReftable extends Reftable {
* {@code tableStack.size() - 1}. The top of the stack (higher * {@code tableStack.size() - 1}. The top of the stack (higher
* index) shadows the base of the stack (lower index). * index) shadows the base of the stack (lower index).
*/ */
public MergedReftable(List<Reftable> tableStack) { public MergedReftable(List<ReftableReader> tableStack) {
tables = tableStack.toArray(new Reftable[0]); tables = tableStack.toArray(new ReftableReader[0]);
// Tables must expose deletes to this instance to correctly // Tables must expose deletes to this instance to correctly
// shadow references from lower tables. // shadow references from lower tables.
for (Reftable t : tables) { for (ReftableReader t : tables) {
t.setIncludeDeletes(true); t.setIncludeDeletes(true);
} }
} }
@ -161,14 +159,6 @@ public class MergedReftable extends Reftable {
return m; return m;
} }
/** {@inheritDoc} */
@Override
public void close() throws IOException {
for (Reftable t : tables) {
t.close();
}
}
int queueSize() { int queueSize() {
return Math.max(1, tables.length); return Math.max(1, tables.length);
} }

6
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java

@ -58,7 +58,7 @@ import org.eclipse.jgit.lib.SymbolicRef;
/** /**
* Abstract table of references. * Abstract table of references.
*/ */
public abstract class Reftable implements AutoCloseable { public abstract class Reftable {
/** /**
* References to convert into a reftable * References to convert into a reftable
* *
@ -295,8 +295,4 @@ public abstract class Reftable implements AutoCloseable {
} }
return new SymbolicRef(ref.getName(), dst, ref.getUpdateIndex()); return new SymbolicRef(ref.getName(), dst, ref.getUpdateIndex());
} }
/** {@inheritDoc} */
@Override
public abstract void close() throws IOException;
} }

12
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableCompactor.java

@ -69,7 +69,7 @@ import org.eclipse.jgit.lib.ReflogEntry;
*/ */
public class ReftableCompactor { public class ReftableCompactor {
private final ReftableWriter writer; private final ReftableWriter writer;
private final ArrayDeque<Reftable> tables = new ArrayDeque<>(); private final ArrayDeque<ReftableReader> tables = new ArrayDeque<>();
private long compactBytesLimit; private long compactBytesLimit;
private long bytesToCompact; private long bytesToCompact;
@ -188,12 +188,10 @@ public class ReftableCompactor {
* @throws java.io.IOException * @throws java.io.IOException
* update indexes of a reader cannot be accessed. * update indexes of a reader cannot be accessed.
*/ */
public void addAll(List<? extends Reftable> readers) throws IOException { public void addAll(List<ReftableReader> readers) throws IOException {
tables.addAll(readers); for (ReftableReader r : readers) {
for (Reftable r : readers) { tables.add(r);
if (r instanceof ReftableReader) { adjustUpdateIndexes(r);
adjustUpdateIndexes((ReftableReader) r);
}
} }
} }

2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReader.java

@ -78,7 +78,7 @@ import org.eclipse.jgit.util.NB;
* {@code ReftableReader} is not thread-safe. Concurrent readers need their own * {@code ReftableReader} is not thread-safe. Concurrent readers need their own
* instance to read from the same file. * instance to read from the same file.
*/ */
public class ReftableReader extends Reftable { public class ReftableReader extends Reftable implements AutoCloseable {
private final BlockSource src; private final BlockSource src;
private int blockSize = -1; private int blockSize = -1;

Loading…
Cancel
Save