Search in sources :

Example 6 with CommitMeta

use of org.projectnessie.model.CommitMeta in project nessie by projectnessie.

the class TreeApiImpl method commitMultipleOperations.

@Override
public Branch commitMultipleOperations(String branch, String hash, Operations operations) throws NessieNotFoundException, NessieConflictException {
    List<org.projectnessie.versioned.Operation<Content>> ops = operations.getOperations().stream().map(TreeApiImpl::toOp).collect(ImmutableList.toImmutableList());
    CommitMeta commitMeta = operations.getCommitMeta();
    if (commitMeta.getCommitter() != null) {
        throw new IllegalArgumentException("Cannot set the committer on the client side. It is set by the server.");
    }
    try {
        Hash newHash = getStore().commit(BranchName.of(Optional.ofNullable(branch).orElse(getConfig().getDefaultBranch())), Optional.ofNullable(hash).map(Hash::of), commitMetaUpdate().apply(commitMeta), ops);
        return Branch.of(branch, newHash.asString());
    } catch (ReferenceNotFoundException e) {
        throw new NessieReferenceNotFoundException(e.getMessage(), e);
    } catch (ReferenceConflictException e) {
        throw new NessieReferenceConflictException(e.getMessage(), e);
    }
}
Also used : NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) NessieReferenceConflictException(org.projectnessie.error.NessieReferenceConflictException) NessieReferenceConflictException(org.projectnessie.error.NessieReferenceConflictException) Operation(org.projectnessie.model.Operation) CommitMeta(org.projectnessie.model.CommitMeta) WithHash(org.projectnessie.versioned.WithHash) Hash(org.projectnessie.versioned.Hash)

Example 7 with CommitMeta

use of org.projectnessie.model.CommitMeta in project nessie by projectnessie.

the class TestStoreWorker method testCommitSerde.

@Test
void testCommitSerde() throws JsonProcessingException {
    CommitMeta expectedCommit = ImmutableCommitMeta.builder().commitTime(Instant.now()).authorTime(Instant.now()).author("bill").committer("ted").hash("xyz").message("commit msg").build();
    ByteString expectedBytes = ByteString.copyFrom(MAPPER.writeValueAsBytes(expectedCommit));
    CommitMeta actualCommit = worker.getMetadataSerializer().fromBytes(expectedBytes);
    Assertions.assertEquals(expectedCommit, actualCommit);
    ByteString actualBytes = worker.getMetadataSerializer().toBytes(expectedCommit);
    Assertions.assertEquals(expectedBytes, actualBytes);
    actualBytes = worker.getMetadataSerializer().toBytes(expectedCommit);
    Assertions.assertEquals(expectedCommit, worker.getMetadataSerializer().fromBytes(actualBytes));
    actualCommit = worker.getMetadataSerializer().fromBytes(expectedBytes);
    Assertions.assertEquals(expectedBytes, worker.getMetadataSerializer().toBytes(actualCommit));
}
Also used : ByteString(com.google.protobuf.ByteString) ImmutableCommitMeta(org.projectnessie.model.ImmutableCommitMeta) CommitMeta(org.projectnessie.model.CommitMeta) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with CommitMeta

use of org.projectnessie.model.CommitMeta in project nessie by projectnessie.

the class GenerateContent method execute.

@Override
public void execute() throws BaseNessieClientServerException {
    if (runtimeDuration != null) {
        if (runtimeDuration.isZero() || runtimeDuration.isNegative()) {
            throw new ParameterException(spec.commandLine(), "Duration must be absent to greater than zero.");
        }
    }
    Duration perCommitDuration = Optional.ofNullable(runtimeDuration).orElse(Duration.ZERO).dividedBy(numCommits);
    ThreadLocalRandom random = ThreadLocalRandom.current();
    String runStartTime = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss").format(LocalDateTime.now());
    List<ContentKey> tableNames = IntStream.range(0, numTables).mapToObj(i -> ContentKey.of(String.format("create-contents-%s", runStartTime), "contents", Integer.toString(i))).collect(Collectors.toList());
    try (NessieApiV1 api = createNessieApiInstance()) {
        Branch defaultBranch;
        if (defaultBranchName == null) {
            // Use the server's default branch.
            defaultBranch = api.getDefaultBranch();
        } else {
            // Use the specified default branch.
            try {
                defaultBranch = (Branch) api.getReference().refName(defaultBranchName).get();
            } catch (NessieReferenceNotFoundException e) {
                // Create branch if it does not exist.
                defaultBranch = api.getDefaultBranch();
                defaultBranch = (Branch) api.createReference().reference(Branch.of(defaultBranchName, defaultBranch.getHash())).sourceRefName(defaultBranch.getName()).create();
            }
        }
        List<String> branches = new ArrayList<>();
        branches.add(defaultBranch.getName());
        while (branches.size() < branchCount) {
            // Create a new branch
            String newBranchName = "branch-" + runStartTime + "_" + (branches.size() - 1);
            Branch branch = Branch.of(newBranchName, defaultBranch.getHash());
            spec.commandLine().getOut().printf("Creating branch '%s' from '%s' at %s%n", branch.getName(), defaultBranch.getName(), branch.getHash());
            api.createReference().reference(branch).sourceRefName(defaultBranch.getName()).create();
            branches.add(newBranchName);
        }
        spec.commandLine().getOut().printf("Starting contents generation, %d commits...%n", numCommits);
        for (int i = 0; i < numCommits; i++) {
            // Choose a random branch to commit to
            String branchName = branches.get(random.nextInt(branches.size()));
            Branch commitToBranch = (Branch) api.getReference().refName(branchName).get();
            ContentKey tableName = tableNames.get(random.nextInt(tableNames.size()));
            Content tableContents = api.getContent().refName(branchName).key(tableName).get().get(tableName);
            Content newContents = createContents(tableContents, random);
            spec.commandLine().getOut().printf("Committing content-key '%s' to branch '%s' at %s%n", tableName, commitToBranch.getName(), commitToBranch.getHash());
            CommitMultipleOperationsBuilder commit = api.commitMultipleOperations().branch(commitToBranch).commitMeta(CommitMeta.builder().message(String.format("%s table %s on %s, commit #%d of %d", tableContents != null ? "Update" : "Create", tableName, branchName, i, numCommits)).author(System.getProperty("user.name")).authorTime(Instant.now()).build());
            if (newContents instanceof IcebergTable || newContents instanceof IcebergView) {
                commit.operation(Put.of(tableName, newContents, tableContents));
            } else {
                commit.operation(Put.of(tableName, newContents));
            }
            Branch newHead = commit.commit();
            if (random.nextDouble() < newTagProbability) {
                Tag tag = Tag.of("new-tag-" + random.nextLong(), newHead.getHash());
                spec.commandLine().getOut().printf("Creating tag '%s' from '%s' at %s%n", tag.getName(), branchName, tag.getHash());
                api.createReference().reference(tag).sourceRefName(branchName).create();
            }
            try {
                TimeUnit.NANOSECONDS.sleep(perCommitDuration.toNanos());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
    }
    spec.commandLine().getOut().printf("Done creating contents.%n");
}
Also used : IntStream(java.util.stream.IntStream) ImmutableIcebergView(org.projectnessie.model.ImmutableIcebergView) Put(org.projectnessie.model.Operation.Put) LocalDateTime(java.time.LocalDateTime) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ParameterException(picocli.CommandLine.ParameterException) Spec(picocli.CommandLine.Spec) ArrayList(java.util.ArrayList) Duration(java.time.Duration) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Content(org.projectnessie.model.Content) CommitMeta(org.projectnessie.model.CommitMeta) Command(picocli.CommandLine.Command) Branch(org.projectnessie.model.Branch) ImmutableDeltaLakeTable(org.projectnessie.model.ImmutableDeltaLakeTable) Min(javax.validation.constraints.Min) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) NessieApiV1(org.projectnessie.client.api.NessieApiV1) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ImmutableIcebergTable(org.projectnessie.model.ImmutableIcebergTable) Option(picocli.CommandLine.Option) IcebergView(org.projectnessie.model.IcebergView) IcebergTable(org.projectnessie.model.IcebergTable) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Tag(org.projectnessie.model.Tag) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException) ContentKey(org.projectnessie.model.ContentKey) CommandSpec(picocli.CommandLine.Model.CommandSpec) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) ArrayList(java.util.ArrayList) Duration(java.time.Duration) ImmutableIcebergView(org.projectnessie.model.ImmutableIcebergView) IcebergView(org.projectnessie.model.IcebergView) ContentKey(org.projectnessie.model.ContentKey) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) Branch(org.projectnessie.model.Branch) Content(org.projectnessie.model.Content) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ImmutableIcebergTable(org.projectnessie.model.ImmutableIcebergTable) IcebergTable(org.projectnessie.model.IcebergTable) ParameterException(picocli.CommandLine.ParameterException) Tag(org.projectnessie.model.Tag) NessieApiV1(org.projectnessie.client.api.NessieApiV1)

Example 9 with CommitMeta

use of org.projectnessie.model.CommitMeta in project nessie by projectnessie.

the class AbstractRestMergeTransplant method transplant.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void transplant(boolean withDetachedCommit) throws BaseNessieClientServerException {
    Branch base = createBranch("transplant-base");
    Branch branch = createBranch("transplant-branch");
    IcebergTable table1 = IcebergTable.of("transplant-table1", 42, 42, 42, 42);
    IcebergTable table2 = IcebergTable.of("transplant-table2", 43, 43, 43, 43);
    Branch committed1 = getApi().commitMultipleOperations().branchName(branch.getName()).hash(branch.getHash()).commitMeta(CommitMeta.fromMessage("test-transplant-branch1")).operation(Put.of(ContentKey.of("key1"), table1)).commit();
    assertThat(committed1.getHash()).isNotNull();
    Branch committed2 = getApi().commitMultipleOperations().branchName(branch.getName()).hash(committed1.getHash()).commitMeta(CommitMeta.fromMessage("test-transplant-branch2")).operation(Put.of(ContentKey.of("key1"), table1, table1)).commit();
    assertThat(committed2.getHash()).isNotNull();
    int commitsToTransplant = 2;
    LogResponse logBranch = getApi().getCommitLog().refName(branch.getName()).untilHash(branch.getHash()).maxRecords(commitsToTransplant).get();
    getApi().commitMultipleOperations().branchName(base.getName()).hash(base.getHash()).commitMeta(CommitMeta.fromMessage("test-transplant-main")).operation(Put.of(ContentKey.of("key2"), table2)).commit();
    getApi().transplantCommitsIntoBranch().hashesToTransplant(ImmutableList.of(committed1.getHash(), committed2.getHash())).fromRefName(maybeAsDetachedName(withDetachedCommit, branch)).branch(base).transplant();
    LogResponse log = getApi().getCommitLog().refName(base.getName()).untilHash(base.getHash()).get();
    assertThat(log.getLogEntries().stream().map(LogEntry::getCommitMeta).map(CommitMeta::getMessage)).containsExactly("test-transplant-branch2", "test-transplant-branch1", "test-transplant-main");
    // Verify that the commit-timestamp was updated
    LogResponse logOfTransplanted = getApi().getCommitLog().refName(base.getName()).maxRecords(commitsToTransplant).get();
    assertThat(logOfTransplanted.getLogEntries().stream().map(LogEntry::getCommitMeta).map(CommitMeta::getCommitTime)).isNotEqualTo(logBranch.getLogEntries().stream().map(LogEntry::getCommitMeta).map(CommitMeta::getCommitTime));
    assertThat(getApi().getEntries().refName(base.getName()).get().getEntries().stream().map(e -> e.getName().getName())).containsExactlyInAnyOrder("key1", "key2");
}
Also used : LogResponse(org.projectnessie.model.LogResponse) Branch(org.projectnessie.model.Branch) IcebergTable(org.projectnessie.model.IcebergTable) CommitMeta(org.projectnessie.model.CommitMeta) LogEntry(org.projectnessie.model.LogResponse.LogEntry) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with CommitMeta

use of org.projectnessie.model.CommitMeta in project nessie by projectnessie.

the class AbstractRestNamespace method testNamespaceMerge.

@Test
public void testNamespaceMerge() throws BaseNessieClientServerException {
    Branch base = createBranch("merge-base");
    Branch branch = createBranch("merge-branch");
    Namespace ns = Namespace.parse("a.b.c");
    // create the same namespace on both branches
    getApi().createNamespace().namespace(ns).refName(branch.getName()).create();
    getApi().createNamespace().namespace(ns).refName(base.getName()).create();
    base = (Branch) getApi().getReference().refName(base.getName()).get();
    branch = (Branch) getApi().getReference().refName(branch.getName()).get();
    getApi().mergeRefIntoBranch().branch(base).fromRef(branch).merge();
    LogResponse log = getApi().getCommitLog().refName(base.getName()).untilHash(base.getHash()).get();
    String expectedCommitMsg = "create namespace a.b.c";
    assertThat(log.getLogEntries().stream().map(LogEntry::getCommitMeta).map(CommitMeta::getMessage)).containsExactly(expectedCommitMsg, expectedCommitMsg);
    assertThat(getApi().getEntries().refName(base.getName()).get().getEntries().stream().map(Entry::getName)).containsExactly(ContentKey.of(ns.getElements()));
    assertThat(getApi().getNamespace().refName(base.getName()).namespace(ns).get()).isNotNull();
}
Also used : LogResponse(org.projectnessie.model.LogResponse) LogEntry(org.projectnessie.model.LogResponse.LogEntry) Entry(org.projectnessie.model.EntriesResponse.Entry) Branch(org.projectnessie.model.Branch) CommitMeta(org.projectnessie.model.CommitMeta) Namespace(org.projectnessie.model.Namespace) Test(org.junit.jupiter.api.Test)

Aggregations

CommitMeta (org.projectnessie.model.CommitMeta)37 Branch (org.projectnessie.model.Branch)17 LogEntry (org.projectnessie.model.LogResponse.LogEntry)15 Test (org.junit.jupiter.api.Test)14 IcebergTable (org.projectnessie.model.IcebergTable)14 ContentKey (org.projectnessie.model.ContentKey)13 LogResponse (org.projectnessie.model.LogResponse)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Content (org.projectnessie.model.Content)10 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 BaseNessieClientServerException (org.projectnessie.error.BaseNessieClientServerException)8 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)8 Reference (org.projectnessie.model.Reference)7 ArrayList (java.util.ArrayList)6 Entry (org.projectnessie.model.EntriesResponse.Entry)6 Operation (org.projectnessie.model.Operation)6 Put (org.projectnessie.model.Operation.Put)6 Stream (java.util.stream.Stream)5