Search in sources :

Example 1 with BaseContent

use of org.projectnessie.versioned.testworker.BaseContent in project nessie by projectnessie.

the class AbstractCommitScenarios method commitRenameTable.

/**
 * Test whether a "table rename operation" works as expected.
 *
 * <p>A "table rename" is effectively a remove-operation plus a put-operation with a different key
 * but the same content-id.
 *
 * <p>Parameterized to force operations to cross/not-cross the number of commits between "full key
 * lists" and whether global-state shall be used.
 */
@ParameterizedTest
@MethodSource("commitRenameTableParams")
void commitRenameTable(RenameTable param) throws Exception {
    BranchName branch = BranchName.of("main");
    Key dummyKey = Key.of("dummy");
    Key oldKey = Key.of("hello", "table");
    Key newKey = Key.of("new", "name");
    ContentId contentId = ContentId.of("id-42");
    IntFunction<Hash> performDummyCommit = i -> {
        try {
            return databaseAdapter.commit(ImmutableCommitAttempt.builder().commitToBranch(branch).commitMetaSerialized(ByteString.copyFromUtf8("dummy commit meta " + i)).addUnchanged(dummyKey).build());
        } catch (ReferenceNotFoundException | ReferenceConflictException e) {
            throw new RuntimeException(e);
        }
    };
    List<Hash> beforeInitial = IntStream.range(0, param.setupCommits).mapToObj(performDummyCommit).collect(Collectors.toList());
    ImmutableCommitAttempt.Builder commit;
    BaseContent initialContent;
    BaseContent renamContent;
    if (param.globalState) {
        initialContent = WithGlobalStateContent.newWithGlobal("0", "initial commit content");
        renamContent = WithGlobalStateContent.withGlobal("0", "rename commit content", initialContent.getId());
    } else {
        initialContent = OnRefOnly.newOnRef("initial commit content");
        renamContent = OnRefOnly.onRef("rename commit content", initialContent.getId());
    }
    byte payload = SimpleStoreWorker.INSTANCE.getPayload(initialContent);
    commit = ImmutableCommitAttempt.builder().commitToBranch(branch).commitMetaSerialized(ByteString.copyFromUtf8("initial commit meta")).addPuts(KeyWithBytes.of(oldKey, contentId, payload, SimpleStoreWorker.INSTANCE.toStoreOnReferenceState(initialContent)));
    if (param.globalState) {
        commit.putGlobal(contentId, SimpleStoreWorker.INSTANCE.toStoreGlobalState(initialContent)).putExpectedStates(contentId, Optional.empty());
    }
    Hash hashInitial = databaseAdapter.commit(commit.build());
    List<Hash> beforeRename = IntStream.range(0, param.afterInitialCommits).mapToObj(performDummyCommit).collect(Collectors.toList());
    commit = ImmutableCommitAttempt.builder().commitToBranch(branch).commitMetaSerialized(ByteString.copyFromUtf8("rename table")).addDeletes(oldKey).addPuts(KeyWithBytes.of(newKey, contentId, payload, SimpleStoreWorker.INSTANCE.toStoreOnReferenceState(renamContent)));
    if (param.globalState) {
        commit.putGlobal(contentId, SimpleStoreWorker.INSTANCE.toStoreGlobalState(renamContent)).putExpectedStates(contentId, Optional.of(SimpleStoreWorker.INSTANCE.toStoreGlobalState(initialContent)));
    }
    Hash hashRename = databaseAdapter.commit(commit.build());
    List<Hash> beforeDelete = IntStream.range(0, param.afterRenameCommits).mapToObj(performDummyCommit).collect(Collectors.toList());
    commit = ImmutableCommitAttempt.builder().commitToBranch(branch).commitMetaSerialized(ByteString.copyFromUtf8("delete table")).addDeletes(newKey);
    if (param.globalState) {
        commit.putGlobal(contentId, ByteString.copyFromUtf8("0")).putExpectedStates(contentId, Optional.of(ByteString.copyFromUtf8("0")));
    }
    Hash hashDelete = databaseAdapter.commit(commit.build());
    List<Hash> afterDelete = IntStream.range(0, param.afterDeleteCommits).mapToObj(performDummyCommit).collect(Collectors.toList());
    int expectedCommitCount = 1;
    // Verify that the commits before the initial put return _no_ keys
    expectedCommitCount = renameCommitVerify(beforeInitial.stream(), expectedCommitCount, keys -> assertThat(keys).isEmpty());
    // Verify that the commits since the initial put and before the rename-operation return the
    // _old_ key
    expectedCommitCount = renameCommitVerify(Stream.concat(Stream.of(hashInitial), beforeRename.stream()), expectedCommitCount, keys -> assertThat(keys).containsExactly(KeyListEntry.of(oldKey, contentId, payload, hashInitial)));
    // Verify that the commits since the rename-operation and before the delete-operation return the
    // _new_ key
    expectedCommitCount = renameCommitVerify(Stream.concat(Stream.of(hashRename), beforeDelete.stream()), expectedCommitCount, keys -> assertThat(keys).containsExactly(KeyListEntry.of(newKey, contentId, payload, hashRename)));
    // Verify that the commits since the delete-operation return _no_ keys
    expectedCommitCount = renameCommitVerify(Stream.concat(Stream.of(hashDelete), afterDelete.stream()), expectedCommitCount, keys -> assertThat(keys).isEmpty());
    assertThat(expectedCommitCount - 1).isEqualTo(param.setupCommits + 1 + param.afterInitialCommits + 1 + param.afterRenameCommits + 1 + param.afterDeleteCommits);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) KeyListEntry(org.projectnessie.versioned.persist.adapter.KeyListEntry) Callable(java.util.concurrent.Callable) GetNamedRefsParams(org.projectnessie.versioned.GetNamedRefsParams) ArrayList(java.util.ArrayList) KeyFilterPredicate(org.projectnessie.versioned.persist.adapter.KeyFilterPredicate) BaseContent(org.projectnessie.versioned.testworker.BaseContent) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) Map(java.util.Map) DatabaseAdapter(org.projectnessie.versioned.persist.adapter.DatabaseAdapter) IntFunction(java.util.function.IntFunction) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) WithGlobalStateContent(org.projectnessie.versioned.testworker.WithGlobalStateContent) Hash(org.projectnessie.versioned.Hash) Key(org.projectnessie.versioned.Key) Collectors(java.util.stream.Collectors) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) KeyWithBytes(org.projectnessie.versioned.persist.adapter.KeyWithBytes) ByteString(com.google.protobuf.ByteString) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) OnRefOnly(org.projectnessie.versioned.testworker.OnRefOnly) BranchName(org.projectnessie.versioned.BranchName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ImmutableCommitAttempt(org.projectnessie.versioned.persist.adapter.ImmutableCommitAttempt) ContentAndState(org.projectnessie.versioned.persist.adapter.ContentAndState) Optional(java.util.Optional) SimpleStoreWorker(org.projectnessie.versioned.testworker.SimpleStoreWorker) Collections(java.util.Collections) ContentId(org.projectnessie.versioned.persist.adapter.ContentId) BaseContent(org.projectnessie.versioned.testworker.BaseContent) ImmutableCommitAttempt(org.projectnessie.versioned.persist.adapter.ImmutableCommitAttempt) Hash(org.projectnessie.versioned.Hash) ContentId(org.projectnessie.versioned.persist.adapter.ContentId) BranchName(org.projectnessie.versioned.BranchName) Key(org.projectnessie.versioned.Key) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with BaseContent

use of org.projectnessie.versioned.testworker.BaseContent in project nessie by projectnessie.

the class AbstractDuplicateTable method duplicateTableOnBranches.

/**
 * Test behavior when a table (content-key) is created on two different branches using either
 * table-types with and without global-states and same/different content-ids.
 */
@ParameterizedTest
@EnumSource(value = DuplicateTableMode.class)
void duplicateTableOnBranches(DuplicateTableMode mode) throws Throwable {
    Key key = Key.of("some", "table");
    BranchName branch0 = BranchName.of("globalStateDuplicateTable-main");
    store().create(branch0, Optional.empty());
    // commit just something to have a "real" common ancestor and not "beginning of time", which
    // means no-common-ancestor
    Hash ancestor = store().commit(branch0, Optional.empty(), commitMessage("initial commit"), ImmutableList.of(Put.of(Key.of("unrelated", "table"), newOnRef("value"))));
    // Create a table with the same name on two branches.
    // WITH global-states, that must fail
    // WITHOUT global-state, it is okay to work
    BranchName branch1 = BranchName.of("globalStateDuplicateTable-branch1");
    BranchName branch2 = BranchName.of("globalStateDuplicateTable-branch2");
    assertThat(store().create(branch1, Optional.of(ancestor))).isEqualTo(ancestor);
    assertThat(store().create(branch2, Optional.of(ancestor))).isEqualTo(ancestor);
    List<Operation<BaseContent>> putForBranch1;
    List<Operation<BaseContent>> putForBranch2;
    BaseContent valuebranch1;
    BaseContent valuebranch2;
    switch(mode) {
        case NO_GLOBAL:
            valuebranch1 = newOnRef("create table");
            valuebranch2 = newOnRef("create table");
            putForBranch1 = ImmutableList.of(Put.of(key, valuebranch1));
            putForBranch2 = ImmutableList.of(Put.of(key, valuebranch2));
            break;
        case STATEFUL_SAME_CONTENT_ID:
            valuebranch1 = withGlobal("state", "create table", "content-id-equal");
            valuebranch2 = withGlobal("state", "create table", "content-id-equal");
            putForBranch1 = singletonList(Put.of(key, withGlobal("state", "create table", "content-id-equal")));
            putForBranch2 = singletonList(Put.of(key, withGlobal("state", "create table", "content-id-equal")));
            break;
        case STATEFUL_DIFFERENT_CONTENT_ID:
            valuebranch1 = withGlobal("state", "create table", "content-id-1");
            valuebranch2 = withGlobal("state", "create table", "content-id-2");
            putForBranch1 = singletonList(Put.of(key, withGlobal("state", "create table", "content-id-1")));
            putForBranch2 = singletonList(Put.of(key, withGlobal("state", "create table", "content-id-2")));
            break;
        default:
            throw new IllegalStateException();
    }
    store().commit(branch1, Optional.empty(), commitMessage("create table"), putForBranch1);
    assertThat(store().getValue(branch1, key)).isEqualTo(valuebranch1);
    ThrowingCallable createTableOnOtherBranch = () -> store().commit(branch2, Optional.empty(), commitMessage("create table on other branch"), putForBranch2);
    if (mode == DuplicateTableMode.STATEFUL_SAME_CONTENT_ID) {
        assertThatThrownBy(createTableOnOtherBranch).isInstanceOf(ReferenceConflictException.class).hasMessage("Global-state for content-id 'content-id-equal' already exists.");
        assertThat(store().getValue(branch2, key)).isNull();
    } else {
        createTableOnOtherBranch.call();
        assertThat(store().getValue(branch2, key)).isEqualTo(valuebranch2);
    }
}
Also used : BaseContent(org.projectnessie.versioned.testworker.BaseContent) ThrowingCallable(org.assertj.core.api.ThrowableAssert.ThrowingCallable) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) BranchName(org.projectnessie.versioned.BranchName) Operation(org.projectnessie.versioned.Operation) Hash(org.projectnessie.versioned.Hash) Key(org.projectnessie.versioned.Key) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with BaseContent

use of org.projectnessie.versioned.testworker.BaseContent in project nessie by projectnessie.

the class AbstractAssign method assignReferenceToFreshMain.

/**
 * Assigning a branch/tag to a fresh main without any commits didn't work in 0.9.2
 */
@Test
public void assignReferenceToFreshMain() throws ReferenceNotFoundException, ReferenceAlreadyExistsException, ReferenceConflictException {
    ReferenceInfo<CommitMessage> main = store.getNamedRef("main", GetNamedRefsParams.DEFAULT);
    try (Stream<Commit<CommitMessage, BaseContent>> commits = store().getCommits(main.getHash(), false)) {
        assertThat(commits).isEmpty();
    }
    try (Stream<ReferenceInfo<CommitMessage>> refs = store().getNamedRefs(GetNamedRefsParams.DEFAULT)) {
        assertThat(refs).extracting(r -> r.getNamedRef().getName()).containsExactly(main.getNamedRef().getName());
    }
    BranchName testBranch = BranchName.of("testBranch");
    Hash testBranchHash = store.create(testBranch, Optional.empty());
    store.assign(testBranch, Optional.of(testBranchHash), main.getHash());
    assertThat(store.getNamedRef(testBranch.getName(), GetNamedRefsParams.DEFAULT).getHash()).isEqualTo(main.getHash());
    TagName testTag = TagName.of("testTag");
    Hash testTagHash = store.create(testTag, Optional.empty());
    store.assign(testTag, Optional.of(testTagHash), main.getHash());
    assertThat(store.getNamedRef(testTag.getName(), GetNamedRefsParams.DEFAULT).getHash()).isEqualTo(main.getHash());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Hash(org.projectnessie.versioned.Hash) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) Commit(org.projectnessie.versioned.Commit) TagName(org.projectnessie.versioned.TagName) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) GetNamedRefsParams(org.projectnessie.versioned.GetNamedRefsParams) Test(org.junit.jupiter.api.Test) BranchName(org.projectnessie.versioned.BranchName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) Stream(java.util.stream.Stream) BaseContent(org.projectnessie.versioned.testworker.BaseContent) VersionStore(org.projectnessie.versioned.VersionStore) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Optional(java.util.Optional) Commit(org.projectnessie.versioned.Commit) CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) TagName(org.projectnessie.versioned.TagName) BranchName(org.projectnessie.versioned.BranchName) Hash(org.projectnessie.versioned.Hash) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Test(org.junit.jupiter.api.Test)

Example 4 with BaseContent

use of org.projectnessie.versioned.testworker.BaseContent 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 5 with BaseContent

use of org.projectnessie.versioned.testworker.BaseContent in project nessie by projectnessie.

the class AbstractCommitLog method commitLogPaging.

@Test
public void commitLogPaging() throws Exception {
    BranchName branch = BranchName.of("commitLogPaging");
    Hash createHash = store().create(branch, Optional.empty());
    // this should be enough
    int commits = 95;
    Hash[] commitHashes = new Hash[commits];
    List<CommitMessage> messages = new ArrayList<>(commits);
    for (int i = 0; i < commits; i++) {
        CommitMessage msg = commitMessage(String.format("commit#%05d", i));
        messages.add(msg);
        commitHashes[i] = store().commit(branch, Optional.of(i == 0 ? createHash : commitHashes[i - 1]), msg, ImmutableList.of(Put.of(Key.of("table"), newOnRef(String.format("value#%05d", i)))));
    }
    Collections.reverse(messages);
    List<CommitMessage> justTwo = commitsList(branch, s -> s.limit(2).map(Commit::getCommitMeta), false);
    assertEquals(messages.subList(0, 2), justTwo);
    List<CommitMessage> justTen = commitsList(branch, s -> s.limit(10).map(Commit::getCommitMeta), false);
    assertEquals(messages.subList(0, 10), justTen);
    int pageSize = 10;
    // Test parameter sanity check. Want the last page to be smaller than the page-size.
    assertNotEquals(0, commits % (pageSize - 1));
    Hash lastHash = null;
    for (int offset = 0; ; ) {
        List<Commit<CommitMessage, BaseContent>> logPage = commitsList(lastHash == null ? branch : lastHash, s -> s.limit(pageSize), false);
        assertEquals(messages.subList(offset, Math.min(offset + pageSize, commits)), logPage.stream().map(Commit::getCommitMeta).collect(Collectors.toList()));
        lastHash = logPage.get(logPage.size() - 1).getHash();
        offset += pageSize - 1;
        if (offset >= commits) {
            // The "next after last page" should always return just a single commit, that's basically
            // the "end of commit-log"-condition.
            logPage = commitsList(lastHash, s -> s.limit(pageSize), false);
            assertEquals(Collections.singletonList(messages.get(commits - 1)), logPage.stream().map(Commit::getCommitMeta).collect(Collectors.toList()));
            break;
        }
    }
}
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) ArrayList(java.util.ArrayList) BranchName(org.projectnessie.versioned.BranchName) Hash(org.projectnessie.versioned.Hash) Test(org.junit.jupiter.api.Test)

Aggregations

BaseContent (org.projectnessie.versioned.testworker.BaseContent)9 BranchName (org.projectnessie.versioned.BranchName)7 Hash (org.projectnessie.versioned.Hash)7 Key (org.projectnessie.versioned.Key)7 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)5 CommitMessage (org.projectnessie.versioned.testworker.CommitMessage)5 Optional (java.util.Optional)4 Stream (java.util.stream.Stream)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Collections (java.util.Collections)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Commit (org.projectnessie.versioned.Commit)3 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)3 VersionStore (org.projectnessie.versioned.VersionStore)3 ImmutableList (com.google.common.collect.ImmutableList)2