Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from stelcheck/vaults/redis
redis vault
- Loading branch information
Showing
12 changed files
with
1,395 additions
and
2,335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Archivist bindings for the RedisVault API | ||
|
||
function Archive(vault) { | ||
this.vault = vault; | ||
} | ||
|
||
|
||
module.exports = Archive; | ||
|
||
|
||
Archive.prototype.get = function (api, value, cb) { | ||
this.vault.get(api.createKey(value.topic, value.index), function (error, data) { | ||
if (error) { | ||
return cb(error); | ||
} | ||
|
||
if (data === undefined) { | ||
return cb(); | ||
} | ||
|
||
try { | ||
api.deserialize(data, value); | ||
} catch (error) { | ||
return cb(error); | ||
} | ||
|
||
return cb(); | ||
}); | ||
}; | ||
|
||
|
||
Archive.prototype.set = function (api, value, cb) { | ||
this.vault.set(api.createKey(value.topic, value.index), api.serialize(value), value.expirationTime, cb); | ||
}; | ||
|
||
|
||
Archive.prototype.touch = function (api, value, cb) { | ||
this.vault.touch(api.createKey(value.topic, value.index), value.expirationTime, cb); | ||
}; | ||
|
||
|
||
Archive.prototype.del = function (api, value, cb) { | ||
this.vault.del(api.createKey(value.topic, value.index), cb); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
exports.serialize = function (value) { | ||
// throws exceptions on failure | ||
|
||
// the header is: | ||
// - 2 bytes to indicate JSON-length in bytes (65535 max header length) | ||
// - JSON containing: { mediaType: "a/b" } | ||
// - a single byte "\n" delimiter | ||
|
||
// serialize the value to a buffer | ||
|
||
var data = value.setEncoding(['buffer']).data; | ||
|
||
// create the meta data | ||
|
||
var meta = JSON.stringify({ | ||
mediaType: value.mediaType | ||
}); | ||
|
||
// create a buffer that will fit both the header and the value | ||
|
||
var jsonSize = Buffer.byteLength(meta); | ||
var headerSize = 2 + jsonSize + 1; | ||
|
||
var output = new Buffer(headerSize + data.length); | ||
|
||
// write the meta JSON length | ||
|
||
output.writeUInt16BE(jsonSize, 0); | ||
|
||
// write the meta JSON and trailing \n | ||
|
||
output.write(meta + '\n', 2, jsonSize + 1, 'utf8'); | ||
|
||
// append the value | ||
|
||
data.copy(output, headerSize); | ||
|
||
return output; | ||
}; | ||
|
||
|
||
exports.deserialize = function (data, value) { | ||
var jsonSize = data.readUInt16BE(0); | ||
var meta = JSON.parse(data.toString('utf8', 2, 2 + jsonSize)); | ||
|
||
data = data.slice(2 + jsonSize + 1); | ||
|
||
value.setDataFromVault(meta.mediaType, data, 'buffer'); | ||
}; | ||
|
||
|
||
exports.createKey = function (topic, index) { | ||
// eg: weapons/actorId:123/bag:main | ||
// eg: weapons/guildId:123 | ||
|
||
var key = topic; | ||
var props, i; | ||
|
||
if (index) { | ||
props = Object.keys(index); | ||
props.sort(); | ||
|
||
for (i = 0; i < props.length; i++) { | ||
key += '/' + props[i] + ':' + index[props[i]]; | ||
} | ||
} | ||
|
||
return key; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// based on node-redis, this vault does not support sharding at this time | ||
// | ||
// key format: string | ||
// | ||
// references: | ||
// ----------- | ||
// node-redis: https://github.com/mranney/node_redis | ||
|
||
var requirePeer = require('codependency').get('mage'); | ||
var redis = requirePeer('redis'); | ||
var Archive = require('./Archive'); | ||
|
||
|
||
exports.defaultTopicApi = require('./defaultTopicApi'); | ||
|
||
|
||
// Vault wrapper around node-redis | ||
|
||
function RedisVault(name, logger) { | ||
// required exposed properties | ||
|
||
this.name = name; // the unique vault name | ||
this.archive = new Archive(this); // archivist bindings | ||
|
||
this.client = null; // node-redis instance | ||
this.logger = logger; | ||
} | ||
|
||
|
||
exports.create = function (name, logger) { | ||
return new RedisVault(name, logger); | ||
}; | ||
|
||
|
||
RedisVault.prototype.setup = function (cfg, cb) { | ||
var cfgOptions = cfg.options || {}; | ||
|
||
// this option-alias is really just to silence jshint | ||
|
||
var returnBuffers = 'return_buffers'; | ||
|
||
if (!cfgOptions.hasOwnProperty(returnBuffers)) { | ||
cfgOptions[returnBuffers] = true; | ||
} | ||
|
||
this.client = redis.createClient(cfg.port, cfg.host, cfgOptions); | ||
this.keyPrefix = cfg.prefix || null; | ||
this.db = cfg.db || null; | ||
|
||
var logger = this.logger; | ||
var name = this.name; | ||
|
||
this.client.on('error', function (error) { | ||
logger.emergency('Error on Redis server (name: ' + name + '):', error); | ||
}); | ||
|
||
setImmediate(cb); | ||
}; | ||
|
||
RedisVault.prototype.open = function (cb) { | ||
if (!this.db) { | ||
return setImmediate(cb); | ||
} | ||
|
||
this.client.select(this.db, cb); | ||
}; | ||
|
||
RedisVault.prototype.close = function () { | ||
this.logger.verbose('Closing vault:', this.name); | ||
|
||
if (this.client) { | ||
this.client.quit(); | ||
this.client = null; | ||
} | ||
}; | ||
|
||
|
||
RedisVault.prototype._prefix = function (key) { | ||
return this.keyPrefix ? this.keyPrefix + key : key; | ||
}; | ||
|
||
/* unprefix will be used once we support readMany | ||
RedisVault.prototype._unprefix = function (key) { | ||
if (!this.keyPrefix) { | ||
return key; | ||
} | ||
var len = this.keyPrefix.length; | ||
if (key.substr(0, len) !== this.keyPrefix) { | ||
throw new Error('Could not unprefix key "' + key + '" with prefix "' + this.keyPrefix + '"'); | ||
} | ||
return key.substr(len); | ||
}; | ||
*/ | ||
|
||
RedisVault.prototype.get = function (key, cb) { | ||
key = this._prefix(key); | ||
|
||
this.logger.verbose('get:', key); | ||
|
||
this.client.get([key], function (error, data) { | ||
if (error) { | ||
return cb(error); | ||
} | ||
|
||
data = data || undefined; | ||
|
||
cb(null, data); | ||
}); | ||
}; | ||
|
||
|
||
function expirationTimeToTTL(expirationTime) { | ||
if (expirationTime) { | ||
return expirationTime - Math.ceil(Date.now() / 1000); | ||
} | ||
} | ||
|
||
|
||
RedisVault.prototype.set = function (key, data, expirationTime, cb) { | ||
key = this._prefix(key); | ||
|
||
this.logger.verbose('set:', key); | ||
|
||
var cmd = expirationTime ? | ||
[key, data, 'EX', expirationTimeToTTL(expirationTime)] : | ||
[key, data]; | ||
|
||
this.client.set(cmd, cb); | ||
}; | ||
|
||
|
||
RedisVault.prototype.touch = function (key, expirationTime, cb) { | ||
key = this._prefix(key); | ||
|
||
this.logger.verbose('touch:', key); | ||
|
||
if (!expirationTime) { | ||
return cb(); | ||
} | ||
|
||
this.client.expire([key, expirationTimeToTTL(expirationTime)], cb); | ||
}; | ||
|
||
|
||
RedisVault.prototype.del = function (key, cb) { | ||
key = this._prefix(key); | ||
|
||
this.logger.verbose('del:', key); | ||
|
||
this.client.del([key], cb); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.