Skip to content

Commit

Permalink
Replace type bitstring() with binary()
Browse files Browse the repository at this point in the history
A bitstring() is any multiple of bits while binary() is a multiple of 8 bits.
Since RESP is a binary protocol which uses control sequences encoded in
standard ASCII, i.e. 8 bits, the type binary() is the proper type to use.
  • Loading branch information
bjosv committed May 17, 2024
1 parent c36c952 commit b7b7be2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
18 changes: 9 additions & 9 deletions doc/eredis_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ there is no need to use the functions with an explicit Cluster parameter.


<pre><code>
anystring() = string() | bitstring()
anystring() = string() | binary()
</code>
</pre>

Expand Down Expand Up @@ -96,7 +96,7 @@ redis_command() = <a href="#type-redis_simple_command">redis_simple_command()</a


<pre><code>
redis_error_result() = bitstring() | no_connection | invalid_cluster_command | tcp_closed
redis_error_result() = binary() | no_connection | invalid_cluster_command | tcp_closed
</code>
</pre>

Expand All @@ -120,7 +120,7 @@ redis_pipeline_command() = [<a href="#type-redis_simple_command">redis_simple_co


<pre><code>
redis_pipeline_result() = [<a href="#type-redis_simple_result">redis_simple_result()</a>]
redis_pipeline_result() = [<a href="#type-redis_simple_result">redis_simple_result()</a>] | {error, <a href="#type-redis_error_result">redis_error_result()</a>}
</code>
</pre>

Expand Down Expand Up @@ -168,7 +168,7 @@ redis_simple_result() = {ok, <a href="#type-redis_success_result">redis_success_


<pre><code>
redis_success_result() = bitstring() | undefined
redis_success_result() = binary() | [binary()] | undefined
</code>
</pre>

Expand Down Expand Up @@ -308,7 +308,7 @@ __See also:__ [load_script/1](#load_script-1).
### flushdb/0 ###

<pre><code>
flushdb() -&gt; ok | {error, Reason::bitstring()}
flushdb() -&gt; ok | {error, <a href="#type-redis_error_result">redis_error_result()</a>}
</code>
</pre>

Expand Down Expand Up @@ -498,7 +498,7 @@ qa(Command) -&gt; Result
</code>
</pre>

<ul class="definitions"><li><code>Command = <a href="#type-redis_command">redis_command()</a></code></li><li><code>Result = [<a href="#type-redis_transaction_result">redis_transaction_result()</a>] | {error, no_connection}</code></li></ul>
<ul class="definitions"><li><code>Command = <a href="#type-redis_command">redis_command()</a></code></li><li><code>Result = [<a href="#type-redis_result">redis_result()</a>] | {error, no_connection}</code></li></ul>

Performs a query on all nodes in the default cluster. When a query to a
master fails, the mapping is refreshed and the query is retried.
Expand All @@ -512,7 +512,7 @@ qa(Cluster, Command) -&gt; Result
</code>
</pre>

<ul class="definitions"><li><code>Cluster = atom()</code></li><li><code>Command = <a href="#type-redis_command">redis_command()</a></code></li><li><code>Result = [<a href="#type-redis_transaction_result">redis_transaction_result()</a>] | {error, no_connection}</code></li></ul>
<ul class="definitions"><li><code>Cluster = atom()</code></li><li><code>Command = <a href="#type-redis_command">redis_command()</a></code></li><li><code>Result = [<a href="#type-redis_result">redis_result()</a>] | {error, no_connection}</code></li></ul>

Performs a query on all nodes in a cluster. When a query to a master
fails, the mapping is refreshed and the query is retried.
Expand Down Expand Up @@ -769,7 +769,7 @@ update_hash_field(Key, Field, UpdateFunction) -&gt; Result
</code>
</pre>

<ul class="definitions"><li><code>Key = <a href="#type-anystring">anystring()</a></code></li><li><code>Field = <a href="#type-anystring">anystring()</a></code></li><li><code>UpdateFunction = fun((any()) -&gt; any())</code></li><li><code>Result = {ok, {[any()], any()}} | {error, <a href="#type-redis_error_result">redis_error_result()</a>}</code></li></ul>
<ul class="definitions"><li><code>Key = <a href="#type-anystring">anystring()</a></code></li><li><code>Field = <a href="#type-anystring">anystring()</a></code></li><li><code>UpdateFunction = fun((any()) -&gt; any())</code></li><li><code>Result = {ok, {any(), any()}} | <a href="#type-optimistic_locking_error_result">optimistic_locking_error_result()</a></code></li></ul>

Update the value of a field stored in a hash by applying the function
passed in the argument. The operation is done atomically using an optimistic
Expand All @@ -786,7 +786,7 @@ update_key(Key, UpdateFunction) -&gt; Result
</code>
</pre>

<ul class="definitions"><li><code>Key = <a href="#type-anystring">anystring()</a></code></li><li><code>UpdateFunction = fun((any()) -&gt; any())</code></li><li><code>Result = <a href="#type-redis_transaction_result">redis_transaction_result()</a></code></li></ul>
<ul class="definitions"><li><code>Key = <a href="#type-anystring">anystring()</a></code></li><li><code>UpdateFunction = fun((any()) -&gt; any())</code></li><li><code>Result = {ok, any()} | <a href="#type-optimistic_locking_error_result">optimistic_locking_error_result()</a></code></li></ul>

Update the value of a key by applying the function passed in the
argument. The operation is done atomically, using an optimistic locking
Expand Down
6 changes: 3 additions & 3 deletions doc/eredis_cluster_monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ useful for advanced scenarios.
### get_cluster_nodes/0 ###

<pre><code>
get_cluster_nodes() -&gt; [[bitstring()]]
get_cluster_nodes() -&gt; [[binary()]]
</code>
</pre>

Expand All @@ -57,7 +57,7 @@ See: https://redis.io/commands/cluster-nodes#serialization-format
### get_cluster_nodes/1 ###

<pre><code>
get_cluster_nodes(Cluster::atom()) -&gt; [[bitstring()]]
get_cluster_nodes(Cluster::atom()) -&gt; [[binary()]]
</code>
</pre>

Expand All @@ -67,7 +67,7 @@ get_cluster_nodes(Cluster::atom()) -&gt; [[bitstring()]]
### get_cluster_slots/0 ###

<pre><code>
get_cluster_slots() -&gt; [[bitstring() | [bitstring()]]]
get_cluster_slots() -&gt; [[binary() | [binary()]]]
</code>
</pre>

Expand Down
6 changes: 3 additions & 3 deletions include/eredis_cluster.hrl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-type anystring() :: string() | bitstring().
-type anystring() :: string() | binary().

-type redis_simple_command() :: [anystring() | integer()].
-type redis_pipeline_command() :: [redis_simple_command()].
-type redis_command() :: redis_simple_command() | redis_pipeline_command().

-type redis_error_result() :: Reason::bitstring() | no_connection
-type redis_error_result() :: Reason::binary() | no_connection
| invalid_cluster_command | tcp_closed.
-type redis_success_result() :: bitstring() | [bitstring()] | undefined.
-type redis_success_result() :: binary() | [binary()] | undefined.
-type redis_simple_result() :: {ok, redis_success_result()}
| {error, redis_error_result()}.
-type redis_pipeline_result() :: [redis_simple_result()]
Expand Down
14 changes: 7 additions & 7 deletions src/eredis_cluster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ transaction_retry_loop(Cluster, Transaction, SlotOrPool, Counter) ->
%% This is equivalent to calling `qa(["FLUSHDB"])' except for the return value.
%% @end
%% =============================================================================
-spec flushdb() -> ok | {error, Reason::bitstring()}.
-spec flushdb() -> ok | {error, redis_error_result()}.
flushdb() ->
case qa(["FLUSHDB"]) of
Result when is_list(Result) ->
Expand Down Expand Up @@ -1053,8 +1053,8 @@ get_all_pools(Cluster) ->
%% @end
%% =============================================================================
-spec get_key_slot(Key::anystring()) -> Slot::integer().
get_key_slot(Key) when is_bitstring(Key) ->
get_key_slot(bitstring_to_list(Key));
get_key_slot(Key) when is_binary(Key) ->
get_key_slot(binary_to_list(Key));
get_key_slot(Key) ->
KeyToBeHashed = case string:chr(Key, ${) of
0 ->
Expand Down Expand Up @@ -1092,17 +1092,17 @@ get_key_slot(Key) ->
%% @end
%% =============================================================================
-spec get_key_from_command(redis_command()) -> string() | undefined.
get_key_from_command([[X|Y]|Z]) when is_bitstring(X) ->
get_key_from_command([[bitstring_to_list(X)|Y]|Z]);
get_key_from_command([[X|Y]|Z]) when is_binary(X) ->
get_key_from_command([[binary_to_list(X)|Y]|Z]);
get_key_from_command([[X|Y]|Z]) when is_list(X) ->
case string:to_lower(X) of
"multi" ->
get_key_from_command(Z);
_ ->
get_key_from_command([X|Y])
end;
get_key_from_command([Name | Args]) when is_bitstring(Name) ->
get_key_from_command([bitstring_to_list(Name) | Args]);
get_key_from_command([Name | Args]) when is_binary(Name) ->
get_key_from_command([binary_to_list(Name) | Args]);
get_key_from_command([Name | [Arg|_Rest]=Args]) ->
case string:to_lower(Name) of
"info" -> undefined;
Expand Down
14 changes: 7 additions & 7 deletions src/eredis_cluster_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ remove_list_elements(Xs, Ys) ->
%% @doc Get cluster slots information.
%% @end
%% =============================================================================
-spec get_cluster_slots() -> [[bitstring() | [bitstring()]]].
-spec get_cluster_slots() -> [[binary() | [binary()]]].
get_cluster_slots() ->
get_cluster_slots(?default_cluster).

%% @private
-spec get_cluster_slots(Cluster :: atom()) -> [[bitstring() | [bitstring()]]].
-spec get_cluster_slots(Cluster :: atom()) -> [[binary() | [binary()]]].
get_cluster_slots(Cluster) ->
State = get_state(Cluster),
Options = get_current_options(State),
Expand All @@ -202,18 +202,18 @@ get_cluster_slots(State, Options) ->
%% See: https://redis.io/commands/cluster-nodes#serialization-format
%% @end
%% =============================================================================
-spec get_cluster_nodes() -> [[bitstring()]].
-spec get_cluster_nodes() -> [[binary()]].
get_cluster_nodes() ->
get_cluster_nodes(?default_cluster).

-spec get_cluster_nodes(Cluster :: atom()) -> [[bitstring()]].
-spec get_cluster_nodes(Cluster :: atom()) -> [[binary()]].
get_cluster_nodes(Cluster) ->
State = get_state(Cluster),
Options = get_current_options(State),
get_cluster_nodes(State, Options).

%% @private
-spec get_cluster_nodes(State :: #state{}, Options :: options()) -> [[bitstring()]].
-spec get_cluster_nodes(State :: #state{}, Options :: options()) -> [[binary()]].
get_cluster_nodes(State, Options) ->
Query = ["CLUSTER", "NODES"],
FailFn = fun(_Node) -> "" end, %% No default data to use when query fails
Expand Down Expand Up @@ -341,12 +341,12 @@ get_cluster_info_from_connection(Connection, Query, FailFn, Node) ->
end.

-spec get_cluster_slots_from_single_node(#node{}) ->
[[bitstring() | [bitstring()]]].
[[binary() | [binary()]]].
get_cluster_slots_from_single_node(Node) ->
[[<<"0">>, integer_to_binary(?REDIS_CLUSTER_HASH_SLOTS-1),
[list_to_binary(Node#node.address), integer_to_binary(Node#node.port)]]].

-spec parse_cluster_slots(ClusterInfo::[[bitstring() | [bitstring()]]],
-spec parse_cluster_slots(ClusterInfo::[[binary() | [binary()]]],
Options::options()) -> [#slots_map{}].
parse_cluster_slots(ClusterInfo, Options) ->
SlotsMaps = parse_cluster_slots(ClusterInfo, 1, []),
Expand Down
6 changes: 3 additions & 3 deletions test/eredis_cluster_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ basic_test_() ->
end
},

{ "bitstring support",
{ "binary support",
fun () ->
eredis_cluster:q([<<"set">>, <<"bitstring">>,<<"support">>]),
?assertEqual({ok, <<"support">>}, eredis_cluster:q([<<"GET">>, <<"bitstring">>]))
eredis_cluster:q([<<"set">>, <<"binary">>, <<"support">>]),
?assertEqual({ok, <<"support">>}, eredis_cluster:q([<<"GET">>, <<"binary">>]))
end
},

Expand Down

0 comments on commit b7b7be2

Please sign in to comment.