Last active
March 7, 2018 03:43
-
-
Save icholy/ccebe91bad7a69097ee480d97b2242e5 to your computer and use it in GitHub Desktop.
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
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; | |
} | |
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
-- 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