use of org.projectnessie.model.Operation.Delete in project nessie by projectnessie.
the class AbstractRestDiff method testDiff.
@ParameterizedTest
@MethodSource("diffRefModes")
public void testDiff(ReferenceMode refModeFrom, ReferenceMode refModeTo) throws BaseNessieClientServerException {
int commitsPerBranch = 10;
Reference fromRef = getApi().createReference().reference(Branch.of("testDiffFromRef", null)).create();
Reference toRef = getApi().createReference().reference(Branch.of("testDiffToRef", null)).create();
String toRefHash = createCommits(toRef, 1, commitsPerBranch, toRef.getHash());
toRef = Branch.of(toRef.getName(), toRefHash);
List<DiffEntry> diffOnRefHeadResponse = getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRef)).get().getDiffs();
// we only committed to toRef, the "from" diff should be null
assertThat(diffOnRefHeadResponse).hasSize(commitsPerBranch).allSatisfy(diff -> {
assertThat(diff.getKey()).isNotNull();
assertThat(diff.getFrom()).isNull();
assertThat(diff.getTo()).isNotNull();
});
// Some combinations with explicit fromHashOnRef/toHashOnRef
assertThat(getApi().getDiff().fromRefName(fromRef.getName()).fromHashOnRef(fromRef.getHash()).toRefName(toRef.getName()).toHashOnRef(toRef.getHash()).get().getDiffs()).isEqualTo(diffOnRefHeadResponse);
// result
if (refModeTo != ReferenceMode.NAME_ONLY) {
Branch toRefAtFrom = Branch.of(toRef.getName(), fromRef.getHash());
assertThat(getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRefAtFrom)).get().getDiffs()).isEmpty();
}
// after committing to fromRef, "from/to" diffs should both have data
fromRef = Branch.of(fromRef.getName(), createCommits(fromRef, 1, commitsPerBranch, fromRef.getHash()));
assertThat(getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRef)).get().getDiffs()).hasSize(commitsPerBranch).allSatisfy(diff -> {
assertThat(diff.getKey()).isNotNull();
assertThat(diff.getFrom()).isNotNull();
assertThat(diff.getTo()).isNotNull();
// we only have a diff on the ID
assertThat(diff.getFrom().getId()).isNotEqualTo(diff.getTo().getId());
Optional<IcebergTable> fromTable = diff.getFrom().unwrap(IcebergTable.class);
assertThat(fromTable).isPresent();
Optional<IcebergTable> toTable = diff.getTo().unwrap(IcebergTable.class);
assertThat(toTable).isPresent();
assertThat(fromTable.get().getMetadataLocation()).isEqualTo(toTable.get().getMetadataLocation());
assertThat(fromTable.get().getSchemaId()).isEqualTo(toTable.get().getSchemaId());
assertThat(fromTable.get().getSnapshotId()).isEqualTo(toTable.get().getSnapshotId());
assertThat(fromTable.get().getSortOrderId()).isEqualTo(toTable.get().getSortOrderId());
assertThat(fromTable.get().getSpecId()).isEqualTo(toTable.get().getSpecId());
});
List<ContentKey> keys = IntStream.rangeClosed(0, commitsPerBranch).mapToObj(i -> ContentKey.of("table" + i)).collect(Collectors.toList());
// request all keys and delete the tables for them on toRef
Map<ContentKey, Content> map = getApi().getContent().refName(toRef.getName()).keys(keys).get();
for (Map.Entry<ContentKey, Content> entry : map.entrySet()) {
toRef = getApi().commitMultipleOperations().branchName(toRef.getName()).hash(toRefHash).commitMeta(CommitMeta.fromMessage("delete")).operation(Delete.of(entry.getKey())).commit();
}
// now that we deleted all tables on toRef, the diff for "to" should be null
assertThat(getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRef)).get().getDiffs()).hasSize(commitsPerBranch).allSatisfy(diff -> {
assertThat(diff.getKey()).isNotNull();
assertThat(diff.getFrom()).isNotNull();
assertThat(diff.getTo()).isNull();
});
}
use of org.projectnessie.model.Operation.Delete in project nessie by projectnessie.
the class NamespaceApiImpl method deleteNamespace.
@Override
public void deleteNamespace(NamespaceParams params) throws NessieReferenceNotFoundException, NessieNamespaceNotEmptyException, NessieNamespaceNotFoundException {
BranchName branch = branchFromRefName(params.getRefName());
try {
Namespace namespace = getNamespace(params, branch);
Delete delete = Delete.of(ContentKey.of(namespace.getElements()));
Callable<Void> validator = () -> {
try (Stream<WithType<Key, Type>> keys = getStore().getKeys(branch)) {
if (keys.anyMatch(k -> Namespace.of(k.getValue().getElements()).name().startsWith(params.getNamespace().name()) && k.getType() != Type.NAMESPACE)) {
throw namespaceNotEmptyException(params);
}
}
return null;
};
commit(branch, "delete namespace " + namespace.name(), TreeApiImpl.toOp(delete), validator);
} catch (ReferenceNotFoundException | ReferenceConflictException e) {
throw new NessieReferenceNotFoundException(e.getMessage(), e);
}
}
use of org.projectnessie.model.Operation.Delete in project nessie by projectnessie.
the class AbstractRestCommitLog method commitLogExtended.
@ParameterizedTest
@EnumSource(ReferenceMode.class)
public void commitLogExtended(ReferenceMode refMode) throws Exception {
String branch = "commitLogExtended";
String firstParent = getApi().createReference().sourceRefName("main").reference(Branch.of(branch, null)).create().getHash();
int numCommits = 10;
// Hack for tests running via Quarkus :(
IntFunction<String> c1 = i -> refMode.name() + "-c1-" + i;
IntFunction<String> c2 = i -> refMode.name() + "-c2-" + i;
List<String> hashes = IntStream.rangeClosed(1, numCommits).mapToObj(i -> {
try {
String head = getApi().getReference().refName(branch).get().getHash();
return getApi().commitMultipleOperations().operation(Put.of(ContentKey.of("k" + i), IcebergTable.of("m" + i, i, i, i, i, c1.apply(i)))).operation(Put.of(ContentKey.of("key" + i), IcebergTable.of("meta" + i, i, i, i, i, c2.apply(i)))).operation(Delete.of(ContentKey.of("delete" + i))).operation(Unchanged.of(ContentKey.of("key" + i))).commitMeta(CommitMeta.fromMessage("Commit #" + i)).branchName(branch).hash(head).commit().getHash();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
List<String> parentHashes = Stream.concat(Stream.of(firstParent), hashes.subList(0, 9).stream()).collect(Collectors.toList());
Reference branchRef = getApi().getReference().refName(branch).get();
assertThat(Lists.reverse(getApi().getCommitLog().untilHash(firstParent).reference(refMode.transform(branchRef)).get().getLogEntries())).allSatisfy(c -> {
assertThat(c.getOperations()).isNull();
assertThat(c.getParentCommitHash()).isNull();
}).extracting(e -> e.getCommitMeta().getHash()).containsExactlyElementsOf(hashes);
List<LogEntry> commits = Lists.reverse(getApi().getCommitLog().fetch(FetchOption.ALL).reference(refMode.transform(branchRef)).untilHash(firstParent).get().getLogEntries());
assertThat(IntStream.rangeClosed(1, numCommits)).allSatisfy(i -> {
LogEntry c = commits.get(i - 1);
assertThat(c).extracting(e -> e.getCommitMeta().getMessage(), e -> e.getCommitMeta().getHash(), LogEntry::getParentCommitHash, LogEntry::getOperations).containsExactly("Commit #" + i, hashes.get(i - 1), parentHashes.get(i - 1), Arrays.asList(Delete.of(ContentKey.of("delete" + i)), Put.of(ContentKey.of("k" + i), IcebergTable.of("m" + i, i, i, i, i, c1.apply(i))), Put.of(ContentKey.of("key" + i), IcebergTable.of("meta" + i, i, i, i, i, c2.apply(i)))));
});
}
use of org.projectnessie.model.Operation.Delete in project nessie by projectnessie.
the class TreeApiImplWithAuthorization method commitMultipleOperations.
@Override
public Branch commitMultipleOperations(String branch, String hash, Operations operations) throws NessieNotFoundException, NessieConflictException {
BranchName branchName = BranchName.of(branch);
BatchAccessChecker check = startAccessCheck().canCommitChangeAgainstReference(branchName);
operations.getOperations().forEach(op -> {
if (op instanceof Delete) {
check.canDeleteEntity(branchName, op.getKey(), null);
} else if (op instanceof Put) {
check.canUpdateEntity(branchName, op.getKey(), null);
}
});
check.checkAndThrow();
return super.commitMultipleOperations(branch, hash, operations);
}
Aggregations