use of org.projectnessie.versioned.TagName in project nessie by projectnessie.
the class NonTransactionalDatabaseAdapter method assign.
@Override
public void assign(NamedRef assignee, Optional<Hash> expectedHead, Hash assignTo) throws ReferenceNotFoundException, ReferenceConflictException {
try {
casOpLoop("assignRef", assignee, CasOpVariant.REF_UPDATE, (ctx, pointer, branchCommits, newKeyLists) -> {
Hash beforeAssign = branchHead(pointer, assignee);
verifyExpectedHash(beforeAssign, assignee, expectedHead);
validateHashExists(ctx, assignTo);
GlobalStateLogEntry newGlobalHead = noopGlobalLogEntry(ctx, pointer);
RefLogEntry.RefType refType = assignee instanceof TagName ? RefLogEntry.RefType.Tag : RefLogEntry.RefType.Branch;
RefLogEntry newRefLog = writeRefLogEntry(ctx, pointer, assignee.getName(), refType, assignTo, RefLogEntry.Operation.ASSIGN_REFERENCE, commitTimeInMicros(), Collections.singletonList(beforeAssign));
return updateGlobalStatePointer(assignee, pointer, assignTo, newGlobalHead, newRefLog);
}, () -> assignConflictMessage("Retry-Failure", assignee, expectedHead, assignTo));
} catch (ReferenceNotFoundException | ReferenceConflictException | RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.projectnessie.versioned.TagName in project nessie by projectnessie.
the class AbstractAssign method assignReferenceToFreshMain.
/**
* Assigning a branch/tag to a fresh main without any commits didn't work in 0.9.2
*/
@Test
public void assignReferenceToFreshMain() throws ReferenceNotFoundException, ReferenceAlreadyExistsException, ReferenceConflictException {
ReferenceInfo<CommitMessage> main = store.getNamedRef("main", GetNamedRefsParams.DEFAULT);
try (Stream<Commit<CommitMessage, BaseContent>> commits = store().getCommits(main.getHash(), false)) {
assertThat(commits).isEmpty();
}
try (Stream<ReferenceInfo<CommitMessage>> refs = store().getNamedRefs(GetNamedRefsParams.DEFAULT)) {
assertThat(refs).extracting(r -> r.getNamedRef().getName()).containsExactly(main.getNamedRef().getName());
}
BranchName testBranch = BranchName.of("testBranch");
Hash testBranchHash = store.create(testBranch, Optional.empty());
store.assign(testBranch, Optional.of(testBranchHash), main.getHash());
assertThat(store.getNamedRef(testBranch.getName(), GetNamedRefsParams.DEFAULT).getHash()).isEqualTo(main.getHash());
TagName testTag = TagName.of("testTag");
Hash testTagHash = store.create(testTag, Optional.empty());
store.assign(testTag, Optional.of(testTagHash), main.getHash());
assertThat(store.getNamedRef(testTag.getName(), GetNamedRefsParams.DEFAULT).getHash()).isEqualTo(main.getHash());
}
use of org.projectnessie.versioned.TagName in project nessie by projectnessie.
the class AbstractReferences method createAndDeleteTag.
/*
* Test:
* - Create a branch with no hash assigned to it
* - add a commit to the branch
* - create a tag for the initial hash
* - create another tag for the hash after the commit
* - check that cannot create existing tags, or tag with no assigned hash
* - check that a hash is returned by toHash
* - check the tags are returned by getNamedRefs
* - check that expected commits are returned by getCommits
* - check the branch can be deleted
*/
@Test
public void createAndDeleteTag() throws Exception {
final BranchName branch = BranchName.of("foo");
store().create(branch, Optional.empty());
final Hash initialHash = store().hashOnReference(branch, Optional.empty());
final Hash commitHash = commit("Some commit").toBranch(branch);
final TagName tag = TagName.of("tag");
store().create(tag, Optional.of(initialHash));
final TagName anotherTag = TagName.of("another-tag");
store().create(anotherTag, Optional.of(commitHash));
assertThrows(ReferenceAlreadyExistsException.class, () -> store().create(tag, Optional.of(initialHash)));
assertThat(store().hashOnReference(tag, Optional.empty())).isEqualTo(initialHash);
assertThat(store().hashOnReference(anotherTag, Optional.empty())).isEqualTo(commitHash);
List<ReferenceInfo<CommitMessage>> namedRefs;
try (Stream<ReferenceInfo<CommitMessage>> str = store().getNamedRefs(GetNamedRefsParams.DEFAULT).filter(this::filterMainBranch)) {
namedRefs = str.collect(Collectors.toList());
}
assertThat(namedRefs).containsExactlyInAnyOrder(ReferenceInfo.of(commitHash, branch), ReferenceInfo.of(initialHash, tag), ReferenceInfo.of(commitHash, anotherTag));
assertThat(commitsList(tag, false)).isEmpty();
// empty commit should not be listed
assertThat(commitsList(initialHash, false)).isEmpty();
assertThat(commitsList(anotherTag, false)).hasSize(1);
// empty commit should not be listed
assertThat(commitsList(commitHash, false)).hasSize(1);
store().delete(tag, Optional.of(initialHash));
assertThrows(ReferenceNotFoundException.class, () -> store().hashOnReference(tag, Optional.empty()));
try (Stream<ReferenceInfo<CommitMessage>> str = store().getNamedRefs(GetNamedRefsParams.DEFAULT).filter(this::filterMainBranch)) {
// foo + another-tag
assertThat(str).hasSize(2);
}
assertThrows(ReferenceNotFoundException.class, () -> store().delete(tag, Optional.of(initialHash)));
}
use of org.projectnessie.versioned.TagName in project nessie by projectnessie.
the class TxDatabaseAdapter method writeRefLogEntry.
private RefLogEntry writeRefLogEntry(ConnectionWrapper connection, NamedRef ref, RefLogHead refLogHead, Hash commitHash, Operation operation, long timeInMicros, List<Hash> sourceHashes) throws ReferenceConflictException {
ByteString newId = randomHash().asBytes();
// Before Nessie 0.21.0: only the "head" of the ref-log is maintained, so Nessie has to read
// the head entry of the ref-log to get the IDs of all the previous parents to fill the parents
// in the new ref-log-entry.
//
// Since Nessie 0.21.0: the "head" for the ref-log and PLUS the parents of if are persisted,
// so Nessie no longer need to read the head entries from the ref-log.
//
// The check of the first entry is there to ensure backwards compatibility and also
// rolling-upgrades work.
Stream<ByteString> newParents;
if (refLogHead.getRefLogParentsInclHead().isEmpty() || !refLogHead.getRefLogParentsInclHead().get(0).equals(refLogHead.getRefLogHead())) {
// Before Nessie 0.21.0
newParents = Stream.of(refLogHead.getRefLogHead().asBytes());
RefLog parentEntry = fetchFromRefLog(connection, refLogHead.getRefLogHead());
if (parentEntry != null) {
newParents = Stream.concat(newParents, parentEntry.getParents().stream().limit(config.getParentsPerRefLogEntry() - 1).map(Hash::asBytes));
}
} else {
// Since Nessie 0.21.0
newParents = refLogHead.getRefLogParentsInclHead().stream().map(Hash::asBytes);
}
RefLogEntry.RefType refType = ref instanceof TagName ? RefLogEntry.RefType.Tag : RefLogEntry.RefType.Branch;
RefLogEntry.Builder entry = RefLogEntry.newBuilder().setRefLogId(newId).setRefName(ByteString.copyFromUtf8(ref.getName())).setRefType(refType).setCommitHash(commitHash.asBytes()).setOperationTime(timeInMicros).setOperation(operation);
sourceHashes.forEach(hash -> entry.addSourceHashes(hash.asBytes()));
newParents.forEach(entry::addParents);
RefLogEntry refLogEntry = entry.build();
writeRefLog(connection, refLogEntry);
return refLogEntry;
}
use of org.projectnessie.versioned.TagName in project nessie by projectnessie.
the class AbstractReferences method getNamedRef.
/**
* Rudimentary test for {@link VersionStore#getNamedRef(String, GetNamedRefsParams)}. Better tests
* in {@code AbstractGetNamedReferences} in {@code :nessie-versioned-persist-tests}.
*/
@Test
void getNamedRef() throws VersionStoreException {
final BranchName branch = BranchName.of("getNamedRef");
Hash hashFromCreate = store().create(branch, Optional.empty());
assertThat(store().hashOnReference(branch, Optional.empty())).isEqualTo(hashFromCreate);
final Hash firstCommitHash = commit("First Commit").toBranch(branch);
assertThat(store().getNamedRef(branch.getName(), GetNamedRefsParams.DEFAULT)).extracting(ReferenceInfo::getHash, ReferenceInfo::getNamedRef).containsExactly(firstCommitHash, branch);
final Hash secondCommitHash = commit("Second Commit").toBranch(branch);
final Hash thirdCommitHash = commit("Third Commit").toBranch(branch);
BranchName branchName = BranchName.of("getNamedRef_branch_" + secondCommitHash.asString());
TagName tagName = TagName.of("getNamedRef_tag_" + thirdCommitHash.asString());
store().create(branchName, Optional.of(secondCommitHash));
store().create(tagName, Optional.of(thirdCommitHash));
// Verifies that the result of "getNamedRef" for the branch created at "firstCommitHash" is
// correct
assertThat(store().getNamedRef(branchName.getName(), GetNamedRefsParams.DEFAULT)).extracting(ReferenceInfo::getHash, ReferenceInfo::getNamedRef).containsExactly(secondCommitHash, branchName);
// Verifies that the result of "getNamedRef" for the tag created at "firstCommitHash" is correct
assertThat(store().getNamedRef(tagName.getName(), GetNamedRefsParams.DEFAULT)).extracting(ReferenceInfo::getHash, ReferenceInfo::getNamedRef).containsExactly(thirdCommitHash, tagName);
// Verifies that the result of "getNamedRef" for the branch created at "firstCommitHash" is
// correct
assertThat(store().getNamedRef(branchName.getName(), GetNamedRefsParams.DEFAULT)).extracting(ReferenceInfo::getHash, ReferenceInfo::getNamedRef).containsExactly(secondCommitHash, branchName);
assertThrows(ReferenceNotFoundException.class, () -> store().getNamedRef("unknown-ref", GetNamedRefsParams.DEFAULT));
assertThrows(ReferenceNotFoundException.class, () -> store().getNamedRef("1234567890abcdef", GetNamedRefsParams.DEFAULT));
}
Aggregations