@atlaspack/cache 3.1.1-canary.9 → 3.1.1-dev.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/lib/LMDBLiteCache.js +42 -16
- package/package.json +9 -8
- package/src/LMDBLiteCache.js +26 -12
- package/test/LMDBLiteCache.test.js +15 -0
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# @atlaspack/cache
|
2
2
|
|
3
|
+
## 3.2.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- [#531](https://github.com/atlassian-labs/atlaspack/pull/531) [`d2c50c2`](https://github.com/atlassian-labs/atlaspack/commit/d2c50c2c020888b33bb25b8690d9320c2b69e2a6) Thanks [@yamadapc](https://github.com/yamadapc)! - Add way to iterate LMDB cache keys
|
8
|
+
|
9
|
+
### Patch Changes
|
10
|
+
|
11
|
+
- Updated dependencies [[`11d6f16`](https://github.com/atlassian-labs/atlaspack/commit/11d6f16b6397dee2f217167e5c98b39edb63f7a7), [`e2ba0f6`](https://github.com/atlassian-labs/atlaspack/commit/e2ba0f69702656f3d1ce95ab1454e35062b13b39), [`d2c50c2`](https://github.com/atlassian-labs/atlaspack/commit/d2c50c2c020888b33bb25b8690d9320c2b69e2a6), [`46a90dc`](https://github.com/atlassian-labs/atlaspack/commit/46a90dccd019a26b222c878a92d23acc75dc67c5), [`4c17141`](https://github.com/atlassian-labs/atlaspack/commit/4c1714103dab2aa9039c488f381551d2b65d1d01)]:
|
12
|
+
- @atlaspack/feature-flags@2.14.3
|
13
|
+
- @atlaspack/rust@3.3.0
|
14
|
+
- @atlaspack/fs@2.15.0
|
15
|
+
- @atlaspack/utils@2.14.5
|
16
|
+
- @atlaspack/logger@2.14.5
|
17
|
+
|
3
18
|
## 3.1.0
|
4
19
|
|
5
20
|
### Minor Changes
|
package/lib/LMDBLiteCache.js
CHANGED
@@ -26,16 +26,16 @@ function _rust() {
|
|
26
26
|
};
|
27
27
|
return data;
|
28
28
|
}
|
29
|
-
function
|
30
|
-
const data = _interopRequireDefault(require("
|
31
|
-
|
29
|
+
function _fs() {
|
30
|
+
const data = _interopRequireDefault(require("fs"));
|
31
|
+
_fs = function () {
|
32
32
|
return data;
|
33
33
|
};
|
34
34
|
return data;
|
35
35
|
}
|
36
|
-
function
|
37
|
-
const data = _interopRequireDefault(require("
|
38
|
-
|
36
|
+
function _ncp() {
|
37
|
+
const data = _interopRequireDefault(require("ncp"));
|
38
|
+
_ncp = function () {
|
39
39
|
return data;
|
40
40
|
};
|
41
41
|
return data;
|
@@ -47,9 +47,23 @@ function _util() {
|
|
47
47
|
};
|
48
48
|
return data;
|
49
49
|
}
|
50
|
-
function
|
50
|
+
function _stream() {
|
51
|
+
const data = _interopRequireDefault(require("stream"));
|
52
|
+
_stream = function () {
|
53
|
+
return data;
|
54
|
+
};
|
55
|
+
return data;
|
56
|
+
}
|
57
|
+
function _path() {
|
58
|
+
const data = _interopRequireDefault(require("path"));
|
59
|
+
_path = function () {
|
60
|
+
return data;
|
61
|
+
};
|
62
|
+
return data;
|
63
|
+
}
|
64
|
+
function _fs2() {
|
51
65
|
const data = require("@atlaspack/fs");
|
52
|
-
|
66
|
+
_fs2 = function () {
|
53
67
|
return data;
|
54
68
|
};
|
55
69
|
return data;
|
@@ -58,6 +72,7 @@ var _package = _interopRequireDefault(require("../package.json"));
|
|
58
72
|
var _FSCache = require("./FSCache");
|
59
73
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
60
74
|
// $FlowFixMe
|
75
|
+
const ncpAsync = (0, _util().promisify)(_ncp().default);
|
61
76
|
class LmdbWrapper {
|
62
77
|
constructor(lmdb) {
|
63
78
|
this.lmdb = lmdb;
|
@@ -90,7 +105,9 @@ class LmdbWrapper {
|
|
90
105
|
currentKeys = this.lmdb.keysSync(currentKeys.length, PAGE_SIZE);
|
91
106
|
}
|
92
107
|
}
|
93
|
-
|
108
|
+
compact(targetPath) {
|
109
|
+
this.lmdb.compact(targetPath);
|
110
|
+
}
|
94
111
|
}
|
95
112
|
exports.LmdbWrapper = LmdbWrapper;
|
96
113
|
function open(directory
|
@@ -105,7 +122,7 @@ function open(directory
|
|
105
122
|
const pipeline = (0, _util().promisify)(_stream().default.pipeline);
|
106
123
|
class LMDBLiteCache {
|
107
124
|
constructor(cacheDir) {
|
108
|
-
this.fs = new (
|
125
|
+
this.fs = new (_fs2().NodeFS)();
|
109
126
|
this.dir = cacheDir;
|
110
127
|
this.fsCache = new _FSCache.FSCache(this.fs, cacheDir);
|
111
128
|
this.store = open(cacheDir, {
|
@@ -214,13 +231,22 @@ class LMDBLiteCache {
|
|
214
231
|
keys() {
|
215
232
|
return this.store.keys();
|
216
233
|
}
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
234
|
+
async compact(targetPath) {
|
235
|
+
await _fs().default.promises.mkdir(targetPath, {
|
236
|
+
recursive: true
|
237
|
+
});
|
238
|
+
const files = await _fs().default.promises.readdir(this.dir);
|
239
|
+
// copy all files except data.mdb and lock.mdb to the target path (recursive)
|
240
|
+
for (const file of files) {
|
241
|
+
const filePath = _path().default.join(this.dir, file);
|
242
|
+
if (file === 'data.mdb' || file === 'lock.mdb') {
|
243
|
+
continue;
|
244
|
+
}
|
245
|
+
await ncpAsync(filePath, _path().default.join(targetPath, file));
|
246
|
+
}
|
247
|
+
this.store.compact(_path().default.join(targetPath, 'data.mdb'));
|
223
248
|
}
|
249
|
+
refresh() {}
|
224
250
|
}
|
225
251
|
exports.LMDBLiteCache = LMDBLiteCache;
|
226
252
|
(0, _buildCache().registerSerializableClass)(`${_package.default.version}:LMDBLiteCache`, LMDBLiteCache);
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@atlaspack/cache",
|
3
3
|
"description": "Interface for defining caches and file-system, IDB and LMDB implementations.",
|
4
|
-
"version": "3.1.1-
|
4
|
+
"version": "3.1.1-dev.14+8c369e38c",
|
5
5
|
"license": "(MIT OR Apache-2.0)",
|
6
6
|
"type": "commonjs",
|
7
7
|
"publishConfig": {
|
@@ -23,12 +23,13 @@
|
|
23
23
|
"check-ts": "tsc --noEmit index.d.ts"
|
24
24
|
},
|
25
25
|
"dependencies": {
|
26
|
-
"@atlaspack/build-cache": "2.13.3-
|
27
|
-
"@atlaspack/feature-flags": "2.14.1-
|
28
|
-
"@atlaspack/fs": "2.14.5-
|
29
|
-
"@atlaspack/logger": "2.14.5-
|
30
|
-
"@atlaspack/rust": "3.2.1-
|
31
|
-
"@atlaspack/utils": "2.14.5-
|
26
|
+
"@atlaspack/build-cache": "2.13.3-dev.82+8c369e38c",
|
27
|
+
"@atlaspack/feature-flags": "2.14.1-dev.82+8c369e38c",
|
28
|
+
"@atlaspack/fs": "2.14.5-dev.14+8c369e38c",
|
29
|
+
"@atlaspack/logger": "2.14.5-dev.14+8c369e38c",
|
30
|
+
"@atlaspack/rust": "3.2.1-dev.14+8c369e38c",
|
31
|
+
"@atlaspack/utils": "2.14.5-dev.14+8c369e38c",
|
32
|
+
"ncp": "^2.0.0"
|
32
33
|
},
|
33
34
|
"devDependencies": {
|
34
35
|
"idb": "^5.0.8"
|
@@ -36,5 +37,5 @@
|
|
36
37
|
"browser": {
|
37
38
|
"./src/IDBCache.js": "./src/IDBCache.browser.js"
|
38
39
|
},
|
39
|
-
"gitHead": "
|
40
|
+
"gitHead": "8c369e38ccd428409811114aebd6044c27f90705"
|
40
41
|
}
|
package/src/LMDBLiteCache.js
CHANGED
@@ -10,18 +10,18 @@ import {Lmdb} from '@atlaspack/rust';
|
|
10
10
|
import type {FilePath} from '@atlaspack/types';
|
11
11
|
import type {Cache} from './types';
|
12
12
|
import type {Readable, Writable} from 'stream';
|
13
|
-
|
13
|
+
import fs from 'fs';
|
14
|
+
import ncp from 'ncp';
|
15
|
+
import {promisify} from 'util';
|
14
16
|
import stream from 'stream';
|
15
17
|
import path from 'path';
|
16
|
-
import {promisify} from 'util';
|
17
|
-
|
18
18
|
import {NodeFS} from '@atlaspack/fs';
|
19
|
-
|
20
19
|
// $FlowFixMe
|
21
20
|
import packageJson from '../package.json';
|
22
|
-
|
23
21
|
import {FSCache} from './FSCache';
|
24
22
|
|
23
|
+
const ncpAsync = promisify(ncp);
|
24
|
+
|
25
25
|
interface DBOpenOptions {
|
26
26
|
name: string;
|
27
27
|
// unused
|
@@ -72,7 +72,9 @@ export class LmdbWrapper {
|
|
72
72
|
}
|
73
73
|
}
|
74
74
|
|
75
|
-
|
75
|
+
compact(targetPath: string) {
|
76
|
+
this.lmdb.compact(targetPath);
|
77
|
+
}
|
76
78
|
}
|
77
79
|
|
78
80
|
export function open(
|
@@ -241,13 +243,25 @@ export class LMDBLiteCache implements Cache {
|
|
241
243
|
return this.store.keys();
|
242
244
|
}
|
243
245
|
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
//
|
249
|
-
|
246
|
+
async compact(targetPath: string): Promise<void> {
|
247
|
+
await fs.promises.mkdir(targetPath, {recursive: true});
|
248
|
+
|
249
|
+
const files = await fs.promises.readdir(this.dir);
|
250
|
+
// copy all files except data.mdb and lock.mdb to the target path (recursive)
|
251
|
+
for (const file of files) {
|
252
|
+
const filePath = path.join(this.dir, file);
|
253
|
+
|
254
|
+
if (file === 'data.mdb' || file === 'lock.mdb') {
|
255
|
+
continue;
|
256
|
+
}
|
257
|
+
|
258
|
+
await ncpAsync(filePath, path.join(targetPath, file));
|
259
|
+
}
|
260
|
+
|
261
|
+
this.store.compact(path.join(targetPath, 'data.mdb'));
|
250
262
|
}
|
263
|
+
|
264
|
+
refresh(): void {}
|
251
265
|
}
|
252
266
|
|
253
267
|
registerSerializableClass(
|
@@ -51,4 +51,19 @@ describe('LMDBLiteCache', () => {
|
|
51
51
|
const keys = cache.keys();
|
52
52
|
assert.deepEqual(Array.from(keys), ['key1', 'key2']);
|
53
53
|
});
|
54
|
+
|
55
|
+
it('can compact databases', async () => {
|
56
|
+
cache = new LMDBLiteCache(path.join(cacheDir, 'compact_test'));
|
57
|
+
await cache.ensure();
|
58
|
+
await cache.setBlob('key1', Buffer.from(serialize({value: 42})));
|
59
|
+
await cache.setBlob('key2', Buffer.from(serialize({value: 43})));
|
60
|
+
await cache.compact(path.join(cacheDir, 'compact_test_compacted'));
|
61
|
+
|
62
|
+
cache.getNativeRef().close();
|
63
|
+
|
64
|
+
cache = new LMDBLiteCache(path.join(cacheDir, 'compact_test_compacted'));
|
65
|
+
await cache.ensure();
|
66
|
+
const keys = cache.keys();
|
67
|
+
assert.deepEqual(Array.from(keys), ['key1', 'key2']);
|
68
|
+
});
|
54
69
|
});
|