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

@ -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) {