Skip to content

Commit 4ac1636

Browse files
authored
fix(core): keep fractional index keys below the next key (#15709)
## Description `generateFractionalIndexingKeyBetween(a, b)` can return a key that sorts after `b`. When the subkeys of `a` and `b` share a prefix, the key is built as `generateKeyBetween(a, b) + '0' + postfix()`. If `b` itself starts with `generateKeyBetween(a, b) + '0'`, the order against `b` is decided by the random postfix. For example, with `a = Zz0KDAn9nhfbtFAOnyTqT45wXkV4SGW7XBh` and `b = Zz10c5IY1TbnCK75NE22mfQEp2rpu6q1n6Z7`, `generateKeyBetween(a, b)` is `Zz1` and the returned key was `Zz10UESACG9CHEXJNxgEDqhyCILR7N91EmcV`, which is greater than `b`. This is easy to reach from the UI helpers: `FolderNode.indexAt('after', firstId)`, the favorites list, pinned collections, workspace properties and table rows/columns all call it with their neighbours and sort with a plain string compare. Starting from an empty list, adding one item before the first and then repeatedly inserting right after the first item breaks the order in 1000 out of 1000 runs within 20 inserts; the inserted item ends up after its intended next neighbour. The fix keeps stepping towards `b` with `generateKeyBetween(key, b)` while `b` still starts with `key + '0'`, so the appended postfix can no longer push the key past `b`. Each step lengthens the shared prefix with `b`, so the loop ends. Both copies of the helper (`@toeverything/infra` and `@blocksuite/affine-shared`) get the same change. Proof: the added tests in `fractional-indexing.spec.ts` and `fractional-indexing.unit.spec.ts` insert right after the first key 50 times, 100 rounds each. Before the fix both fail (`key < b` is false); after, both pass across 5 reruns. I also fuzzed 1500 sequences of 200 inserts with mixed insertion strategies: 677 ordering violations before, 0 after, no throws.
1 parent 2e0f115 commit 4ac1636

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect, it } from 'vitest';
2+
3+
import { generateFractionalIndexingKeyBetween } from '../../utils/fractional-indexing.js';
4+
5+
it('keeps keys ordered when inserting right after the first key', () => {
6+
for (let i = 0; i < 100; i++) {
7+
const keys = [generateFractionalIndexingKeyBetween(null, null)];
8+
keys.unshift(generateFractionalIndexingKeyBetween(null, keys[0]));
9+
for (let j = 0; j < 50; j++) {
10+
const [a, b] = keys;
11+
const key = generateFractionalIndexingKeyBetween(a, b);
12+
expect(a < key && key < b, `${a} ${key} ${b}`).toBe(true);
13+
keys.splice(1, 0, key);
14+
}
15+
}
16+
});

‎blocksuite/affine/shared/src/utils/fractional-indexing.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export function generateFractionalIndexingKeyBetween(
6565
// generate a key between a and b
6666
if (hasSamePrefix(aSubkey, bSubkey) && a !== null && b !== null) {
6767
// conflict, if the subkeys are the same, generate a key between fullkeys
68-
return generateKeyBetween(a, b) + '0' + postfix();
68+
let key = generateKeyBetween(a, b);
69+
// the postfix would sort the key after b if b continues with key + '0'
70+
while (b.startsWith(key + '0')) {
71+
key = generateKeyBetween(key, b);
72+
}
73+
return key + '0' + postfix();
6974
} else {
7075
return generateKeyBetween(aSubkey, bSubkey) + '0' + postfix();
7176
}

‎packages/common/infra/src/utils/__tests__/fractional-indexing.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ test('edge cases', () => {
7272
expect(d < b).toBeTruthy();
7373
}
7474
});
75+
76+
test('repeatedly inserting right after the first key', () => {
77+
for (let i = 0; i < 100; i++) {
78+
const keys = [generateFractionalIndexingKeyBetween(null, null)];
79+
keys.unshift(gen(null, keys[0]));
80+
for (let j = 0; j < 50; j++) {
81+
keys.splice(1, 0, gen(keys[0], keys[1]));
82+
}
83+
}
84+
});

‎packages/common/infra/src/utils/fractional-indexing.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export function generateFractionalIndexingKeyBetween(
6565
// generate a key between a and b
6666
if (hasSamePrefix(aSubkey, bSubkey) && a !== null && b !== null) {
6767
// conflict, if the subkeys are the same, generate a key between fullkeys
68-
return generateKeyBetween(a, b) + '0' + postfix();
68+
let key = generateKeyBetween(a, b);
69+
// the postfix would sort the key after b if b continues with key + '0'
70+
while (b.startsWith(key + '0')) {
71+
key = generateKeyBetween(key, b);
72+
}
73+
return key + '0' + postfix();
6974
} else {
7075
return generateKeyBetween(aSubkey, bSubkey) + '0' + postfix();
7176
}

0 commit comments

Comments
 (0)