Skip to content

Commit

Permalink
implement client diffing via cvr too
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Dec 17, 2023
1 parent e3b4171 commit 1bd827b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 190 deletions.
52 changes: 12 additions & 40 deletions server/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ export type SearchResult = {
export type ClientGroupRecord = {
id: string;
cvrVersion: number | null;
clientVersion: number;
};

export type ClientRecord = {
id: string;
clientGroupID: string;
lastMutationID: number;
clientVersion: number;
};

export type Affected = {
Expand Down Expand Up @@ -224,15 +222,15 @@ export async function putClientGroup(
executor: Executor,
clientGroup: ClientGroupRecord,
) {
const {id, cvrVersion, clientVersion} = clientGroup;
const {id, cvrVersion} = clientGroup;
await executor(
/*sql*/ `insert into replicache_client_group
(id, cvrversion, clientversion, lastmodified)
(id, cvrversion, lastmodified)
values
($1, $2, $3, now())
($1, $2, now())
on conflict (id) do update set
cvrversion = $2, clientversion = $3, lastmodified = now()`,
[id, cvrVersion, clientVersion],
cvrversion = $2, lastmodified = now()`,
[id, cvrVersion],
);
}

Expand All @@ -247,7 +245,6 @@ export async function getClientGroupForUpdate(
prevClientGroup ?? {
id: clientGroupID,
cvrVersion: null,
clientVersion: 0,
}
);
}
Expand All @@ -258,7 +255,7 @@ export async function getClientGroup(
{forUpdate}: {forUpdate?: boolean} = {},
) {
const {rows} = await executor(
/*sql*/ `select cvrversion, clientversion from replicache_client_group where id = $1 ${
/*sql*/ `select cvrversion from replicache_client_group where id = $1 ${
forUpdate ? 'for update' : ''
}`,
[clientGroupID],
Expand All @@ -268,41 +265,17 @@ export async function getClientGroup(
const res: ClientGroupRecord = {
id: clientGroupID,
cvrVersion: r.cvrversion,
clientVersion: r.clientversion,
};
return res;
}

export async function searchClients(
executor: Executor,
{
clientGroupID,
sinceClientVersion,
}: {clientGroupID: string; sinceClientVersion: number},
) {
const {rows} = await executor(
/*sql*/ `select id, lastmutationid, clientversion from replicache_client where clientGroupID = $1 and clientversion > $2`,
[clientGroupID, sinceClientVersion],
);
return rows.map(r => {
const client: ClientRecord = {
id: r.id,
clientGroupID,
lastMutationID: r.lastmutationid,
clientVersion: r.clientversion,
};
return client;
});
}

export async function getClientForUpdate(executor: Executor, clientID: string) {
const prevClient = await getClient(executor, clientID, {forUpdate: true});
return (
prevClient ?? {
id: clientID,
clientGroupID: '',
lastMutationID: 0,
clientVersion: 0,
}
);
}
Expand All @@ -313,7 +286,7 @@ export async function getClient(
{forUpdate}: {forUpdate?: boolean} = {},
) {
const {rows} = await executor(
/*sql*/ `select clientgroupid, lastmutationid, clientversion from replicache_client where id = $1 ${
/*sql*/ `select clientgroupid, lastmutationid from replicache_client where id = $1 ${
forUpdate ? 'for update' : ''
}`,
[clientID],
Expand All @@ -324,23 +297,22 @@ export async function getClient(
id: r.id,
clientGroupID: r.clientgroupid,
lastMutationID: r.lastmutationid,
clientVersion: r.lastclientversion,
};
return res;
}

export async function putClient(executor: Executor, client: ClientRecord) {
const {id, clientGroupID, lastMutationID, clientVersion} = client;
const {id, clientGroupID, lastMutationID} = client;
await executor(
/*sql*/ `
insert into replicache_client
(id, clientgroupid, lastmutationid, clientversion, lastmodified)
(id, clientgroupid, lastmutationid, lastmodified)
values
($1, $2, $3, $4, now())
($1, $2, $3, now())
on conflict (id) do update set
lastmutationid = $3, clientversion = $4, lastmodified = now()
lastmutationid = $3, lastmodified = now()
`,
[id, clientGroupID, lastMutationID, clientVersion],
[id, clientGroupID, lastMutationID],
);
}

Expand Down
52 changes: 13 additions & 39 deletions server/src/pull/cvr.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type {Comment, Description, Issue} from 'shared';
import type {Executor} from '../pg.js';

export type CVR = {
clientGroupID: string;
clientVersion: number;
order: number;
// This represents the same row as ClientRecord in data.ts, but with the same
// casing as is used in the db rather than camelCase. This is needed because
// this code in pull and cvr.ts assumes they match.
export type Client = {
id: string;
lastmutationid: number;
};

// These two consts drive the tables to sync.
Expand All @@ -14,11 +16,15 @@ export const TableOrdinal = {
issue: 1,
description: 2,
comment: 3,
// eslint-disable-next-line @typescript-eslint/naming-convention
replicache_client: 4,
} as const;
export type TableType = {
issue: Issue & {version: number};
description: Description & {version: number};
comment: Comment & {version: number};
issue: Issue;
description: Description;
comment: Comment;
// eslint-disable-next-line @typescript-eslint/naming-convention
replicache_client: Client;
};

export type SyncedTables = keyof typeof TableOrdinal;
Expand All @@ -30,38 +36,6 @@ export type Deletes = {
[P in keyof TableType]: string[];
};

export async function getCVR(
executor: Executor,
clientGroupID: string,
order: number,
): Promise<CVR | undefined> {
const result = await executor(
/*sql*/ `SELECT "client_version" FROM "client_view" WHERE "client_group_id" = $1 AND "version" = $2`,
[clientGroupID, order],
);
if (result.rowCount === 0) {
return undefined;
}
return {
clientGroupID,
order,
clientVersion: result.rows[0].client_version,
};
}

export function putCVR(executor: Executor, cvr: CVR) {
return executor(
/*sql*/ `INSERT INTO client_view (
"client_group_id",
"client_version",
"version"
) VALUES ($1, $2, $3) ON CONFLICT ("client_group_id", "version") DO UPDATE SET
client_version = excluded.client_version
`,
[cvr.clientGroupID, cvr.clientVersion, cvr.order],
);
}

export async function recordCreates<T extends keyof TableType>(
executor: Executor,
table: T,
Expand Down
Loading

0 comments on commit 1bd827b

Please sign in to comment.