Search in sources :

Example 6 with NamedRef

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);
    }
}
Also used : Detached(org.projectnessie.model.Detached) TreeApi(org.projectnessie.api.TreeApi) ServerConfig(org.projectnessie.services.config.ServerConfig) VAR_REF_TYPE(org.projectnessie.services.cel.CELUtil.VAR_REF_TYPE) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) NessieConflictException(org.projectnessie.error.NessieConflictException) GetNamedRefsParams(org.projectnessie.versioned.GetNamedRefsParams) VAR_REF(org.projectnessie.services.cel.CELUtil.VAR_REF) VersionStore(org.projectnessie.versioned.VersionStore) Delete(org.projectnessie.versioned.Delete) Type(org.projectnessie.model.Content.Type) Map(java.util.Map) Content(org.projectnessie.model.Content) Ref(org.projectnessie.versioned.Ref) NamedRef(org.projectnessie.versioned.NamedRef) VAR_CONTENT_TYPE(org.projectnessie.services.cel.CELUtil.VAR_CONTENT_TYPE) ScriptException(org.projectnessie.cel.tools.ScriptException) ImmutableMap(com.google.common.collect.ImmutableMap) Validation(org.projectnessie.model.Validation) ReferencesResponse(org.projectnessie.model.ReferencesResponse) REFERENCES_DECLARATIONS(org.projectnessie.services.cel.CELUtil.REFERENCES_DECLARATIONS) Branch(org.projectnessie.model.Branch) LogEntry(org.projectnessie.model.LogResponse.LogEntry) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) EntriesResponse(org.projectnessie.model.EntriesResponse) COMMIT_LOG_TYPES(org.projectnessie.services.cel.CELUtil.COMMIT_LOG_TYPES) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) BranchName(org.projectnessie.versioned.BranchName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) List(java.util.List) Principal(java.security.Principal) Stream(java.util.stream.Stream) ENTRIES_DECLARATIONS(org.projectnessie.services.cel.CELUtil.ENTRIES_DECLARATIONS) Transplant(org.projectnessie.model.Transplant) ImmutableReferencesResponse(org.projectnessie.model.ImmutableReferencesResponse) Optional(java.util.Optional) ContentKey(org.projectnessie.model.ContentKey) CELUtil(org.projectnessie.services.cel.CELUtil) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) Put(org.projectnessie.versioned.Put) ReferenceMetadata(org.projectnessie.model.ReferenceMetadata) LogResponse(org.projectnessie.model.LogResponse) NessieReferenceAlreadyExistsException(org.projectnessie.error.NessieReferenceAlreadyExistsException) Authorizer(org.projectnessie.services.authz.Authorizer) VAR_ENTRY(org.projectnessie.services.cel.CELUtil.VAR_ENTRY) TagName(org.projectnessie.versioned.TagName) Reference(org.projectnessie.model.Reference) Strings(com.google.common.base.Strings) Script(org.projectnessie.cel.tools.Script) COMMIT_LOG_DECLARATIONS(org.projectnessie.services.cel.CELUtil.COMMIT_LOG_DECLARATIONS) ImmutableLogResponse(org.projectnessie.model.ImmutableLogResponse) ImmutableList(com.google.common.collect.ImmutableList) GetReferenceParams(org.projectnessie.api.params.GetReferenceParams) ReferencesParams(org.projectnessie.api.params.ReferencesParams) Merge(org.projectnessie.model.Merge) NessieReferenceConflictException(org.projectnessie.error.NessieReferenceConflictException) StreamSupport(java.util.stream.StreamSupport) CommitMeta(org.projectnessie.model.CommitMeta) ImmutableLogEntry(org.projectnessie.model.ImmutableLogEntry) Nullable(javax.annotation.Nullable) VAR_COMMIT(org.projectnessie.services.cel.CELUtil.VAR_COMMIT) WithHash(org.projectnessie.versioned.WithHash) Operation(org.projectnessie.model.Operation) REFERENCES_TYPES(org.projectnessie.services.cel.CELUtil.REFERENCES_TYPES) CommitLogParams(org.projectnessie.api.params.CommitLogParams) Hash(org.projectnessie.versioned.Hash) ImmutableTag(org.projectnessie.model.ImmutableTag) ImmutableBranch(org.projectnessie.model.ImmutableBranch) SCRIPT_HOST(org.projectnessie.services.cel.CELUtil.SCRIPT_HOST) Commit(org.projectnessie.versioned.Commit) ImmutableReferenceMetadata(org.projectnessie.model.ImmutableReferenceMetadata) CONTAINER(org.projectnessie.services.cel.CELUtil.CONTAINER) VAR_NAMESPACE(org.projectnessie.services.cel.CELUtil.VAR_NAMESPACE) Key(org.projectnessie.versioned.Key) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) VAR_REF_META(org.projectnessie.services.cel.CELUtil.VAR_REF_META) RetrieveOptions(org.projectnessie.versioned.GetNamedRefsParams.RetrieveOptions) EntriesParams(org.projectnessie.api.params.EntriesParams) FetchOption(org.projectnessie.api.params.FetchOption) Operations(org.projectnessie.model.Operations) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Preconditions(com.google.common.base.Preconditions) RefUtil.toNamedRef(org.projectnessie.services.impl.RefUtil.toNamedRef) Collections(java.util.Collections) VAR_OPERATIONS(org.projectnessie.services.cel.CELUtil.VAR_OPERATIONS) Unchanged(org.projectnessie.versioned.Unchanged) LogEntry(org.projectnessie.model.LogResponse.LogEntry) ImmutableLogEntry(org.projectnessie.model.ImmutableLogEntry) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) NamedRef(org.projectnessie.versioned.NamedRef) RefUtil.toNamedRef(org.projectnessie.services.impl.RefUtil.toNamedRef)

Example 7 with NamedRef

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();
}
Also used : LogResponse(org.projectnessie.model.LogResponse) Put(org.projectnessie.model.Operation.Put) ServerConfig(org.projectnessie.services.config.ServerConfig) Authorizer(org.projectnessie.services.authz.Authorizer) BatchAccessChecker(org.projectnessie.services.authz.BatchAccessChecker) Reference(org.projectnessie.model.Reference) NessieConflictException(org.projectnessie.error.NessieConflictException) VersionStore(org.projectnessie.versioned.VersionStore) GetReferenceParams(org.projectnessie.api.params.GetReferenceParams) ReferencesParams(org.projectnessie.api.params.ReferencesParams) Merge(org.projectnessie.model.Merge) Type(org.projectnessie.model.Content.Type) Content(org.projectnessie.model.Content) CommitMeta(org.projectnessie.model.CommitMeta) Check(org.projectnessie.services.authz.Check) Nullable(javax.annotation.Nullable) NamedRef(org.projectnessie.versioned.NamedRef) CommitLogParams(org.projectnessie.api.params.CommitLogParams) ReferencesResponse(org.projectnessie.model.ReferencesResponse) Branch(org.projectnessie.model.Branch) Set(java.util.Set) EntriesResponse(org.projectnessie.model.EntriesResponse) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) BranchName(org.projectnessie.versioned.BranchName) List(java.util.List) EntriesParams(org.projectnessie.api.params.EntriesParams) Principal(java.security.Principal) Delete(org.projectnessie.model.Operation.Delete) Operations(org.projectnessie.model.Operations) Transplant(org.projectnessie.model.Transplant) ImmutableReferencesResponse(org.projectnessie.model.ImmutableReferencesResponse) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) BatchAccessChecker(org.projectnessie.services.authz.BatchAccessChecker) Reference(org.projectnessie.model.Reference) Check(org.projectnessie.services.authz.Check) ImmutableReferencesResponse(org.projectnessie.model.ImmutableReferencesResponse) NamedRef(org.projectnessie.versioned.NamedRef)

Example 8 with NamedRef

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);
    }
}
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 9 with NamedRef

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);
    }
}
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 10 with NamedRef

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

Aggregations

NamedRef (org.projectnessie.versioned.NamedRef)12 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)6 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)5 CommitMeta (org.projectnessie.model.CommitMeta)5 Hash (org.projectnessie.versioned.Hash)5 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Content (org.projectnessie.model.Content)4 Principal (java.security.Principal)3 Map (java.util.Map)3 Nullable (javax.annotation.Nullable)3 NessieNotFoundException (org.projectnessie.error.NessieNotFoundException)3 ContentKey (org.projectnessie.model.ContentKey)3 RefUtil.toNamedRef (org.projectnessie.services.impl.RefUtil.toNamedRef)3 BranchName (org.projectnessie.versioned.BranchName)3 ReferenceAlreadyExistsException (org.projectnessie.versioned.ReferenceAlreadyExistsException)3 Preconditions (com.google.common.base.Preconditions)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ByteString (com.google.protobuf.ByteString)2 Collections (java.util.Collections)2