Skip to content

Instantly share code, notes, and snippets.

@fritzy
Last active August 29, 2015 14:01
Show Gist options
  • Save fritzy/236b3c727264ded700aa to your computer and use it in GitHub Desktop.
Save fritzy/236b3c727264ded700aa to your computer and use it in GitHub Desktop.
keep reverse sorted mapped indexes in addition to forward
function reverseString(sin) {
sin = new Buffer(sin);
for (var idx = 0, l = sin.length; idx < l; idx++) {
sin.writeUInt8(0xFF - sin.readUInt8(idx), idx);
}
return sin;
}
function reverseString(sin) {
sin = String(sin);
var out = '';
for (var idx = 0, l = sin.length; idx < l; idx++) {
out += String.fromCharCode(~ sin[idx].charCodeAt() & 0xFF);
}
return out;
}
function put(riak, opts, callback) {
var indexes = opts.indexes || {};
Object.keys(indexes).forEach(function (index) {
if (Array.isArray(indexes[index])) {
indexes['_reverse_' + index] = indexes[index].map(reverseString);
} else {
indexes['_reverse_' + index] = reverseString(indexes[index]);
}
});
indexes['_reverse_$key'] = reverseString(opts.key)
opts.indexes = indexes;
riak.put(opts, callback);
}
function getByIndex(riak, opts, callback) {
if (opts.reverse) {
opts.index = '_reverse_' + opts.index;
opts.key = reverseString(opts.key);
}
riak.getByIndex(opts, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment