From 263a8c1c06f9a237520959934dc0a8b93b56a2f4 Mon Sep 17 00:00:00 2001 From: Minh Thai Date: Tue, 10 Jul 2018 12:41:23 -0700 Subject: [PATCH] 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 Signed-off-by: Jonathan Nieder --- .../eclipse/jgit/pgm/debug/ReadReftable.java | 2 +- .../storage/reftable/MergedReftableTest.java | 6 ++-- .../storage/reftable/ReftableTest.java | 12 +++---- .../storage/dfs/DfsReftableDatabase.java | 6 ++-- .../storage/reftable/MergedReftable.java | 4 +-- .../internal/storage/reftable/Reftable.java | 32 ++++++++++++------- .../storage/reftable/ReftableReader.java | 2 +- 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ReadReftable.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ReadReftable.java index cebb1c449..0b36e9ca6 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ReadReftable.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ReadReftable.java @@ -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()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java index a19f4cb5f..1d11573b9 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/MergedReftableTest.java @@ -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()); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java index 35e407f3c..0ee785c60 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java @@ -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()) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableDatabase.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableDatabase.java index b891910c0..70816307f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableDatabase.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableDatabase.java @@ -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) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/MergedReftable.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/MergedReftable.java index 7e2242092..17894b166 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/MergedReftable.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/MergedReftable.java @@ -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; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java index 0593d94b2..a1087e202 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java @@ -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. - *

- * If {@code refName} ends with {@code "/"}, the method tests if any - * reference starts with {@code refName} as a prefix. - *

- * 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(); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReader.java index 80e386a62..bd52256c2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReader.java @@ -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);