mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 07:01:31 +08:00 
			
		
		
		
	small improvements to sync table handling
This commit is contained in:
		
							parent
							
								
									3582013a33
								
							
						
					
					
						commit
						17d7ff3ff1
					
				@ -49,7 +49,7 @@ async function loginSync(req) {
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
        sourceId: sourceIdService.getCurrentSourceId(),
 | 
			
		||||
        maxSyncId: await sql.getValue("SELECT MAX(id) FROM sync WHERE isSynced = 1")
 | 
			
		||||
        maxSyncId: await sql.getValue("SELECT COALESCE(MAX(id), 0) FROM sync WHERE isSynced = 1")
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -50,7 +50,7 @@ async function getStats() {
 | 
			
		||||
async function checkSync() {
 | 
			
		||||
    return {
 | 
			
		||||
        entityHashes: await contentHashService.getEntityHashes(),
 | 
			
		||||
        maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync WHERE isSynced = 1')
 | 
			
		||||
        maxSyncId: await sql.getValue('SELECT COALESCE(MAX(id), 0) FROM sync WHERE isSynced = 1')
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -124,7 +124,7 @@ async function getChanged(req) {
 | 
			
		||||
 | 
			
		||||
    const ret = {
 | 
			
		||||
        syncs: await syncService.getSyncRecords(syncs),
 | 
			
		||||
        maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync WHERE isSynced = 1')
 | 
			
		||||
        maxSyncId: await sql.getValue('SELECT COALESCE(MAX(id), 0) FROM sync WHERE isSynced = 1')
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if (ret.syncs.length > 0) {
 | 
			
		||||
 | 
			
		||||
@ -23,7 +23,7 @@ async function index(req, res) {
 | 
			
		||||
        treeFontSize: parseInt(options.treeFontSize),
 | 
			
		||||
        detailFontSize: parseInt(options.detailFontSize),
 | 
			
		||||
        sourceId: await sourceIdService.generateSourceId(),
 | 
			
		||||
        maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync"),
 | 
			
		||||
        maxSyncIdAtLoad: await sql.getValue("SELECT COALESCE(MAX(id), 0) FROM sync"),
 | 
			
		||||
        instanceName: config.General ? config.General.instanceName : null,
 | 
			
		||||
        appCssNoteIds: await getAppCssNoteIds(),
 | 
			
		||||
        isDev: env.isDev(),
 | 
			
		||||
 | 
			
		||||
@ -123,7 +123,7 @@ async function doLogin() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function pullSync(syncContext) {
 | 
			
		||||
    let appliedPulls = 0;
 | 
			
		||||
    let atLeastOnePullApplied = false;
 | 
			
		||||
 | 
			
		||||
    while (true) {
 | 
			
		||||
        const lastSyncedPull = await getLastSyncedPull();
 | 
			
		||||
@ -150,10 +150,10 @@ async function pullSync(syncContext) {
 | 
			
		||||
        await sql.transactional(async () => {
 | 
			
		||||
            for (const {sync, entity} of rows) {
 | 
			
		||||
                if (!sourceIdService.isLocalSourceId(sync.sourceId)) {
 | 
			
		||||
                    if (appliedPulls === 0 && sync.entity !== 'recent_notes') { // send only for first
 | 
			
		||||
                    if (!atLeastOnePullApplied && sync.entity !== 'recent_notes') { // send only for first
 | 
			
		||||
                        ws.syncPullInProgress();
 | 
			
		||||
 | 
			
		||||
                        appliedPulls++;
 | 
			
		||||
                        atLeastOnePullApplied = true;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    await syncUpdateService.updateEntity(sync, entity, syncContext.sourceId);
 | 
			
		||||
@ -165,10 +165,10 @@ async function pullSync(syncContext) {
 | 
			
		||||
            await setLastSyncedPull(rows[rows.length - 1].sync.id);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        log.info(`Pulled ${rows.length} changes in ${pulledDate - startDate}ms from ${changesUri} and applied them in ${Date.now() - pulledDate}ms`);
 | 
			
		||||
        log.info(`Pulled ${rows.length} changes starting at syncId=${lastSyncedPull} in ${pulledDate - startDate}ms and applied them in ${Date.now() - pulledDate}ms, ${stats.outstandingPulls} outstanding pulls`);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (appliedPulls > 0) {
 | 
			
		||||
    if (atLeastOnePullApplied) {
 | 
			
		||||
        ws.syncPullFinished();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -368,7 +368,7 @@ async function updatePushStats() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getMaxSyncId() {
 | 
			
		||||
    return await sql.getValue('SELECT MAX(id) FROM sync');
 | 
			
		||||
    return await sql.getValue('SELECT COALESCE(MAX(id), 0) FROM sync');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sqlInit.dbReady.then(async () => {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user