@atlaspack/cache 3.1.1-canary.3 → 3.1.1-canary.6

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.
@@ -80,6 +80,16 @@ class LmdbWrapper {
80
80
  const buffer = typeof value === 'string' ? Buffer.from(value) : value;
81
81
  await this.lmdb.put(key, buffer);
82
82
  }
83
+ *keys() {
84
+ const PAGE_SIZE = 10000000;
85
+ let currentKeys = this.lmdb.keysSync(0, PAGE_SIZE);
86
+ while (currentKeys.length > 0) {
87
+ for (const key of currentKeys) {
88
+ yield key;
89
+ }
90
+ currentKeys = this.lmdb.keysSync(currentKeys.length, PAGE_SIZE);
91
+ }
92
+ }
83
93
  resetReadTxn() {}
84
94
  }
85
95
  exports.LmdbWrapper = LmdbWrapper;
@@ -201,6 +211,9 @@ class LMDBLiteCache {
201
211
  }
202
212
  return this.store.delete(key);
203
213
  }
214
+ keys() {
215
+ return this.store.keys();
216
+ }
204
217
  refresh() {
205
218
  // Reset the read transaction for the store. This guarantees that
206
219
  // the next read will see the latest changes to the store.
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.3+2e90c9bd0",
4
+ "version": "3.1.1-canary.6+b5da6b749",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "type": "commonjs",
7
7
  "publishConfig": {
@@ -23,12 +23,12 @@
23
23
  "check-ts": "tsc --noEmit index.d.ts"
24
24
  },
25
25
  "dependencies": {
26
- "@atlaspack/build-cache": "2.13.3-canary.71+2e90c9bd0",
27
- "@atlaspack/feature-flags": "2.14.1-canary.71+2e90c9bd0",
28
- "@atlaspack/fs": "2.14.5-canary.3+2e90c9bd0",
29
- "@atlaspack/logger": "2.14.5-canary.3+2e90c9bd0",
30
- "@atlaspack/rust": "3.2.1-canary.3+2e90c9bd0",
31
- "@atlaspack/utils": "2.14.5-canary.3+2e90c9bd0"
26
+ "@atlaspack/build-cache": "2.13.3-canary.74+b5da6b749",
27
+ "@atlaspack/feature-flags": "2.14.1-canary.74+b5da6b749",
28
+ "@atlaspack/fs": "2.14.5-canary.6+b5da6b749",
29
+ "@atlaspack/logger": "2.14.5-canary.6+b5da6b749",
30
+ "@atlaspack/rust": "3.2.1-canary.6+b5da6b749",
31
+ "@atlaspack/utils": "2.14.5-canary.6+b5da6b749"
32
32
  },
33
33
  "devDependencies": {
34
34
  "idb": "^5.0.8"
@@ -36,5 +36,5 @@
36
36
  "browser": {
37
37
  "./src/IDBCache.js": "./src/IDBCache.browser.js"
38
38
  },
39
- "gitHead": "2e90c9bd07d7eb52645f9d84ccbb7f82685cbc8c"
39
+ "gitHead": "b5da6b749ddb23cfc212a640df1f07850da8307f"
40
40
  }
@@ -60,6 +60,18 @@ export class LmdbWrapper {
60
60
  await this.lmdb.put(key, buffer);
61
61
  }
62
62
 
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
+
63
75
  resetReadTxn() {}
64
76
  }
65
77
 
@@ -225,6 +237,10 @@ export class LMDBLiteCache implements Cache {
225
237
  return this.store.delete(key);
226
238
  }
227
239
 
240
+ keys(): Iterable<string> {
241
+ return this.store.keys();
242
+ }
243
+
228
244
  refresh(): void {
229
245
  // Reset the read transaction for the store. This guarantees that
230
246
  // the next read will see the latest changes to the store.
@@ -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,20 @@ 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
+ });
33
54
  });