You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2025/07/27 03:40:17 UTC

(couchdb) branch dry-mem3-hex-range created (now f0a2f332c)

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a change to branch dry-mem3-hex-range
in repository https://gitbox.apache.org/repos/asf/couchdb.git


      at f0a2f332c Add a range_to_hex/1 utility function

This branch includes the following new commits:

     new f0a2f332c Add a range_to_hex/1 utility function

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(couchdb) 01/01: Add a range_to_hex/1 utility function

Posted by va...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch dry-mem3-hex-range
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit f0a2f332cc04e64c129c6154374a6d5cb03d3580
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Sat Jul 26 23:26:00 2025 -0400

    Add a range_to_hex/1 utility function
    
    We were performing the same formatting logic in four places previously, so make
    it a utility function to DRY out the the code a bit.
---
 src/fabric/src/fabric_db_create.erl |  6 +-----
 src/mem3/src/mem3_httpd.erl         |  4 +---
 src/mem3/src/mem3_reshard_dbdoc.erl |  4 +---
 src/mem3/src/mem3_util.erl          | 23 +++++++++++++----------
 4 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/src/fabric/src/fabric_db_create.erl b/src/fabric/src/fabric_db_create.erl
index f7c6e5402..b3677fa90 100644
--- a/src/fabric/src/fabric_db_create.erl
+++ b/src/fabric/src/fabric_db_create.erl
@@ -158,11 +158,7 @@ make_document([#shard{dbname = DbName} | _] = Shards, Suffix, Options) ->
     {RawOut, ByNodeOut, ByRangeOut} =
         lists:foldl(
             fun(#shard{node = N, range = [B, E]}, {Raw, ByNode, ByRange}) ->
-                Range = ?l2b([
-                    couch_util:to_hex(<<B:32/integer>>),
-                    "-",
-                    couch_util:to_hex(<<E:32/integer>>)
-                ]),
+                Range = mem3_util:range_to_hex([B, E]),
                 Node = couch_util:to_binary(N),
                 {
                     [[<<"add">>, Range, Node] | Raw],
diff --git a/src/mem3/src/mem3_httpd.erl b/src/mem3/src/mem3_httpd.erl
index 745fe815c..fb58bd8d1 100644
--- a/src/mem3/src/mem3_httpd.erl
+++ b/src/mem3/src/mem3_httpd.erl
@@ -104,9 +104,7 @@ json_shards([], AccIn) ->
     List = dict:to_list(AccIn),
     {lists:sort(List)};
 json_shards([#shard{node = Node, range = [B, E]} | Rest], AccIn) ->
-    HexBeg = couch_util:to_hex(<<B:32/integer>>),
-    HexEnd = couch_util:to_hex(<<E:32/integer>>),
-    Range = list_to_binary(HexBeg ++ "-" ++ HexEnd),
+    Range = mem3_util:range_to_hex([B, E]),
     json_shards(Rest, dict:append(Range, Node, AccIn)).
 
 sync_shard(ShardName) ->
diff --git a/src/mem3/src/mem3_reshard_dbdoc.erl b/src/mem3/src/mem3_reshard_dbdoc.erl
index cb5bf3aee..c39444806 100644
--- a/src/mem3/src/mem3_reshard_dbdoc.erl
+++ b/src/mem3/src/mem3_reshard_dbdoc.erl
@@ -205,9 +205,7 @@ node_key(#shard{node = Node}) ->
     couch_util:to_binary(Node).
 
 range_key(#shard{range = [B, E]}) ->
-    BHex = couch_util:to_hex(<<B:32/integer>>),
-    EHex = couch_util:to_hex(<<E:32/integer>>),
-    list_to_binary([BHex, "-", EHex]).
+    mem3_util:range_to_hex([B, E]).
 
 shard_update_timeout_msec() ->
     config:get_integer("reshard", "shard_update_timeout_msec", 300000).
diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl
index 0ff22f07f..c3b096a6d 100644
--- a/src/mem3/src/mem3_util.erl
+++ b/src/mem3/src/mem3_util.erl
@@ -44,7 +44,8 @@
     non_overlapping_shards/1,
     non_overlapping_shards/3,
     calculate_max_n/1,
-    calculate_max_n/3
+    calculate_max_n/3,
+    range_to_hex/1
 ]).
 
 %% do not use outside mem3.
@@ -71,15 +72,7 @@ name_shard(#ordered_shard{dbname = DbName, range = Range} = Shard, Suffix) ->
     Shard#ordered_shard{name = ?l2b(Name)}.
 
 make_name(DbName, [B, E], Suffix) ->
-    [
-        "shards/",
-        couch_util:to_hex(<<B:32/integer>>),
-        "-",
-        couch_util:to_hex(<<E:32/integer>>),
-        "/",
-        DbName,
-        Suffix
-    ].
+    ["shards/", ?b2l(range_to_hex([B, E])), "/", DbName, Suffix].
 
 create_partition_map(DbName, N, Q, Nodes) ->
     create_partition_map(DbName, N, Q, Nodes, "").
@@ -172,6 +165,12 @@ update_db_doc(DbName, #doc{id = Id, body = Body} = Doc, ShouldMutate) ->
         couch_db:close(Db)
     end.
 
+-spec range_to_hex([non_neg_integer()]) -> binary().
+range_to_hex([B, E]) when is_integer(B), is_integer(E) ->
+    HexB = couch_util:to_hex(<<B:32/integer>>),
+    HexE = couch_util:to_hex(<<E:32/integer>>),
+    ?l2b(HexB ++ "-" ++ HexE).
+
 delete_db_doc(DocId) ->
     gen_server:cast(mem3_shards, {cache_remove, DocId}),
     delete_db_doc(mem3_sync:shards_db(), DocId, true).
@@ -780,4 +779,8 @@ calculate_max_n_custom_range_test_() ->
 shard(Begin, End) ->
     #shard{range = [Begin, End]}.
 
+range_to_hex_test() ->
+    Range = [2147483648, 4294967295],
+    ?assertEqual(<<"80000000-ffffffff">>, range_to_hex(Range)).
+
 -endif.