Skip to content

Commit

Permalink
chore(replicache: Remove MetaType.SnapshotSDD (#3508)
Browse files Browse the repository at this point in the history
  • Loading branch information
arv authored Jan 10, 2025
1 parent 8076cbe commit 9167419
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 1,156 deletions.
74 changes: 0 additions & 74 deletions packages/replicache/src/db/commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
commitChain,
newLocalDD31 as commitNewLocalDD31,
newSnapshotDD31 as commitNewSnapshotDD31,
newSnapshotSDD as commitNewSnapshotSDD,
fromChunk,
getMutationID,
localMutations,
Expand Down Expand Up @@ -368,27 +367,6 @@ test('load roundtrip', () => {

const cookie = deepFreeze({foo: 'bar', order: 1});
for (const basisHash of [null, fakeHash('000'), fakeHash('face3')]) {
t(
makeCommit(
{
type: MetaType.SnapshotSDD,
basisHash,
lastMutationID: 0,
cookieJSON: cookie,
},
fakeHash('face6'),
[fakeHash('face6')],
),
commitNewSnapshotSDD(
createChunk,
basisHash,
0,
cookie,
fakeHash('face6'),
[],
),
);

t(
makeCommit(
{
Expand All @@ -411,21 +389,6 @@ test('load roundtrip', () => {
);
}

t(
makeCommit(
// @ts-expect-error We are testing invalid types
{
type: MetaType.SnapshotSDD,
basisHash: emptyStringHash,
lastMutationID: 0,
// missing cookieJSON
},
fakeHash('face6'),
[fakeHash('face6'), fakeHash('000')],
),
new Error('Invalid type: undefined, expected JSON value'),
);

t(
makeCommit(
// @ts-expect-error we are testing invalid types
Expand All @@ -445,41 +408,6 @@ test('load roundtrip', () => {
test('accessors', async () => {
const clientID = 'client-id';

const fakeRead = {
// eslint-disable-next-line require-await
async mustGetChunk() {
// This test does not read from the dag and if it does, lets just fail.
throw new Error('Method not implemented.');
},
};

const snapshot = fromChunk(
makeCommit(
{
type: MetaType.SnapshotSDD,
basisHash: fakeHash('face9'),
lastMutationID: 2,
cookieJSON: 'cookie 2',
},
fakeHash('face10'),
[fakeHash('face10'), fakeHash('face9')],
),
);
const sm = snapshot.meta;
if (sm.type === MetaType.SnapshotSDD) {
expect(sm.lastMutationID).to.equal(2);
} else {
throw new Error('unexpected type');
}
expect(sm.cookieJSON).to.deep.equal('cookie 2');
expect(sm.basisHash).to.equal(fakeHash('face9'));
expect(snapshot.valueHash).to.equal(fakeHash('face10'));
expect(await snapshot.getNextMutationID(clientID, fakeRead)).to.equal(3);
});

test('accessors DD31', async () => {
const clientID = 'client-id';

const originalHash = fakeHash('face7');
const basisHash = fakeHash('face8');
const baseSnapshotHash = fakeHash('face9');
Expand Down Expand Up @@ -541,8 +469,6 @@ test('accessors DD31', async () => {
const sm = snapshot.meta;
if (sm.type === MetaType.SnapshotDD31) {
expect(sm.lastMutationIDs[clientID]).to.equal(2);
} else if (sm.type === MetaType.SnapshotSDD) {
expect(sm.lastMutationID).to.equal(2);
} else {
throw new Error('unexpected type');
}
Expand Down
110 changes: 12 additions & 98 deletions packages/replicache/src/db/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,12 @@ export function commitIsLocal(
return commitIsLocalDD31(commit);
}

function commitIsSnapshotDD31(
export function commitIsSnapshot(
commit: Commit<Meta>,
): commit is Commit<SnapshotMetaDD31> {
return isSnapshotMetaDD31(commit.meta);
}

function commitIsSnapshotSDD(
commit: Commit<Meta>,
): commit is Commit<SnapshotMetaSDD> {
return isSnapshotMetaSDD(commit.meta);
}

export function commitIsSnapshot(
commit: Commit<Meta>,
): commit is Commit<SnapshotMetaDD31 | SnapshotMetaSDD> {
return commitIsSnapshotDD31(commit) || commitIsSnapshotSDD(commit);
}

export class Commit<M extends Meta> {
readonly chunk: Chunk<CommitData<M>>;

Expand Down Expand Up @@ -94,9 +82,6 @@ export async function getMutationID(
meta: Meta,
): Promise<number> {
switch (meta.type) {
case MetaType.SnapshotSDD:
return meta.lastMutationID;

case MetaType.SnapshotDD31:
return meta.lastMutationIDs[clientID] ?? 0;

Expand Down Expand Up @@ -173,7 +158,7 @@ export async function localMutationsGreaterThan(
export async function baseSnapshotFromHead(
name: string,
dagRead: Read,
): Promise<Commit<SnapshotMetaSDD | SnapshotMetaDD31>> {
): Promise<Commit<SnapshotMetaDD31>> {
const hash = await dagRead.getHead(name);
assert(hash, `Missing head ${name}`);
return baseSnapshotFromHash(hash, dagRead);
Expand All @@ -189,15 +174,15 @@ export async function baseSnapshotHashFromHash(
export async function baseSnapshotFromHash(
hash: Hash,
dagRead: Read,
): Promise<Commit<SnapshotMetaSDD | SnapshotMetaDD31>> {
): Promise<Commit<SnapshotMetaDD31>> {
const commit = await commitFromHash(hash, dagRead);
return baseSnapshotFromCommit(commit, dagRead);
}

export async function baseSnapshotFromCommit(
commit: Commit<Meta>,
dagRead: Read,
): Promise<Commit<SnapshotMetaSDD | SnapshotMetaDD31>> {
): Promise<Commit<SnapshotMetaDD31>> {
while (!commitIsSnapshot(commit)) {
const {meta} = commit;
if (isLocalMetaDD31(meta)) {
Expand All @@ -214,15 +199,12 @@ export async function baseSnapshotFromCommit(
}

export function snapshotMetaParts(
c: Commit<SnapshotMetaSDD | SnapshotMetaDD31>,
c: Commit<SnapshotMetaDD31>,
clientID: ClientID,
): [lastMutationID: number, cookie: FrozenCookie | FrozenJSONValue] {
const m = c.meta;
if (isSnapshotMetaDD31(m)) {
const lmid = m.lastMutationIDs[clientID] ?? 0;
return [lmid, m.cookieJSON];
}
return [m.lastMutationID, m.cookieJSON];
const lmid = m.lastMutationIDs[clientID] ?? 0;
return [lmid, m.cookieJSON];
}

export function compareCookiesForSnapshots(
Expand Down Expand Up @@ -313,41 +295,23 @@ export function assertLocalCommitDD31(
assertLocalMetaDD31(c.meta);
}

export type SnapshotMetaSDD = {
readonly type: MetaType.SnapshotSDD;
readonly basisHash: Hash | null;
readonly lastMutationID: number;
readonly cookieJSON: FrozenJSONValue;
};

export type SnapshotMetaDD31 = {
readonly type: MetaType.SnapshotDD31;
readonly basisHash: Hash | null;
readonly lastMutationIDs: Record<ClientID, number>;
readonly cookieJSON: FrozenCookie;
};

export type SnapshotMeta = SnapshotMetaSDD | SnapshotMetaDD31;
export type SnapshotMeta = SnapshotMetaDD31;

function assertSnapshotMetaBase(v: Record<string, unknown>) {
export function assertSnapshotMetaDD31(
v: Record<string, unknown>,
): asserts v is SnapshotMetaDD31 {
// type already asserted
if (v.basisHash !== null) {
assertHash(v.basisHash);
}
assertJSONValue(v.cookieJSON);
}

export function assertSnapshotMetaSDD(
v: Record<string, unknown>,
): asserts v is SnapshotMetaSDD {
assertSnapshotMetaBase(v);
assertNumber(v.lastMutationID);
}

export function assertSnapshotMetaDD31(
v: Record<string, unknown>,
): asserts v is SnapshotMetaDD31 {
assertSnapshotMetaBase(v);
assertLastMutationIDs(v.lastMutationIDs);
}

Expand All @@ -360,13 +324,7 @@ function assertLastMutationIDs(
}
}

export function assertSnapshotCommitSDD(
c: Commit<Meta>,
): asserts c is Commit<SnapshotMetaSDD> {
assertSnapshotMetaSDD(c.meta);
}

export type Meta = LocalMetaDD31 | SnapshotMetaSDD | SnapshotMetaDD31;
export type Meta = LocalMetaDD31 | SnapshotMetaDD31;

export function assertSnapshotCommitDD31(
c: Commit<Meta>,
Expand All @@ -378,10 +336,6 @@ function isSnapshotMetaDD31(meta: Meta): meta is SnapshotMetaDD31 {
return meta.type === MetaType.SnapshotDD31;
}

function isSnapshotMetaSDD(meta: Meta): meta is SnapshotMetaSDD {
return meta.type === MetaType.SnapshotSDD;
}

function assertMeta(v: unknown): asserts v is Meta {
assertObject(v);
assertDeepFrozen(v);
Expand All @@ -394,9 +348,6 @@ function assertMeta(v: unknown): asserts v is Meta {
case MetaType.LocalDD31:
assertLocalMetaDD31(v);
break;
case MetaType.SnapshotSDD:
assertSnapshotMetaSDD(v);
break;
case MetaType.SnapshotDD31:
assertSnapshotMetaDD31(v);
break;
Expand Down Expand Up @@ -504,26 +455,6 @@ export function newLocalDD31(
);
}

export function newSnapshotSDD(
createChunk: CreateChunk,
basisHash: Hash | null,
lastMutationID: number,
cookieJSON: FrozenJSONValue,
valueHash: Hash,
indexes: readonly IndexRecord[],
): Commit<SnapshotMetaSDD> {
return commitFromCommitData(
createChunk,
newSnapshotCommitDataSDD(
basisHash,
lastMutationID,
cookieJSON,
valueHash,
indexes,
),
);
}

export function newSnapshotDD31(
createChunk: CreateChunk,
basisHash: Hash | null,
Expand All @@ -544,22 +475,6 @@ export function newSnapshotDD31(
);
}

export function newSnapshotCommitDataSDD(
basisHash: Hash | null,
lastMutationID: number,
cookieJSON: FrozenJSONValue,
valueHash: Hash,
indexes: readonly IndexRecord[],
): CommitData<SnapshotMetaSDD> {
const meta: SnapshotMetaSDD = {
type: MetaType.SnapshotSDD,
basisHash,
lastMutationID,
cookieJSON,
};
return makeCommitData(meta, valueHash, indexes);
}

export function newSnapshotCommitDataDD31(
basisHash: Hash | null,
lastMutationIDs: Record<ClientID, number>,
Expand Down Expand Up @@ -597,7 +512,6 @@ export function getRefs(data: CommitData<Meta>): Refs {
meta.basisHash && refs.add(meta.basisHash);
// Local has weak originalHash
break;
case MetaType.SnapshotSDD:
case MetaType.SnapshotDD31:
// Snapshot has weak basisHash
break;
Expand Down
8 changes: 5 additions & 3 deletions packages/replicache/src/db/meta-type-enum.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable @typescript-eslint/naming-convention */

export const SnapshotSDD = 3;
// These three were used before...
// IndexChangeSDD = 1;
// LocalSDD = 2;
// SnapshotSDD = 3;
export const LocalDD31 = 4;
export const SnapshotDD31 = 5;

export type SnapshotSDD = typeof SnapshotSDD;
export type LocalDD31 = typeof LocalDD31;
export type SnapshotDD31 = typeof SnapshotDD31;

export type Type = SnapshotSDD | LocalDD31 | SnapshotDD31;
export type Type = LocalDD31 | SnapshotDD31;
5 changes: 2 additions & 3 deletions packages/replicache/src/db/rebase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
type LocalMetaDD31,
type Meta,
type SnapshotMetaDD31,
type SnapshotMetaSDD,
assertLocalCommitDD31,
commitFromHead,
commitIsLocal,
Expand Down Expand Up @@ -449,7 +448,7 @@ async function testThrowsErrorOnClientIDMismatch(
await b.addLocal(clientID);
const localCommit = b.chain[b.chain.length - 1] as Commit<LocalMetaDD31>;
const syncChain = await b.addSyncSnapshot(1, clientID);
const syncSnapshotCommit = syncChain[0] as Commit<SnapshotMetaSDD>;
const syncSnapshotCommit = syncChain[0] as Commit<SnapshotMetaDD31>;

let testMutatorCallCount = 0;
const testMutator = async (tx: WriteTransaction, args?: unknown) => {
Expand Down Expand Up @@ -508,7 +507,7 @@ async function testThrowsErrorOnMutationIDMismatch(
await b.addLocal(clientID);
const localCommit2 = b.chain[b.chain.length - 1] as Commit<LocalMetaDD31>;
const syncChain = await b.addSyncSnapshot(1, clientID);
const syncSnapshotCommit = syncChain[0] as Commit<SnapshotMetaSDD>;
const syncSnapshotCommit = syncChain[0] as Commit<SnapshotMetaDD31>;

let testMutator1CallCount = 0;
const testMutator1 = async (tx: WriteTransaction, args?: unknown) => {
Expand Down
Loading

0 comments on commit 9167419

Please sign in to comment.