abstract getSyncPlan

This commit is contained in:
fyears 2021-11-07 00:39:57 +08:00
parent d3b812c8fa
commit 254790cd61
2 changed files with 51 additions and 22 deletions

View File

@ -22,7 +22,7 @@ import {
} from "./localdb"; } from "./localdb";
import type { SyncStatusType } from "./sync"; import type { SyncStatusType } from "./sync";
import { ensembleMixedStates, getOperation, doActualSync } from "./sync"; import { getSyncPlan, doActualSync } from "./sync";
import { DEFAULT_S3_CONFIG, getS3Client, listFromRemote, S3Config } from "./s3"; import { DEFAULT_S3_CONFIG, getS3Client, listFromRemote, S3Config } from "./s3";
interface SaveRemotePluginSettings { interface SaveRemotePluginSettings {
@ -87,19 +87,14 @@ export default class SaveRemotePlugin extends Plugin {
new Notice("4/6 Starting to generate sync plan."); new Notice("4/6 Starting to generate sync plan.");
this.syncStatus = "generating_plan"; this.syncStatus = "generating_plan";
const mixedStates = await ensembleMixedStates( const syncPlan = await getSyncPlan(
remoteRsp.Contents, remoteRsp.Contents,
local, local,
localHistory, localHistory,
this.db, this.db,
this.settings.password this.settings.password
); );
console.log(syncPlan.mixedStates); // for debugging
for (const [key, val] of Object.entries(mixedStates)) {
getOperation(val, true);
}
console.log(mixedStates);
// The operations above are read only and kind of safe. // The operations above are read only and kind of safe.
// The operations below begins to write or delete (!!!) something. // The operations below begins to write or delete (!!!) something.
@ -112,7 +107,7 @@ export default class SaveRemotePlugin extends Plugin {
this.settings.s3, this.settings.s3,
this.db, this.db,
this.app.vault, this.app.vault,
mixedStates, syncPlan,
this.settings.password this.settings.password
); );
@ -262,5 +257,7 @@ class SaveRemoteSettingTab extends PluginSettingTab {
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}) })
); );
containerEl.createEl("h2", { text: "Debug" });
} }
} }

View File

@ -16,9 +16,18 @@ import {
deleteFromRemote, deleteFromRemote,
downloadFromRemote, downloadFromRemote,
} from "./s3"; } from "./s3";
import { mkdirpInVault } from "./misc"; import { mkdirpInVault, SUPPORTED_SERVICES_TYPE } from "./misc";
import { decryptBase32ToString, encryptStringToBase32 } from "./encrypt"; import { decryptBase32ToString, encryptStringToBase32 } from "./encrypt";
export type SyncStatusType =
| "idle"
| "preparing"
| "getting_remote_meta"
| "getting_local_meta"
| "generating_plan"
| "syncing"
| "finish";
type DecisionType = type DecisionType =
| "undecided" | "undecided"
| "unknown" | "unknown"
@ -31,15 +40,6 @@ type DecisionType =
| "mkdirplocal" | "mkdirplocal"
| "skip"; | "skip";
export type SyncStatusType =
| "idle"
| "preparing"
| "getting_remote_meta"
| "getting_local_meta"
| "generating_plan"
| "syncing"
| "finish";
interface FileOrFolderMixedState { interface FileOrFolderMixedState {
key: string; key: string;
exist_local?: boolean; exist_local?: boolean;
@ -55,7 +55,13 @@ interface FileOrFolderMixedState {
remote_encrypted_key?: string; remote_encrypted_key?: string;
} }
export const ensembleMixedStates = async ( interface SyncPlanType {
ts: number;
remoteType: SUPPORTED_SERVICES_TYPE;
mixedStates: Record<string, FileOrFolderMixedState>;
}
const ensembleMixedStates = async (
remote: S3ObjectType[], remote: S3ObjectType[],
local: TAbstractFile[], local: TAbstractFile[],
deleteHistory: FileFolderHistoryRecord[], deleteHistory: FileFolderHistoryRecord[],
@ -173,7 +179,7 @@ export const ensembleMixedStates = async (
return results; return results;
}; };
export const getOperation = ( const getOperation = (
origRecord: FileOrFolderMixedState, origRecord: FileOrFolderMixedState,
inplace: boolean = false inplace: boolean = false
) => { ) => {
@ -289,14 +295,40 @@ export const getOperation = (
return r; return r;
}; };
export const getSyncPlan = async (
remote: S3ObjectType[],
local: TAbstractFile[],
deleteHistory: FileFolderHistoryRecord[],
db: lf.DatabaseConnection,
password: string = ""
) => {
const mixedStates = await ensembleMixedStates(
remote,
local,
deleteHistory,
db,
password
);
for (const [key, val] of Object.entries(mixedStates)) {
getOperation(val, true);
}
const plan = {
ts: Date.now(),
remoteType: "s3",
mixedStates: mixedStates,
} as SyncPlanType;
return plan;
};
export const doActualSync = async ( export const doActualSync = async (
s3Client: S3Client, s3Client: S3Client,
s3Config: S3Config, s3Config: S3Config,
db: lf.DatabaseConnection, db: lf.DatabaseConnection,
vault: Vault, vault: Vault,
keyStates: Record<string, FileOrFolderMixedState>, syncPlan: SyncPlanType,
password: string = "" password: string = ""
) => { ) => {
const keyStates = syncPlan.mixedStates;
await Promise.all( await Promise.all(
Object.entries(keyStates) Object.entries(keyStates)
.sort((k, v) => -(k as string).length) .sort((k, v) => -(k as string).length)