Search in sources :

Example 1 with Put

use of org.projectnessie.versioned.Put 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());
}
Also used : Delete(org.projectnessie.versioned.Delete) ByteString(com.google.protobuf.ByteString) ImmutableCommitAttempt(org.projectnessie.versioned.persist.adapter.ImmutableCommitAttempt) ContentId(org.projectnessie.versioned.persist.adapter.ContentId) Put(org.projectnessie.versioned.Put) Unchanged(org.projectnessie.versioned.Unchanged)

Example 2 with Put

use of org.projectnessie.versioned.Put 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))));
    });
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) BaseContent(org.projectnessie.versioned.testworker.BaseContent) ImmutableList(com.google.common.collect.ImmutableList) VersionStore(org.projectnessie.versioned.VersionStore) Delete(org.projectnessie.versioned.Delete) OnRefOnly.onRef(org.projectnessie.versioned.testworker.OnRefOnly.onRef) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CommitMessage.commitMessage(org.projectnessie.versioned.testworker.CommitMessage.commitMessage) Hash(org.projectnessie.versioned.Hash) Commit(org.projectnessie.versioned.Commit) Key(org.projectnessie.versioned.Key) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) BranchName(org.projectnessie.versioned.BranchName) List(java.util.List) Stream(java.util.stream.Stream) OnRefOnly.newOnRef(org.projectnessie.versioned.testworker.OnRefOnly.newOnRef) Optional(java.util.Optional) Put(org.projectnessie.versioned.Put) Collections(java.util.Collections) Commit(org.projectnessie.versioned.Commit) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) BaseContent(org.projectnessie.versioned.testworker.BaseContent) BranchName(org.projectnessie.versioned.BranchName) Hash(org.projectnessie.versioned.Hash) Test(org.junit.jupiter.api.Test)

Example 3 with Put

use of org.projectnessie.versioned.Put 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)

Aggregations

Delete (org.projectnessie.versioned.Delete)3 Put (org.projectnessie.versioned.Put)3 Commit (org.projectnessie.versioned.Commit)2 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertNotEquals (org.junit.jupiter.api.Assertions.assertNotEquals)1 Test (org.junit.jupiter.api.Test)1 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)1 CommitMeta (org.projectnessie.model.CommitMeta)1