From 7d2d585c54270aa0171ed5593c8fadbab64e46ed Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 13 Feb 2013 16:43:04 +0100 Subject: [PATCH] Check CDB_QueryTable response before saving into cache Also add pid to /cachestatus (#83) --- NEWS.md | 2 ++ app/controllers/app.js | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0212d516..fe4aed50 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ----- * Fix skipfields use with SHP output format (#81) * Fix Content-Disposition for error responses (#82) +* Add pid to /cachestatus (#83) +* Check CDB_QueryTable response before saving into cache (#83) 1.3.4 (21/01/13) ----- diff --git a/app/controllers/app.js b/app/controllers/app.js index 61c6ea52..ef7eb908 100755 --- a/app/controllers/app.js +++ b/app/controllers/app.js @@ -171,7 +171,7 @@ function handleQuery(req, res) { // get all the tables from Cache or SQL if (!_.isNull(tableCache[sql_md5]) && !_.isUndefined(tableCache[sql_md5])){ tableCache[sql_md5].hits++; - return true; + return false; } else { pg.query("SELECT CDB_QueryTables($quotesql$" + sql + "$quotesql$)", this); } @@ -180,10 +180,20 @@ function handleQuery(req, res) { if (err) throw err; // store explain result in local Cache - if (_.isUndefined(tableCache[sql_md5])){ - tableCache[sql_md5] = result; - tableCache[sql_md5].may_write = queryMayWrite(sql); - tableCache[sql_md5].hits = 1; //initialise hit counter + if ( result !== false ) { + + if ( result.rowCount === 1 ) { + tableCache[sql_md5] = { + affected_tables: result.rows[0].cdb_querytables, + // check if query may possibly write + may_write: queryMayWrite(sql), + // initialise hit counter + hits: 1 + }; + } else { + console.log("[ERROR] Unexpected result from CDB_QueryTables: "); + console.dir(result); + } } // TODO: refactor formats to external object @@ -301,8 +311,7 @@ function handleCacheStatus(req, res){ var tableCacheValues = _.values(tableCache); var totalExplainHits = _.reduce(tableCacheValues, function(memo, res) { return memo + res.hits}, 0); var totalExplainKeys = tableCacheValues.length; - - res.send({explain: {hits: totalExplainHits, keys : totalExplainKeys }}); + res.send({explain: {pid: process.pid, hits: totalExplainHits, keys : totalExplainKeys }}); } // helper functions @@ -813,11 +822,11 @@ function setCrossDomain(res){ res.header("Access-Control-Allow-Headers", "X-Requested-With, X-Prototype-Version, X-CSRF-Token"); } -function generateCacheKey(database,tables,is_authenticated){ - if ( is_authenticated && tables.may_write ) { +function generateCacheKey(database, query_info, is_authenticated){ + if ( is_authenticated && query_info.may_write ) { return "NONE"; } else { - return database + ":" + tables.rows[0].cdb_querytables.split(/^\{(.*)\}$/)[1]; + return database + ":" + query_info.affected_tables.split(/^\{(.*)\}$/)[1]; } }