Skip to content

Commit bfb246b

Browse files
authored
chore(zero-cache): review feedback on rocicorp#2337 (rocicorp#2345)
1 parent cb7dcd4 commit bfb246b

File tree

10 files changed

+33
-24
lines changed

10 files changed

+33
-24
lines changed

packages/replicache/src/cookies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {assertJSONObject, ReadonlyJSONValue} from 'shared/src/json.js';
22
import type {FrozenJSONValue} from './frozen-json.js';
3-
import {stringCompare} from './string-compare.js';
3+
import {stringCompare} from 'shared/src/string-compare.js';
44

55
/**
66
* A cookie is a value that is used to determine the order of snapshots. It

packages/replicache/src/dag/test-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
parse as parseHash,
77
} from '../hash.js';
88
import {TestMemStore} from '../kv/test-mem-store.js';
9-
import {stringCompare} from '../string-compare.js';
9+
import {stringCompare} from 'shared/src/string-compare.js';
1010
import {Chunk, ChunkHasher, Refs, toRefs as chunkToRefs} from './chunk.js';
1111
import {KeyType, chunkMetaKey, parse as parseKey} from './key.js';
1212
import {StoreImpl} from './store-impl.js';

packages/replicache/src/db/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {BTreeWrite} from '../btree/write.js';
66
import {TestStore} from '../dag/test-store.js';
77
import {FormatVersion} from '../format-version.js';
88
import {deepFreeze} from '../frozen-json.js';
9-
import {stringCompare} from '../string-compare.js';
9+
import {stringCompare} from 'shared/src/string-compare.js';
1010
import {withWrite} from '../with-transactions.js';
1111
import {
1212
IndexKey,

packages/replicache/src/kv/test-mem-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {RWLock} from '@rocicorp/lock';
22
import {promiseVoid} from 'shared/src/resolved-promises.js';
33
import type {FrozenJSONValue} from '../frozen-json.js';
4-
import {stringCompare} from '../string-compare.js';
4+
import {stringCompare} from 'shared/src/string-compare.js';
55
import {ReadImpl} from './read-impl.js';
66
import type {Read, Store, Write} from './store.js';
77
import {WriteImpl} from './write-impl.js';

packages/replicache/src/merge-async-iterables.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {expect} from 'chai';
22
import {asyncIterableToArray} from './async-iterable-to-array.js';
33
import {mergeAsyncIterables} from './merge-async-iterables.js';
4-
import {stringCompare} from './string-compare.js';
4+
import {stringCompare} from 'shared/src/string-compare.js';
55

66
export async function* makeAsyncIterable<V>(
77
values: Iterable<V>,

packages/replicache/src/replicache-mutation-recovery-dd31.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from './persist/clients.js';
2727
import type {PullResponseV0, PullResponseV1} from './puller.js';
2828
import type {PushResponse} from './pusher.js';
29-
import {stringCompare} from './string-compare.js';
29+
import {stringCompare} from 'shared/src/string-compare.js';
3030
import type {ClientID} from './sync/ids.js';
3131
import {
3232
PULL_VERSION_DD31,

packages/replicache/src/sync/pull.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import type {
4646
PullerResultV0,
4747
PullerResultV1,
4848
} from '../puller.js';
49-
import {stringCompare} from '../string-compare.js';
49+
import {stringCompare} from 'shared/src/string-compare.js';
5050
import {testSubscriptionsManagerOptions} from '../test-util.js';
5151
import {
5252
withRead,

packages/zero-cache/src/services/view-syncer/cvr-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,11 @@ export class CVRStore {
498498
row => {
499499
const existing = existingRowRecords.get(row.id);
500500
return (
501+
(existing !== undefined || row.refCounts !== null) &&
501502
!deepEqual(
502503
row as ReadonlyJSONValue,
503504
existing as ReadonlyJSONValue | undefined,
504-
) &&
505-
(existing !== undefined || row.refCounts !== null)
505+
)
506506
);
507507
},
508508
);

packages/zero-cache/src/services/view-syncer/schema/cvr.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import type {LogContext} from '@rocicorp/logger';
22
import type postgres from 'postgres';
3-
import type {JSONValue, JSONObject} from '../../../types/bigint-json.js';
3+
import {
4+
type JSONValue,
5+
type JSONObject,
6+
stringify,
7+
} from '../../../types/bigint-json.js';
48
import {
59
RowID,
610
versionFromString,
711
versionString,
812
type RowRecord,
913
} from './types.js';
14+
import {normalizedKeyOrder, RowKey} from 'zero-cache/src/types/row-key.js';
15+
import {stringCompare} from 'shared/src/string-compare.js';
1016

1117
export const PG_SCHEMA = 'cvr';
1218

@@ -27,7 +33,7 @@ CREATE TABLE cvr.instances (
2733
`;
2834

2935
export function compareInstancesRows(a: InstancesRow, b: InstancesRow) {
30-
return a.clientGroupID.localeCompare(b.clientGroupID);
36+
return stringCompare(a.clientGroupID, b.clientGroupID);
3137
}
3238

3339
export type ClientsRow = {
@@ -56,11 +62,11 @@ CREATE INDEX client_patch_version ON cvr.clients ("patchVersion");
5662
`;
5763

5864
export function compareClientsRows(a: ClientsRow, b: ClientsRow) {
59-
const clientGroupIDComp = a.clientGroupID.localeCompare(b.clientGroupID);
65+
const clientGroupIDComp = stringCompare(a.clientGroupID, b.clientGroupID);
6066
if (clientGroupIDComp !== 0) {
6167
return clientGroupIDComp;
6268
}
63-
return a.clientID.localeCompare(b.clientID);
69+
return stringCompare(a.clientID, b.clientID);
6470
}
6571

6672
export type QueriesRow = {
@@ -97,11 +103,11 @@ CREATE INDEX queries_patch_version ON cvr.queries ("patchVersion" NULLS FIRST);
97103
`;
98104

99105
export function compareQueriesRows(a: QueriesRow, b: QueriesRow) {
100-
const clientGroupIDComp = a.clientGroupID.localeCompare(b.clientGroupID);
106+
const clientGroupIDComp = stringCompare(a.clientGroupID, b.clientGroupID);
101107
if (clientGroupIDComp !== 0) {
102108
return clientGroupIDComp;
103109
}
104-
return a.queryHash.localeCompare(b.queryHash);
110+
return stringCompare(a.queryHash, b.queryHash);
105111
}
106112

107113
export type DesiresRow = {
@@ -137,15 +143,15 @@ CREATE INDEX desires_patch_version ON cvr.desires ("patchVersion");
137143
`;
138144

139145
export function compareDesiresRows(a: DesiresRow, b: DesiresRow) {
140-
const clientGroupIDComp = a.clientGroupID.localeCompare(b.clientGroupID);
146+
const clientGroupIDComp = stringCompare(a.clientGroupID, b.clientGroupID);
141147
if (clientGroupIDComp !== 0) {
142148
return clientGroupIDComp;
143149
}
144-
const clientIDComp = a.clientID.localeCompare(b.clientID);
150+
const clientIDComp = stringCompare(a.clientID, b.clientID);
145151
if (clientIDComp !== 0) {
146152
return clientIDComp;
147153
}
148-
return a.queryHash.localeCompare(b.queryHash);
154+
return stringCompare(a.queryHash, b.queryHash);
149155
}
150156

151157
export type RowsRow = {
@@ -191,19 +197,22 @@ export function rowRecordToRowsRow(
191197
}
192198

193199
export function compareRowsRows(a: RowsRow, b: RowsRow) {
194-
const clientGroupIDComp = a.clientGroupID.localeCompare(b.clientGroupID);
200+
const clientGroupIDComp = stringCompare(a.clientGroupID, b.clientGroupID);
195201
if (clientGroupIDComp !== 0) {
196202
return clientGroupIDComp;
197203
}
198-
const schemaComp = a.schema.localeCompare(b.schema);
204+
const schemaComp = stringCompare(a.schema, b.schema);
199205
if (schemaComp !== 0) {
200206
return schemaComp;
201207
}
202-
const tableComp = b.table.localeCompare(b.table);
208+
const tableComp = stringCompare(b.table, b.table);
203209
if (tableComp !== 0) {
204210
return tableComp;
205211
}
206-
return stringifySorted(a.rowKey).localeCompare(stringifySorted(b.rowKey));
212+
return stringCompare(
213+
stringifySorted(a.rowKey as RowKey),
214+
stringifySorted(b.rowKey as RowKey),
215+
);
207216
}
208217

209218
const CREATE_CVR_ROWS_TABLE = `
@@ -247,6 +256,6 @@ export async function setupCVRTables(
247256
await db.unsafe(CREATE_CVR_TABLES);
248257
}
249258

250-
function stringifySorted(o: JSONObject) {
251-
return JSON.stringify(o, Object.keys(o).sort());
259+
function stringifySorted(r: RowKey) {
260+
return stringify(normalizedKeyOrder(r));
252261
}

0 commit comments

Comments
 (0)