Skip to content

Commit

Permalink
Type specs: maybe/1 => option/1
Browse files Browse the repository at this point in the history
so that we don't have to think about quoting.
'maybe' is a keyword in Erlang 25+ and as of 26,
it is an always-on language feature.

Closes #401
  • Loading branch information
michaelklishin committed Nov 1, 2023
1 parent 4e417c0 commit 517e21c
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/ra.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%%
%% Copyright (c) 2017-2022 VMware, Inc. or its affiliates. All rights reserved.
%%
-type 'maybe'(T) :: undefined | T.
-type option(T) :: undefined | T.

%%
%% Most of the records here are covered on Figure 2
Expand Down
8 changes: 4 additions & 4 deletions src/ra_directory.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ deinit(#{directory := Name,
_ = dets:close(NameRev),
ok.

-spec register_name(ra_system:names() | atom(), ra_uid(), pid(), 'maybe'(pid()), atom(),
-spec register_name(ra_system:names() | atom(), ra_uid(), pid(), option(pid()), atom(),
ra_cluster_name()) -> ok.
register_name(System, UId, Pid, ParentPid, ServerName, ClusterName)
when is_atom(System) ->
Expand Down Expand Up @@ -135,7 +135,7 @@ where_is_parent(#{directory := Dir}, UId) when is_binary(UId) ->
[] -> undefined
end.

-spec name_of(atom() | ra_system:names(), ra_uid()) -> 'maybe'(atom()).
-spec name_of(atom() | ra_system:names(), ra_uid()) -> option(atom()).
name_of(SystemOrNames, UId) ->
Tbl = get_name(SystemOrNames),
case ets:lookup(Tbl, UId) of
Expand All @@ -144,7 +144,7 @@ name_of(SystemOrNames, UId) ->
end.

-spec cluster_name_of(ra_system:names() | atom(), ra_uid()) ->
'maybe'(ra_cluster_name()).
option(ra_cluster_name()).
cluster_name_of(SystemOrNames, UId) ->
Tbl = get_name(SystemOrNames),
case ets:lookup(Tbl, UId) of
Expand All @@ -153,7 +153,7 @@ cluster_name_of(SystemOrNames, UId) ->
end.


-spec pid_of(atom() | ra_system:names(), ra_uid()) -> 'maybe'(pid()).
-spec pid_of(atom() | ra_system:names(), ra_uid()) -> option(pid()).
pid_of(SystemOrNames, UId) ->
case ets:lookup(get_name(SystemOrNames), UId) of
[{_, Pid, _, _, _}] -> Pid;
Expand Down
10 changes: 5 additions & 5 deletions src/ra_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
% if this is set a snapshot write is in progress for the
% index specified
cache = ra_log_cache:init() :: ra_log_cache:state(),
last_resend_time :: 'maybe'(integer()),
last_resend_time :: option(integer()),
reader :: ra_log_reader:state(),
readers = [] :: [pid()]
}).
Expand Down Expand Up @@ -546,7 +546,7 @@ next_index(#?MODULE{last_index = LastIdx}) ->
LastIdx + 1.

-spec fetch(ra_index(), state()) ->
{maybe(log_entry()), state()}.
{option(log_entry()), state()}.
fetch(Idx, State0) ->
case fold(Idx, Idx, fun(E, Acc) -> [E | Acc] end, [], State0) of
{[], State} ->
Expand All @@ -556,7 +556,7 @@ fetch(Idx, State0) ->
end.

-spec fetch_term(ra_index(), state()) ->
{'maybe'(ra_term()), state()}.
{option(ra_term()), state()}.
fetch_term(Idx, #?MODULE{last_index = LastIdx,
first_index = FirstIdx} = State0)
when Idx < FirstIdx orelse Idx > LastIdx ->
Expand Down Expand Up @@ -598,7 +598,7 @@ install_snapshot({SnapIdx, _} = IdxTerm, SnapState,
Effs}.

-spec recover_snapshot(State :: state()) ->
'maybe'({ra_snapshot:meta(), term()}).
option({ra_snapshot:meta(), term()}).
recover_snapshot(#?MODULE{snapshot_state = SnapState}) ->
case ra_snapshot:recover(SnapState) of
{ok, Meta, MacState} ->
Expand All @@ -607,7 +607,7 @@ recover_snapshot(#?MODULE{snapshot_state = SnapState}) ->
undefined
end.

-spec snapshot_index_term(State :: state()) -> 'maybe'(ra_idxterm()).
-spec snapshot_index_term(State :: state()) -> option(ra_idxterm()).
snapshot_index_term(#?MODULE{snapshot_state = SS}) ->
ra_snapshot:current(SS).

Expand Down
12 changes: 6 additions & 6 deletions src/ra_log_segment.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
max_count = ?SEGMENT_MAX_ENTRIES :: non_neg_integer(),
max_pending = ?SEGMENT_MAX_PENDING :: non_neg_integer(),
filename :: file:filename_all(),
fd :: 'maybe'(file:io_device()),
fd :: option(file:io_device()),
index_size :: pos_integer(),
access_pattern :: sequential | random,
mode = append :: read | append,
Expand All @@ -59,8 +59,8 @@
data_start :: pos_integer(),
data_offset :: pos_integer(),
data_write_offset :: pos_integer(),
index = undefined :: maybe(ra_segment_index()),
range :: 'maybe'({ra_index(), ra_index()}),
index = undefined :: option(ra_segment_index()),
range :: option({ra_index(), ra_index()}),
pending_data = [] :: iodata(),
pending_index = [] :: iodata(),
pending_count = 0 :: non_neg_integer(),
Expand Down Expand Up @@ -331,7 +331,7 @@ map_get_(Key, Map) when is_map_key(Key, Map) ->
map_get_(Key, _Map) ->
exit({missing_key, Key}).

-spec term_query(state(), Idx :: ra_index()) -> 'maybe'(ra_term()).
-spec term_query(state(), Idx :: ra_index()) -> option(ra_term()).
term_query(#state{index = Index}, Idx) ->
case Index of
#{Idx := {Term, _, _, _}} ->
Expand Down Expand Up @@ -367,7 +367,7 @@ fold0(Cfg, Cache0, Idx, FinalIdx, Index, Fun, AccFun, Acc0) ->
Cfg#cfg.filename})
end.

-spec range(state()) -> maybe({ra_index(), ra_index()}).
-spec range(state()) -> option({ra_index(), ra_index()}).
range(#state{range = Range}) ->
Range.

Expand All @@ -379,7 +379,7 @@ max_count(#state{cfg = #cfg{max_count = Max}}) ->
filename(#state{cfg = #cfg{filename = Fn}}) ->
Fn.

-spec segref(state()) -> 'maybe'(ra_log:segment_ref()).
-spec segref(state()) -> option(ra_log:segment_ref()).
segref(#state{range = undefined}) ->
undefined;
segref(#state{range = {Start, End},
Expand Down
6 changes: 3 additions & 3 deletions src/ra_log_wal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
compress_mem_tables = false :: boolean()
}).

-record(wal, {fd :: 'maybe'(file:io_device()),
filename :: 'maybe'(file:filename()),
-record(wal, {fd :: option(file:io_device()),
filename :: option(file:filename()),
writer_name_cache = {0, #{}} :: writer_name_cache(),
max_size :: non_neg_integer(),
entry_count = 0 :: non_neg_integer()
Expand All @@ -117,7 +117,7 @@
% and the last index seen
writers = #{} :: #{ra_uid() =>
{in_seq | out_of_seq, ra_index()}},
batch :: 'maybe'(#batch{})
batch :: option(#batch{})
}).

-type state() :: #state{}.
Expand Down
10 changes: 5 additions & 5 deletions src/ra_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@

-type ra_server_state() ::
#{cfg := #cfg{},
leader_id => 'maybe'(ra_server_id()),
leader_id => option(ra_server_id()),
cluster := ra_cluster(),
cluster_change_permitted := boolean(),
cluster_index_term := ra_idxterm(),
previous_cluster => {ra_index(), ra_term(), ra_cluster()},
current_term := ra_term(),
log := term(),
voted_for => 'maybe'(ra_server_id()), % persistent
voted_for => option(ra_server_id()), % persistent
votes => non_neg_integer(),
membership => ra_membership(),
commit_index := ra_index(),
Expand All @@ -87,7 +87,7 @@
query_index := non_neg_integer(),
queries_waiting_heartbeats := queue:queue({non_neg_integer(), consistent_query_ref()}),
pending_consistent_queries := [consistent_query_ref()],
commit_latency => 'maybe'(non_neg_integer())
commit_latency => option(non_neg_integer())
}.

-type ra_state() :: leader | follower | candidate
Expand Down Expand Up @@ -1498,15 +1498,15 @@ uid(#{cfg := #cfg{uid = UId}}) -> UId.
-spec system_config(ra_server_state()) -> ra_system:config().
system_config(#{cfg := #cfg{system_config = SC}}) -> SC.

-spec leader_id(ra_server_state()) -> 'maybe'(ra_server_id()).
-spec leader_id(ra_server_state()) -> option(ra_server_id()).
leader_id(State) ->
maps:get(leader_id, State, undefined).

-spec clear_leader_id(ra_server_state()) -> ra_server_state().
clear_leader_id(State) ->
State#{leader_id => undefined}.

-spec current_term(ra_server_state()) -> 'maybe'(ra_term()).
-spec current_term(ra_server_state()) -> option(ra_term()).
current_term(State) ->
maps:get(current_term, State).

Expand Down
2 changes: 1 addition & 1 deletion src/ra_server_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
-type gen_statem_start_ret() :: {ok, pid()} | ignore | {error, term()}.

%% ra_event types
-type ra_event_reject_detail() :: {not_leader, Leader :: 'maybe'(ra_server_id()),
-type ra_event_reject_detail() :: {not_leader, Leader :: option(ra_server_id()),
ra_server:command_correlation()}.

-type ra_event_body() ::
Expand Down
16 changes: 8 additions & 8 deletions src/ra_snapshot.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
%% snapshot subdirs are store below
%% this as <data_dir>/snapshots/Term_Index
directory :: file:filename(),
pending :: 'maybe'({pid(), ra_idxterm()}),
accepting :: 'maybe'(#accept{}),
current :: 'maybe'(ra_idxterm())}).
pending :: option({pid(), ra_idxterm()}),
accepting :: option(#accept{}),
current :: option(ra_idxterm())}).

-define(ETSTBL, ra_log_snapshot_state).

Expand Down Expand Up @@ -197,14 +197,14 @@ init_ets() ->
_ = ets:new(?ETSTBL, TableFlags),
ok.

-spec current(state()) -> 'maybe'(ra_idxterm()).
-spec current(state()) -> option(ra_idxterm()).
current(#?MODULE{current = Current}) -> Current.

-spec pending(state()) -> 'maybe'({pid(), ra_idxterm()}).
-spec pending(state()) -> option({pid(), ra_idxterm()}).
pending(#?MODULE{pending = Pending}) ->
Pending.

-spec accepting(state()) -> 'maybe'(ra_idxterm()).
-spec accepting(state()) -> option(ra_idxterm()).
accepting(#?MODULE{accepting = undefined}) ->
undefined;
accepting(#?MODULE{accepting = #accept{idxterm = Accepting}}) ->
Expand All @@ -213,7 +213,7 @@ accepting(#?MODULE{accepting = #accept{idxterm = Accepting}}) ->
-spec directory(state()) -> file:filename().
directory(#?MODULE{directory = Dir}) -> Dir.

-spec last_index_for(ra_uid()) -> 'maybe'(ra_index()).
-spec last_index_for(ra_uid()) -> option(ra_index()).
last_index_for(UId) ->
case ets:lookup(?ETSTBL, UId) of
[] -> undefined;
Expand Down Expand Up @@ -400,7 +400,7 @@ read_meta(Module, Location) ->
Module:read_meta(Location).

-spec current_snapshot_dir(state()) ->
'maybe'(file:filename()).
option(file:filename()).
current_snapshot_dir(#?MODULE{directory = Dir,
current = {Idx, Term}}) ->
make_snapshot_dir(Dir, Idx, Term);
Expand Down
10 changes: 5 additions & 5 deletions test/ra_fifo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
cancel.

-type protocol() ::
{enqueue, Sender :: 'maybe'(pid()), MsgSeq :: 'maybe'(msg_seqno()),
{enqueue, Sender :: option(pid()), MsgSeq :: option(msg_seqno()),
Msg :: raw_msg()} |
{checkout, Spec :: checkout_spec(), Customer :: customer_id()} |
{settle, MsgIds :: [msg_id()], Customer :: customer_id()} |
Expand Down Expand Up @@ -160,10 +160,10 @@
% customers that require further service are queued here
% needs to be part of snapshot
service_queue = queue:new() :: queue:queue(customer_id()),
dead_letter_handler :: 'maybe'(applied_mfa()),
cancel_customer_handler :: 'maybe'(applied_mfa()),
become_leader_handler :: 'maybe'(applied_mfa()),
metrics_handler :: 'maybe'(applied_mfa()),
dead_letter_handler :: option(applied_mfa()),
cancel_customer_handler :: option(applied_mfa()),
become_leader_handler :: option(applied_mfa()),
metrics_handler :: option(applied_mfa()),
prefix_msg_count = 0 :: non_neg_integer()
}).

Expand Down
6 changes: 3 additions & 3 deletions test/ra_fifo_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@

-record(state, {cluster_name :: ra_cluster_name(),
servers = [] :: [ra_server_id()],
leader :: 'maybe'(ra_server_id()),
leader :: option(ra_server_id()),
next_seq = 0 :: seq(),
last_applied :: 'maybe'(seq()),
last_applied :: option(seq()),
next_enqueue_seq = 1 :: seq(),
%% indicates that we've exceeded the soft limit
slow = false :: boolean(),
unsent_commands = #{} :: #{ra_fifo:customer_id() =>
{[seq()], [seq()], [seq()]}},
soft_limit = ?SOFT_LIMIT :: non_neg_integer(),
pending = #{} :: #{seq() =>
{'maybe'(term()), ra_fifo:command()}},
{option(term()), ra_fifo:command()}},
customer_deliveries = #{} :: #{ra_fifo:customer_tag() =>
seq()},
priority = normal :: normal | low,
Expand Down
10 changes: 5 additions & 5 deletions test/ra_log_memory.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
last_written = {0, 0} :: ra_idxterm(), % only here to fake the async api of the file based one
entries = #{0 => {0, undefined}} :: #{ra_term() => {ra_index(), term()}},
meta = #{} :: ra_log_memory_meta(),
snapshot :: 'maybe'(snapshot())}).
snapshot :: option(snapshot())}).

-type ra_log_memory_state() :: #state{} | ra_log:state().

Expand Down Expand Up @@ -127,7 +127,7 @@ sparse_take(Idx, Log, Num, Max, Res) ->
end.


-spec last_index_term(ra_log_memory_state()) -> 'maybe'(ra_idxterm()).
-spec last_index_term(ra_log_memory_state()) -> option(ra_idxterm()).
last_index_term(#state{last_index = LastIdx,
entries = Log,
snapshot = Snapshot}) ->
Expand Down Expand Up @@ -184,7 +184,7 @@ next_index(#state{last_index = LastIdx}) ->
LastIdx + 1.

-spec fetch(ra_index(), ra_log_memory_state()) ->
{'maybe'(log_entry()), ra_log_memory_state()}.
{option(log_entry()), ra_log_memory_state()}.
fetch(Idx, #state{entries = Log} = State) ->
case Log of
#{Idx := {T, D}} ->
Expand All @@ -193,7 +193,7 @@ fetch(Idx, #state{entries = Log} = State) ->
end.

-spec fetch_term(ra_index(), ra_log_memory_state()) ->
{'maybe'(ra_term()), ra_log_memory_state()}.
{option(ra_term()), ra_log_memory_state()}.
fetch_term(Idx, #state{entries = Log} = State) ->
case Log of
#{Idx := {T, _}} ->
Expand Down Expand Up @@ -229,7 +229,7 @@ recover_snapshot(#state{snapshot = {Meta, Data}}) ->


-spec read_meta(Key :: ra_log:ra_meta_key(), State :: ra_log_memory_state()) ->
'maybe'(term()).
option(term()).
read_meta(Key, #state{meta = Meta}) ->
maps:get(Key, Meta, undefined).

Expand Down

0 comments on commit 517e21c

Please sign in to comment.