Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic resolveHash method to VersionStore #7305

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ public Hash hashOnReference(
return databaseAdapter.hashOnReference(namedReference, hashOnReference);
}

@Override
public Hash resolveHash(Hash hash, List<RelativeCommitSpec> relativeLookups) {
checkArgument(
relativeLookups.isEmpty(), "Relative lookups not supported for old database model");
return hash;
}

@Nonnull
@jakarta.annotation.Nonnull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public Hash hashOnReference(
return delegate.hashOnReference(namedReference, hashOnReference, relativeLookups);
}

@Override
public Hash resolveHash(Hash hash, List<RelativeCommitSpec> relativeLookups)
throws ReferenceNotFoundException {
return delegate.resolveHash(hash, relativeLookups);
}

@Nonnull
@jakarta.annotation.Nonnull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public Hash hashOnReference(
() -> delegate.hashOnReference(namedReference, hashOnReference, relativeLookups));
}

@Override
public Hash resolveHash(Hash hash, List<RelativeCommitSpec> relativeLookups)
throws ReferenceNotFoundException {
return delegate1Ex("resolvehash", () -> delegate.resolveHash(hash, relativeLookups));
}

@Nonnull
@jakarta.annotation.Nonnull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public Hash hashOnReference(
() -> delegate.hashOnReference(namedReference, hashOnReference, relativeLookups));
}

@Override
public Hash resolveHash(Hash hash, List<RelativeCommitSpec> relativeLookups)
throws ReferenceNotFoundException {
return callWithOneException(
tracer,
"ResolveHash",
b -> b.setAttribute(TAG_HASH, safeToString(hash)),
() -> delegate.resolveHash(hash, relativeLookups));
}

@Nonnull
@jakarta.annotation.Nonnull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ Hash hashOnReference(
List<RelativeCommitSpec> relativeLookups)
throws ReferenceNotFoundException;

/**
* Resolves the given hash by applying the relative lookup specs and returns the resolved hash.
*
* <p>If {@code relativeLookups} is empty, {@code hash} will be returned.
*
* <p>Contrary to {@link #hashOnReference(NamedRef, Optional, List)}, this method does not
* validate that the hash is reachable from any reference.
*
* @param hash The starting hash
* @param relativeLookups The relative lookup specs to apply
* @return The resolved hash
* @throws ReferenceNotFoundException if the hash does not exist
*/
Hash resolveHash(Hash hash, List<RelativeCommitSpec> relativeLookups)
throws ReferenceNotFoundException;

/**
* Retrieve the hash for "no ancestor" (or "beginning of time"), which is a hash for which no
* commit exists. "no ancestor" or "beginning of time" are the initial hash of the default branch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,17 @@ void testHashOnReferenceSuccess() throws ReferenceNotFoundException {
verifyNoInteractions(sink);
}

@Test
void testResolveHash() throws ReferenceNotFoundException {
when(delegate.resolveHash(hash1, emptyList())).thenReturn(hash1);
EventsVersionStore versionStore = new EventsVersionStore(delegate, sink);
Hash actualHash = versionStore.resolveHash(hash1, emptyList());
assertThat(actualHash).isEqualTo(hash1);
verify(delegate).resolveHash(eq(hash1), eq(emptyList()));
verifyNoMoreInteractions(delegate);
verifyNoInteractions(sink);
}

@Test
void testNoAncestor() throws ReferenceNotFoundException {
when(delegate.noAncestorHash()).thenReturn(hash1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ public CommitObj commitInChain(
return startCommit;
}

@VisibleForTesting
CommitObj relativeSpec(CommitObj startCommit, List<RelativeCommitSpec> relativespecs)
public CommitObj relativeSpec(CommitObj startCommit, List<RelativeCommitSpec> relativespecs)
throws ReferenceNotFoundException {
CommitLogic commitLogic = commitLogic(persist);
for (RelativeCommitSpec spec : relativespecs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ public Hash hashOnReference(
return commit != null ? objIdToHash(commit.id()) : NO_ANCESTOR;
}

@Override
public Hash resolveHash(Hash hash, List<RelativeCommitSpec> relativeLookups)
throws ReferenceNotFoundException {
if (relativeLookups.isEmpty()) {
return hash;
}
try {
CommitObj commit = commitLogic(persist).fetchCommit(hashToObjId(hash));
commit = new RefMapping(persist).relativeSpec(commit, relativeLookups);
return commit != null ? objIdToHash(commit.id()) : NO_ANCESTOR;
} catch (ObjNotFoundException e) {
throw referenceNotFound(e);
}
}

@Override
public ReferenceCreatedResult create(NamedRef namedRef, Optional<Hash> targetHash)
throws ReferenceNotFoundException, ReferenceAlreadyExistsException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
package org.projectnessie.versioned.tests;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.assertj.core.util.Streams.stream;
import static org.projectnessie.versioned.GetNamedRefsParams.RetrieveOptions.BARE;
import static org.projectnessie.versioned.GetNamedRefsParams.RetrieveOptions.BASE_REFERENCE_RELATED_AND_COMMIT_META;
import static org.projectnessie.versioned.GetNamedRefsParams.RetrieveOptions.OMIT;
import static org.projectnessie.versioned.RelativeCommitSpec.Type.N_TH_PARENT;
import static org.projectnessie.versioned.RelativeCommitSpec.Type.N_TH_PREDECESSOR;
import static org.projectnessie.versioned.RelativeCommitSpec.Type.TIMESTAMP_MILLIS_EPOCH;
import static org.projectnessie.versioned.RelativeCommitSpec.relativeCommitSpec;

import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -305,4 +312,40 @@ void listBranchesOrTags() throws Exception {
.containsAll(tags);
}
}

@Test
void resolveHash() throws VersionStoreException, InterruptedException {
assumeThat(isNewStorageModel()).isTrue();

BranchName branch = BranchName.of("resolveHash");
store().create(branch, Optional.empty());

Hash c1 = commit("c1").toBranch(branch);
Instant i = Instant.now();
Thread.sleep(100);
Hash c2 = commit("c2").toBranch(branch);
Thread.sleep(100);
commit("c3").toBranch(branch);

soft.assertThat(
store().resolveHash(c2, singletonList(relativeCommitSpec(N_TH_PREDECESSOR, "1"))))
.isEqualTo(c1);

soft.assertThat(store().resolveHash(c2, singletonList(relativeCommitSpec(N_TH_PARENT, "1"))))
.isEqualTo(c1);

soft.assertThat(
store()
.resolveHash(c2, singletonList(relativeCommitSpec(TIMESTAMP_MILLIS_EPOCH, 0L, i))))
.isEqualTo(c1);

// c2 + timestamp in the future => c2 (even if c3 is closer)
soft.assertThat(
store()
.resolveHash(
c2,
singletonList(
relativeCommitSpec(TIMESTAMP_MILLIS_EPOCH, 0L, i.plusSeconds(60)))))
.isEqualTo(c2);
}
}