Created
August 29, 2012 07:52
-
-
Save yamingd/3508181 to your computer and use it in GitHub Desktop.
How Pinterest Generate Global Uuid?
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
class PinId(object): | |
SHARD_LEN = 46 | |
OBJECT_LEN = 10 | |
@classmethod | |
def uuid(clz, shard_id, object_type, local_id): | |
sid = shard_id << clz.SHARD_LEN | object_type << clz.OBJECT_LEN | local_id | |
return sid | |
@classmethod | |
def parse(clz, vuuid): | |
shard_id = vuuid >> clz.SHARD_LEN | |
temp = vuuid - (shard_id << clz.SHARD_LEN) | |
object_type = temp >> clz.OBJECT_LEN | |
local_id = temp - (object_type << clz.OBJECT_LEN) | |
return (shard_id, object_type, local_id) | |
if __name__ == '__main__': | |
ss = PinId.uuid(123,200,45) | |
print ss | |
print PinId.parse(ss) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment