Search in sources :

Example 21 with ReferenceNotFoundException

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

the class TreeApiImpl method getCommitLog.

@Override
public LogResponse getCommitLog(String namedRef, CommitLogParams params) throws NessieNotFoundException {
    int max = Math.min(params.maxRecords() != null ? params.maxRecords() : MAX_COMMIT_LOG_ENTRIES, MAX_COMMIT_LOG_ENTRIES);
    // we should only allow named references when no paging is defined
    Ref endRef = namedRefWithHashOrThrow(namedRef, null == params.pageToken() ? params.endHash() : params.pageToken()).getHash();
    boolean fetchAll = FetchOption.isFetchAll(params.fetchOption());
    try (Stream<Commit<CommitMeta, Content>> commits = getStore().getCommits(endRef, fetchAll)) {
        Stream<LogEntry> logEntries = commits.map(commit -> {
            CommitMeta commitMetaWithHash = addHashToCommitMeta(commit.getHash(), commit.getCommitMeta());
            ImmutableLogEntry.Builder logEntry = LogEntry.builder();
            logEntry.commitMeta(commitMetaWithHash);
            if (fetchAll) {
                if (commit.getParentHash() != null) {
                    logEntry.parentCommitHash(commit.getParentHash().asString());
                }
                if (commit.getOperations() != null) {
                    commit.getOperations().forEach(op -> {
                        ContentKey key = ContentKey.of(op.getKey().getElements());
                        if (op instanceof Put) {
                            Content content = ((Put<Content>) op).getValue();
                            logEntry.addOperations(Operation.Put.of(key, content));
                        }
                        if (op instanceof Delete) {
                            logEntry.addOperations(Operation.Delete.of(key));
                        }
                    });
                }
            }
            return logEntry.build();
        });
        logEntries = StreamSupport.stream(StreamUtil.takeUntilIncl(logEntries.spliterator(), x -> Objects.equals(x.getCommitMeta().getHash(), params.startHash())), false);
        List<LogEntry> items = filterCommitLog(logEntries, params.filter()).limit(max + 1).collect(Collectors.toList());
        if (items.size() == max + 1) {
            return ImmutableLogResponse.builder().addAllLogEntries(items.subList(0, max)).isHasMore(true).token(items.get(max).getCommitMeta().getHash()).build();
        }
        return ImmutableLogResponse.builder().addAllLogEntries(items).build();
    } catch (ReferenceNotFoundException e) {
        throw new NessieReferenceNotFoundException(e.getMessage(), e);
    }
}
Also used : Delete(org.projectnessie.versioned.Delete) ImmutableLogEntry(org.projectnessie.model.ImmutableLogEntry) Put(org.projectnessie.versioned.Put) ContentKey(org.projectnessie.model.ContentKey) Ref(org.projectnessie.versioned.Ref) NamedRef(org.projectnessie.versioned.NamedRef) RefUtil.toNamedRef(org.projectnessie.services.impl.RefUtil.toNamedRef) Commit(org.projectnessie.versioned.Commit) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) Content(org.projectnessie.model.Content) CommitMeta(org.projectnessie.model.CommitMeta) LogEntry(org.projectnessie.model.LogResponse.LogEntry) ImmutableLogEntry(org.projectnessie.model.ImmutableLogEntry)

Example 22 with ReferenceNotFoundException

use of org.projectnessie.versioned.ReferenceNotFoundException 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)

Example 23 with ReferenceNotFoundException

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

the class ErrorTestService method unhandledExceptionInTvsStore.

/**
 * Throws an exception depending on the parameter.
 *
 * @return nothing
 * @see TestNessieError#unhandledRuntimeExceptionInStore()
 * @see TestNessieError#backendThrottledExceptionInStore()
 */
@Path("unhandledExceptionInTvsStore/{exception}")
@GET
@Consumes(MediaType.APPLICATION_JSON)
public String unhandledExceptionInTvsStore(@PathParam("exception") String exception) throws ReferenceNotFoundException {
    Exception ex;
    switch(exception) {
        case "runtime":
            ex = new RuntimeException("Store.getValues-throwing");
            break;
        case "throttle":
            ex = new BackendLimitExceededException("Store.getValues-throttled");
            break;
        default:
            throw new IllegalArgumentException("test code error");
    }
    DatabaseAdapter databaseAdapter = Mockito.mock(DatabaseAdapter.class);
    Mockito.when(databaseAdapter.namedRefs(Mockito.any())).thenThrow(ex);
    PersistVersionStore<BaseContent, CommitMessage, BaseContent.Type> tvs = new PersistVersionStore<>(databaseAdapter, SimpleStoreWorker.INSTANCE);
    try (Stream<ReferenceInfo<CommitMessage>> refs = tvs.getNamedRefs(GetNamedRefsParams.DEFAULT)) {
        refs.forEach(ref -> {
        });
    }
    return "we should not get here";
}
Also used : PersistVersionStore(org.projectnessie.versioned.persist.store.PersistVersionStore) MediaType(javax.ws.rs.core.MediaType) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) BackendLimitExceededException(org.projectnessie.versioned.BackendLimitExceededException) BaseContent(org.projectnessie.versioned.testworker.BaseContent) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) BackendLimitExceededException(org.projectnessie.versioned.BackendLimitExceededException) ConstraintDefinitionException(javax.validation.ConstraintDefinitionException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) ConstraintDeclarationException(javax.validation.ConstraintDeclarationException) GroupDefinitionException(javax.validation.GroupDefinitionException) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) DatabaseAdapter(org.projectnessie.versioned.persist.adapter.DatabaseAdapter) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) GET(javax.ws.rs.GET)

Example 24 with ReferenceNotFoundException

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

the class BaseApiImpl method namedRefWithHashOrThrow.

WithHash<NamedRef> namedRefWithHashOrThrow(@Nullable String namedRef, @Nullable String hashOnRef) throws NessieNotFoundException {
    if (null == namedRef) {
        namedRef = config.getDefaultBranch();
    }
    if (DetachedRef.REF_NAME.equals(namedRef)) {
        Objects.requireNonNull(hashOnRef, String.format("hashOnRef must not be null for '%s'", DetachedRef.REF_NAME));
        return WithHash.of(Hash.of(hashOnRef), DetachedRef.INSTANCE);
    }
    WithHash<NamedRef> namedRefWithHash;
    try {
        ReferenceInfo<CommitMeta> ref = getStore().getNamedRef(namedRef, GetNamedRefsParams.DEFAULT);
        namedRefWithHash = WithHash.of(ref.getHash(), ref.getNamedRef());
    } catch (ReferenceNotFoundException e) {
        throw new NessieReferenceNotFoundException(e.getMessage(), e);
    }
    try {
        if (null == hashOnRef) {
            return namedRefWithHash;
        }
        if (store.noAncestorHash().asString().equals(hashOnRef)) {
            // necessarily the same, so construct a new instance to return.
            return WithHash.of(store.noAncestorHash(), namedRefWithHash.getValue());
        }
        // hash actually exists on the named reference and return early here
        if (namedRefWithHash.getHash().asString().equals(hashOnRef)) {
            return namedRefWithHash;
        }
        // we need to make sure that the hash in fact exists on the named ref
        return WithHash.of(getStore().hashOnReference(namedRefWithHash.getValue(), Optional.of(Hash.of(hashOnRef))), namedRefWithHash.getValue());
    } catch (ReferenceNotFoundException e) {
        throw new NessieReferenceNotFoundException(e.getMessage(), e);
    }
}
Also used : NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) NamedRef(org.projectnessie.versioned.NamedRef) CommitMeta(org.projectnessie.model.CommitMeta)

Example 25 with ReferenceNotFoundException

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

the class TxDatabaseAdapter method commit.

@Override
public Hash commit(CommitAttempt commitAttempt) throws ReferenceConflictException, ReferenceNotFoundException {
    try {
        return opLoop("commit", commitAttempt.getCommitToBranch(), false, (conn, branchHead) -> {
            long timeInMicros = commitTimeInMicros();
            CommitLogEntry newBranchCommit = commitAttempt(conn, timeInMicros, branchHead, commitAttempt, h -> {
            });
            upsertGlobalStates(commitAttempt, conn, newBranchCommit.getCreatedTime());
            Hash resultHash = tryMoveNamedReference(conn, commitAttempt.getCommitToBranch(), branchHead, newBranchCommit.getHash());
            commitRefLog(conn, timeInMicros, newBranchCommit.getHash(), commitAttempt.getCommitToBranch(), RefLogEntry.Operation.COMMIT, Collections.emptyList());
            return resultHash;
        }, () -> commitConflictMessage("Conflict", commitAttempt.getCommitToBranch(), commitAttempt.getExpectedHead()), () -> commitConflictMessage("Retry-Failure", commitAttempt.getCommitToBranch(), commitAttempt.getExpectedHead()));
    } catch (ReferenceNotFoundException | ReferenceConflictException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) ProtoSerialization.protoToCommitLogEntry(org.projectnessie.versioned.persist.serialize.ProtoSerialization.protoToCommitLogEntry) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) 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) 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)

Aggregations

ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)33 ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)25 Hash (org.projectnessie.versioned.Hash)24 RefLogNotFoundException (org.projectnessie.versioned.RefLogNotFoundException)17 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)17 ReferenceAlreadyExistsException (org.projectnessie.versioned.ReferenceAlreadyExistsException)16 VersionStoreException (org.projectnessie.versioned.VersionStoreException)14 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)13 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)12 BranchName (org.projectnessie.versioned.BranchName)11 Key (org.projectnessie.versioned.Key)10 NamedRef (org.projectnessie.versioned.NamedRef)9 TagName (org.projectnessie.versioned.TagName)9 List (java.util.List)8 Map (java.util.Map)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 CommitLogEntry (org.projectnessie.versioned.persist.adapter.CommitLogEntry)8 ByteString (com.google.protobuf.ByteString)7