Search in sources :

Example 1 with ReferenceAlreadyExistsException

use of org.projectnessie.versioned.ReferenceAlreadyExistsException in project nessie by projectnessie.

the class TxDatabaseAdapter method create.

@SuppressWarnings("RedundantThrows")
@Override
public Hash create(NamedRef ref, Hash target) throws ReferenceAlreadyExistsException, ReferenceNotFoundException {
    try {
        return opLoop("createRef", ref, true, (conn, nullHead) -> {
            if (checkNamedRefExistence(conn, ref.getName())) {
                throw referenceAlreadyExists(ref);
            }
            Hash hash = target;
            if (hash == null) {
                // Special case: Don't validate, if the 'target' parameter is null.
                // This is mostly used for tests that re-create the default-branch.
                hash = NO_ANCESTOR;
            }
            validateHashExists(conn, hash);
            insertNewReference(conn, ref, hash);
            commitRefLog(conn, commitTimeInMicros(), hash, ref, RefLogEntry.Operation.CREATE_REFERENCE, Collections.emptyList());
            return hash;
        }, () -> createConflictMessage("Conflict", ref, target), () -> createConflictMessage("Retry-Failure", ref, target));
    } catch (ReferenceAlreadyExistsException | ReferenceNotFoundException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) 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) ReferenceRetryFailureException(org.projectnessie.versioned.ReferenceRetryFailureException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLException(java.sql.SQLException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException)

Example 2 with ReferenceAlreadyExistsException

use of org.projectnessie.versioned.ReferenceAlreadyExistsException in project nessie by projectnessie.

the class NonTransactionalDatabaseAdapter method create.

@Override
public Hash create(NamedRef ref, Hash target) throws ReferenceAlreadyExistsException, ReferenceNotFoundException {
    try {
        return casOpLoop("createRef", ref, CasOpVariant.REF_UPDATE, (ctx, pointer, branchCommits, newKeyLists) -> {
            if (refFromGlobalState(pointer, ref.getName()) != null) {
                throw referenceAlreadyExists(ref);
            }
            Hash hash = target;
            if (hash == null) {
                // Special case: Don't validate, if the 'target' parameter is null.
                // This is mostly used for tests that re-create the default-branch.
                hash = NO_ANCESTOR;
            }
            validateHashExists(ctx, hash);
            // Need a new empty global-log entry to be able to CAS
            GlobalStateLogEntry newGlobalHead = noopGlobalLogEntry(ctx, pointer);
            RefLogEntry.RefType refType = ref instanceof TagName ? RefLogEntry.RefType.Tag : RefLogEntry.RefType.Branch;
            RefLogEntry newRefLog = writeRefLogEntry(ctx, pointer, ref.getName(), refType, hash, RefLogEntry.Operation.CREATE_REFERENCE, commitTimeInMicros(), Collections.emptyList());
            return updateGlobalStatePointer(ref, pointer, hash, newGlobalHead, newRefLog);
        }, () -> createConflictMessage("Retry-Failure", ref, target));
    } catch (ReferenceAlreadyExistsException | ReferenceNotFoundException | 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) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) 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 3 with ReferenceAlreadyExistsException

use of org.projectnessie.versioned.ReferenceAlreadyExistsException 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 4 with ReferenceAlreadyExistsException

use of org.projectnessie.versioned.ReferenceAlreadyExistsException in project nessie by projectnessie.

the class TreeApiImpl method createReference.

@Override
public Reference createReference(String sourceRefName, Reference reference) throws NessieNotFoundException, NessieConflictException {
    Validation.validateForbiddenReferenceName(reference.getName());
    NamedRef namedReference = toNamedRef(reference);
    if (reference.getType() == Reference.ReferenceType.TAG && reference.getHash() == null) {
        throw new IllegalArgumentException("Tag-creation requires a target named-reference and hash.");
    }
    try {
        Hash hash = getStore().create(namedReference, toHash(reference.getHash(), false));
        return RefUtil.toReference(namedReference, hash);
    } catch (ReferenceNotFoundException e) {
        throw new NessieReferenceNotFoundException(e.getMessage(), e);
    } catch (ReferenceAlreadyExistsException e) {
        throw new NessieReferenceAlreadyExistsException(e.getMessage(), e);
    }
}
Also used : NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) NessieReferenceAlreadyExistsException(org.projectnessie.error.NessieReferenceAlreadyExistsException) NamedRef(org.projectnessie.versioned.NamedRef) RefUtil.toNamedRef(org.projectnessie.services.impl.RefUtil.toNamedRef) WithHash(org.projectnessie.versioned.WithHash) Hash(org.projectnessie.versioned.Hash) NessieReferenceAlreadyExistsException(org.projectnessie.error.NessieReferenceAlreadyExistsException)

Aggregations

Hash (org.projectnessie.versioned.Hash)4 ReferenceAlreadyExistsException (org.projectnessie.versioned.ReferenceAlreadyExistsException)4 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)4 ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)3 VersionStoreException (org.projectnessie.versioned.VersionStoreException)3 RefLogNotFoundException (org.projectnessie.versioned.RefLogNotFoundException)2 TagName (org.projectnessie.versioned.TagName)2 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)2 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 SQLException (java.sql.SQLException)1 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)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 Test (org.junit.jupiter.api.Test)1 NessieReferenceAlreadyExistsException (org.projectnessie.error.NessieReferenceAlreadyExistsException)1 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)1 RefUtil.toNamedRef (org.projectnessie.services.impl.RefUtil.toNamedRef)1