use of org.projectnessie.versioned.persist.adapter.ImmutableCommitAttempt in project nessie by projectnessie.
the class AbstractGlobalStates method globalStates.
/**
* Rudimentary test for Nessie-GC related basic operations to collect all globally known keys and
* the global-state-logs.
*/
@ParameterizedTest
@MethodSource("globalStatesParams")
void globalStates(GlobalStateParam param) throws Exception {
List<BranchName> branches = IntStream.range(0, param.branches).mapToObj(i -> BranchName.of("globalStates-" + i)).collect(Collectors.toList());
Map<BranchName, Hash> heads = branches.stream().collect(Collectors.toMap(b -> b, b -> catchingFunction(() -> databaseAdapter.create(b, databaseAdapter.hashOnReference(BranchName.of("main"), Optional.empty())))));
Map<ContentId, ByteString> currentStates = new HashMap<>();
Set<Key> keys = IntStream.range(0, param.tables).mapToObj(i -> Key.of("table", Integer.toString(i))).collect(Collectors.toSet());
Set<ContentId> usedContentIds = new HashSet<>();
Map<ContentId, ByteString> expectedGlobalStates = new HashMap<>();
for (int commit = 0; commit < param.commitsPerBranch; commit++) {
for (BranchName branch : branches) {
ImmutableCommitAttempt.Builder commitAttempt = ImmutableCommitAttempt.builder().commitToBranch(branch).expectedHead(Optional.of(heads.get(branch))).commitMetaSerialized(ByteString.copyFromUtf8("some commit#" + commit + " branch " + branch.getName()));
for (Key key : keys) {
if (param.tableCommitProbability == 1.0f || ThreadLocalRandom.current().nextDouble(0d, 1d) <= param.tableCommitProbability) {
String state = "state-commit-" + commit + "+" + key;
String value = "value-commit-" + commit + "+" + key;
ContentId contentId = ContentId.of(key.toString() + "-" + branch.getName());
ByteString put = ByteString.copyFromUtf8(value);
ByteString global = ByteString.copyFromUtf8(state);
commitAttempt.putExpectedStates(contentId, Optional.ofNullable(currentStates.get(contentId))).putGlobal(contentId, global).addPuts(KeyWithBytes.of(key, contentId, (byte) 0, put));
expectedGlobalStates.put(contentId, global);
usedContentIds.add(contentId);
currentStates.put(contentId, global);
}
}
ImmutableCommitAttempt attempt = commitAttempt.build();
if (!attempt.getPuts().isEmpty()) {
heads.put(branch, databaseAdapter.commit(attempt));
}
}
}
// verify that all global-state keys (== Key + content-id) are returned (in any order)
try (Stream<ContentId> globalKeys = databaseAdapter.globalKeys()) {
assertThat(globalKeys).containsExactlyInAnyOrderElementsOf(expectedGlobalStates.keySet());
}
try (Stream<ContentIdAndBytes> allStates = databaseAdapter.globalContent(expectedGlobalStates.keySet())) {
List<ContentIdAndBytes> all = allStates.collect(Collectors.toList());
// verify that the global-state-log returns all keys (in any order)
assertThat(all.stream().map(ContentIdAndBytes::getContentId).distinct()).containsExactlyInAnyOrderElementsOf(usedContentIds);
// verify that the global-state-log returns all content-ids (in any order)
assertThat(all.stream().map(ContentIdAndBytes::getContentId).distinct()).containsExactlyInAnyOrderElementsOf(currentStates.keySet());
Collection<ByteString> allExpected = expectedGlobalStates.values();
// verify that the global-state-log returns all state-values
assertThat(all.stream().map(ContentIdAndBytes::getValue)).containsExactlyInAnyOrderElementsOf(allExpected);
}
}
Aggregations