Browse Source

Make Reftable seek* and has* method names more consistent

Make the method names more consistent and their semantics simpler:
hasRef and seekRef to look up a single exact reference by name and
hasRefsByPrefix and seekRefsByPrefix to look up multiple references by
name prefix.

In particular, splitting hasRef into two separate methods for its
different uses makes DfsReftableDatabase.isNameConflicting easier to
follow.

[jn: fleshed out commit message]

Change-Id: I71106068ff3ec4f7e14dd9eb6ee6b5fab8d14d0b
Signed-off-by: Minh Thai <mthai@google.com>
Signed-off-by: Jonathan Nieder <jrn@google.com>
tags/v5.1.0.201808281540-m3
Minh Thai 5 years ago
parent
commit
263a8c1c06

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ReadReftable.java View File

@@ -70,7 +70,7 @@ class ReadReftable extends TextBuiltin {
BlockSource src = BlockSource.from(in);
ReftableReader reader = new ReftableReader(src)) {
try (RefCursor rc = ref != null
? reader.seekPrefix(ref)
? reader.seekRefsWithPrefix(ref)
: reader.allRefs()) {
while (rc.next()) {
write(rc.getRef());

+ 3
- 3
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java View File

@@ -80,7 +80,7 @@ public class MergedReftableTest {
try (RefCursor rc = mr.seekRef(HEAD)) {
assertFalse(rc.next());
}
try (RefCursor rc = mr.seekPrefix(R_HEADS)) {
try (RefCursor rc = mr.seekRefsWithPrefix(R_HEADS)) {
assertFalse(rc.next());
}
}
@@ -94,7 +94,7 @@ public class MergedReftableTest {
try (RefCursor rc = mr.seekRef(HEAD)) {
assertFalse(rc.next());
}
try (RefCursor rc = mr.seekPrefix(R_HEADS)) {
try (RefCursor rc = mr.seekRefsWithPrefix(R_HEADS)) {
assertFalse(rc.next());
}
}
@@ -108,7 +108,7 @@ public class MergedReftableTest {
try (RefCursor rc = mr.seekRef(HEAD)) {
assertFalse(rc.next());
}
try (RefCursor rc = mr.seekPrefix(R_HEADS)) {
try (RefCursor rc = mr.seekRefsWithPrefix(R_HEADS)) {
assertFalse(rc.next());
}
}

+ 6
- 6
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java View File

@@ -101,7 +101,7 @@ public class ReftableTest {
try (RefCursor rc = t.seekRef(HEAD)) {
assertFalse(rc.next());
}
try (RefCursor rc = t.seekPrefix(R_HEADS)) {
try (RefCursor rc = t.seekRefsWithPrefix(R_HEADS)) {
assertFalse(rc.next());
}
try (LogCursor rc = t.allLogs()) {
@@ -317,10 +317,10 @@ public class ReftableTest {
public void namespaceNotFound() throws IOException {
Ref exp = ref(MASTER, 1);
ReftableReader t = read(write(exp));
try (RefCursor rc = t.seekPrefix("refs/changes/")) {
try (RefCursor rc = t.seekRefsWithPrefix("refs/changes/")) {
assertFalse(rc.next());
}
try (RefCursor rc = t.seekPrefix("refs/tags/")) {
try (RefCursor rc = t.seekRefsWithPrefix("refs/tags/")) {
assertFalse(rc.next());
}
}
@@ -332,12 +332,12 @@ public class ReftableTest {
Ref v1 = tag(V1_0, 3, 4);

ReftableReader t = read(write(master, next, v1));
try (RefCursor rc = t.seekPrefix("refs/tags/")) {
try (RefCursor rc = t.seekRefsWithPrefix("refs/tags/")) {
assertTrue(rc.next());
assertEquals(V1_0, rc.getRef().getName());
assertFalse(rc.next());
}
try (RefCursor rc = t.seekPrefix("refs/heads/")) {
try (RefCursor rc = t.seekRefsWithPrefix("refs/heads/")) {
assertTrue(rc.next());
assertEquals(MASTER, rc.getRef().getName());

@@ -484,7 +484,7 @@ public class ReftableTest {
try (RefCursor rc = t.allRefs()) {
assertFalse(rc.next());
}
try (RefCursor rc = t.seekPrefix("refs/heads/")) {
try (RefCursor rc = t.seekRefsWithPrefix("refs/heads/")) {
assertFalse(rc.next());
}
try (LogCursor lc = t.allLogs()) {

+ 3
- 3
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableDatabase.java View File

@@ -199,7 +199,7 @@ public class DfsReftableDatabase extends DfsRefDatabase {
}

// Cannot be the container of an existing reference.
return table.hasRef(refName + '/');
return table.hasRefsWithPrefix(refName + '/');
} finally {
lock.unlock();
}
@@ -241,7 +241,7 @@ public class DfsReftableDatabase extends DfsRefDatabase {
try {
Reftable table = reader();
try (RefCursor rc = ALL.equals(prefix) ? table.allRefs()
: (prefix.endsWith("/") ? table.seekPrefix(prefix) //$NON-NLS-1$
: (prefix.endsWith("/") ? table.seekRefsWithPrefix(prefix) //$NON-NLS-1$
: table.seekRef(prefix))) {
while (rc.next()) {
Ref ref = table.resolve(rc.getRef());
@@ -266,7 +266,7 @@ public class DfsReftableDatabase extends DfsRefDatabase {
try {
Reftable table = reader();
try (RefCursor rc = ALL.equals(prefix) ? table.allRefs()
: table.seekPrefix(prefix)) {
: table.seekRefsWithPrefix(prefix)) {
while (rc.next()) {
Ref ref = table.resolve(rc.getRef());
if (ref != null && ref.getObjectId() != null) {

+ 2
- 2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/MergedReftable.java View File

@@ -113,10 +113,10 @@ public class MergedReftable extends Reftable {

/** {@inheritDoc} */
@Override
public RefCursor seekPrefix(String prefix) throws IOException {
public RefCursor seekRefsWithPrefix(String prefix) throws IOException {
MergedRefCursor m = new MergedRefCursor();
for (int i = 0; i < tables.length; i++) {
m.add(new RefQueueEntry(tables[i].seekPrefix(prefix), i));
m.add(new RefQueueEntry(tables[i].seekRefsWithPrefix(prefix), i));
}
return m;
}

+ 20
- 12
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java View File

@@ -135,7 +135,7 @@ public abstract class Reftable implements AutoCloseable {
* @throws java.io.IOException
* if references cannot be read.
*/
public abstract RefCursor seekPrefix(String prefix) throws IOException;
public abstract RefCursor seekRefsWithPrefix(String prefix) throws IOException;

/**
* Match references pointing to a specific object.
@@ -202,24 +202,32 @@ public abstract class Reftable implements AutoCloseable {
}

/**
* Test if a reference or reference subtree exists.
* <p>
* If {@code refName} ends with {@code "/"}, the method tests if any
* reference starts with {@code refName} as a prefix.
* <p>
* Otherwise, the method checks if {@code refName} exists.
* Test if a reference exists.
*
* @param refName
* reference name or subtree to find.
* @return {@code true} if the reference exists, or at least one reference
* exists in the subtree.
* @return {@code true} if the reference exists.
* @throws java.io.IOException
* if references cannot be read.
*/
public boolean hasRef(String refName) throws IOException {
try (RefCursor rc = seekPrefix(refName)) {
return rc.next() && (refName.endsWith("/") //$NON-NLS-1$
|| refName.equals(rc.getRef().getName()));
try (RefCursor rc = seekRef(refName)) {
return rc.next();
}
}

/**
* Test if any reference starts with {@code prefix} as a prefix.
*
* @param prefix
* prefix to find.
* @return {@code true} if at least one reference exists with prefix.
* @throws java.io.IOException
* if references cannot be read.
*/
public boolean hasRefsWithPrefix(String prefix) throws IOException {
try (RefCursor rc = seekRefsWithPrefix(prefix)) {
return rc.next();
}
}


+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReader.java View File

@@ -190,7 +190,7 @@ public class ReftableReader extends Reftable {

/** {@inheritDoc} */
@Override
public RefCursor seekPrefix(String prefix) throws IOException {
public RefCursor seekRefsWithPrefix(String prefix) throws IOException {
initRefIndex();

byte[] key = prefix.getBytes(CHARSET);

Loading…
Cancel
Save