def snowflake(datacenterId, workerId, epoch): global lastTimestamp, sequence, sequenceMask datacenterId = datacenterId & datacenterIdBits workerId = workerId & workerIdBits timestamp = get_timestamp() if (timestamp < lastTimestamp): raise "Clock moved backwards" if (timestamp == lastTimestamp): sequence = (sequence + 1) & sequenceMask if (sequence == 0): timestamp = til_next_millis(lastTimestamp) else: sequence = 0 lastTimestamp = timestamp timestamp = timestamp - (int(epoch*1000)) guoidValue = (timestamp << timestampLeftShift) |\ (datacenterId << datacenterIdShift) |\ (workerId << workerIdShift) |\ sequence return guoidValue
def next(self, logical_shard_id): timestamp = get_timestamp() if (timestamp < self.last_timestamp): raise "Clock moved backwards" if (timestamp == self.last_timestamp): self.sequence = (self.sequence + 1) & Config.SEQUENCE_MASK if (self.sequence == 0): timestamp = til_next_millis(self.last_timestamp) else: self.sequence = 0 self.last_timestamp = timestamp timestamp = timestamp - (int(self.epoch)*1000) guoidValue = (timestamp << Config.TIMESTAMP_LEFT_SHIFT) |\ (logical_shard_id << Config.LOGICAL_SHARD_ID_SHIFT) | self.sequence return guoidValue
def next(self): timestamp = get_timestamp() if (timestamp < self.last_timestamp): raise "Clock moved backwards" if (timestamp == self.last_timestamp): self.sequence = (self.sequence + 1) & Config.SEQUENCE_MASK if (self.sequence == 0): timestamp = til_next_millis(self.last_timestamp) else: self.sequence = 0 self.last_timestamp = timestamp timestamp = timestamp - (int(self.epoch*1000)) guoidValue = (timestamp << Config.TIMESTAMP_LEFT_SHIFT) |\ (self.datacenter_id | (self.worker_id) | self.sequence) return guoidValue
def instagram(id, epoch): global lastTimestamp, sequence, sequenceMask global logicalShardIdMask, logicalShardIdBits timestamp = get_timestamp() if (timestamp < lastTimestamp): raise "Clock moved backwards" if (timestamp == lastTimestamp): sequence = (sequence + 1) & sequenceMask if (sequence == 0): timestamp = til_next_millis(lastTimestamp) else: sequence = 0 logicalShardId = getUserLogicalShardId(id) lastTimestamp = timestamp timestamp = timestamp - (int(epoch)*1000) guoidValue = (timestamp << timestampLeftShift) |\ (logicalShardId << logicalShardIdShift) |\ sequence return guoidValue