Search in sources :

Example 1 with ReferenceInfo

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

the class AbstractGetNamedReferences method verifyReferences.

private void verifyReferences(GetNamedRefsParams params, ExpectedNamedReference... references) throws ReferenceNotFoundException {
    List<ReferenceInfo<ByteString>> expectedRefs = Arrays.stream(references).map(expectedRef -> expectedRef.expected(params)).filter(Objects::nonNull).collect(Collectors.toList());
    try (Stream<ReferenceInfo<ByteString>> refs = databaseAdapter.namedRefs(params)) {
        assertThat(refs).describedAs("GetNamedRefsParams=%s - references=%s", params, references).containsExactlyInAnyOrderElementsOf(expectedRefs);
    }
    for (ReferenceInfo<ByteString> expected : expectedRefs) {
        assertThat(databaseAdapter.namedRef(expected.getNamedRef().getName(), params)).isEqualTo(expected);
    }
    List<NamedRef> failureRefs = Arrays.stream(references).map(expectedRef -> {
        ReferenceInfo<ByteString> expected = expectedRef.expected(params);
        if (expected == null) {
            return expectedRef.ref;
        }
        return null;
    }).filter(Objects::nonNull).collect(Collectors.toList());
    for (NamedRef ref : failureRefs) {
        assertThatThrownBy(() -> databaseAdapter.namedRef(ref.getName(), params));
    }
}
Also used : ByteString(com.google.protobuf.ByteString) NamedRef(org.projectnessie.versioned.NamedRef) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) ImmutableReferenceInfo(org.projectnessie.versioned.ImmutableReferenceInfo)

Example 2 with ReferenceInfo

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

the class TxDatabaseAdapter method namedRef.

@Override
public ReferenceInfo<ByteString> namedRef(String ref, GetNamedRefsParams params) throws ReferenceNotFoundException {
    Preconditions.checkNotNull(params, "Parameter for GetNamedRefsParams must not be null");
    try (ConnectionWrapper conn = borrowConnection()) {
        ReferenceInfo<ByteString> refInfo = fetchNamedRef(conn, ref);
        Hash defaultBranchHead = namedRefsDefaultBranchHead(conn, params);
        Stream<ReferenceInfo<ByteString>> refs = Stream.of(refInfo);
        return namedRefsFilterAndEnhance(conn, params, defaultBranchHead, refs).findFirst().orElseThrow(() -> referenceNotFound(ref));
    }
}
Also used : ByteString(com.google.protobuf.ByteString) 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) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo)

Example 3 with ReferenceInfo

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

the class TxDatabaseAdapter method namedRefs.

public Stream<ReferenceInfo<ByteString>> namedRefs(GetNamedRefsParams params) throws ReferenceNotFoundException {
    Preconditions.checkNotNull(params, "Parameter for GetNamedRefsParams must not be null.");
    Preconditions.checkArgument(namedRefsAnyRetrieves(params), "Must retrieve branches or tags or both.");
    ConnectionWrapper conn = borrowConnection();
    boolean failed = true;
    try {
        Hash defaultBranchHead = namedRefsDefaultBranchHead(conn, params);
        Stream<ReferenceInfo<ByteString>> refs = fetchNamedRefs(conn);
        refs = namedRefsFilterAndEnhance(conn, params, defaultBranchHead, refs);
        failed = false;
        return refs.onClose(conn::close);
    } finally {
        if (failed) {
            conn.close();
        }
    }
}
Also used : 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) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo)

Example 4 with ReferenceInfo

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

the class AbstractDatabaseAdapter method namedRefsWithDefaultBranchRelatedInfo.

/**
 * If necessary based on the given {@link GetNamedRefsParams}, updates the returned {@link
 * ReferenceInfo}s with the common-ancestor of the named reference and the default branch and the
 * number of commits behind/ahead compared to the default branch.
 *
 * <p>The common ancestor and/or information of commits behind/ahead is meaningless ({@code null})
 * for the default branch. Both fields are also {@code null} if the named reference points to the
 * {@link #noAncestorHash()} (beginning of time).
 */
protected Stream<ReferenceInfo<ByteString>> namedRefsWithDefaultBranchRelatedInfo(OP_CONTEXT ctx, GetNamedRefsParams params, Stream<ReferenceInfo<ByteString>> refs, Hash defaultBranchHead) {
    if (defaultBranchHead == null) {
        // No enhancement of common ancestor and/or commits behind/ahead.
        return refs;
    }
    CommonAncestorState commonAncestorState = new CommonAncestorState(ctx, defaultBranchHead, params.getBranchRetrieveOptions().isComputeAheadBehind() || params.getTagRetrieveOptions().isComputeAheadBehind());
    return refs.map(ref -> {
        if (ref.getNamedRef().equals(params.getBaseReference())) {
            return ref;
        }
        RetrieveOptions retrieveOptions = namedRefsRetrieveOptionsForReference(params, ref);
        ReferenceInfo<ByteString> updated = namedRefsRequiresBaseReference(retrieveOptions) ? findCommonAncestor(ctx, ref.getHash(), commonAncestorState, (diffOnFrom, hash) -> {
            ReferenceInfo<ByteString> newRef = ref;
            if (retrieveOptions.isComputeCommonAncestor()) {
                newRef = newRef.withCommonAncestor(hash);
            }
            if (retrieveOptions.isComputeAheadBehind()) {
                int behind = commonAncestorState.indexOf(hash);
                CommitsAheadBehind aheadBehind = CommitsAheadBehind.of(diffOnFrom, behind);
                newRef = newRef.withAheadBehind(aheadBehind);
            }
            return newRef;
        }) : null;
        return updated != null ? updated : ref;
    });
}
Also used : Spliterators(java.util.Spliterators) BiFunction(java.util.function.BiFunction) DatabaseAdapterUtil.referenceNotFound(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.referenceNotFound) KeyListEntity(org.projectnessie.versioned.persist.adapter.KeyListEntity) DatabaseAdapterUtil.takeUntilExcludeLast(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.takeUntilExcludeLast) GetNamedRefsParams(org.projectnessie.versioned.GetNamedRefsParams) DatabaseAdapterMetrics.tryLoopFinished(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterMetrics.tryLoopFinished) CommitAttempt(org.projectnessie.versioned.persist.adapter.CommitAttempt) Map(java.util.Map) ContentVariant(org.projectnessie.versioned.persist.adapter.ContentVariant) DatabaseAdapter(org.projectnessie.versioned.persist.adapter.DatabaseAdapter) DatabaseAdapterUtil.hashKey(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.hashKey) NamedRef(org.projectnessie.versioned.NamedRef) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) DatabaseAdapterUtil.takeUntilIncludeLast(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.takeUntilIncludeLast) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) DatabaseAdapterUtil.newHasher(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.newHasher) ByteString(com.google.protobuf.ByteString) Objects(java.util.Objects) BranchName(org.projectnessie.versioned.BranchName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) List(java.util.List) KeyList(org.projectnessie.versioned.persist.adapter.KeyList) Stream(java.util.stream.Stream) Difference(org.projectnessie.versioned.persist.adapter.Difference) Entry(java.util.Map.Entry) DatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig) Optional(java.util.Optional) Spliterator(java.util.Spliterator) IntStream(java.util.stream.IntStream) AbstractSpliterator(java.util.Spliterators.AbstractSpliterator) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) TagName(org.projectnessie.versioned.TagName) KeyListEntry(org.projectnessie.versioned.persist.adapter.KeyListEntry) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Function(java.util.function.Function) UnsafeByteOperations(com.google.protobuf.UnsafeByteOperations) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KeyFilterPredicate(org.projectnessie.versioned.persist.adapter.KeyFilterPredicate) RefLog(org.projectnessie.versioned.persist.adapter.RefLog) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) StreamSupport(java.util.stream.StreamSupport) Hasher(com.google.common.hash.Hasher) Nonnull(javax.annotation.Nonnull) ContentVariantSupplier(org.projectnessie.versioned.persist.adapter.ContentVariantSupplier) Iterator(java.util.Iterator) Hash(org.projectnessie.versioned.Hash) Traced.trace(org.projectnessie.versioned.persist.adapter.spi.Traced.trace) ALLOW_ALL(org.projectnessie.versioned.persist.adapter.KeyFilterPredicate.ALLOW_ALL) DatabaseAdapterUtil.hashNotFound(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.hashNotFound) Key(org.projectnessie.versioned.Key) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) KeyWithBytes(org.projectnessie.versioned.persist.adapter.KeyWithBytes) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) RetrieveOptions(org.projectnessie.versioned.GetNamedRefsParams.RetrieveOptions) CommitsAheadBehind(org.projectnessie.versioned.ReferenceInfo.CommitsAheadBehind) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ContentAndState(org.projectnessie.versioned.persist.adapter.ContentAndState) ImmutableCommitLogEntry(org.projectnessie.versioned.persist.adapter.ImmutableCommitLogEntry) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Preconditions(com.google.common.base.Preconditions) Diff(org.projectnessie.versioned.Diff) ImmutableReferenceInfo(org.projectnessie.versioned.ImmutableReferenceInfo) Collections(java.util.Collections) ContentId(org.projectnessie.versioned.persist.adapter.ContentId) ImmutableKeyList(org.projectnessie.versioned.persist.adapter.ImmutableKeyList) ByteString(com.google.protobuf.ByteString) CommitsAheadBehind(org.projectnessie.versioned.ReferenceInfo.CommitsAheadBehind) RetrieveOptions(org.projectnessie.versioned.GetNamedRefsParams.RetrieveOptions) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) ImmutableReferenceInfo(org.projectnessie.versioned.ImmutableReferenceInfo)

Example 5 with ReferenceInfo

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

Aggregations

ReferenceInfo (org.projectnessie.versioned.ReferenceInfo)13 Hash (org.projectnessie.versioned.Hash)11 BranchName (org.projectnessie.versioned.BranchName)7 ByteString (com.google.protobuf.ByteString)5 Test (org.junit.jupiter.api.Test)5 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)5 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)4 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)3 TagName (org.projectnessie.versioned.TagName)3 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2 GetNamedRefsParams (org.projectnessie.versioned.GetNamedRefsParams)2 ImmutableReferenceInfo (org.projectnessie.versioned.ImmutableReferenceInfo)2 NamedRef (org.projectnessie.versioned.NamedRef)2 ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)2 GlobalStatePointer (org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStatePointer)2 Preconditions (com.google.common.base.Preconditions)1 Hasher (com.google.common.hash.Hasher)1 UnsafeByteOperations (com.google.protobuf.UnsafeByteOperations)1 StandardCharsets (java.nio.charset.StandardCharsets)1