-
Notifications
You must be signed in to change notification settings - Fork 4
/
fs.ts
431 lines (386 loc) · 14.7 KB
/
fs.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { openDB, IDBPDatabase } from "idb";
import _ from 'lodash';
import { generateId, getParentPath, getFileName, formatBytes } from './utils';
interface BlobInfo {
id: string;
size: number;
compressed: boolean;
}
export interface Entry {
isDirectory: boolean;
mtime: number;
mode: number;
blobs: Array<BlobInfo>;
parent: string;
fullPath: string;
}
interface FullEntry extends Entry {
name: string;
}
function typedArrayToBuffer(array: Uint8Array): ArrayBuffer {
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
}
interface OpenedFile {
entry: Entry;
cachedBlobs: Map<string, ArrayBuffer>;
accessedBlobs: Map<string, number>;
dirty: boolean;
readonly: boolean;
}
// Cache blobs smaller than 2MB, if it's read for more than
// 3 times in a single session
//
// This mainly optimizes reading speed of yaml-cpp and opencc files,
// which read files in small chunks, and thus very slow.
//
// In librime, larger files (like table.bin, prism.bin)
// have been optimized to read once into memory, so there's
// no need for caching big files.
export const kCacheBlobSizeLimit = 2097152;
// For big files, only cache at most 3 chunks (most files are actually read sequentially,
// so 3 chunks are more than enough)
const kCacheTotalSizeLimit = kCacheBlobSizeLimit * 3;
const kCacheFrequency = 3;
// If a file took more than 50ms to read, warn about it
const kTimeWarning = 50;
export class FastIndexedDbFsController {
dbName: string;
openedFiles: Map<String, OpenedFile>;
constructor(name) {
this.dbName = name;
this.openedFiles = new Map();
}
db: IDBPDatabase
async open() {
this.db = await openDB(this.dbName, 1, {
upgrade(db) {
db.createObjectStore("blobs");
const entryStore = db.createObjectStore("entries", { keyPath: "fullPath" });
entryStore.createIndex("parent", "parent");
entryStore.createIndex("type", "type");
}
});
}
async readEntryRaw(path: string): Promise<Entry> {
return await this.db.get("entries", path);
}
async readEntry(path: string): Promise<Entry> {
if (this.openedFiles.has(path)) {
return this.openedFiles.get(path).entry;
} else {
return await this.readEntryRaw(path);
}
}
async getFileSize(path: string): Promise<number> {
const details: Entry = await this.readEntry(path);
if (details) {
return details.blobs.map(b => b.size).reduce((partialSum, a) => partialSum + a, 0);
} else {
return 0;
}
}
async createBlob(rawData: ArrayBuffer): Promise<BlobInfo> {
const id = generateId(16);
let data = rawData;
await this.db.put("blobs", data, id);
return { id: id, size: rawData.byteLength, compressed: false };
}
async readBlob(ident: BlobInfo): Promise<ArrayBuffer> {
let buf = await this.db.get("blobs", ident.id) as ArrayBuffer;
return buf;
}
async writeEntry(entry: Entry): Promise<void> {
entry.parent = getParentPath(entry.fullPath);
if (this.openedFiles.has(entry.fullPath)) {
const o = this.openedFiles.get(entry.fullPath);
o.entry = entry;
o.dirty = false;
}
await this.db.put("entries", entry);
}
async delPath(path: string): Promise<void> {
await this.db.delete("entries", path);
}
async setFileSize(path: string, size: number): Promise<void> {
let curEntry = await this.readEntry(path);
if (!curEntry) {
curEntry = {
isDirectory: false,
mtime: 0,
mode: 0o777,
blobs: [],
parent: getParentPath(path),
fullPath: path
};
}
if (curEntry.isDirectory) {
throw new Error("Path " + path + " is a directory, cannot set its size.");
}
const curBlobs = curEntry.blobs;
let newBlobs = [];
let seek = 0;
for (let i = 0; i < curBlobs.length; i++) {
let curBlob = curBlobs[i];
if (size >= seek + curBlob.size) {
// not arrived yet
newBlobs.push(curBlob);
} else {
const preserve = size - seek;
if (preserve > 0) {
const cutBlobData = (await this.readBlob(curBlob)).slice(0, preserve);
const cutBlob = await this.createBlob(cutBlobData);
newBlobs.push(cutBlob);
}
break;
}
seek += curBlob.size;
}
if (seek < size) {
let stubData = new ArrayBuffer(size - seek);
const stubBlob = await this.createBlob(stubData);
newBlobs.push(stubBlob);
}
curEntry.blobs = newBlobs;
curEntry.mtime = new Date().getTime();
await this.writeEntry(curEntry);
}
async openFile(path: string, readonly: boolean): Promise<void> {
if (this.openedFiles.has(path)) {
// already opened
const inst = this.openedFiles.get(path);
inst.readonly = readonly;
} else {
let entry = await this.readEntryRaw(path);
let created = false;
if (!entry) {
if (!readonly) {
created = true;
entry = {
isDirectory: false,
mtime: 0,
mode: 0o777,
blobs: [],
parent: getParentPath(path),
fullPath: path
};
} else {
throw new Error("Path " + path + " does not exist, cannot open for read");
}
}
if (entry.isDirectory) {
throw new Error("Path " + path + " is a directory, cannot open as a file");
}
this.openedFiles.set(path, {
entry,
cachedBlobs: new Map(),
accessedBlobs: new Map(),
readonly: readonly,
dirty: created
});
}
}
async writeFile(path: string, data: Uint8Array, pos: number): Promise<number> {
const handle = this.openedFiles.get(path);
if (!handle || handle.readonly) {
throw Error(`File ${path} is not opened for writing, cannot write to it`);
}
const curFile = handle.entry;
const curBlobs = curFile.blobs;
let newBlobs = [];
let seek = 0;
for (let i = 0; i < curBlobs.length; i++) {
let curBlob = curBlobs[i];
if (pos >= seek + curBlob.size) {
// not arrived yet
newBlobs.push(curBlob);
} else {
const preserve = pos - seek;
if (preserve > 0) {
const cutBlobData = (await this.readBlob(curBlob)).slice(0, preserve);
const cutBlob = await this.createBlob(cutBlobData);
newBlobs.push(cutBlob);
}
break;
}
seek += curBlob.size;
}
const contentBlob = await this.createBlob(typedArrayToBuffer(data));
newBlobs.push(contentBlob);
seek = 0;
const end = pos + data.length;
for (let i = 0; i < curBlobs.length; i++) {
let curBlob = curBlobs[i];
if (end <= seek) {
newBlobs.push(curBlob);
} else if (end > seek && end <= seek + curBlob.size) {
const preserveStart = end - seek;
if (preserveStart != curBlob.size) {
const cutBlobData = (await this.readBlob(curBlob)).slice(preserveStart, curBlob.size);
const cutBlob = await this.createBlob(cutBlobData);
newBlobs.push(cutBlob);
}
}
seek += curBlob.size;
}
curFile.blobs = newBlobs;
curFile.mtime = new Date().getTime();
handle.dirty = true;
return data.length;
}
async readFile(path: string, data: Uint8Array, pos: number): Promise<number> {
const start = performance.now();
const handle = this.openedFiles.get(path);
if (!handle) {
throw Error(`File ${path} is not opened for reading, cannot read from it`);
}
const curBlobs = handle.entry.blobs;
let seek = 0;
let read = 0;
for (let i = 0; i < curBlobs.length; i++) {
let curBlob = curBlobs[i];
let start = pos - seek + read;
let end = pos + data.length - seek;
if (end > curBlob.size)
end = curBlob.size;
if (start >= 0 && start < curBlob.size && end > 0) {
let blobData = handle.cachedBlobs.get(curBlob.id);
if (blobData === undefined) {
// blobData not in cache, read it from DB
blobData = await this.readBlob(curBlob);
// If blob is smaller than cache size limit, and read more than 3 times, then cache it
if (blobData.byteLength <= kCacheBlobSizeLimit) {
let readCount = handle.accessedBlobs.get(curBlob.id) || 1;
if (readCount >= kCacheFrequency) {
// If total cache size would be bigger than limit, remove one randomly
function totalCacheSize() {
const allCaches = Array.from(handle.cachedBlobs.values());
return _.sumBy(allCaches, c => c.byteLength);
}
while (totalCacheSize() + blobData.byteLength > kCacheTotalSizeLimit) {
const b = _.sample(Array.from(handle.cachedBlobs.keys()));
handle.cachedBlobs.delete(b);
handle.accessedBlobs.delete(b);
}
handle.cachedBlobs.set(curBlob.id, blobData);
} else {
handle.accessedBlobs.set(curBlob.id, readCount + 1);
}
}
}
data.set(new Uint8Array(blobData, start, end - start), read);
read += (end - start);
if (read >= data.length)
break;
}
seek += curBlob.size;
}
const end = performance.now();
if (end - start > kTimeWarning) {
console.warn(`Slow read: took ${(end - start).toFixed(2)}ms to read ${formatBytes(read)} from file ${path}`);
}
return read;
}
async readWholeFile(path: string): Promise<Uint8Array> {
let file = this.openedFiles.get(path)?.entry;
if (!file) {
file = await this.readEntryRaw(path);
}
if (!file) {
throw Error(`File ${path} is not found, cannot read from it`);
}
const totalSize = _.sumBy(file.blobs, b => b.size);
const buf = new Uint8Array(totalSize);
let pos = 0;
for (let b of file.blobs) {
const blobData = new Uint8Array(await this.readBlob(b));
buf.set(blobData, pos);
pos += blobData.length;
}
return buf;
}
async writeWholeFile(path: string, buffer: Uint8Array): Promise<void> {
let curEntry = await this.readEntry(path);
if (!curEntry) {
curEntry = {
isDirectory: false,
mtime: 0,
mode: 0o777,
blobs: [],
parent: getParentPath(path),
fullPath: path
};
}
if (curEntry.isDirectory) {
throw new Error("Path " + path + " is a directory, cannot set its size.");
}
const blob = await this.createBlob(typedArrayToBuffer(buffer));
curEntry.blobs = [blob];
curEntry.mtime = new Date().getTime();
await this.writeEntry(curEntry);
}
async closeFile(path: string): Promise<void> {
let f = this.openedFiles.get(path);
if (f.dirty) {
await this.writeEntry(f.entry);
}
this.openedFiles.delete(path);
}
async move(oldPath: string, newPath: string): Promise<void> {
if (this.openedFiles.has(oldPath)) {
throw new Error(`${oldPath} already opened, cannot move`);
}
if (this.openedFiles.has(newPath)) {
throw new Error(`${newPath} already opened, cannot move`);
}
let curEntry = await this.readEntry(oldPath);
if (curEntry == null) {
throw new Error("Path " + oldPath + " does not exist, cannot move.");
}
curEntry.fullPath = newPath;
await this.writeEntry(curEntry);
await this.delPath(oldPath);
}
async createDirectory(path: string): Promise<void> {
let newEntry: Entry = {
isDirectory: true,
mode: 0o777,
mtime: 0,
blobs: [],
fullPath: path,
parent: null
};
await this.writeEntry(newEntry);
}
async getDirectoryEntriesCount(path: string): Promise<number> {
return await this.db.countFromIndex("entries", "parent", path);
}
async readDirectory(path: string): Promise<FullEntry[]> {
const r: Array<Entry> = await this.db.getAllFromIndex("entries", "parent", path);
return r.map(x => ({ ...x, name: getFileName(x.fullPath) }));
}
async readAll(): Promise<Entry[]> {
return await this.db.getAll("entries");
}
async collectGarbage(): Promise<void> {
const allBlobs = new Set((await this.db.getAllKeys("blobs")) as string[]);
const allEntries = (await this.db.getAll("entries")) as Entry[];
const usedBlobs = new Set(allEntries.flatMap(e => e.blobs).map(e => e.id));
this.openedFiles.forEach((v) => {
v.entry.blobs.forEach((b) => {
usedBlobs.add(b.id);
});
});
console.log(`There're totally ${allBlobs.size} blobs, of which ${usedBlobs.size} are used.`);
const tx = this.db.transaction("blobs", "readwrite");
const blobStore = tx.objectStore("blobs");
for (let b of allBlobs.values()) {
if (!usedBlobs.has(b)) {
console.log(`Removing unused blob ${b}`);
blobStore.delete(b);
}
}
console.log("Waiting for transaction to finish...");
await tx.done;
console.log("Garbage collection done.");
}
}