diff --git a/src/LocalPouchDB.ts b/src/LocalPouchDB.ts index 517b4a0..0f871dd 100644 --- a/src/LocalPouchDB.ts +++ b/src/LocalPouchDB.ts @@ -411,11 +411,11 @@ export class LocalPouchDB { let children: string[] = []; if (this.settings.readChunksOnline) { - const items = await this.fetchLeafFromRemote(obj.children); + const items = await this.CollectChunks(obj.children); if (items) { for (const v of items) { - if (v.doc && v.doc.type == "leaf") { - children.push(v.doc.data); + if (v && v.type == "leaf") { + children.push(v.data); } else { if (!opt) { Logger(`Chunks of ${obj._id} are not valid.`, LOG_LEVEL.NOTICE); @@ -1357,16 +1357,31 @@ export class LocalPouchDB { } return true; } - async fetchLeafFromRemote(ids: string[], showResult = false) { + + // Collect chunks from both local and remote. + async CollectChunks(ids: string[], showResult = false) { + // Fetch local chunks. + const localChunks = await this.localDatabase.allDocs({ keys: ids, include_docs: true }); + const missingChunks = localChunks.rows.filter(e => "error" in e).map(e => e.key); + // If we have enough chunks, return them. + if (missingChunks.length == 0) { + return localChunks.rows.map(e => e.doc); + } + + // Fetching remote chunks. const ret = await connectRemoteCouchDBWithSetting(this.settings, this.isMobile); if (typeof (ret) === "string") { Logger(`Could not connect to server.${ret} `, showResult ? LOG_LEVEL.NOTICE : LOG_LEVEL.INFO, "fetch"); - return; + return false; } - const leafs = await ret.db.allDocs({ keys: ids, include_docs: true }); - return leafs.rows; + const remoteChunks = await ret.db.allDocs({ keys: missingChunks, include_docs: true }); + if (remoteChunks.rows.some(e => "error" in e)) { + return false; + } + // Merge them + const chunkMap: { [key: string]: EntryDoc } = remoteChunks.rows.reduce((p, c) => ({ ...p, [c.key]: c.doc }), {}) + return localChunks.rows.map(e => ("error" in e) ? (chunkMap[e.key]) : e.doc); } - } diff --git a/updates.md b/updates.md index 66a469a..25604a7 100644 --- a/updates.md +++ b/updates.md @@ -1,3 +1,19 @@ +### 0.14.1 +- The target selecting filter was implemented. + Now we can set what files are synchronised by regular expression. +- We can configure the size of chunks. + We can use larger chunks to improve performance. + (This feature can not be used with IBM Cloudant) +- Read chunks online. + Now we can synchronise only metadata and retrieve chunks on demand. It reduces local database size and time for replication. +- Added this note. +- Use local chunks in preference to remote them if present, + +#### Recommended configuration for Self-hosted CouchDB +- Set chunk size to around 100 to 250 (10MB - 25MB per chunk) +- Be sure to `Read chunks online` checked. + + ### 0.13.0 - The metadata of the deleted files will be kept on the database by default. If you want to delete this as the previous version, please turn on `Delete metadata of deleted files.`. And, if you have upgraded from the older version, please ensure every device has been upgraded. @@ -14,4 +30,4 @@ - Now, we can synchronise hidden files that conflicted on each devices. - We can search for conflicting docs. - Pending processes can now be run at any time. - - Performance improved on synchronising large numbers of files at once. \ No newline at end of file + - Performance improved on synchronising large numbers of files at once.