fix(nbstore): fix v1 doc storage timestamp (#10282)

This commit is contained in:
EYHN
2025-02-19 16:04:37 +08:00
committed by GitHub
parent 53cada4640
commit c39a93e1fd
3 changed files with 31 additions and 8 deletions

View File

@@ -17,6 +17,13 @@ import {
import { getIdConverter } from '../../../utils/id-converter'; import { getIdConverter } from '../../../utils/id-converter';
import { DocIDBConnection } from './db'; import { DocIDBConnection } from './db';
/**
* We use a fixed timestamp in v1 because the v1 should never be changed.
* This date is chosen because it is large enough to overwrite some previous error data.
* In our sync storage, only a larger timestamp can overwrite smaller one.
*/
const CONST_TIMESTAMP = new Date(1893456000000);
/** /**
* @deprecated readonly * @deprecated readonly
*/ */
@@ -45,7 +52,14 @@ export class IndexedDBV1DocStorage extends DocStorageBase {
return null; return null;
} }
const oldId = (await this.getIdConverter()).newIdToOldId(docId); const oldId = (await this.getIdConverter()).newIdToOldId(docId);
return this.rawGetDoc(oldId); const rawDoc = await this.rawGetDoc(oldId);
return rawDoc
? {
docId: rawDoc.docId,
bin: rawDoc.bin,
timestamp: CONST_TIMESTAMP,
}
: null;
} }
protected override async getDocSnapshot() { protected override async getDocSnapshot() {
@@ -54,7 +68,7 @@ export class IndexedDBV1DocStorage extends DocStorageBase {
override async pushDocUpdate(update: DocUpdate) { override async pushDocUpdate(update: DocUpdate) {
// no more writes to old db // no more writes to old db
return { docId: update.docId, timestamp: new Date() }; return { docId: update.docId, timestamp: CONST_TIMESTAMP };
} }
override async deleteDoc(docId: string) { override async deleteDoc(docId: string) {
@@ -110,7 +124,7 @@ export class IndexedDBV1DocStorage extends DocStorageBase {
); );
return Object.fromEntries( return Object.fromEntries(
oldIds.map(id => [idConverter.oldIdToNewId(id), new Date(1)]) oldIds.map(id => [idConverter.oldIdToNewId(id), CONST_TIMESTAMP])
); );
} }

View File

@@ -9,6 +9,13 @@ import { isEmptyUpdate } from '../../../utils/is-empty-update';
import type { SpaceType } from '../../../utils/universal-id'; import type { SpaceType } from '../../../utils/universal-id';
import { apis } from './db'; import { apis } from './db';
/**
* We use a fixed timestamp in v1 because the v1 should never be changed.
* This date is chosen because it is large enough to overwrite some previous error data.
* In our sync storage, only a larger timestamp can overwrite smaller one.
*/
const CONST_TIMESTAMP = new Date(1893456000000);
/** /**
* @deprecated readonly * @deprecated readonly
*/ */
@@ -34,7 +41,7 @@ export class SqliteV1DocStorage extends DocStorageBase<{
override async pushDocUpdate(update: DocUpdate) { override async pushDocUpdate(update: DocUpdate) {
// no more writes // no more writes
return { docId: update.docId, timestamp: new Date() }; return { docId: update.docId, timestamp: CONST_TIMESTAMP };
} }
override async getDoc(docId: string) { override async getDoc(docId: string) {
@@ -52,7 +59,7 @@ export class SqliteV1DocStorage extends DocStorageBase<{
return { return {
docId, docId,
bin, bin,
timestamp: new Date(), timestamp: CONST_TIMESTAMP,
}; };
} }
@@ -69,8 +76,9 @@ export class SqliteV1DocStorage extends DocStorageBase<{
const idConverter = await this.getIdConverter(); const idConverter = await this.getIdConverter();
return timestamps.reduce( return timestamps.reduce(
(ret, { docId, timestamp }) => { (ret, { docId }) => {
ret[idConverter.oldIdToNewId(docId ?? this.options.id)] = timestamp; ret[idConverter.oldIdToNewId(docId ?? this.options.id)] =
CONST_TIMESTAMP;
return ret; return ret;
}, },
{} as Record<string, Date> {} as Record<string, Date>

View File

@@ -199,7 +199,8 @@ export class DocSyncPeer {
throwIfAborted(signal); throwIfAborted(signal);
if ( if (
!this.remote.isReadonly && !this.remote.isReadonly &&
(pushedClock === null || pushedClock !== clock?.timestamp) clock &&
(pushedClock === null || pushedClock !== clock.timestamp)
) { ) {
await this.jobs.pullAndPush(docId, signal); await this.jobs.pullAndPush(docId, signal);
} else { } else {