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 2023/11/22 07:42:40 UTC
(couchdb) 01/20: Convert `term_to_binary` to `?term_to_bin`
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch 3.3.x-pending-changes-3.3.3
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 1184816e955d8d33508d7470cf5f7da13619aa74
Author: Jay Doane <ja...@apache.org>
AuthorDate: Thu May 18 13:48:43 2023 -0700
Convert `term_to_binary` to `?term_to_bin`
* Replace `term_to_binary` calls that don't specify the
`minor_version` option with the `?term_to_bin` macro, which adds the
option `{minor_version, 1}`, to maintain backward compatibility.
* Define new `term_to_bin/2` in `couchd_db.hrl` for existing uses of
`term_to_binary/2`.
* Define `term_to_bin` in couch_epi.hrl since `couch_db.hrl` cannot be
imported into `couch_epi` modules.
---
src/chttpd/src/chttpd.erl | 2 +-
src/chttpd/src/chttpd_misc.erl | 2 +-
src/couch/include/couch_db.hrl | 1 +
src/couch/src/couch_file.erl | 2 +-
src/couch/src/couch_native_process.erl | 2 +-
src/couch/src/couch_query_servers.erl | 2 +-
src/couch_epi/src/couch_epi.hrl | 3 +++
src/couch_epi/src/couch_epi_util.erl | 4 +++-
src/couch_mrview/src/couch_mrview.erl | 2 +-
src/couch_mrview/src/couch_mrview_util.erl | 8 ++++----
src/couch_pse_tests/src/cpse_util.erl | 4 ++--
src/couch_replicator/src/couch_replicator_ids.erl | 2 +-
src/ddoc_cache/src/ddoc_cache_value.erl | 4 +++-
src/dreyfus/src/dreyfus_index.erl | 2 +-
src/fabric/src/fabric_view_changes.erl | 2 +-
src/mango/src/mango_idx.erl | 2 +-
src/mem3/src/mem3_hash.erl | 3 ++-
src/mem3/src/mem3_rep.erl | 2 +-
src/mem3/src/mem3_util.erl | 2 +-
src/smoosh/src/smoosh_persist.erl | 3 ++-
20 files changed, 32 insertions(+), 22 deletions(-)
diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index f395a236e..b8b977bf9 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -1417,7 +1417,7 @@ stack_trace_id(Stack) ->
{"X-Couch-Stack-Hash", stack_hash(Stack)}.
stack_hash(Stack) ->
- erlang:crc32(term_to_binary(Stack)).
+ erlang:crc32(?term_to_bin(Stack)).
%% @doc CouchDB uses a chunked transfer-encoding to stream responses to
%% _all_docs, _changes, _view and other similar requests. This configuration
diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl
index 0dedeba4d..e28dbffc2 100644
--- a/src/chttpd/src/chttpd_misc.erl
+++ b/src/chttpd/src/chttpd_misc.erl
@@ -292,7 +292,7 @@ choose_node(Key) when is_binary(Key) ->
Nodes = lists:sort([node() | erlang:nodes()]),
lists:nth(1 + Checksum rem length(Nodes), Nodes);
choose_node(Key) ->
- choose_node(term_to_binary(Key)).
+ choose_node(?term_to_bin(Key)).
handle_reload_query_servers_req(#httpd{method = 'POST'} = Req) ->
chttpd:validate_ctype(Req, "application/json"),
diff --git a/src/couch/include/couch_db.hrl b/src/couch/include/couch_db.hrl
index 0cb01d0f4..9478066a1 100644
--- a/src/couch/include/couch_db.hrl
+++ b/src/couch/include/couch_db.hrl
@@ -32,6 +32,7 @@
-define(i2b(V), couch_util:integer_to_boolean(V)).
-define(b2i(V), couch_util:boolean_to_integer(V)).
-define(term_to_bin(T), term_to_binary(T, [{minor_version, 1}])).
+-define(term_to_bin(T, O), term_to_binary(T, O ++ [{minor_version, 1}])).
-define(term_size(T), erlang:external_size(T, [{minor_version, 1}])).
-define(DEFAULT_ATTACHMENT_CONTENT_TYPE, <<"application/octet-stream">>).
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index ba8d9c42f..ae687e6a0 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -398,7 +398,7 @@ read_header(Fd) ->
end.
write_header(Fd, Data) ->
- Bin = term_to_binary(Data),
+ Bin = ?term_to_bin(Data),
Md5 = couch_hash:md5_hash(Bin),
% now we assemble the final header binary and write to disk
FinalBin = <<Md5/binary, Bin/binary>>,
diff --git a/src/couch/src/couch_native_process.erl b/src/couch/src/couch_native_process.erl
index 69fbcb116..3ca7dd1f8 100644
--- a/src/couch/src/couch_native_process.erl
+++ b/src/couch/src/couch_native_process.erl
@@ -392,7 +392,7 @@ makefun(State, Source) ->
BindFuns = bindings(State, Sig),
{Sig, makefun(State, Source, BindFuns)}.
makefun(State, Source, {DDoc}) ->
- Sig = couch_hash:md5_hash(lists:flatten([Source, term_to_binary(DDoc)])),
+ Sig = couch_hash:md5_hash(lists:flatten([Source, ?term_to_bin(DDoc)])),
BindFuns = bindings(State, Sig, {DDoc}),
{Sig, makefun(State, Source, BindFuns)};
makefun(_State, Source, BindFuns) when is_list(BindFuns) ->
diff --git a/src/couch/src/couch_query_servers.erl b/src/couch/src/couch_query_servers.erl
index 6752980c9..151bdc805 100644
--- a/src/couch/src/couch_query_servers.erl
+++ b/src/couch/src/couch_query_servers.erl
@@ -377,7 +377,7 @@ get_number(Key, Props) ->
approx_count_distinct(reduce, KVs) ->
lists:foldl(
fun([[Key, _Id], _Value], Filter) ->
- hyper:insert(term_to_binary(Key), Filter)
+ hyper:insert(?term_to_bin(Key), Filter)
end,
hyper:new(11),
KVs
diff --git a/src/couch_epi/src/couch_epi.hrl b/src/couch_epi/src/couch_epi.hrl
index a8bd1d542..8fa3335e4 100644
--- a/src/couch_epi/src/couch_epi.hrl
+++ b/src/couch_epi/src/couch_epi.hrl
@@ -13,3 +13,6 @@
-record(couch_epi_spec, {
behaviour, app, kind, options, key, value, codegen, type
}).
+
+% Copied from `couch_db.hrl` which cannot be included in this application
+-define(term_to_bin(T), term_to_binary(T, [{minor_version, 1}])).
diff --git a/src/couch_epi/src/couch_epi_util.erl b/src/couch_epi/src/couch_epi_util.erl
index 2c86a96e2..9f34dca90 100644
--- a/src/couch_epi/src/couch_epi_util.erl
+++ b/src/couch_epi/src/couch_epi_util.erl
@@ -12,6 +12,8 @@
-module(couch_epi_util).
+-include("couch_epi.hrl").
+
-export([module_version/1, hash/1, module_exists/1]).
-compile([nowarn_deprecated_function]).
@@ -22,7 +24,7 @@ module_version(Module) ->
VSNs.
hash(Term) ->
- <<SigInt:128/integer>> = couch_hash:md5_hash(term_to_binary(Term)),
+ <<SigInt:128/integer>> = couch_hash:md5_hash(?term_to_bin(Term)),
lists:flatten(io_lib:format("\"~.36B\"", [SigInt])).
module_exists(Module) ->
diff --git a/src/couch_mrview/src/couch_mrview.erl b/src/couch_mrview/src/couch_mrview.erl
index d8640c903..836bfbad1 100644
--- a/src/couch_mrview/src/couch_mrview.erl
+++ b/src/couch_mrview/src/couch_mrview.erl
@@ -261,7 +261,7 @@ query_all_docs(Db, Args, Callback, Acc) when is_list(Args) ->
query_all_docs(Db, Args0, Callback, Acc) ->
Sig = couch_util:with_db(Db, fun(WDb) ->
{ok, Info} = couch_db:get_db_info(WDb),
- couch_index_util:hexsig(couch_hash:md5_hash(term_to_binary(Info)))
+ couch_index_util:hexsig(couch_hash:md5_hash(?term_to_bin(Info)))
end),
Args1 = Args0#mrargs{view_type = map},
Args2 = couch_mrview_util:validate_all_docs_args(Db, Args1),
diff --git a/src/couch_mrview/src/couch_mrview_util.erl b/src/couch_mrview/src/couch_mrview_util.erl
index e1e75f34f..d70b5b107 100644
--- a/src/couch_mrview/src/couch_mrview_util.erl
+++ b/src/couch_mrview/src/couch_mrview_util.erl
@@ -265,7 +265,7 @@ ddoc_to_mrst(DbName, #doc{id = Id, body = {Fields}}) ->
partitioned = Partitioned
},
SigInfo = {Views, Language, DesignOpts, couch_index_util:sort_lib(Lib)},
- {ok, IdxState#mrst{sig = couch_hash:md5_hash(term_to_binary(SigInfo))}}.
+ {ok, IdxState#mrst{sig = couch_hash:md5_hash(?term_to_bin(SigInfo))}}.
set_view_type(_Args, _ViewName, []) ->
throw({not_found, missing_named_view});
@@ -315,7 +315,7 @@ view_sig(Db, State, View, #mrargs{include_docs = true} = Args) ->
UpdateSeq = couch_db:get_update_seq(Db),
PurgeSeq = couch_db:get_purge_seq(Db),
Term = view_sig_term(BaseSig, UpdateSeq, PurgeSeq),
- couch_index_util:hexsig(couch_hash:md5_hash(term_to_binary(Term)));
+ couch_index_util:hexsig(couch_hash:md5_hash(?term_to_bin(Term)));
view_sig(Db, State, {_Nth, _Lang, View}, Args) ->
view_sig(Db, State, View, Args);
view_sig(_Db, State, View, Args0) ->
@@ -327,7 +327,7 @@ view_sig(_Db, State, View, Args0) ->
extra = []
},
Term = view_sig_term(Sig, UpdateSeq, PurgeSeq, Args),
- couch_index_util:hexsig(couch_hash:md5_hash(term_to_binary(Term))).
+ couch_index_util:hexsig(couch_hash:md5_hash(?term_to_bin(Term))).
view_sig_term(BaseSig, UpdateSeq, PurgeSeq) ->
{BaseSig, UpdateSeq, PurgeSeq}.
@@ -1115,7 +1115,7 @@ sig_vsn_2x(State) ->
KSI = proplists:get_value(<<"keyseq_indexed">>, DesignOpts, false),
Views = [old_view_format(V, SI, KSI) || V <- State#mrst.views],
SigInfo = {Views, Language, DesignOpts, couch_index_util:sort_lib(Lib)},
- couch_hash:md5_hash(term_to_binary(SigInfo)).
+ couch_hash:md5_hash(?term_to_bin(SigInfo)).
old_view_format(View, SI, KSI) ->
{
diff --git a/src/couch_pse_tests/src/cpse_util.erl b/src/couch_pse_tests/src/cpse_util.erl
index b0e5b0c2b..c5aac94c0 100644
--- a/src/couch_pse_tests/src/cpse_util.erl
+++ b/src/couch_pse_tests/src/cpse_util.erl
@@ -366,11 +366,11 @@ gen_write(Db, {Action, {DocId, Body, Atts}}) ->
}}.
gen_rev(A, DocId, {Pos, Rev}, Body, Atts) when A == update; A == delete ->
- NewRev = couch_hash:md5_hash(term_to_binary({DocId, Rev, Body, Atts})),
+ NewRev = couch_hash:md5_hash(?term_to_bin({DocId, Rev, Body, Atts})),
{Pos + 1, [NewRev, Rev]};
gen_rev(conflict, DocId, _, Body, Atts) ->
UUID = couch_uuids:random(),
- NewRev = couch_hash:md5_hash(term_to_binary({DocId, UUID, Body, Atts})),
+ NewRev = couch_hash:md5_hash(?term_to_bin({DocId, UUID, Body, Atts})),
{1, [NewRev]}.
prep_atts(_Db, []) ->
diff --git a/src/couch_replicator/src/couch_replicator_ids.erl b/src/couch_replicator/src/couch_replicator_ids.erl
index 86fe1f26e..0c381b009 100644
--- a/src/couch_replicator/src/couch_replicator_ids.erl
+++ b/src/couch_replicator/src/couch_replicator_ids.erl
@@ -120,7 +120,7 @@ maybe_append_filters(
true ->
[<<"winning_revs_only">>]
end,
- couch_util:to_hex(couch_hash:md5_hash(term_to_binary(Base3))).
+ couch_util:to_hex(couch_hash:md5_hash(?term_to_bin(Base3))).
maybe_append_options(Options, RepOptions) ->
lists:foldl(
diff --git a/src/ddoc_cache/src/ddoc_cache_value.erl b/src/ddoc_cache/src/ddoc_cache_value.erl
index 59585ee58..825fd4539 100644
--- a/src/ddoc_cache/src/ddoc_cache_value.erl
+++ b/src/ddoc_cache/src/ddoc_cache_value.erl
@@ -17,8 +17,10 @@
unwrap/1
]).
+-include_lib("couch/include/couch_db.hrl").
+
wrap(Value) ->
- {?MODULE, term_to_binary(Value)}.
+ {?MODULE, ?term_to_bin(Value)}.
unwrap({?MODULE, Bin}) when is_binary(Bin) ->
binary_to_term(Bin).
diff --git a/src/dreyfus/src/dreyfus_index.erl b/src/dreyfus/src/dreyfus_index.erl
index df3e68f84..1c4b209bb 100644
--- a/src/dreyfus/src/dreyfus_index.erl
+++ b/src/dreyfus/src/dreyfus_index.erl
@@ -327,7 +327,7 @@ design_doc_to_index(#doc{id = Id, body = {Fields}}, IndexName) ->
Sig = ?l2b(
couch_util:to_hex(
couch_hash:md5_hash(
- term_to_binary({Analyzer, Def})
+ ?term_to_bin({Analyzer, Def})
)
)
),
diff --git a/src/fabric/src/fabric_view_changes.erl b/src/fabric/src/fabric_view_changes.erl
index 693e24384..fd439c5c3 100644
--- a/src/fabric/src/fabric_view_changes.erl
+++ b/src/fabric/src/fabric_view_changes.erl
@@ -422,7 +422,7 @@ pending_count(Dict) ->
pack_seqs(Workers) ->
SeqList = [{N, R, S} || {#shard{node = N, range = R}, S} <- Workers],
SeqSum = lists:sum([seq(S) || {_, _, S} <- SeqList]),
- Opaque = couch_util:encodeBase64Url(term_to_binary(SeqList, [compressed])),
+ Opaque = couch_util:encodeBase64Url(?term_to_bin(SeqList, [compressed])),
?l2b([integer_to_list(SeqSum), $-, Opaque]).
seq({Seq, _Uuid, _Node}) -> Seq;
diff --git a/src/mango/src/mango_idx.erl b/src/mango/src/mango_idx.erl
index a20d730a2..de2b0f096 100644
--- a/src/mango/src/mango_idx.erl
+++ b/src/mango/src/mango_idx.erl
@@ -323,7 +323,7 @@ get_idx_name(Idx, Opts) ->
gen_name(Idx, Opts0) ->
Opts = lists:usort(Opts0),
- TermBin = term_to_binary({Idx, Opts}),
+ TermBin = ?term_to_bin({Idx, Opts}),
Sha = crypto:hash(sha, TermBin),
mango_util:enc_hex(Sha).
diff --git a/src/mem3/src/mem3_hash.erl b/src/mem3/src/mem3_hash.erl
index ccaab7223..6dfe3f45a 100644
--- a/src/mem3/src/mem3_hash.erl
+++ b/src/mem3/src/mem3_hash.erl
@@ -21,6 +21,7 @@
]).
-include_lib("mem3/include/mem3.hrl").
+-include_lib("couch/include/couch_db.hrl").
calculate(#shard{opts = Opts}, DocId) ->
Props = couch_util:get_value(props, Opts, []),
@@ -53,7 +54,7 @@ get_hash_fun(DbName0) when is_binary(DbName0) ->
crc32(Item) when is_binary(Item) ->
erlang:crc32(Item);
crc32(Item) ->
- erlang:crc32(term_to_binary(Item)).
+ erlang:crc32(?term_to_bin(Item)).
get_hash_fun_int(Opts) when is_list(Opts) ->
case lists:keyfind(hash, 1, Opts) of
diff --git a/src/mem3/src/mem3_rep.erl b/src/mem3/src/mem3_rep.erl
index 010d21364..d186f9e38 100644
--- a/src/mem3/src/mem3_rep.erl
+++ b/src/mem3/src/mem3_rep.erl
@@ -152,7 +152,7 @@ filter_hash(_) ->
<<>>.
local_id_hash(Thing) ->
- couch_util:encodeBase64Url(couch_hash:md5_hash(term_to_binary(Thing))).
+ couch_util:encodeBase64Url(couch_hash:md5_hash(?term_to_bin(Thing))).
make_purge_id(SourceUUID, TargetUUID) ->
<<"_local/purge-mem3-", SourceUUID/binary, "-", TargetUUID/binary>>.
diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl
index f05fe7378..520f54629 100644
--- a/src/mem3/src/mem3_util.erl
+++ b/src/mem3/src/mem3_util.erl
@@ -360,7 +360,7 @@ is_deleted(Change) ->
rotate_list(_Key, []) ->
[];
rotate_list(Key, List) when not is_binary(Key) ->
- rotate_list(term_to_binary(Key), List);
+ rotate_list(?term_to_bin(Key), List);
rotate_list(Key, List) ->
{H, T} = lists:split(erlang:crc32(Key) rem length(List), List),
T ++ H.
diff --git a/src/smoosh/src/smoosh_persist.erl b/src/smoosh/src/smoosh_persist.erl
index 68d8a418d..1fa272f05 100644
--- a/src/smoosh/src/smoosh_persist.erl
+++ b/src/smoosh/src/smoosh_persist.erl
@@ -19,6 +19,7 @@
]).
-include_lib("kernel/include/file.hrl").
+-include_lib("couch/include/couch_db.hrl").
-define(SUFFIX, ".smooshq").
@@ -190,7 +191,7 @@ t_corrupted_read(_) ->
Path = file_path("foochan"),
?assertEqual(ok, write(#{<<"a">> => 1}, Path)),
- ok = file:write_file(Path, term_to_binary(foo), [raw]),
+ ok = file:write_file(Path, ?term_to_bin(foo), [raw]),
?assertEqual({error, term_not_a_map}, read(Path)),
ok = file:write_file(Path, <<"42">>, [raw]),