Merge pull request #1939 from ether/fix/padList-cache-performance-hole

You don't need the padList if you want to alter it
pull/1940/head
John McLear 2013-10-13 12:22:07 -07:00
commit ef4e0d0eb0
1 changed files with 37 additions and 32 deletions

View File

@ -24,7 +24,9 @@ var Pad = require("../db/Pad").Pad;
var db = require("./DB").db; var db = require("./DB").db;
/** /**
* An Object containing all known Pads. Provides "get" and "set" functions, * A cache of all loaded Pads.
*
* Provides "get" and "set" functions,
* which should be used instead of indexing with brackets. These prepend a * which should be used instead of indexing with brackets. These prepend a
* colon to the key, to avoid conflicting with built-in Object methods or with * colon to the key, to avoid conflicting with built-in Object methods or with
* these functions themselves. * these functions themselves.
@ -37,44 +39,55 @@ var globalPads = {
set: function (name, value) set: function (name, value)
{ {
this[':'+name] = value; this[':'+name] = value;
if(padList.list.length == 0){ // If we haven't populated the padList.list yet
padList.init();
}
padList.addPad(name);
}, },
remove: function (name) { delete this[':'+name]; } remove: function (name) {
delete this[':'+name];
}
}; };
/**
* A cache of the list of all pads.
*
* Updated without db access as new pads are created/old ones removed.
*/
var padList = { var padList = {
list: [], list: [],
sorted : false, sorted : false,
init: function() initiated: false,
init: function(cb)
{ {
db.findKeys("pad:*", "*:*:*", function(err, dbData) db.findKeys("pad:*", "*:*:*", function(err, dbData)
{ {
if(ERR(err)) return; if(ERR(err, cb)) return;
if(dbData != null){ if(dbData != null){
dbData.forEach(function(val){ dbData.forEach(function(val){
padList.addPad(val.replace(/pad:/,""),false); padList.addPad(val.replace(/pad:/,""),false);
}); });
padList.initiated = true
cb && cb()
} }
}); });
return this; return this;
}, },
load: function(cb) {
if(this.initiated) cb && cb()
else this.init(cb)
},
/** /**
* Returns all pads in alphabetical order as array. * Returns all pads in alphabetical order as array.
*/ */
getPads: function(){ getPads: function(cb){
if(!this.sorted){ this.load(function() {
this.list=this.list.sort(); if(!padList.sorted){
this.sorted=true; padList.list = padList.list.sort();
} padList.sorted = true;
return this.list; }
cb && cb(padList.list);
})
}, },
addPad: function(name) addPad: function(name)
{ {
if(!this.initiated) return;
if(this.list.indexOf(name) == -1){ if(this.list.indexOf(name) == -1){
this.list.push(name); this.list.push(name);
this.sorted=false; this.sorted=false;
@ -82,7 +95,8 @@ var padList = {
}, },
removePad: function(name) removePad: function(name)
{ {
var index=this.list.indexOf(name); if(!this.initiated) return;
var index = this.list.indexOf(name);
if(index>-1){ if(index>-1){
this.list.splice(index,1); this.list.splice(index,1);
this.sorted=false; this.sorted=false;
@ -161,16 +175,11 @@ exports.getPad = function(id, text, callback)
} }
} }
exports.listAllPads = function(callback) exports.listAllPads = function(cb)
{ {
if(padList.list.length == 0){ // If we haven't populated the padList.list yet padList.getPads(function(list) {
padList.init(); cb && cb(null, {padIDs: list});
} });
if(callback != null){
callback(null,{padIDs: padList.getPads()});
}else{
return {padIDs: padList.getPads()};
}
} }
//checks if a pad exists //checks if a pad exists
@ -231,17 +240,13 @@ exports.isValidPadId = function(padId)
* Removes the pad from database and unloads it. * Removes the pad from database and unloads it.
*/ */
exports.removePad = function(padId){ exports.removePad = function(padId){
if(padList.list.length == 0){ // If we haven't populated the padList.list yet
padList.init();
}
db.remove("pad:"+padId); db.remove("pad:"+padId);
exports.unloadPad(padId); exports.unloadPad(padId);
padList.removePad(padId); padList.removePad(padId);
} }
//removes a pad from the array //removes a pad from the cache
exports.unloadPad = function(padId) exports.unloadPad = function(padId)
{ {
if(globalPads.get(padId)) globalPads.remove(padId);
globalPads.remove(padId);
} }