use of org.projectnessie.versioned.Delete in project nessie by projectnessie.
the class PersistVersionStore method commit.
@Override
public Hash commit(@Nonnull BranchName branch, @Nonnull Optional<Hash> expectedHead, @Nonnull METADATA metadata, @Nonnull List<Operation<CONTENT>> operations, @Nonnull Callable<Void> validator) throws ReferenceNotFoundException, ReferenceConflictException {
ImmutableCommitAttempt.Builder commitAttempt = ImmutableCommitAttempt.builder().commitToBranch(branch).expectedHead(expectedHead).commitMetaSerialized(serializeMetadata(metadata)).validator(validator);
for (Operation<CONTENT> operation : operations) {
if (operation instanceof Put) {
Put<CONTENT> op = (Put<CONTENT>) operation;
ContentId contentId = ContentId.of(storeWorker.getId(op.getValue()));
commitAttempt.addPuts(KeyWithBytes.of(op.getKey(), contentId, storeWorker.getPayload(op.getValue()), storeWorker.toStoreOnReferenceState(op.getValue())));
if (storeWorker.requiresGlobalState(op.getValue())) {
ByteString newState = storeWorker.toStoreGlobalState(op.getValue());
Optional<ByteString> expectedValue;
if (op.getExpectedValue() != null) {
if (storeWorker.getType(op.getValue()) != storeWorker.getType(op.getExpectedValue())) {
throw new IllegalArgumentException(String.format("Content-type for conditional put-operation for key '%s' for 'value' and 'expectedValue' must be the same, but are '%s' and '%s'.", op.getKey(), storeWorker.getType(op.getValue()), storeWorker.getType(op.getExpectedValue())));
}
if (!contentId.equals(ContentId.of(storeWorker.getId(op.getExpectedValue())))) {
throw new IllegalArgumentException(String.format("Conditional put-operation key '%s' has different content-ids.", op.getKey()));
}
expectedValue = Optional.of(storeWorker.toStoreGlobalState(op.getExpectedValue()));
} else {
expectedValue = Optional.empty();
}
commitAttempt.putExpectedStates(contentId, expectedValue);
commitAttempt.putGlobal(contentId, newState);
} else {
if (op.getExpectedValue() != null) {
throw new IllegalArgumentException(String.format("Content-type '%s' for put-operation for key '%s' does not support global state, expected-value not supported for this content-type.", storeWorker.getType(op.getValue()), op.getKey()));
}
}
} else if (operation instanceof Delete) {
commitAttempt.addDeletes(operation.getKey());
} else if (operation instanceof Unchanged) {
commitAttempt.addUnchanged(operation.getKey());
} else {
throw new IllegalArgumentException(String.format("Unknown operation type '%s'", operation));
}
}
return databaseAdapter.commit(commitAttempt.build());
}
use of org.projectnessie.versioned.Delete in project nessie by projectnessie.
the class AbstractCommitLog method commitLogExtendedNoGlobalState.
@Test
public void commitLogExtendedNoGlobalState() throws Exception {
BranchName branch = BranchName.of("commitLogExtended");
Hash firstParent = store().create(branch, Optional.empty());
int numCommits = 10;
List<Hash> hashes = IntStream.rangeClosed(1, numCommits).mapToObj(i -> {
try {
return commit("Commit #" + i).put("k" + i, onRef("v" + i, "c" + i)).put("key" + i, onRef("value" + i, "cid" + i)).delete("delete" + i).toBranch(branch);
} catch (Exception e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
List<Hash> parentHashes = Stream.concat(Stream.of(firstParent), hashes.subList(0, 9).stream()).collect(Collectors.toList());
assertThat(Lists.reverse(commitsList(branch, false))).allSatisfy(c -> {
assertThat(c.getOperations()).isNull();
assertThat(c.getParentHash()).isNull();
}).extracting(Commit::getHash).containsExactlyElementsOf(hashes);
List<Commit<CommitMessage, BaseContent>> commits = Lists.reverse(commitsList(branch, true));
assertThat(IntStream.rangeClosed(1, numCommits)).allSatisfy(i -> {
Commit<CommitMessage, BaseContent> c = commits.get(i - 1);
assertThat(c).extracting(Commit::getCommitMeta, Commit::getHash, Commit::getParentHash, Commit::getOperations).containsExactly(commitMessage("Commit #" + i), hashes.get(i - 1), parentHashes.get(i - 1), Arrays.asList(Delete.of(Key.of("delete" + i)), Put.of(Key.of("k" + i), onRef("v" + i, "c" + i)), Put.of(Key.of("key" + i), onRef("value" + i, "cid" + i))));
});
}
use of org.projectnessie.versioned.Delete 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);
}
}
Aggregations