Search in sources :

Example 6 with TagName

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);
    }
}
Also used : ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) GlobalStateLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStateLogEntry) RefType(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry.RefType) TagName(org.projectnessie.versioned.TagName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) RefLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException)

Example 7 with TagName

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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Hash(org.projectnessie.versioned.Hash) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) Commit(org.projectnessie.versioned.Commit) TagName(org.projectnessie.versioned.TagName) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) GetNamedRefsParams(org.projectnessie.versioned.GetNamedRefsParams) Test(org.junit.jupiter.api.Test) BranchName(org.projectnessie.versioned.BranchName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) Stream(java.util.stream.Stream) BaseContent(org.projectnessie.versioned.testworker.BaseContent) VersionStore(org.projectnessie.versioned.VersionStore) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Optional(java.util.Optional) Commit(org.projectnessie.versioned.Commit) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) TagName(org.projectnessie.versioned.TagName) BranchName(org.projectnessie.versioned.BranchName) Hash(org.projectnessie.versioned.Hash) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Test(org.junit.jupiter.api.Test)

Example 8 with TagName

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)));
}
Also used : TagName(org.projectnessie.versioned.TagName) BranchName(org.projectnessie.versioned.BranchName) Hash(org.projectnessie.versioned.Hash) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Test(org.junit.jupiter.api.Test)

Example 9 with TagName

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;
}
Also used : TagName(org.projectnessie.versioned.TagName) ByteString(com.google.protobuf.ByteString) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) RefLog(org.projectnessie.versioned.persist.adapter.RefLog) ProtoSerialization.protoToRefLog(org.projectnessie.versioned.persist.serialize.ProtoSerialization.protoToRefLog) RefLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry)

Example 10 with TagName

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));
}
Also used : TagName(org.projectnessie.versioned.TagName) BranchName(org.projectnessie.versioned.BranchName) Hash(org.projectnessie.versioned.Hash) Test(org.junit.jupiter.api.Test)

Aggregations

TagName (org.projectnessie.versioned.TagName)12 Hash (org.projectnessie.versioned.Hash)11 Test (org.junit.jupiter.api.Test)8 BranchName (org.projectnessie.versioned.BranchName)7 ReferenceAlreadyExistsException (org.projectnessie.versioned.ReferenceAlreadyExistsException)4 ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)4 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)4 VersionStoreException (org.projectnessie.versioned.VersionStoreException)4 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)4 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)4 RefLogEntry (org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry)4 RefLogNotFoundException (org.projectnessie.versioned.RefLogNotFoundException)3 GlobalStateLogEntry (org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStateLogEntry)3 RefType (org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry.RefType)3 ReferenceInfo (org.projectnessie.versioned.ReferenceInfo)2 ByteString (com.google.protobuf.ByteString)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1