Generating Reverse Sort Indexes for Riak in Nodenode.jsleveldbriak
While developing the prototype for what became @quitlahok's riakdown,
I quickly realized that while Riak can give keys back in lexical order of indexes, it cannot do the same in reverse.
We needed reverse support in order to support the levelup interface.
Our solution was to generate extra indexes that would be the lexical mirror of each byte for each index given, as well as the key.
This creates double the indexes, but for our purpose was important.
In order to mirror the byte, I did a NOT operation on it, which is not straight forward in JS.
For each byte, we need to get the ascii value, and NOT that, but the ascii value is returned as a JS Number.
When we perform a NOT on a Number, we get an unexpected result, due to its signed nature.
Really, what we want is the last byte, so we mask it with AND 0xFF.
@brycebaril points out that things get weird with unicode characters,
and that we can reduce the number of operations by doing 255 - value, rather than NOT+AND.
@quitlahok noted that riakdown doesn't actually produce these extra indexes (although my prototype did).
Instead he uses a map-reduce query to accomplish this.