refactor(server): use doc model on PgWorkspaceDocStorageAdapter (#9852)

close CLOUD-104
This commit is contained in:
fengmk2
2025-02-07 12:21:58 +00:00
parent 0b9d30b55a
commit 36ed81bcc6
5 changed files with 105 additions and 425 deletions

View File

@@ -243,7 +243,7 @@ test('should be able to get last history record', async t => {
);
t.truthy(history);
t.is(history?.timestamp.getTime(), timestamp + 9);
t.is(history?.timestamp, timestamp + 9);
});
test('should be able to recover from history', async t => {

View File

@@ -41,10 +41,7 @@ test.after.always(async () => {
await m?.close();
});
/**
* @deprecated `seq` would be removed
*/
test('should have sequential update number', async t => {
test('should have timestamp update', async t => {
const doc = new YDoc();
const text = doc.getText('content');
const updates: Buffer[] = [];
@@ -59,7 +56,6 @@ test('should have sequential update number', async t => {
await adapter.pushDocUpdates('2', '2', updates);
// [1,2,3]
let records = await db.update.findMany({
where: {
workspaceId: '2',
@@ -67,27 +63,16 @@ test('should have sequential update number', async t => {
},
});
let firstTimestamp = records[0].createdAt.getTime();
t.deepEqual(
records.map(({ seq }) => seq),
[1, 2, 3]
records.map(({ createdAt }) => createdAt.getTime()),
[firstTimestamp, firstTimestamp + 1, firstTimestamp + 2]
);
// merge
await adapter.getDoc('2', '2');
// fake the seq num is about to overflow
await db.snapshot.update({
where: {
workspaceId_id: {
id: '2',
workspaceId: '2',
},
},
data: {
seq: 0x3ffffffe,
},
});
// change timestamp again
await adapter.pushDocUpdates('2', '2', updates);
records = await db.update.findMany({
@@ -97,12 +82,13 @@ test('should have sequential update number', async t => {
},
});
firstTimestamp = records[0].createdAt.getTime();
t.deepEqual(
records.map(({ seq }) => seq),
[0x3ffffffe + 1, 0x3ffffffe + 2, 0x3ffffffe + 3]
records.map(({ createdAt }) => createdAt.getTime()),
[firstTimestamp, firstTimestamp + 1, firstTimestamp + 2]
);
// push a new update with new seq num
// push a new update
await adapter.pushDocUpdates('2', '2', updates.slice(0, 1));
// let the manager ignore update with the new seq num
@@ -174,7 +160,6 @@ test('should be able to merge updates as snapshot', async t => {
id: '1',
workspaceId: '1',
blob: Buffer.from(update),
seq: 1,
createdAt: new Date(Date.now() + 1),
createdBy: null,
},
@@ -197,7 +182,6 @@ test('should be able to merge updates as snapshot', async t => {
workspaceId: '1',
id: '1',
blob: appendUpdate,
seq: 2,
createdAt: new Date(),
createdBy: null,
},

View File

@@ -52,14 +52,12 @@ test('should create a batch updates on a doc', async t => {
blob: Buffer.from('blob1'),
timestamp: Date.now(),
editorId: user.id,
seq: 1,
},
{
spaceId: workspace.id,
docId,
blob: Buffer.from('blob2'),
timestamp: Date.now() + 1000,
seq: 2,
},
]);
t.is(updates.count, 2);
@@ -75,7 +73,6 @@ test('should create error when createdAt timestamp is not unique', async t => {
blob: Buffer.from('blob1'),
timestamp,
editorId: user.id,
seq: 1,
},
]);
await t.throwsAsync(
@@ -86,7 +83,6 @@ test('should create error when createdAt timestamp is not unique', async t => {
blob: Buffer.from('blob2'),
timestamp,
editorId: user.id,
seq: 2,
},
]),
{
@@ -105,7 +101,6 @@ test('should find updates by spaceId and docId', async t => {
blob: Buffer.from('blob1'),
timestamp: Date.now(),
editorId: user.id,
seq: 1,
},
{
spaceId: workspace.id,
@@ -113,7 +108,6 @@ test('should find updates by spaceId and docId', async t => {
blob: Buffer.from('blob2'),
timestamp: Date.now() + 1000,
editorId: user.id,
seq: 2,
},
]);
const foundUpdates = await t.context.doc.findUpdates(workspace.id, docId);
@@ -130,7 +124,6 @@ test('should find updates by spaceId and docId', async t => {
blob: Buffer.from('blob3'),
timestamp: Date.now(),
editorId: user.id,
seq: 3,
},
]);
count = await t.context.doc.getUpdateCount(workspace.id, docId);
@@ -147,14 +140,12 @@ test('should delete updates by spaceId, docId, and createdAts', async t => {
blob: Buffer.from('blob1'),
timestamp: timestamps[0],
editorId: user.id,
seq: 1,
},
{
spaceId: workspace.id,
docId,
blob: Buffer.from('blob2'),
timestamp: timestamps[1],
seq: 2,
},
]);
let count = await t.context.doc.deleteUpdates(
@@ -181,7 +172,6 @@ test('should get global update count', async t => {
blob: Buffer.from('blob1'),
timestamp: Date.now(),
editorId: user.id,
seq: 1,
},
{
spaceId: workspace.id,
@@ -189,7 +179,6 @@ test('should get global update count', async t => {
blob: Buffer.from('blob2'),
timestamp: Date.now() + 1000,
editorId: user.id,
seq: 2,
},
{
spaceId: workspace.id,
@@ -197,7 +186,6 @@ test('should get global update count', async t => {
blob: Buffer.from('blob2'),
timestamp: Date.now() + 1000,
editorId: user.id,
seq: 2,
},
]);
const count = await t.context.doc.getGlobalUpdateCount();
@@ -449,7 +437,6 @@ test('should delete a doc, including histories, snapshots and updates', async t
blob: Buffer.from('blob2'),
timestamp: Date.now(),
editorId: user.id,
seq: 1,
},
]);
await t.context.doc.delete(workspace.id, docId);
@@ -490,7 +477,6 @@ test('should delete all docs in a workspace', async t => {
blob: Buffer.from('blob2'),
timestamp: Date.now(),
editorId: user.id,
seq: 1,
},
]);
await t.context.doc.upsert(snapshot2);
@@ -502,7 +488,6 @@ test('should delete all docs in a workspace', async t => {
blob: Buffer.from('blob2'),
timestamp: Date.now(),
editorId: user.id,
seq: 1,
},
]);
const deletedCount = await t.context.doc.deleteAllByWorkspaceId(workspace.id);
@@ -555,7 +540,6 @@ test('should find all docs timestamps in a workspace', async t => {
blob: Buffer.from('blob2'),
timestamp: timestamp3,
editorId: user.id,
seq: 1,
},
]);
await t.context.doc.upsert(snapshot2);
@@ -568,22 +552,30 @@ test('should find all docs timestamps in a workspace', async t => {
});
});
test('should increase doc seq', async t => {
test('should detect doc exists or not', async t => {
const docId = randomUUID();
t.false(await t.context.doc.exists(workspace.id, docId));
const snapshot = {
spaceId: workspace.id,
docId,
docId: docId,
blob: Buffer.from('blob1'),
timestamp: Date.now(),
editorId: user.id,
};
await t.context.doc.upsert(snapshot);
const seq1 = await t.context.doc.increaseSeq(workspace.id, docId, 88);
t.is(seq1, 88);
const seq2 = await t.context.doc.increaseSeq(workspace.id, docId, 2);
t.is(seq2, 90);
// hit max seq, then reset to zero
await t.context.doc.increaseSeq(workspace.id, docId, 0x3fffffff);
const seq3 = await t.context.doc.increaseSeq(workspace.id, docId, 1);
t.is(seq3, 1);
t.true(await t.context.doc.exists(workspace.id, docId));
});
test('should detect doc exists on only updates exists', async t => {
const docId = randomUUID();
await t.context.doc.createUpdates([
{
spaceId: workspace.id,
docId: docId,
blob: Buffer.from('blob2'),
timestamp: Date.now(),
editorId: user.id,
},
]);
t.true(await t.context.doc.exists(workspace.id, docId));
});