Skip to content

Instantly share code, notes, and snippets.

@icholy
Last active March 7, 2018 03:43
Show Gist options
  • Save icholy/ccebe91bad7a69097ee480d97b2242e5 to your computer and use it in GitHub Desktop.
Save icholy/ccebe91bad7a69097ee480d97b2242e5 to your computer and use it in GitHub Desktop.
const charset = ['a', 'b', 'c'];
function parse(key: string): number[] {
return key.split('').map(c => charset.indexOf(c));
}
function format(key: number[]): string {
return key.map(i => charset[i]).join('');
}
function increment(key: number[]): void {
let last = key[key.length - 1];
if (last === charset.length - 1) {
key.push(0);
} else {
key.push(last + 1);
}
}
function decrement(key: number[]): void {
// ?
}
function generate(lo: string, hi: string): string {
let inc = format(increment(parse(low))),
dec = format(decrement(parse(hi)));
return inc < hi ? inc : dec;
}
-- create table
CREATE TABLE test (
id bigserial,
rank bigint
);
-- rebalance ranks
UPDATE test
SET rank = (v_test.row_num * 1000)
FROM (
SELECT id, ROW_NUMBER() OVER (ORDER BY rank) as row_num
FROM test
) AS v_test
WHERE test.id = v_test.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment