Skip to content

Commit c35389b

Browse files
committed
[hash] Move common functions together
1 parent af507da commit c35389b

4 files changed

Lines changed: 50 additions & 16 deletions

File tree

src/common/hash.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import {arrayForEach} from './array.ts';
1+
import type {
2+
CellOrUndefined,
3+
Hash,
4+
Hlc,
5+
Id,
6+
ValueOrUndefined,
7+
} from '../@types/index.d.ts';
8+
import {arrayForEach, arrayReduce} from './array.ts';
9+
import {jsonStringWithUndefined} from './json.ts';
10+
import {objEntries} from './obj.ts';
211
import {GLOBAL} from './other.ts';
312

413
const textEncoder = /* @__PURE__ */ new GLOBAL.TextEncoder();
@@ -13,3 +22,27 @@ export const getHash = (value: string): number => {
1322
});
1423
return hash >>> 0;
1524
};
25+
26+
export const addOrRemoveHash = (hash1: Hash, hash2: Hash): Hash =>
27+
hash1 ^ hash2;
28+
29+
export const getHashOfChild = (id: Id, hash: Hash): Hash =>
30+
getHash(id + ':' + hash);
31+
32+
export const getValuesHash = (children: {[id: Id]: Hash}, hlc: Hlc): Hash =>
33+
arrayReduce(
34+
objEntries(children),
35+
(hash, [childId, childHash]) =>
36+
addOrRemoveHash(hash, getHashOfChild(childId, childHash)),
37+
getHash(hlc),
38+
);
39+
40+
export const getValueHash = (
41+
cellOrValue: CellOrUndefined | ValueOrUndefined,
42+
hlc: Hlc,
43+
): Hash => getHash(jsonStringWithUndefined(cellOrValue) + ':' + hlc);
44+
45+
export const getCellHash = getValueHash;
46+
export const getRowHash = getValuesHash;
47+
export const getTableHash = getValuesHash;
48+
export const getTablesHash = getTableHash;

src/common/obj.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ export type IdObj2<Value> = IdObj<IdObj<Value>>;
77

88
export const object = Object;
99
const getPrototypeOf = (obj: any) => object.getPrototypeOf(obj);
10-
const objEntries = object.entries;
1110
const objFrozen = object.isFrozen;
1211

12+
export const objEntries = object.entries;
13+
1314
export const isObject = (obj: unknown): obj is IdObj<unknown> =>
1415
!isUndefined(obj) &&
1516
(ifNotUndefined(

src/common/stamps.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ export const stampNewWithHash = <Value>(
4141

4242
export const getStampHash = (stamp: Stamp<unknown, true>): Hash => stamp[2];
4343

44-
export const hashIdAndHash = (id: Id, hash: Hash) => getHash(id + ':' + hash);
45-
4644
export const replaceHlcHash = (oldHlc: Hlc, newHlc: Hlc) =>
4745
newHlc > oldHlc ? (oldHlc ? getHash(oldHlc) : 0) ^ getHash(newHlc) : 0;
4846

src/mergeable-store/index.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import type {
2424
} from '../@types/store/index.d.ts';
2525
import {isCellOrValueOrNullOrUndefined} from '../common/cell.ts';
2626
import {collClear, collForEach} from '../common/coll.ts';
27-
import {getHash} from '../common/hash.ts';
27+
import {addOrRemoveHash, getCellHash, getHashOfChild} from '../common/hash.ts';
2828
import {getHlcFunctions} from '../common/hlc.ts';
29-
import {jsonStringWithUndefined} from '../common/json.ts';
3029
import {
3130
mapEnsure,
3231
mapForEach,
@@ -54,7 +53,6 @@ import {
5453
ValuesStampMap,
5554
getLatestHlc,
5655
getStampHash,
57-
hashIdAndHash,
5856
replaceHlcHash,
5957
stampClone,
6058
stampMapToObjWithHash,
@@ -217,8 +215,10 @@ export const createMergeableStore = ((
217215

218216
tableHash ^= isContent
219217
? 0
220-
: (oldRowHash ? hashIdAndHash(rowId, oldRowHash) : 0) ^
221-
hashIdAndHash(rowId, rowHash);
218+
: addOrRemoveHash(
219+
oldRowHash ? getHashOfChild(rowId, oldRowHash) : 0,
220+
getHashOfChild(rowId, rowHash),
221+
);
222222
tableHlc = getLatestHlc(tableHlc, rowHlc);
223223
});
224224

@@ -229,8 +229,10 @@ export const createMergeableStore = ((
229229

230230
tablesHash ^= isContent
231231
? 0
232-
: (oldTableHash ? hashIdAndHash(tableId, oldTableHash) : 0) ^
233-
hashIdAndHash(tableId, tableStampMap[2]);
232+
: addOrRemoveHash(
233+
oldTableHash ? getHashOfChild(tableId, oldTableHash) : 0,
234+
getHashOfChild(tableId, tableStampMap[2]),
235+
);
234236
tablesHlc = getLatestHlc(tablesHlc, tableHlc);
235237
},
236238
);
@@ -283,16 +285,16 @@ export const createMergeableStore = ((
283285
stampUpdate(
284286
thingStampMap,
285287
thingHlc,
286-
isContent
287-
? incomingThingHash
288-
: getHash(jsonStringWithUndefined(thing) + ':' + thingHlc),
288+
isContent ? incomingThingHash : getCellHash(thing, thingHlc),
289289
);
290290
thingStampMap[0] = thing;
291291
thingsChanges[thingId] = thing;
292292
thingsHash ^= isContent
293293
? 0
294-
: hashIdAndHash(thingId, oldThingHash) ^
295-
hashIdAndHash(thingId, thingStampMap[2]);
294+
: addOrRemoveHash(
295+
getHashOfChild(thingId, oldThingHash),
296+
getHashOfChild(thingId, thingStampMap[2]),
297+
);
296298
thingsHlc = getLatestHlc(thingsHlc, thingHlc);
297299
}
298300
},

0 commit comments

Comments
 (0)