Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 improve text hash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed May 8, 2024
1 parent d64b8ab commit ed6ce96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/json-crdt-extensions/peritext/overlay/Overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,12 @@ export class Overlay<T = string> implements Printable, Stateful {
hash = this.refreshSlices(hash, txt.savedSlices);
hash = this.refreshSlices(hash, txt.extraSlices);
hash = this.refreshSlices(hash, txt.localSlices);
if (!slicesOnly) hash = this.computeSplitTextHashes(hash);

// TODO: Move test hash calculation out of the overlay.
if (!slicesOnly) {
// hash = updateRga(hash, txt.str);
hash = this.refreshTextSlices(hash);
}
return (this.hash = hash);
}

Expand Down Expand Up @@ -461,7 +466,7 @@ export class Overlay<T = string> implements Printable, Stateful {

public leadingTextHash: number = 0;

protected computeSplitTextHashes(stateTotal: number): number {
protected refreshTextSlices(stateTotal: number): number {
const txt = this.txt;
const str = txt.str;
const firstChunk = str.first();
Expand All @@ -472,7 +477,6 @@ export class Overlay<T = string> implements Printable, Stateful {
let state: number = CONST.START_STATE;
for (let pair = i(); pair; pair = i()) {
const [p1, p2] = pair;
// TODO: need to incorporate slice attribute hash here?
const id1 = p1.id;
state = (state << 5) + state + (id1.sid >>> 0) + id1.time;
let overlayPointHash = CONST.START_STATE;
Expand All @@ -482,10 +486,8 @@ export class Overlay<T = string> implements Printable, Stateful {
(overlayPointHash << 5) + overlayPointHash + ((((id.sid >>> 0) + id.time) << 8) + (off << 4) + len);
});
state = updateNum(state, overlayPointHash);
if (p1) {
p1.hash = overlayPointHash;
stateTotal = updateNum(stateTotal, overlayPointHash);
}
p1.hash = overlayPointHash;
stateTotal = updateNum(stateTotal, overlayPointHash);
if (p2 instanceof MarkerOverlayPoint) {
if (marker) {
marker.textHash = state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {SliceBehavior} from '../../slice/constants';

const setup = () => {
const sid = 123456789;
const model = Model.withLogicalClock(sid);
const model = Model.create(undefined, sid);
model.api.root({
text: '',
slices: [],
Expand Down Expand Up @@ -307,18 +307,35 @@ describe('Overlay.refresh()', () => {
describe('updates hash', () => {
testRefresh('when the first character is deleted and reinserted', (kit, refresh) => {
const index = 0;
const char = kit.peritext.strApi().view()[index];
const str = kit.peritext.strApi();
const char = str.view()[index];
const view = str.view();
refresh();
kit.peritext.strApi().del(index, 1);
kit.peritext.strApi().ins(index, char);
expect(str.view()).toEqual(view);
});

testRefresh('when the last character is deleted and reinserted', (kit, refresh) => {
const index = kit.peritext.strApi().view().length - 1;
const char = kit.peritext.strApi().view()[index];
const str = kit.peritext.strApi();
const char = str.view()[index];
const view = str.view();
refresh();
kit.peritext.strApi().del(index, 1);
kit.peritext.strApi().ins(index, char);
expect(str.view()).toEqual(view);
});

testRefresh('when the third character is reinserted', (kit, refresh) => {
const index = 3;
const str = kit.peritext.strApi();
const char = str.view()[index];
const view = str.view();
refresh();
kit.peritext.strApi().del(index, 1);
kit.peritext.strApi().ins(index, char);
expect(str.view()).toEqual(view);
});
});
});
Expand Down

0 comments on commit ed6ce96

Please sign in to comment.