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