Windshaft-cartodb/lib/cartodb/carto_data.js

218 lines
6.5 KiB
JavaScript
Raw Normal View History

2011-09-05 07:00:41 +08:00
/**
* User: simon
* Date: 30/08/2011
* Time: 21:10
* Desc: CartoDB helper.
* Retrieves dbname (based on subdomain/username)
* and geometry type from the redis stores of cartodb
*/
var RedisPool = require("./redis_pool")
, _ = require('underscore')
, Step = require('step');
module.exports = function() {
var redis_pool = new RedisPool();
var me = {
user_metadata_db: 5,
table_metadata_db: 0,
user_key: "rails:users:<%= username %>",
2011-11-22 11:46:59 +08:00
map_key: "rails:users:<%= username %>:map_key",
2011-09-05 07:00:41 +08:00
table_key: "rails:<%= database_name %>:<%= table_name %>"
};
/**
* Get the database name for this particular subdomain/username
*
* @param req - standard express req object. importantly contains host information
* @param callback
*/
me.getDatabase = function(req, callback) {
// strip subdomain from header host
var username = req.headers.host.split('.')[0]
var redisKey = _.template(this.user_key, {username: username});
this.retrieve(this.user_metadata_db, redisKey, 'database_name', callback);
};
/**
* Get the user id for this particular subdomain/username
*
* @param req - standard express req object. importantly contains host information
* @param callback
*/
me.getId= function(req, callback) {
// strip subdomain from header host
2011-11-22 11:46:59 +08:00
var username = req.headers.host.split('.')[0];
2011-09-05 07:00:41 +08:00
var redisKey = _.template(this.user_key, {username: username});
this.retrieve(this.user_metadata_db, redisKey, 'id', callback);
};
2011-09-22 04:33:25 +08:00
/**
* Get the user map key for this particular subdomain/username
*
* @param req - standard express req object. importantly contains host information
* @param callback
*/
2011-11-22 11:46:59 +08:00
me.checkMapKey = function(req, callback) {
2011-09-22 04:33:25 +08:00
// strip subdomain from header host
2011-11-22 11:46:59 +08:00
var username = req.headers.host.split('.')[0];
var redisKey = _.template(this.map_key, {username: username});
this.inSet(this.user_metadata_db, redisKey, req.query.map_key, callback);
2011-09-22 04:33:25 +08:00
};
/**
* Get privacy for cartodb table
*
* @param req - standard req object. Importantly contains table and host information
* @param callback - is the table private or not?
*/
me.authorize= function(req, callback) {
var that = this;
Step(
function(){
2011-11-22 11:46:59 +08:00
that.checkMapKey(req, this);
2011-09-22 04:33:25 +08:00
},
2011-11-22 11:46:59 +08:00
function checkIfInternal(err, check_result){
2011-09-22 04:33:25 +08:00
if (err) throw err;
2011-11-22 11:46:59 +08:00
if (check_result === 1){
2011-09-22 04:33:25 +08:00
callback(err, true); // Internal access so early exit with access.
} else {
return true; // continue to check if the table is public/private
}
},
function (err, data){
if (err) throw err;
that.getDatabase(req, this);
},
function(err, data){
if (err) throw err;
var redisKey = _.template(that.table_key, {database_name: data, table_name: req.params.table});
that.retrieve(that.table_metadata_db, redisKey, 'privacy', this);
},
function(err, data){
if (err) throw err;
callback(err, data);
}
);
};
2011-09-05 07:00:41 +08:00
/**
* Get the geometry type for this particular table;
* @param req - standard req object. Importantly contains table and host information
* @param callback
*/
me.getGeometryType = function(req, callback){
var that = this;
Step(
function(){
that.getDatabase(req, this)
},
function(err, data){
if (err) throw err;
var redisKey = _.template(that.table_key, {database_name: data, table_name: req.params.table});
that.retrieve(that.table_metadata_db, redisKey, 'the_geom_type', this);
},
function(err, data){
if (err) throw err;
callback(err, data);
}
);
};
me.getInfowindow = function(req, callback){
var that = this;
Step(
function(){
that.getDatabase(req, this);
},
2011-09-14 11:51:51 +08:00
function(err, data) {
2011-09-05 07:00:41 +08:00
if (err) throw err;
var redisKey = _.template(that.table_key, {database_name: data, table_name: req.params.table});
2011-09-05 07:00:41 +08:00
that.retrieve(that.table_metadata_db, redisKey, 'infowindow', this);
},
function(err, data){
if (err) throw err;
callback(err, data);
}
);
};
me.getMapMetadata = function(req, callback){
var that = this;
Step(
function(){
that.getDatabase(req, this);
},
function(err, data) {
if (err) throw err;
var redisKey = _.template(that.table_key, {database_name: data, table_name: req.params.table});
that.retrieve(that.table_metadata_db, redisKey, 'map_metadata', this);
},
function(err, data){
if (err) throw err;
callback(err, data);
}
);
};
2011-11-22 11:46:59 +08:00
// Redis Hash lookup
me.retrieve = function(db, redisKey, hashKey, callback) {
this.redisCmd(db,'HGET',[redisKey, hashKey], callback);
};
// Redis Set member check
me.inSet = function(db, setKey, member, callback) {
this.redisCmd(db,'SISMEMBER',[setKey, member], callback);
};
2011-09-05 07:00:41 +08:00
/**
2011-11-22 11:46:59 +08:00
* Use Redis
2011-09-05 07:00:41 +08:00
*
2011-11-22 11:46:59 +08:00
* @param db - redis database number
* @param redisFunc - the redis function to execute
* @param redisArgs - the arguments for the redis function in an array
* @param callback - function to pass results too.
2011-09-05 07:00:41 +08:00
*/
2011-11-22 11:46:59 +08:00
me.redisCmd = function(db, redisFunc, redisArgs, callback) {
2011-09-05 07:00:41 +08:00
var redisClient;
Step(
function getRedisClient() {
redis_pool.acquire(db, this);
},
2011-11-22 11:46:59 +08:00
function executeQuery(err, data) {
2011-09-05 07:00:41 +08:00
redisClient = data;
2011-11-22 11:46:59 +08:00
redisArgs.push(this);
redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs);
2011-09-05 07:00:41 +08:00
},
function releaseRedisClient(err, data) {
if (err) throw err;
redis_pool.release(db, redisClient);
callback(err, data);
}
);
};
return me;
}();