use of org.projectnessie.versioned.NamedRef in project nessie by projectnessie.
the class TreeApiImpl method getEntries.
@Override
public EntriesResponse getEntries(String namedRef, EntriesParams params) throws NessieNotFoundException {
Preconditions.checkArgument(params.pageToken() == null, "Paging not supported");
WithHash<NamedRef> refWithHash = namedRefWithHashOrThrow(namedRef, params.hashOnRef());
// all existing VersionStore implementations have to read all keys anyways so we don't get much
try {
List<EntriesResponse.Entry> entries;
try (Stream<EntriesResponse.Entry> entryStream = getStore().getKeys(refWithHash.getHash()).map(key -> EntriesResponse.Entry.builder().name(fromKey(key.getValue())).type((Type) key.getType()).build())) {
Stream<EntriesResponse.Entry> entriesStream = filterEntries(entryStream, params.filter());
if (params.namespaceDepth() != null && params.namespaceDepth() > 0) {
entriesStream = entriesStream.filter(e -> e.getName().getElements().size() >= params.namespaceDepth()).map(e -> truncate(e, params.namespaceDepth())).distinct();
}
entries = entriesStream.collect(ImmutableList.toImmutableList());
}
return EntriesResponse.builder().addAllEntries(entries).build();
} catch (ReferenceNotFoundException e) {
throw new NessieReferenceNotFoundException(e.getMessage(), e);
}
}
use of org.projectnessie.versioned.NamedRef in project nessie by projectnessie.
the class TreeApiImplWithAuthorization method getAllReferences.
@Override
public ReferencesResponse getAllReferences(ReferencesParams params) {
ImmutableReferencesResponse.Builder resp = ReferencesResponse.builder();
BatchAccessChecker check = startAccessCheck();
List<Reference> refs = super.getAllReferences(params).getReferences().stream().peek(ref -> check.canViewReference(RefUtil.toNamedRef(ref))).collect(Collectors.toList());
Set<NamedRef> notAllowed = check.check().keySet().stream().map(Check::ref).filter(Objects::nonNull).collect(Collectors.toSet());
refs.stream().filter(ref -> !notAllowed.contains(RefUtil.toNamedRef(ref))).forEach(resp::addReferences);
return resp.build();
}
use of org.projectnessie.versioned.NamedRef 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.NamedRef 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.NamedRef 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);
}
}
Aggregations