From 7c24757f30d4c36149209e9c6515cd3502f67d50 Mon Sep 17 00:00:00 2001 From: Nicolas Thouvenin Date: Wed, 14 Sep 2022 16:36:34 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20[expand]=20use=20mem?= =?UTF-8?q?ory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/analytics/src/expand.js | 27 ++++++++++++------------ packages/analytics/test/expand.js | 34 +------------------------------ 2 files changed, 14 insertions(+), 47 deletions(-) diff --git a/packages/analytics/src/expand.js b/packages/analytics/src/expand.js index 0e032216..ffd390b9 100644 --- a/packages/analytics/src/expand.js +++ b/packages/analytics/src/expand.js @@ -2,7 +2,6 @@ import { resolve as resolvePath } from 'path'; import { tmpdir } from 'os'; import get from 'lodash.get'; import set from 'lodash.set'; -import { createStore } from '@ezs/store'; import cacache from 'cacache'; import each from 'async-each-series'; import makeDir from 'make-dir'; @@ -39,8 +38,9 @@ async function mergeWith(data, feed) { const { id, value } = data; const path = this.getParam('path'); try { - const obj = await store.cut(id); - if (obj === null) { + const obj = store[id]; + delete store[id]; + if (obj === undefined || obj === null) { throw new Error('id was corrupted'); } const source = get(obj, path); @@ -51,7 +51,8 @@ async function mergeWith(data, feed) { set(obj, path, value); return feed.send(obj); } catch (e) { - return feed.stop(e); + // avoid to break the pipe + return feed.send(e); } } @@ -68,12 +69,13 @@ async function drainWith(data, feed) { async (cur, next) => { let obj; try { - obj = await store.get(cur); + obj = store[cur]; + delete store[cur]; } catch (e) { feed.write(e); } - if (obj === null) { - feed.stop(new Error(`Unable to find ${cur} in the store ${store.id()}`)); + if (obj === undefined || obj === null) { + feed.stop(new Error(`Unable to find ${cur} in the store`)); } else { feed.write(obj); } @@ -138,9 +140,7 @@ export default async function expand(data, feed) { const cacheName = this.getParam('cacheName'); if (!this.store) { - const location = this.getParam('location'); - this.store = createStore(ezs, 'expand', location); - await this.store.reset(); + this.store = {} this.flows = []; } if (!this.createStatements) { @@ -203,8 +203,7 @@ export default async function expand(data, feed) { this.flows.push(feed.flow(strm)); } await Promise.all(this.flows); - await this.store.close(); - + delete this.store; return feed.close(); } const value = get(data, path); @@ -221,7 +220,7 @@ export default async function expand(data, feed) { const id = this.getIndex().toString().padStart(20, '0'); const size = Number(this.getParam('size', 1)); - await this.store.put(id, data); + this.store[id] = data; this.stack[this.bufferID][id] = true; this.buffer.push(core(id, value)); @@ -232,7 +231,7 @@ export default async function expand(data, feed) { return feed.end(); } catch (e) { if (this.store) { - this.store.close(); + delete this.store; } return feed.stop(e); } diff --git a/packages/analytics/test/expand.js b/packages/analytics/test/expand.js index 550efd23..f76de5ce 100644 --- a/packages/analytics/test/expand.js +++ b/packages/analytics/test/expand.js @@ -246,38 +246,6 @@ test('with no script', (done) => { done(new Error('Error is the right behavior')); }); }); -test('with wrong location ', (done) => { - ezs.use(statements); - const input = [ - { a: 1, b: 'a' }, - { a: 2, b: 'b' }, - { a: 3, b: 'c' }, - { a: 4, b: 'd' }, - { a: 5, b: 'e' }, - { a: 6, b: 'f' }, - ]; - const script = ` - [use] - plugin = analytics - - [assign] - path = value - value = get('value').toUpper() - `; - from(input) - .pipe(ezs('expand', { path: 'b', script, location: '/no/where' })) - .pipe(ezs.catch()) - .on('error', (e) => { - expect(e.message).toEqual(expect.stringContaining('EACCES: permission denied')); - done(); - }) - .on('data', () => { - done(new Error('Error is the right behavior')); - }) - .on('end', () => { - done(new Error('Error is the right behavior')); - }); -}); test('with no path', (done) => { ezs.use(statements); @@ -423,7 +391,7 @@ test('with a script that loses the identifier', (done) => { .pipe(ezs('expand', { path: 'b', script })) .pipe(ezs.catch()) .on('error', (e) => { - expect(e.message).toEqual(expect.stringContaining('key cannot be `null`')); + expect(e.message).toEqual(expect.stringContaining('id was corrupted')); done(); }) .on('end', () => {