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);
}
}
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);
}
}
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";
}
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);
}
}
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);
}
}
Aggregations