This is an automated email from the ASF dual-hosted git repository. vatamane pushed a commit to branch optimize-purge in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 4c4fc21d46260514992d5562301b6d26ef5e1125 Author: Nick Vatamaniuc <[email protected]> AuthorDate: Sun Oct 12 02:05:36 2025 -0400 Add UUID v7 Add UUID v7 as an option for `couch_uuid` https://datatracker.ietf.org/doc/html/rfc9562#section-5.7 Unlike some other existing algorithms, UUID v7 doesn't need updating a shared state with each generated value and has an incrementing time prefix, which can help when using these IDs as B-tree keys. --- src/couch/src/couch_uuids.erl | 32 +++++++++++++++++++++++++++++++- src/docs/src/config/misc.rst | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/couch/src/couch_uuids.erl b/src/couch/src/couch_uuids.erl index 588136159..f007eab91 100644 --- a/src/couch/src/couch_uuids.erl +++ b/src/couch/src/couch_uuids.erl @@ -17,7 +17,7 @@ -export([start/0, stop/0]). -export([new/0, random/0]). - +-export([v7_hex/0, v7_bin/0]). -export([init/1]). -export([handle_call/3, handle_cast/2, handle_info/2]). @@ -44,6 +44,8 @@ init([]) -> handle_call(create, _From, random) -> {reply, random(), random}; +handle_call(create, _From, uuid_v7) -> + {reply, v7_hex(), uuid_v7}; handle_call(create, _From, {utc_random, ClockSeq}) -> {UtcRandom, NewClockSeq} = utc_random(ClockSeq), {reply, UtcRandom, {utc_random, NewClockSeq}}; @@ -84,6 +86,32 @@ handle_config_terminate(_Server, _Reason, _State) -> gen_server:cast(?MODULE, change), erlang:send_after(?RELISTEN_DELAY, whereis(?MODULE), restart_config_listener). +%% UUID Version 7 +%% https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-7 +%% +%% 0 1 2 3 +%% 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +%% +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +%% | unix_ts_ms | +%% +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +%% | unix_ts_ms | ver | rand_a | +%% +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +%% |var| rand_b | +%% +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +%% | rand_b | +%% +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +%% +%% ver = 0111 = 7 +%% var = 10 = 2 +%% +v7_bin() -> + MSec = os:system_time(millisecond), + <<RandA:12, RandB:62, _:6>> = crypto:strong_rand_bytes(10), + <<MSec:48, 7:4, RandA:12, 2:2, RandB:62>>. + +v7_hex() -> + couch_util:to_hex_bin(v7_bin()). + new_prefix() -> couch_util:to_hex((crypto:strong_rand_bytes(13))). @@ -104,6 +132,8 @@ state() -> {utc_id, UtcIdSuffix, ClockSeq}; sequential -> {sequential, new_prefix(), inc()}; + uuid_v7 -> + uuid_v7; Unknown -> throw({unknown_uuid_algorithm, Unknown}) end. diff --git a/src/docs/src/config/misc.rst b/src/docs/src/config/misc.rst index ff4619753..8373e0f98 100644 --- a/src/docs/src/config/misc.rst +++ b/src/docs/src/config/misc.rst @@ -66,6 +66,7 @@ UUIDs Configuration .. config:option:: algorithm :: Generation Algorithm .. versionchanged:: 1.3 Added ``utc_id`` algorithm. + .. versionchanged:: 3.6 Added ``uuid_v7`` algorithm. CouchDB provides various algorithms to generate the UUID values that are used for document `_id`'s by default:: @@ -158,6 +159,25 @@ UUIDs Configuration ] } + - ``uuid_v7``: UUID v7 string in hex. + + .. code-block:: javascript + + { + "uuids": [ + "0199d2456e7f7b0a9b7130f9a9db8bee", + "0199d2456e7f72dda9f758fcc259c5fc", + "0199d2456e7f751c80b461180f7c7717", + "0199d2456e7f7c569b317d53367ca45a", + "0199d2456e7f77bfbffe92682c9c8c69", + "0199d2456e7f703ea97286f3d976343e", + "0199d2456e7f7f729142ed3b2da9101f", + "0199d2456e7f7723905c1f91f40d54f5", + "0199d2456e7f7e40979c7e2e22ffeb6a", + "0199d2456e7f7a42b43acfcc1e18eb84" + ] + } + .. note:: **Impact of UUID choices:** the choice of UUID has a significant impact on the layout of the B-tree, prior to compaction.
