Skip to content

Commit f96e5e3

Browse files
committed
Encapsulate layout of reftable stack in FileReftableStack
The filesystem layout of reftables is fixed in the reftable specification hence the constructor FileReftableStack(File tablesListFile, File reftableDir, @nullable Runnable onChange, Supplier<Config> configSupplier) should not allow to pass the path of the tables.list file independently from the refs/reftable/ directory containing both the tables.list file and the reftable files. Hence remove the path of the tables.list file from its argument list and instead set it inside the constructor. Use reftableDir instead of tablesListFile.getParentFile(). Also rename FileReftableStack.stackPath to tablesListFile which is easier to understand and remove the useless return value of FileReftableDatabase#convertFrom which was unused and always returned null. Change-Id: If1efba5d49d979d99a86871996abe550422d8945
1 parent 28a38bc commit f96e5e3

File tree

4 files changed

+31
-53
lines changed

4 files changed

+31
-53
lines changed

org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/BenchmarkReftable.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,12 @@ private void printf(String fmt, Object... args) throws IOException {
107107
@SuppressWarnings({ "nls", "boxing" })
108108
private void writeStack() throws Exception {
109109
File dir = new File(reftablePath);
110-
File stackFile = new File(reftablePath + ".stack");
111110

112111
dir.mkdirs();
113112

114113
long start = System.currentTimeMillis();
115-
try (FileReftableStack stack = new FileReftableStack(stackFile, dir,
116-
null, () -> new Config())) {
114+
try (FileReftableStack stack = new FileReftableStack(dir, null,
115+
() -> new Config())) {
117116

118117
List<Ref> refs = readLsRemote().asList();
119118
for (Ref r : refs) {

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileReftableStackTest.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ void writeBranches(FileReftableStack stack, String template, int start,
8181
}
8282

8383
public void testCompaction(int N) throws Exception {
84-
try (FileReftableStack stack = new FileReftableStack(
85-
new File(reftableDir, "refs"), reftableDir, null,
84+
try (FileReftableStack stack = new FileReftableStack(reftableDir, null,
8685
() -> new Config())) {
8786
writeBranches(stack, "refs/heads/branch%d", 0, N);
8887
MergedReftable table = stack.getMergedReftable();
@@ -124,8 +123,7 @@ public void missingReftable() throws Exception {
124123
// Can't delete in-use files on Windows.
125124
assumeFalse(SystemReader.getInstance().isWindows());
126125

127-
try (FileReftableStack stack = new FileReftableStack(
128-
new File(reftableDir, "refs"), reftableDir, null,
126+
try (FileReftableStack stack = new FileReftableStack(reftableDir, null,
129127
() -> new Config())) {
130128
outer: for (int i = 0; i < 10; i++) {
131129
final long next = stack.getMergedReftable().maxUpdateIndex()
@@ -152,8 +150,8 @@ public void missingReftable() throws Exception {
152150
}
153151
}
154152
assertThrows(FileNotFoundException.class,
155-
() -> new FileReftableStack(new File(reftableDir, "refs"),
156-
reftableDir, null, () -> new Config()));
153+
() -> new FileReftableStack(reftableDir, null,
154+
() -> new Config()));
157155
}
158156

159157
@Test

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileReftableDatabase.java

+11-28
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,11 @@ public class FileReftableDatabase extends RefDatabase {
7575
private volatile boolean autoRefresh;
7676

7777
FileReftableDatabase(FileRepository repo) throws IOException {
78-
this(repo, new File(new File(repo.getCommonDirectory(), Constants.REFTABLE),
79-
Constants.TABLES_LIST));
80-
}
81-
82-
FileReftableDatabase(FileRepository repo, File refstackName) throws IOException {
8378
this.fileRepository = repo;
8479
this.autoRefresh = repo.getConfig().getBoolean(
8580
ConfigConstants.CONFIG_REFTABLE_SECTION,
8681
ConfigConstants.CONFIG_KEY_AUTOREFRESH, false);
87-
this.reftableStack = new FileReftableStack(refstackName,
82+
this.reftableStack = new FileReftableStack(
8883
new File(fileRepository.getCommonDirectory(), Constants.REFTABLE),
8984
() -> fileRepository.fireEvent(new RefsChangedEvent()),
9085
() -> fileRepository.getConfig());
@@ -683,32 +678,20 @@ private static Ref refForWrite(RevWalk rw, Ref r) throws IOException {
683678
* the repository
684679
* @param writeLogs
685680
* whether to write reflogs
686-
* @return a reftable based RefDB from an existing repository.
687681
* @throws IOException
688682
* on IO error
689683
*/
690-
public static FileReftableDatabase convertFrom(FileRepository repo,
691-
boolean writeLogs) throws IOException {
692-
FileReftableDatabase newDb = null;
693-
File reftableList = null;
694-
try {
695-
File reftableDir = new File(repo.getCommonDirectory(),
696-
Constants.REFTABLE);
697-
reftableList = new File(reftableDir, Constants.TABLES_LIST);
698-
if (!reftableDir.isDirectory()) {
699-
reftableDir.mkdir();
700-
}
684+
public static void convertFrom(FileRepository repo, boolean writeLogs)
685+
throws IOException {
686+
File reftableDir = new File(repo.getCommonDirectory(),
687+
Constants.REFTABLE);
688+
if (!reftableDir.isDirectory()) {
689+
reftableDir.mkdir();
690+
}
701691

702-
try (FileReftableStack stack = new FileReftableStack(reftableList,
703-
reftableDir, null, () -> repo.getConfig())) {
704-
stack.addReftable(rw -> writeConvertTable(repo, rw, writeLogs));
705-
}
706-
reftableList = null;
707-
} finally {
708-
if (reftableList != null) {
709-
reftableList.delete();
710-
}
692+
try (FileReftableStack stack = new FileReftableStack(reftableDir, null,
693+
() -> repo.getConfig())) {
694+
stack.addReftable(rw -> writeConvertTable(repo, rw, writeLogs));
711695
}
712-
return newDb;
713696
}
714697
}

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileReftableStack.java

+14-16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
4343
import org.eclipse.jgit.internal.storage.reftable.ReftableWriter;
4444
import org.eclipse.jgit.lib.Config;
45+
import org.eclipse.jgit.lib.Constants;
4546
import org.eclipse.jgit.lib.CoreConfig;
4647
import org.eclipse.jgit.lib.CoreConfig.TrustStat;
4748
import org.eclipse.jgit.util.FileUtils;
@@ -69,7 +70,7 @@ private static class StackEntry {
6970

7071
private long lastNextUpdateIndex;
7172

72-
private final File stackPath;
73+
private final File tablesListFile;
7374

7475
private final File reftableDir;
7576

@@ -111,8 +112,6 @@ static class CompactionStats {
111112
/**
112113
* Creates a stack corresponding to the list of reftables in the argument
113114
*
114-
* @param stackPath
115-
* the filename for the stack.
116115
* @param reftableDir
117116
* the dir holding the tables.
118117
* @param onChange
@@ -122,10 +121,10 @@ static class CompactionStats {
122121
* @throws IOException
123122
* on I/O problems
124123
*/
125-
public FileReftableStack(File stackPath, File reftableDir,
124+
public FileReftableStack(File reftableDir,
126125
@Nullable Runnable onChange, Supplier<Config> configSupplier)
127126
throws IOException {
128-
this.stackPath = stackPath;
127+
this.tablesListFile = new File(reftableDir, Constants.TABLES_LIST);
129128
this.reftableDir = reftableDir;
130129
this.stack = new ArrayList<>();
131130
this.configSupplier = configSupplier;
@@ -244,7 +243,7 @@ void reload() throws IOException {
244243
}
245244

246245
if (!success) {
247-
throw new LockFailedException(stackPath);
246+
throw new LockFailedException(tablesListFile);
248247
}
249248

250249
mergedReftable = new MergedReftable(stack.stream()
@@ -288,14 +287,14 @@ private List<String> readTableNames() throws IOException {
288287
List<String> names = new ArrayList<>(stack.size() + 1);
289288
old = snapshot.get();
290289
try (BufferedReader br = new BufferedReader(
291-
new InputStreamReader(new FileInputStream(stackPath), UTF_8))) {
290+
new InputStreamReader(new FileInputStream(tablesListFile), UTF_8))) {
292291
String line;
293292
while ((line = br.readLine()) != null) {
294293
if (!line.isEmpty()) {
295294
names.add(line);
296295
}
297296
}
298-
snapshot.compareAndSet(old, FileSnapshot.save(stackPath));
297+
snapshot.compareAndSet(old, FileSnapshot.save(tablesListFile));
299298
} catch (FileNotFoundException e) {
300299
// file isn't there: empty repository.
301300
snapshot.compareAndSet(old, FileSnapshot.MISSING_FILE);
@@ -315,15 +314,15 @@ boolean isUpToDate() throws IOException {
315314
break;
316315
case AFTER_OPEN:
317316
try (InputStream stream = Files
318-
.newInputStream(stackPath.toPath())) {
317+
.newInputStream(tablesListFile.toPath())) {
319318
// open the tables.list file to refresh attributes (on some
320319
// NFS clients)
321320
} catch (FileNotFoundException | NoSuchFileException e) {
322321
// ignore
323322
}
324323
//$FALL-THROUGH$
325324
case ALWAYS:
326-
if (!snapshot.get().isModified(stackPath)) {
325+
if (!snapshot.get().isModified(tablesListFile)) {
327326
return true;
328327
}
329328
break;
@@ -387,7 +386,7 @@ private String filename(long low, long high) {
387386
*/
388387
@SuppressWarnings("nls")
389388
public boolean addReftable(Writer w) throws IOException {
390-
LockFile lock = new LockFile(stackPath);
389+
LockFile lock = new LockFile(tablesListFile);
391390
try {
392391
if (!lock.lockForAppend()) {
393392
return false;
@@ -398,8 +397,7 @@ public boolean addReftable(Writer w) throws IOException {
398397

399398
String fn = filename(nextUpdateIndex(), nextUpdateIndex());
400399

401-
File tmpTable = File.createTempFile(fn + "_", ".ref",
402-
stackPath.getParentFile());
400+
File tmpTable = File.createTempFile(fn + "_", ".ref", reftableDir);
403401

404402
ReftableWriter.Stats s;
405403
try (FileOutputStream fos = new FileOutputStream(tmpTable)) {
@@ -453,7 +451,7 @@ private File compactLocked(int first, int last) throws IOException {
453451
String fn = filename(first, last);
454452

455453
File tmpTable = File.createTempFile(fn + "_", ".ref", //$NON-NLS-1$//$NON-NLS-2$
456-
stackPath.getParentFile());
454+
reftableDir);
457455
try (FileOutputStream fos = new FileOutputStream(tmpTable)) {
458456
ReftableCompactor c = new ReftableCompactor(fos)
459457
.setConfig(reftableConfig())
@@ -497,7 +495,7 @@ boolean compactRange(int first, int last) throws IOException {
497495
if (first >= last) {
498496
return true;
499497
}
500-
LockFile lock = new LockFile(stackPath);
498+
LockFile lock = new LockFile(tablesListFile);
501499

502500
File tmpTable = null;
503501
List<LockFile> subtableLocks = new ArrayList<>();
@@ -526,7 +524,7 @@ boolean compactRange(int first, int last) throws IOException {
526524

527525
tmpTable = compactLocked(first, last);
528526

529-
lock = new LockFile(stackPath);
527+
lock = new LockFile(tablesListFile);
530528
if (!lock.lock()) {
531529
return false;
532530
}

0 commit comments

Comments
 (0)