Search in sources :

Example 31 with ContentKey

use of org.projectnessie.model.ContentKey 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 32 with ContentKey

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

the class TestAuthorizationRules method testCanCommitButNotUpdateOrDeleteEntity.

@Test
// test_user2 has all permissions on a Branch, but not permissions on a Key
@TestSecurity(user = "test_user2")
void testCanCommitButNotUpdateOrDeleteEntity() throws BaseNessieClientServerException {
    String role = "test_user2";
    ContentKey key = ContentKey.of("allowed", "some");
    String branchName = "allowedBranchForTestUser2";
    createBranch(Branch.of(branchName, null), role, false);
    listAllReferences(branchName, false);
    final Branch branch = retrieveBranch(branchName, role, false);
    assertThatThrownBy(() -> api().commitMultipleOperations().branch(branch).commitMeta(CommitMeta.fromMessage("add stuff")).operation(Put.of(key, IcebergTable.of("foo", 42, 42, 42, 42, "cid-foo"))).commit()).isInstanceOf(NessieForbiddenException.class).hasMessageContaining(String.format("'UPDATE_ENTITY' is not allowed for role '%s' on content '%s'", role, key.toPathString()));
    readContent(branchName, key, role, true);
    final Branch b = retrieveBranch(branchName, role, false);
    assertThatThrownBy(() -> api().commitMultipleOperations().branch(b).commitMeta(CommitMeta.fromMessage("delete stuff")).operation(Delete.of(key)).commit()).isInstanceOf(NessieForbiddenException.class).hasMessageContaining(String.format("'DELETE_ENTITY' is not allowed for role '%s' on content '%s'", role, key.toPathString()));
    deleteBranch(branch, role, false);
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Branch(org.projectnessie.model.Branch) NessieForbiddenException(org.projectnessie.error.NessieForbiddenException) TestSecurity(io.quarkus.test.security.TestSecurity) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with ContentKey

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

the class TestAuthorizationRules method testAllOps.

private void testAllOps(String branchName, String role, boolean shouldFail) throws BaseNessieClientServerException {
    boolean isAdmin = role.equals("admin_user");
    ContentKey key = ContentKey.of("allowed", "x");
    if (shouldFail) {
        branchName = "disallowedBranchForTestUser";
        key = ContentKey.of("disallowed", "x");
    }
    createBranch(Branch.of(branchName, null), role, shouldFail);
    Branch branchWithInvalidHash = Branch.of(branchName, "1234567890123456");
    Branch branch = shouldFail ? branchWithInvalidHash : retrieveBranch(branchName, role, shouldFail);
    listAllReferences(branchName, shouldFail);
    String cid = "cid-foo-" + UUID.randomUUID();
    addContent(branch, Put.of(key, IcebergTable.of("foo", 42, 42, 42, 42, cid)), role, shouldFail);
    if (!shouldFail) {
        // These requests cannot succeed, because "disallowedBranchForTestUser" could not be created
        getCommitLog(branchName, role, shouldFail);
        getEntriesFor(branchName, role, shouldFail);
        readContent(branchName, key, role, shouldFail);
    }
    branch = shouldFail ? branchWithInvalidHash : retrieveBranch(branchName, role, shouldFail);
    deleteContent(branch, Delete.of(key), role, shouldFail);
    branch = shouldFail ? branchWithInvalidHash : retrieveBranch(branchName, role, shouldFail);
    deleteBranch(branch, role, shouldFail);
    getRefLog(role, !isAdmin);
    Branch defaultBranch = api().getDefaultBranch();
    deleteBranch(defaultBranch, role, !isAdmin);
    if (isAdmin) {
        // need to recreate the default branch, so the test can continue normally
        api().createReference().reference(Branch.of(defaultBranch.getName(), null)).create();
    }
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Branch(org.projectnessie.model.Branch)

Example 34 with ContentKey

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

the class ITDeltaLogBranches method testCheckpoint.

@Test
void testCheckpoint() throws NessieNotFoundException {
    Dataset<Row> targetTable = createKVDataSet(Arrays.asList(tuple2(1, 10), tuple2(2, 20), tuple2(3, 30), tuple2(4, 40)), "key", "value");
    // write some data to table
    targetTable.write().format("delta").save(tempPath.getAbsolutePath());
    // write enough to trigger a checkpoint generation
    for (int i = 0; i < 15; i++) {
        targetTable.write().format("delta").mode("append").save(tempPath.getAbsolutePath());
    }
    DeltaTable target = DeltaTable.forPath(spark, tempPath.getAbsolutePath());
    int expectedSize = target.toDF().collectAsList().size();
    Assertions.assertEquals(64, expectedSize);
    String tableName = tempPath.getAbsolutePath() + "/_delta_log";
    ContentKey key = ContentKey.of(tableName.split("/"));
    Content content = api.getContent().key(key).refName("main").get().get(key);
    Optional<DeltaLakeTable> table = content.unwrap(DeltaLakeTable.class);
    Assertions.assertTrue(table.isPresent());
    Assertions.assertEquals(1, table.get().getCheckpointLocationHistory().size());
    Assertions.assertEquals(5, table.get().getMetadataLocationHistory().size());
    Assertions.assertNotNull(table.get().getLastCheckpoint());
}
Also used : DeltaTable(io.delta.tables.DeltaTable) ContentKey(org.projectnessie.model.ContentKey) DeltaLakeTable(org.projectnessie.model.DeltaLakeTable) Content(org.projectnessie.model.Content) Row(org.apache.spark.sql.Row) Test(org.junit.jupiter.api.Test)

Example 35 with ContentKey

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

the class NessieViewOperations method view.

private IcebergView view(TableIdentifier viewIdentifier) {
    try {
        ContentKey key = NessieUtil.toKey(viewIdentifier);
        Content view = api.getContent().key(key).reference(reference.getReference()).get().get(key);
        return view != null ? view.unwrap(IcebergView.class).orElse(null) : null;
    } catch (NessieNotFoundException e) {
        return null;
    }
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Content(org.projectnessie.model.Content) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException)

Aggregations

ContentKey (org.projectnessie.model.ContentKey)35 Branch (org.projectnessie.model.Branch)20 Test (org.junit.jupiter.api.Test)15 Content (org.projectnessie.model.Content)15 IcebergTable (org.projectnessie.model.IcebergTable)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 CommitMeta (org.projectnessie.model.CommitMeta)10 Put (org.projectnessie.model.Operation.Put)7 Map (java.util.Map)6 Entry (org.projectnessie.model.EntriesResponse.Entry)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 NessieApiV1 (org.projectnessie.client.api.NessieApiV1)5 NessieNotFoundException (org.projectnessie.error.NessieNotFoundException)5 LogResponse (org.projectnessie.model.LogResponse)5 Reference (org.projectnessie.model.Reference)5 HashMap (java.util.HashMap)4 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)4 LogEntry (org.projectnessie.model.LogResponse.LogEntry)4