Windshaft-cartodb/lib/models/mapconfig/provider/map-store-provider.js

83 lines
2.5 KiB
JavaScript
Raw Normal View History

'use strict';
const BaseMapConfigProvider = require('./base-mapconfig-adapter');
2018-05-30 23:22:02 +08:00
const dot = require('dot');
2018-05-31 02:08:35 +08:00
// Configure bases for cache keys suitable for string interpolation
const baseKey = '{{=it.dbname}}:{{=it.token}}';
const rendererKey = baseKey + ':{{=it.dbuser}}:{{=it.format}}:{{=it.layer}}:{{=it.scale_factor}}';
const baseKeyTpl = dot.template(baseKey);
const rendererKeyTpl = dot.template(rendererKey);
module.exports = class MapStoreMapConfigProvider extends BaseMapConfigProvider {
2018-05-31 02:08:35 +08:00
/**
* @param {MapStore} mapStore
* @param {String} user
* @param {UserLimitsBackend} userLimitsBackend
* @param {Object} params
* @constructor
* @type {MapStoreMapConfigProvider}
*/
constructor (mapStore, user, userLimitsBackend, pgConnection, affectedTablesCache, params) {
super();
2018-05-31 02:08:35 +08:00
this.mapStore = mapStore;
this.user = user;
this.userLimitsBackend = userLimitsBackend;
this.pgConnection = pgConnection;
this.affectedTablesCache = affectedTablesCache;
this.params = params;
2018-05-31 02:08:35 +08:00
this.token = params.token;
this.cacheBuster = params.cache_buster || 0;
this.mapConfig = null;
this.context = null;
}
2018-05-31 02:08:35 +08:00
getMapConfig (callback) {
if (this.mapConfig !== null) {
return callback(null, this.mapConfig, this.params, this.context);
2015-07-11 01:10:55 +08:00
}
2018-05-30 23:09:01 +08:00
2018-05-31 02:08:35 +08:00
const context = {};
2018-05-30 23:09:01 +08:00
2018-05-31 02:08:35 +08:00
this.userLimitsBackend.getRenderLimits(this.user, this.params.api_key, (err, renderLimits) => {
2018-05-30 23:09:01 +08:00
if (err) {
return callback(err);
}
2018-05-31 02:08:35 +08:00
context.limits = renderLimits;
2018-05-30 23:09:01 +08:00
2018-05-31 02:08:35 +08:00
this.mapStore.load(this.token, (err, mapConfig) => {
if (err) {
return callback(err);
}
2018-05-31 02:08:35 +08:00
this.mapConfig = mapConfig;
this.context = context;
2018-05-31 02:08:35 +08:00
return callback(null, mapConfig, this.params, context);
});
});
}
2018-05-31 02:08:35 +08:00
getKey () {
return this.createKey(false);
}
2018-05-31 02:08:35 +08:00
getCacheBuster () {
return this.cacheBuster;
}
2018-05-31 02:08:35 +08:00
filter (key) {
const regex = new RegExp('^' + this.createKey(true) + '.*');
return key && key.match(regex);
}
2018-05-31 02:08:35 +08:00
createKey (base) {
const { dbname = '', token = '', dbuser = '', format = '', layer = '', scale_factor = 1 } = this.params;
const tplValues = { dbname, token, dbuser, format, layer, scale_factor };
2018-05-31 02:08:35 +08:00
return (base) ? baseKeyTpl(tplValues) : rendererKeyTpl(tplValues);
}
2018-04-06 18:59:53 +08:00
};