Skip to content

Instantly share code, notes, and snippets.

@yamingd
Created August 29, 2012 07:52
Show Gist options
  • Save yamingd/3508181 to your computer and use it in GitHub Desktop.
Save yamingd/3508181 to your computer and use it in GitHub Desktop.
How Pinterest Generate Global Uuid?
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