From 7d21651fc013154d9704e22e89be800f968f3c08 Mon Sep 17 00:00:00 2001 From: tokebe <43009413+tokebe@users.noreply.github.com> Date: Fri, 6 Aug 2021 10:50:32 -0400 Subject: [PATCH 1/4] fix: correctly cache semanticType --- src/cache_handler.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cache_handler.js b/src/cache_handler.js index beceaacf..21aa20fc 100644 --- a/src/cache_handler.js +++ b/src/cache_handler.js @@ -6,9 +6,8 @@ module.exports = class { constructor(qEdges, caching, logs = []) { this.qEdges = qEdges; this.logs = logs; - this.cacheEnabled = (caching === 'false') ? - false : - (!(process.env.REDIS_HOST === undefined) && !(process.env.REDIS_PORT === undefined)); + this.cacheEnabled = + caching === 'false' ? false : !(process.env.REDIS_HOST === undefined) && !(process.env.REDIS_PORT === undefined); this.logs.push( new LogEntry('DEBUG', null, `REDIS cache is ${this.cacheEnabled === true ? '' : 'not'} enabled.`).getLog(), ); @@ -59,6 +58,7 @@ module.exports = class { curies: record.$input.obj[0].curies, label: record.$input.obj[0].label, primaryID: record.$input.obj[0].primaryID, + semanticType: record.$input.obj[0].semanticType, }, ], }, @@ -70,6 +70,7 @@ module.exports = class { curies: record.$output.obj[0].curies, label: record.$output.obj[0].label, primaryID: record.$output.obj[0].primaryID, + semanticType: record.$input.obj[0].semanticType, }, ], }, From aa4a529a597efc5c33fddbed6796527e2ba7438d Mon Sep 17 00:00:00 2001 From: tokebe <43009413+tokebe@users.noreply.github.com> Date: Fri, 6 Aug 2021 11:51:24 -0400 Subject: [PATCH 2/4] fix: input->output for output node --- src/cache_handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache_handler.js b/src/cache_handler.js index 21aa20fc..f0da38b3 100644 --- a/src/cache_handler.js +++ b/src/cache_handler.js @@ -70,7 +70,7 @@ module.exports = class { curies: record.$output.obj[0].curies, label: record.$output.obj[0].label, primaryID: record.$output.obj[0].primaryID, - semanticType: record.$input.obj[0].semanticType, + semanticType: record.$output.obj[0].semanticType, }, ], }, From 344aabd2037e172bd3f3970db1e31f0ac7ce00c6 Mon Sep 17 00:00:00 2001 From: tokebe <43009413+tokebe@users.noreply.github.com> Date: Fri, 6 Aug 2021 11:52:40 -0400 Subject: [PATCH 3/4] fix: cache extra properties just in case --- src/cache_handler.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cache_handler.js b/src/cache_handler.js index f0da38b3..d99e89d7 100644 --- a/src/cache_handler.js +++ b/src/cache_handler.js @@ -59,6 +59,8 @@ module.exports = class { label: record.$input.obj[0].label, primaryID: record.$input.obj[0].primaryID, semanticType: record.$input.obj[0].semanticType, + semanticTypes: record.$input.obj[0].semanticTypes, + attributes: record.$input.obj[0].attributes, }, ], }, @@ -71,6 +73,8 @@ module.exports = class { label: record.$output.obj[0].label, primaryID: record.$output.obj[0].primaryID, semanticType: record.$output.obj[0].semanticType, + semanticTypes: record.$output.obj[0].semanticTypes, + attributes: record.$output.obj[0].attributes, }, ], }, From b7a71c49c7e29f124b87b6b73e2eed747844aa13 Mon Sep 17 00:00:00 2001 From: tokebe <43009413+tokebe@users.noreply.github.com> Date: Fri, 6 Aug 2021 16:53:19 -0400 Subject: [PATCH 4/4] fix: clone records recursively for caching --- src/cache_handler.js | 73 ++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/src/cache_handler.js b/src/cache_handler.js index d99e89d7..23d9371f 100644 --- a/src/cache_handler.js +++ b/src/cache_handler.js @@ -1,6 +1,7 @@ const redisClient = require('./redis-client'); const debug = require('debug')('bte:biothings-explorer-trapi:cache_handler'); const LogEntry = require('./log_entry'); +const _ = require('lodash'); module.exports = class { constructor(qEdges, caching, logs = []) { @@ -40,50 +41,36 @@ module.exports = class { } _copyRecord(record) { - const new_record = { - $edge_metadata: { - input_id: record.$edge_metadata.input_id, - output_id: record.$edge_metadata.output_id, - output_type: record.$edge_metadata.output_type, - input_type: record.$edge_metadata.input_type, - predicate: record.$edge_metadata.predicate, - source: record.$edge_metadata.source, - api_name: record.$edge_metadata.api_name, - }, - $input: { - original: record.$input.original, - obj: [ - { - dbIDs: record.$input.obj[0].dbIDs, - curies: record.$input.obj[0].curies, - label: record.$input.obj[0].label, - primaryID: record.$input.obj[0].primaryID, - semanticType: record.$input.obj[0].semanticType, - semanticTypes: record.$input.obj[0].semanticTypes, - attributes: record.$input.obj[0].attributes, - }, - ], - }, - $output: { - original: record.$output.original, - obj: [ - { - dbIDs: record.$output.obj[0].dbIDs, - curies: record.$output.obj[0].curies, - label: record.$output.obj[0].label, - primaryID: record.$output.obj[0].primaryID, - semanticType: record.$output.obj[0].semanticType, - semanticTypes: record.$output.obj[0].semanticTypes, - attributes: record.$output.obj[0].attributes, - }, - ], - }, + let new_record = {}; + + const freezeClone = (obj, clone) => { + // 'freeze' getters on this layer + Object.entries(Object.getOwnPropertyDescriptors(Object.getPrototypeOf(obj))) + .filter(([key, descriptor]) => typeof descriptor.get === 'function') + .map(([key]) => key) + .forEach((key) => { + clone[key] = + typeof obj[key] === 'object' ? (clone[key] = Array.isArray(obj[key]) ? [] : {}) : _.clone(obj[key]); + }); + // look for sublayers + Object.keys(obj).forEach((key) => { + if (typeof obj[key] === 'object') { + clone[key] = Array.isArray(obj[key]) ? [] : {}; + freezeClone(obj[key], clone[key]); + } else { + clone[key] = obj[key]; + } + }); + // check for sublayers of getter returns + Object.keys(clone) + .filter((key) => typeof Object.getOwnPropertyDescriptor(obj, key) === 'undefined') + .forEach((key) => { + if (typeof clone[key] === 'object') { + freezeClone(obj[key], clone[key]); + } + }); }; - Object.keys(record).map((k) => { - if (!['$edge_metadata', '$input', '$output'].includes(k)) { - new_record[k] = record[k]; - } - }); + freezeClone(record, new_record); return new_record; }