Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree: Refactor RangeMap to support generic key types #23560

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

alex-pardes
Copy link
Contributor

Description

This PR refactors RangeMap to support arbitrary types of keys. Various uses of RangeMap nested inside of a map of revisions are flattened into a single range map keyed on ChangeAtomId. ModularChangeset.crossFieldKeys is also refactored to use a RangeMap, and some functionality has been added to RangeMap to support this use case.

@github-actions github-actions bot added area: dds Issues related to distributed data structures area: dds: tree base: main PRs targeted against main branch labels Jan 15, 2025
this.tree.clear();
}

public get(start: K, length: number): RangeQueryEntry<K, V>[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have doc comments on this to clarify the contract. Especially with respect to the first and last RangeQueryEntry returned

Comment on lines +68 to +69
result.push({ start, length: overlappingLength, value });
nextKey = this.offsetKey(lastEntryKey, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be strictly necessary for correctness of this code (I have a hard time telling), but it seems like it would be good to update remainingLength here at least to avoid confusion.

}

const [key, { length: entryLength, value }] = entry;
const lastEntryKey = this.offsetKey(key, entryLength);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: move the computation of lastEntryKey below the if block since it may not be used if we do break out of the loop

@@ -36,16 +99,16 @@ export class RangeMap<T> {
* @returns A RangeQueryResult containing the value associated with `start`,
* and the number of consecutive keys with that same value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* and the number of consecutive keys with that same value.
* and the number of consecutive keys with that same value (at least 1, at most `length`).

Comment on lines +95 to +100
const cmpRevision = compareRevisions(a.revision, b.revision);
if (cmpRevision !== 0) {
return cmpRevision * Number.POSITIVE_INFINITY;
}

return a.localId - b.localId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you've dealt with the target part, can you reuse subtractChangeAtomIds from src\core\rebase\types.ts?

/**
* Returns a new map which contains the entries from both input maps.
*/
public static mergeMaps<K, V>(a: RangeMap<K, V>, b: RangeMap<K, V>): RangeMap<K, V> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action verbs like "merge" suggest that side effects may occur. (This would be less ambiguous if we had readonly versions of the RangeMap types.)
Either way, how about naming this union instead?

getCrossFieldTargetFromMove(markEffect),
markEffect.revision,
markEffect.id,
count,
true,
Copy link
Contributor

@yann-achard-MS yann-achard-MS Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated to the PR. Was there a bug here? If so, some test coverage for it would be good. Maybe this change is best kept for another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: dds: tree area: dds Issues related to distributed data structures base: main PRs targeted against main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants