2

如何实现给定版本 4 UUID A 生成有效版本 4 UUID B 的函数 increment(uuid),其中:

  1. 乙>甲
  2. 存在 NO C 其中 B > C > A

与给定版本 4 UUID A 的减量(uuid)相同,会产生有效的版本 4 UUID B,其中:

  1. 乙<甲
  2. 存在 NO C 其中 B < C < A
4

2 回答 2

1

这是一个python函数来实现你想要的:

def increment_uuid(uuid, increment):
    # Return a UUID value incremented by an integer

    hex_str = uuid[:8] + uuid[9:13] + uuid[14:18] + uuid[19:23] + uuid[24:]
    incremented_val = int(hex_str, 16) + increment
    assert(incremented_val >= 0 and incremented_val <= 2 ** 128)
    incremented_hex = format(incremented_val, '032x')
    incrmented_uuid = incremented_hex[:8] + '-' + incremented_hex[8:12] + '-' + incremented_hex[12:16] \
                      + '-' + incremented_hex[16:20] + '-' + incremented_hex[20:]
    return incrmented_uuid   

您现在可以简单地将 UUID 增加 1 以获取“序列”中的下一个:

import uuid
uuid_a = uuid.uuid4() # e.g. a32258b5-2781-44ec-9e00-f4b1185e9936
uuid_b = increment_uuid(str(uuid_a),1) # e.g. a32258b5-2781-44ec-9e00-f4b1185e9937

当然,它只是将数据视为 128 位十六进制值,而忽略了版本号和变体位的约束。

于 2017-02-19T14:35:20.413 回答
0

不可能。除了为版本和变体字段保留的 6 位之外,v4 UUID 由完全随机的数据组成 - 即不存在用于定义有意义的递增/递减操作的“序列”的固有概念。

于 2012-10-19T19:05:06.393 回答