Skip to content

Commit

Permalink
chore(replicache): Remove MetaType.IndexChangeSDD (#3500)
Browse files Browse the repository at this point in the history
This is one of many to remove SDD from Replicache
  • Loading branch information
arv authored Jan 10, 2025
1 parent 2c419cb commit b13fdd0
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 1,249 deletions.
81 changes: 8 additions & 73 deletions packages/replicache/src/db/commit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {describe, expect, test} from 'vitest';
import {assert} from '../../../shared/src/asserts.js';
import {Chunk, type Refs, toRefs} from '../dag/chunk.js';
import {TestStore} from '../dag/test-store.js';
import * as FormatVersion from '../format-version-enum.js';
Expand All @@ -9,12 +10,10 @@ import {
type ChunkIndexDefinition,
Commit,
type CommitData,
type IndexChangeMetaSDD,
type Meta,
baseSnapshotFromHash,
chunkIndexDefinitionEqualIgnoreName,
commitChain,
newIndexChange as commitNewIndexChange,
newLocalDD31 as commitNewLocalDD31,
newLocalSDD as commitNewLocalSDD,
newSnapshotDD31 as commitNewSnapshotDD31,
Expand Down Expand Up @@ -42,9 +41,7 @@ describe('base snapshot', () => {
});

await b.addLocal(clientID);
if (formatVersion <= FormatVersion.SDD) {
await b.addIndexChange(clientID);
}
assert(formatVersion >= FormatVersion.DD31);
await b.addLocal(clientID);
genesisHash = b.chain[0].chunk.hash;
await withRead(store, async dagRead => {
Expand Down Expand Up @@ -87,7 +84,6 @@ describe('base snapshot', () => {
};

test('DD31', () => t(FormatVersion.Latest));
test('SDD', () => t(FormatVersion.SDD));
});

describe('local mutations', () => {
Expand All @@ -102,13 +98,10 @@ describe('local mutations', () => {
});

await b.addLocal(clientID);
if (formatVersion <= FormatVersion.SDD) {
await b.addIndexChange(clientID);
}
assert(formatVersion >= FormatVersion.DD31);

await b.addLocal(clientID);
if (formatVersion <= FormatVersion.SDD) {
await b.addIndexChange(clientID);
}

const headHash = b.chain[b.chain.length - 1].chunk.hash;
const commits = await withRead(store, dagRead =>
localMutations(headHash, dagRead),
Expand All @@ -120,7 +113,6 @@ describe('local mutations', () => {
};

test('DD31', () => t(FormatVersion.Latest));
test('SDD', () => t(FormatVersion.SDD));
});
test('local mutations greater than', async () => {
const clientID1 = 'client-id-1';
Expand Down Expand Up @@ -214,11 +206,9 @@ describe('chain', () => {

await b.addSnapshot(undefined, clientID);
await b.addLocal(clientID);
if (formatVersion <= FormatVersion.SDD) {
await b.addIndexChange(clientID);
} else {
await b.addLocal(clientID);
}
assert(formatVersion >= FormatVersion.DD31);
await b.addLocal(clientID);

const headHash = b.chain[b.chain.length - 1].chunk.hash;
got = await withRead(store, dagRead => commitChain(headHash, dagRead));
expect(got).to.have.lengthOf(3);
Expand All @@ -228,7 +218,6 @@ describe('chain', () => {
};

test('dd31', () => t(FormatVersion.Latest));
test('sdd', () => t(FormatVersion.SDD));
});

test('load roundtrip', () => {
Expand Down Expand Up @@ -560,19 +549,6 @@ test('load roundtrip', () => {
),
new Error('Invalid type: undefined, expected JSON value'),
);

for (const basisHash of [fakeHash('000'), fakeHash('face3')]) {
t(
makeCommit(
makeIndexChangeMeta(basisHash, 0),
fakeHash('face2'),
basisHash === null
? [fakeHash('face2')]
: [fakeHash('face2'), basisHash],
),
commitNewIndexChange(createChunk, basisHash, 0, fakeHash('face2'), []),
);
}
});

test('accessors', async () => {
Expand Down Expand Up @@ -642,21 +618,6 @@ test('accessors', async () => {
expect(sm.basisHash).to.equal(fakeHash('face9'));
expect(snapshot.valueHash).to.equal(fakeHash('face10'));
expect(await snapshot.getNextMutationID(clientID, fakeRead)).to.equal(3);

const indexChange = fromChunk(
makeCommit(makeIndexChangeMeta(fakeHash('face11'), 3), fakeHash('face12'), [
fakeHash('face12'),
fakeHash('face11'),
]),
);
const ic = indexChange.meta;
if (ic.type === MetaType.IndexChangeSDD) {
expect(ic.lastMutationID).to.equal(3);
} else {
throw new Error('unexpected type');
}
expect(indexChange.meta.basisHash).to.equal(fakeHash('face11'));
expect(indexChange.valueHash).to.equal(fakeHash('face12'));
});

test('accessors DD31', async () => {
Expand Down Expand Up @@ -732,21 +693,6 @@ test('accessors DD31', async () => {
expect(sm.basisHash).to.equal(fakeHash('face9'));
expect(snapshot.valueHash).to.equal(fakeHash('face10'));
expect(await snapshot.getNextMutationID(clientID, fakeRead)).to.equal(3);

const indexChange = fromChunk(
makeCommit(makeIndexChangeMeta(fakeHash('face11'), 3), fakeHash('face12'), [
fakeHash('face12'),
fakeHash('face11'),
]),
);
const ic = indexChange.meta;
if (ic.type === MetaType.IndexChangeSDD) {
expect(ic.lastMutationID).to.equal(3);
} else {
throw new Error('unexpected type');
}
expect(indexChange.meta.basisHash).to.equal(fakeHash('face11'));
expect(indexChange.valueHash).to.equal(fakeHash('face12'));
});

const chunkHasher = makeNewFakeHashFunction('face55');
Expand All @@ -773,17 +719,6 @@ function makeCommit<M extends Meta>(
return createChunk(data, toRefs(refs));
}

function makeIndexChangeMeta(
basisHash: Hash,
lastMutationID: number,
): IndexChangeMetaSDD {
return {
type: MetaType.IndexChangeSDD,
basisHash,
lastMutationID,
};
}

test('getMutationID across commits with different clients', async () => {
// In DD31 the commits can be from different clients.

Expand Down
54 changes: 0 additions & 54 deletions packages/replicache/src/db/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ export async function getMutationID(
meta: Meta,
): Promise<number> {
switch (meta.type) {
case MetaType.IndexChangeSDD:
return meta.lastMutationID;

case MetaType.SnapshotSDD:
return meta.lastMutationID;

Expand Down Expand Up @@ -286,32 +283,6 @@ export async function commitFromHead(
return commitFromHash(hash, dagRead);
}

export type IndexChangeMetaSDD = {
readonly type: MetaType.IndexChangeSDD;
readonly basisHash: Hash;
readonly lastMutationID: number;
};

function assertIndexChangeMeta(
v: Record<string, unknown>,
): asserts v is IndexChangeMetaSDD {
// type already asserted
assertNumber(v.lastMutationID);

// Note: indexes are already validated for all commit types. Only additional
// things to validate are:
// - lastMutationID is equal to the basis
// - valueHash has not been changed
// However we don't have a write transaction this deep, so these validated at
// commit time.
}

export function assertIndexChangeCommit(
c: Commit<Meta>,
): asserts c is Commit<IndexChangeMetaSDD> {
assertIndexChangeMeta(c.meta);
}

export type LocalMetaSDD = {
readonly type: MetaType.LocalSDD;
readonly basisHash: Hash;
Expand Down Expand Up @@ -428,7 +399,6 @@ export function assertSnapshotCommitSDD(
}

export type Meta =
| IndexChangeMetaSDD
| LocalMetaSDD
| LocalMetaDD31
| SnapshotMetaSDD
Expand Down Expand Up @@ -457,9 +427,6 @@ function assertMeta(v: unknown): asserts v is Meta {

assertNumber(v.type);
switch (v.type) {
case MetaType.IndexChangeSDD:
assertIndexChangeMeta(v);
break;
case MetaType.LocalSDD:
assertLocalMetaSDD(v);
break;
Expand Down Expand Up @@ -674,24 +641,6 @@ export function newSnapshotCommitDataDD31(
return makeCommitData(meta, valueHash, indexes);
}

export function newIndexChange(
createChunk: CreateChunk,
basisHash: Hash,
lastMutationID: number,
valueHash: Hash,
indexes: readonly IndexRecord[],
): Commit<IndexChangeMetaSDD> {
const meta: IndexChangeMetaSDD = {
type: MetaType.IndexChangeSDD,
basisHash,
lastMutationID,
};
return commitFromCommitData(
createChunk,
makeCommitData(meta, valueHash, indexes),
);
}

export function fromChunk(chunk: Chunk): Commit<Meta> {
validateChunk(chunk);
return new Commit(chunk);
Expand All @@ -709,9 +658,6 @@ export function getRefs(data: CommitData<Meta>): Refs {
refs.add(data.valueHash);
const {meta} = data;
switch (meta.type) {
case MetaType.IndexChangeSDD:
meta.basisHash && refs.add(meta.basisHash);
break;
case MetaType.LocalSDD:
case MetaType.LocalDD31:
meta.basisHash && refs.add(meta.basisHash);
Expand Down
9 changes: 1 addition & 8 deletions packages/replicache/src/db/meta-type-enum.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
/* eslint-disable @typescript-eslint/naming-convention */

export const IndexChangeSDD = 1;
export const LocalSDD = 2;
export const SnapshotSDD = 3;
export const LocalDD31 = 4;
export const SnapshotDD31 = 5;

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

export type Type =
| IndexChangeSDD
| LocalSDD
| SnapshotSDD
| LocalDD31
| SnapshotDD31;
export type Type = LocalSDD | SnapshotSDD | LocalDD31 | SnapshotDD31;
Loading

0 comments on commit b13fdd0

Please sign in to comment.