Cache channel ID for quicker loads/saves

This commit is contained in:
Calvin Montgomery 2016-09-26 22:20:58 -07:00
parent b4b23f748f
commit e1120455b2
8 changed files with 47 additions and 85 deletions

View file

@ -2,7 +2,7 @@
"author": "Calvin Montgomery",
"name": "CyTube",
"description": "Online media synchronizer and chat",
"version": "3.22.1",
"version": "3.22.2",
"repository": {
"url": "http://github.com/calzoneman/sync"
},

View file

@ -9,20 +9,20 @@ export function init() {
CHANNEL_STORE = loadChannelStore();
}
export function load(channelName) {
export function load(id, channelName) {
if (CHANNEL_STORE === null) {
return Promise.reject(new Error('ChannelStore not initialized yet'));
}
return CHANNEL_STORE.load(channelName);
return CHANNEL_STORE.load(id, channelName);
}
export function save(channelName, data) {
export function save(id, channelName, data) {
if (CHANNEL_STORE === null) {
return Promise.reject(new Error('ChannelStore not initialized yet'));
}
return CHANNEL_STORE.save(channelName, data);
return CHANNEL_STORE.save(id, channelName, data);
}
function loadChannelStore() {

View file

@ -34,14 +34,13 @@ function buildUpdateQuery(numEntries) {
}
export class DatabaseStore {
load(channelName) {
return queryAsync(QUERY_CHANNEL_ID_FOR_NAME, [channelName]).then((rows) => {
if (rows.length === 0) {
throw new ChannelNotFoundError(`Channel does not exist: "${channelName}"`);
}
load(id, channelName) {
if (!id || id === 0) {
return Promise.reject(new Error(`Cannot load state for [${channelName}]: ` +
`id was passed as [${id}]`));
}
return queryAsync(QUERY_CHANNEL_DATA, [rows[0].id]);
}).then(rows => {
return queryAsync(QUERY_CHANNEL_DATA, [id]).then(rows => {
const data = {};
rows.forEach(row => {
try {
@ -56,36 +55,34 @@ export class DatabaseStore {
});
}
save(channelName, data) {
return queryAsync(QUERY_CHANNEL_ID_FOR_NAME, [channelName]).then((rows) => {
if (rows.length === 0) {
throw new ChannelNotFoundError(`Channel does not exist: "${channelName}"`);
}
save(id, channelName, data) {
if (!id || id === 0) {
return Promise.reject(new Error(`Cannot save state for [${channelName}]: ` +
`id was passed as [${id}]`));
}
let totalSize = 0;
let rowCount = 0;
const id = rows[0].id;
const substitutions = [];
for (const key in data) {
if (typeof data[key] === 'undefined') {
continue;
}
rowCount++;
const value = JSON.stringify(data[key]);
totalSize += value.length;
substitutions.push(id);
substitutions.push(key);
substitutions.push(value);
let totalSize = 0;
let rowCount = 0;
const substitutions = [];
for (const key in data) {
if (typeof data[key] === 'undefined') {
continue;
}
rowCount++;
const value = JSON.stringify(data[key]);
totalSize += value.length;
substitutions.push(id);
substitutions.push(key);
substitutions.push(value);
}
if (totalSize > SIZE_LIMIT) {
throw new ChannelStateSizeError('Channel state size is too large', {
limit: SIZE_LIMIT,
actual: totalSize
});
}
if (totalSize > SIZE_LIMIT) {
throw new ChannelStateSizeError('Channel state size is too large', {
limit: SIZE_LIMIT,
actual: totalSize
});
}
return queryAsync(buildUpdateQuery(rowCount), substitutions);
});
return queryAsync(buildUpdateQuery(rowCount), substitutions);
}
}

View file

@ -16,7 +16,7 @@ export class FileStore {
return path.join(CHANDUMP_DIR, channelName);
}
load(channelName) {
load(id, channelName) {
const filename = this.filenameForChannel(channelName);
return statAsync(filename).then(stats => {
if (stats.size > SIZE_LIMIT) {
@ -36,7 +36,7 @@ export class FileStore {
});
}
save(channelName, data) {
save(id, channelName, data) {
const filename = this.filenameForChannel(channelName);
const fileContents = new Buffer(JSON.stringify(data), 'utf8');
if (fileContents.length > SIZE_LIMIT) {

View file

@ -84,6 +84,7 @@ function Channel(name) {
this.users = [];
this.refCounter = new ReferenceCounter(this);
this.flags = 0;
this.id = 0;
this.broadcastUsercount = throttle(() => {
this.broadcastAll("usercount", this.users.length);
}, USERCOUNT_THROTTLE);
@ -206,7 +207,7 @@ Channel.prototype.loadState = function () {
self.setFlag(Flags.C_READY | Flags.C_ERROR);
}
ChannelStore.load(this.uniqueName).then(data => {
ChannelStore.load(this.id, this.uniqueName).then(data => {
Object.keys(this.modules).forEach(m => {
try {
this.modules[m].load(data);
@ -260,7 +261,8 @@ Channel.prototype.saveState = function () {
this.modules[m].save(data);
});
return ChannelStore.save(this.uniqueName, data).catch(ChannelStateSizeError, err => {
return ChannelStore.save(this.id, this.uniqueName, data)
.catch(ChannelStateSizeError, err => {
this.users.forEach(u => {
if (u.account.effectiveRank >= 2) {
u.socket.emit("warnLargeChandump", {

View file

@ -80,6 +80,10 @@ module.exports.query = function (query, sub, callback) {
Metrics.stopTimer(timer);
}
if (process.env.SHOW_SQL) {
console.log(query);
}
if (sub) {
conn.query(query, sub, cback);
} else {

View file

@ -232,48 +232,6 @@ module.exports = {
});
},
/**
* Verify an auth string of the form name:hash
*/
verifyAuth: function (auth, callback) {
if (typeof callback !== "function") {
return;
}
if (typeof auth !== "string") {
callback("Invalid auth string", null);
return;
}
var split = auth.split(":");
if (split.length !== 2) {
callback("Invalid auth string", null);
return;
}
var name = split[0];
var hash = split[1];
db.query("SELECT name,password,global_rank FROM `users` WHERE " +
"name=? and password=?", [name, hash],
function (err, rows) {
if (err) {
callback(err, null);
return;
}
if (rows.length === 0) {
callback("Auth string does not match an existing user", null);
return;
}
callback(null, {
name: rows[0].name,
hash: rows[0].password,
global_rank: rows[0].global_rank
});
});
},
/**
* Change a user's password
*/

View file

@ -273,6 +273,7 @@ module.exports = {
// than the database has stored. Update accordingly.
chan.name = res[0].name;
chan.uniqueName = chan.name.toLowerCase();
chan.id = res[0].id;
chan.setFlag(Flags.C_REGISTERED);
chan.logger.log("[init] Loaded channel from database");
callback(null, true);