@atlaspack/cache 3.1.1-canary.2 → 3.1.1-canary.20

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 CHANGED
@@ -1,5 +1,42 @@
1
1
  # @atlaspack/cache
2
2
 
3
+ ## 3.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`a1773d2`](https://github.com/atlassian-labs/atlaspack/commit/a1773d2a62d0ef7805ac7524621dcabcc1afe929), [`556d6ab`](https://github.com/atlassian-labs/atlaspack/commit/556d6ab8ede759fa7f37fcd3f4da336ef1c55e8f)]:
8
+ - @atlaspack/feature-flags@2.15.0
9
+ - @atlaspack/logger@2.14.7
10
+ - @atlaspack/rust@3.3.2
11
+ - @atlaspack/fs@2.15.2
12
+ - @atlaspack/utils@2.14.7
13
+
14
+ ## 3.2.1
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [[`e0f5337`](https://github.com/atlassian-labs/atlaspack/commit/e0f533757bd1019dbd108a04952c87da15286e09)]:
19
+ - @atlaspack/feature-flags@2.14.4
20
+ - @atlaspack/rust@3.3.1
21
+ - @atlaspack/fs@2.15.1
22
+ - @atlaspack/utils@2.14.6
23
+ - @atlaspack/logger@2.14.6
24
+
25
+ ## 3.2.0
26
+
27
+ ### Minor Changes
28
+
29
+ - [#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
30
+
31
+ ### Patch Changes
32
+
33
+ - 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)]:
34
+ - @atlaspack/feature-flags@2.14.3
35
+ - @atlaspack/rust@3.3.0
36
+ - @atlaspack/fs@2.15.0
37
+ - @atlaspack/utils@2.14.5
38
+ - @atlaspack/logger@2.14.5
39
+
3
40
  ## 3.1.0
4
41
 
5
42
  ### Minor Changes
@@ -26,16 +26,16 @@ function _rust() {
26
26
  };
27
27
  return data;
28
28
  }
29
- function _stream() {
30
- const data = _interopRequireDefault(require("stream"));
31
- _stream = function () {
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 _path() {
37
- const data = _interopRequireDefault(require("path"));
38
- _path = function () {
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 _fs() {
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
- _fs = function () {
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;
@@ -80,7 +95,19 @@ class LmdbWrapper {
80
95
  const buffer = typeof value === 'string' ? Buffer.from(value) : value;
81
96
  await this.lmdb.put(key, buffer);
82
97
  }
83
- resetReadTxn() {}
98
+ *keys() {
99
+ const PAGE_SIZE = 10000000;
100
+ let currentKeys = this.lmdb.keysSync(0, PAGE_SIZE);
101
+ while (currentKeys.length > 0) {
102
+ for (const key of currentKeys) {
103
+ yield key;
104
+ }
105
+ currentKeys = this.lmdb.keysSync(currentKeys.length, PAGE_SIZE);
106
+ }
107
+ }
108
+ compact(targetPath) {
109
+ this.lmdb.compact(targetPath);
110
+ }
84
111
  }
85
112
  exports.LmdbWrapper = LmdbWrapper;
86
113
  function open(directory
@@ -95,7 +122,7 @@ function open(directory
95
122
  const pipeline = (0, _util().promisify)(_stream().default.pipeline);
96
123
  class LMDBLiteCache {
97
124
  constructor(cacheDir) {
98
- this.fs = new (_fs().NodeFS)();
125
+ this.fs = new (_fs2().NodeFS)();
99
126
  this.dir = cacheDir;
100
127
  this.fsCache = new _FSCache.FSCache(this.fs, cacheDir);
101
128
  this.store = open(cacheDir, {
@@ -201,13 +228,25 @@ class LMDBLiteCache {
201
228
  }
202
229
  return this.store.delete(key);
203
230
  }
204
- refresh() {
205
- // Reset the read transaction for the store. This guarantees that
206
- // the next read will see the latest changes to the store.
207
- // Useful in scenarios where reads and writes are multi-threaded.
208
- // See https://github.com/kriszyp/lmdb-js#resetreadtxn-void
209
- this.store.resetReadTxn();
231
+ keys() {
232
+ return this.store.keys();
233
+ }
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'));
210
248
  }
249
+ refresh() {}
211
250
  }
212
251
  exports.LMDBLiteCache = LMDBLiteCache;
213
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-canary.2+88fcaf47c",
4
+ "version": "3.1.1-canary.20+30f60175b",
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-canary.70+88fcaf47c",
27
- "@atlaspack/feature-flags": "2.14.1-canary.70+88fcaf47c",
28
- "@atlaspack/fs": "2.14.5-canary.2+88fcaf47c",
29
- "@atlaspack/logger": "2.14.5-canary.2+88fcaf47c",
30
- "@atlaspack/rust": "3.2.1-canary.2+88fcaf47c",
31
- "@atlaspack/utils": "2.14.5-canary.2+88fcaf47c"
26
+ "@atlaspack/build-cache": "2.13.3-canary.88+30f60175b",
27
+ "@atlaspack/feature-flags": "2.14.1-canary.88+30f60175b",
28
+ "@atlaspack/fs": "2.14.5-canary.20+30f60175b",
29
+ "@atlaspack/logger": "2.14.5-canary.20+30f60175b",
30
+ "@atlaspack/rust": "3.2.1-canary.20+30f60175b",
31
+ "@atlaspack/utils": "2.14.5-canary.20+30f60175b",
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": "88fcaf47c77a6b7a16003028dd65a4b7a1f19dd7"
40
+ "gitHead": "30f60175ba4d272c5fc193973c63bc298584775b"
40
41
  }
@@ -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
@@ -60,7 +60,21 @@ export class LmdbWrapper {
60
60
  await this.lmdb.put(key, buffer);
61
61
  }
62
62
 
63
- resetReadTxn() {}
63
+ *keys(): Iterable<string> {
64
+ const PAGE_SIZE = 10000000;
65
+
66
+ let currentKeys = this.lmdb.keysSync(0, PAGE_SIZE);
67
+ while (currentKeys.length > 0) {
68
+ for (const key of currentKeys) {
69
+ yield key;
70
+ }
71
+ currentKeys = this.lmdb.keysSync(currentKeys.length, PAGE_SIZE);
72
+ }
73
+ }
74
+
75
+ compact(targetPath: string) {
76
+ this.lmdb.compact(targetPath);
77
+ }
64
78
  }
65
79
 
66
80
  export function open(
@@ -225,13 +239,29 @@ export class LMDBLiteCache implements Cache {
225
239
  return this.store.delete(key);
226
240
  }
227
241
 
228
- refresh(): void {
229
- // Reset the read transaction for the store. This guarantees that
230
- // the next read will see the latest changes to the store.
231
- // Useful in scenarios where reads and writes are multi-threaded.
232
- // See https://github.com/kriszyp/lmdb-js#resetreadtxn-void
233
- this.store.resetReadTxn();
242
+ keys(): Iterable<string> {
243
+ return this.store.keys();
244
+ }
245
+
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'));
234
262
  }
263
+
264
+ refresh(): void {}
235
265
  }
236
266
 
237
267
  registerSerializableClass(
@@ -1,4 +1,6 @@
1
1
  // @flow
2
+
3
+ import * as fs from 'fs';
2
4
  import * as path from 'path';
3
5
  import {tmpdir} from 'os';
4
6
  import {LMDBLiteCache} from '../src/index';
@@ -8,13 +10,23 @@ import assert from 'assert';
8
10
  const cacheDir = path.join(tmpdir(), 'lmdb-lite-cache-tests');
9
11
 
10
12
  describe('LMDBLiteCache', () => {
13
+ let cache;
14
+
15
+ beforeEach(async () => {
16
+ await fs.promises.rm(cacheDir, {recursive: true, force: true});
17
+ });
18
+
19
+ afterEach(() => {
20
+ cache.getNativeRef().close();
21
+ });
22
+
11
23
  it('can be constructed', async () => {
12
- const cache = new LMDBLiteCache(cacheDir);
24
+ cache = new LMDBLiteCache(cacheDir);
13
25
  await cache.ensure();
14
26
  });
15
27
 
16
28
  it('can retrieve keys', async () => {
17
- const cache = new LMDBLiteCache(cacheDir);
29
+ cache = new LMDBLiteCache(cacheDir);
18
30
  await cache.ensure();
19
31
  await cache.setBlob('key', Buffer.from(serialize({value: 42})));
20
32
  const buffer = await cache.getBlob('key');
@@ -23,11 +35,35 @@ describe('LMDBLiteCache', () => {
23
35
  });
24
36
 
25
37
  it('can retrieve keys synchronously', async () => {
26
- const cache = new LMDBLiteCache(cacheDir);
38
+ cache = new LMDBLiteCache(path.join(cacheDir, 'retrieve_keys_test'));
27
39
  await cache.ensure();
28
- cache.setBlob('key', Buffer.from(serialize({value: 42})));
40
+ await cache.setBlob('key', Buffer.from(serialize({value: 42})));
29
41
  const buffer = cache.getBlobSync('key');
30
42
  const result = deserialize(buffer);
31
43
  assert.equal(result.value, 42);
32
44
  });
45
+
46
+ it('can iterate over keys', async () => {
47
+ cache = new LMDBLiteCache(path.join(cacheDir, 'keys_test'));
48
+ await cache.ensure();
49
+ await cache.setBlob('key1', Buffer.from(serialize({value: 42})));
50
+ await cache.setBlob('key2', Buffer.from(serialize({value: 43})));
51
+ const keys = cache.keys();
52
+ assert.deepEqual(Array.from(keys), ['key1', 'key2']);
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
+ });
33
69
  });