-
Notifications
You must be signed in to change notification settings - Fork 30.6k
/
Copy pathstorageService.ts
188 lines (146 loc) · 7.4 KB
/
storageService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Promises } from 'vs/base/common/async';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { joinPath } from 'vs/base/common/resources';
import { IStorage, Storage } from 'vs/base/parts/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IRemoteService } from 'vs/platform/ipc/common/services';
import { AbstractStorageService, isProfileUsingDefaultStorage, StorageScope, WillSaveStateReason } from 'vs/platform/storage/common/storage';
import { ApplicationStorageDatabaseClient, ProfileStorageDatabaseClient, WorkspaceStorageDatabaseClient } from 'vs/platform/storage/common/storageIpc';
import { isUserDataProfile, IUserDataProfile } from 'vs/platform/userDataProfile/common/userDataProfile';
import { IAnyWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';
export class RemoteStorageService extends AbstractStorageService {
private readonly applicationStorageProfile = this.initialProfiles.defaultProfile;
private readonly applicationStorage = this.createApplicationStorage();
private profileStorageProfile = this.initialProfiles.currentProfile;
private readonly profileStorageDisposables = this._register(new DisposableStore());
private profileStorage = this.createProfileStorage(this.profileStorageProfile);
private workspaceStorageId = this.initialWorkspace?.id;
private readonly workspaceStorageDisposables = this._register(new DisposableStore());
private workspaceStorage = this.createWorkspaceStorage(this.initialWorkspace);
constructor(
private readonly initialWorkspace: IAnyWorkspaceIdentifier | undefined,
private readonly initialProfiles: { defaultProfile: IUserDataProfile; currentProfile: IUserDataProfile },
private readonly remoteService: IRemoteService,
private readonly environmentService: IEnvironmentService
) {
super();
}
private createApplicationStorage(): IStorage {
const storageDataBaseClient = this._register(new ApplicationStorageDatabaseClient(this.remoteService.getChannel('storage')));
const applicationStorage = this._register(new Storage(storageDataBaseClient));
this._register(applicationStorage.onDidChangeStorage(key => this.emitDidChangeValue(StorageScope.APPLICATION, key)));
return applicationStorage;
}
private createProfileStorage(profile: IUserDataProfile): IStorage {
// First clear any previously associated disposables
this.profileStorageDisposables.clear();
// Remember profile associated to profile storage
this.profileStorageProfile = profile;
let profileStorage: IStorage;
if (isProfileUsingDefaultStorage(profile)) {
// If we are using default profile storage, the profile storage is
// actually the same as application storage. As such we
// avoid creating the storage library a second time on
// the same DB.
profileStorage = this.applicationStorage;
} else {
const storageDataBaseClient = this.profileStorageDisposables.add(new ProfileStorageDatabaseClient(this.remoteService.getChannel('storage'), profile));
profileStorage = this.profileStorageDisposables.add(new Storage(storageDataBaseClient));
}
this.profileStorageDisposables.add(profileStorage.onDidChangeStorage(key => this.emitDidChangeValue(StorageScope.PROFILE, key)));
return profileStorage;
}
private createWorkspaceStorage(workspace: IAnyWorkspaceIdentifier): IStorage;
private createWorkspaceStorage(workspace: IAnyWorkspaceIdentifier | undefined): IStorage | undefined;
private createWorkspaceStorage(workspace: IAnyWorkspaceIdentifier | undefined): IStorage | undefined {
// First clear any previously associated disposables
this.workspaceStorageDisposables.clear();
// Remember workspace ID for logging later
this.workspaceStorageId = workspace?.id;
let workspaceStorage: IStorage | undefined = undefined;
if (workspace) {
const storageDataBaseClient = this.workspaceStorageDisposables.add(new WorkspaceStorageDatabaseClient(this.remoteService.getChannel('storage'), workspace));
workspaceStorage = this.workspaceStorageDisposables.add(new Storage(storageDataBaseClient));
this.workspaceStorageDisposables.add(workspaceStorage.onDidChangeStorage(key => this.emitDidChangeValue(StorageScope.WORKSPACE, key)));
}
return workspaceStorage;
}
protected async doInitialize(): Promise<void> {
// Init all storage locations
await Promises.settled([
this.applicationStorage.init(),
this.profileStorage.init(),
this.workspaceStorage?.init() ?? Promise.resolve()
]);
}
protected getStorage(scope: StorageScope): IStorage | undefined {
switch (scope) {
case StorageScope.APPLICATION:
return this.applicationStorage;
case StorageScope.PROFILE:
return this.profileStorage;
default:
return this.workspaceStorage;
}
}
protected getLogDetails(scope: StorageScope): string | undefined {
switch (scope) {
case StorageScope.APPLICATION:
return this.applicationStorageProfile.globalStorageHome.fsPath;
case StorageScope.PROFILE:
return this.profileStorageProfile?.globalStorageHome.fsPath;
default:
return this.workspaceStorageId ? `${joinPath(this.environmentService.workspaceStorageHome, this.workspaceStorageId, 'state.vscdb').fsPath}` : undefined;
}
}
async close(): Promise<void> {
// Stop periodic scheduler and idle runner as we now collect state normally
this.stopFlushWhenIdle();
// Signal as event so that clients can still store data
this.emitWillSaveState(WillSaveStateReason.SHUTDOWN);
// Do it
await Promises.settled([
this.applicationStorage.close(),
this.profileStorage.close(),
this.workspaceStorage?.close() ?? Promise.resolve()
]);
}
protected async switchToProfile(toProfile: IUserDataProfile, preserveData: boolean): Promise<void> {
if (!this.canSwitchProfile(this.profileStorageProfile, toProfile)) {
return;
}
const oldProfileStorage = this.profileStorage;
const oldItems = oldProfileStorage.items;
// Close old profile storage but only if this is
// different from application storage!
if (oldProfileStorage !== this.applicationStorage) {
await oldProfileStorage.close();
}
// Create new profile storage & init
this.profileStorage = this.createProfileStorage(toProfile);
await this.profileStorage.init();
// Handle data switch and eventing
this.switchData(oldItems, this.profileStorage, StorageScope.PROFILE, preserveData);
}
protected async switchToWorkspace(toWorkspace: IAnyWorkspaceIdentifier, preserveData: boolean): Promise<void> {
const oldWorkspaceStorage = this.workspaceStorage;
const oldItems = oldWorkspaceStorage?.items ?? new Map();
// Close old workspace storage
await oldWorkspaceStorage?.close();
// Create new workspace storage & init
this.workspaceStorage = this.createWorkspaceStorage(toWorkspace);
await this.workspaceStorage.init();
// Handle data switch and eventing
this.switchData(oldItems, this.workspaceStorage, StorageScope.WORKSPACE, preserveData);
}
hasScope(scope: IAnyWorkspaceIdentifier | IUserDataProfile): boolean {
if (isUserDataProfile(scope)) {
return this.profileStorageProfile.id === scope.id;
}
return this.workspaceStorageId === scope.id;
}
}