Last active
August 29, 2015 14:01
-
-
Save fritzy/236b3c727264ded700aa to your computer and use it in GitHub Desktop.
keep reverse sorted mapped indexes in addition to forward
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
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; | |
} |
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
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