From ddb3f34c277a5613402d9888609694a761a56f49 Mon Sep 17 00:00:00 2001 From: Nelson Vides Date: Tue, 17 Sep 2024 09:13:39 +0200 Subject: [PATCH] Improve types and compiler warnings --- rebar.config | 5 ++++- src/segmented_cache_helpers.erl | 1 + src/segmented_cache_server.erl | 11 ++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/rebar.config b/rebar.config index 7c73e43..a994b02 100644 --- a/rebar.config +++ b/rebar.config @@ -1,4 +1,7 @@ -{erl_opts, []}. +{erl_opts, + [warn_missing_doc, warn_missing_spec, warn_unused_import, + warn_export_vars, verbose, report, debug_info +]}. {deps, [ {telemetry, "1.3.0"} diff --git a/src/segmented_cache_helpers.erl b/src/segmented_cache_helpers.erl index fb21eac..2e65df7 100644 --- a/src/segmented_cache_helpers.erl +++ b/src/segmented_cache_helpers.erl @@ -238,6 +238,7 @@ compare_and_swap(Attempts, EtsSegment, Key, Value, MergerFun) -> %% Note that we must first empty the last table, and then rotate the index. If it was done %% in the opposite order, there's a chance a worker can insert an entry at the front just %% before the table is purged. +-spec purge_last_segment_and_rotate(segmented_cache:name()) -> non_neg_integer(). purge_last_segment_and_rotate(Name) -> SegmentRecord = get_cache_config(Name), Index = atomics:get(SegmentRecord#segmented_cache.index, 1), diff --git a/src/segmented_cache_server.erl b/src/segmented_cache_server.erl index 996f928..9210d22 100644 --- a/src/segmented_cache_server.erl +++ b/src/segmented_cache_server.erl @@ -10,11 +10,11 @@ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]). -type request_content() :: term(). - -record(cache_state, {scope :: segmented_cache:scope(), name :: segmented_cache:name(), ttl :: timeout(), timer_ref :: undefined | reference()}). +-type state() :: #cache_state{}. %%==================================================================== %% API @@ -40,7 +40,7 @@ request_delete_pattern(Name, Pattern) -> %% gen_server callbacks %%==================================================================== --spec init({segmented_cache:name(), segmented_cache:opts()}) -> {ok, #cache_state{}}. +-spec init({segmented_cache:name(), segmented_cache:opts()}) -> {ok, state()}. init({Name, Opts}) -> #{scope := Scope, ttl := TTL} = segmented_cache_helpers:init_cache_config(Name, Opts), pg:join(Scope, Name, self()), @@ -52,11 +52,11 @@ init({Name, Opts}) -> {ok, #cache_state{scope = Scope, name = Name, ttl = TTL, timer_ref = TimerRef}} end. --spec handle_call(any(), gen_server:from(), #cache_state{}) -> {reply, ok, #cache_state{}}. +-spec handle_call(any(), gen_server:from(), state()) -> {reply, ok, state()}. handle_call(_Msg, _From, State) -> {reply, ok, State}. --spec handle_cast(term(), #cache_state{}) -> {noreply, #cache_state{}}. +-spec handle_cast(term(), state()) -> {noreply, state()}. handle_cast({delete_entry, Key}, #cache_state{name = Name} = State) -> segmented_cache_helpers:delete_entry(Name, Key), {noreply, State}; @@ -66,7 +66,7 @@ handle_cast({delete_pattern, Pattern}, #cache_state{name = Name} = State) -> handle_cast(_Msg, State) -> {noreply, State}. --spec handle_info(any(), #cache_state{}) -> {noreply, #cache_state{}}. +-spec handle_info(any(), state()) -> {noreply, state()}. handle_info(purge, #cache_state{name = Name, ttl = TTL} = State) -> segmented_cache_helpers:purge_last_segment_and_rotate(Name), case TTL of @@ -76,6 +76,7 @@ handle_info(purge, #cache_state{name = Name, ttl = TTL} = State) -> handle_info(_Msg, State) -> {noreply, State}. +-spec terminate(normal | shutdown | {shutdown, term()} | term(), state()) -> term(). terminate(_Reason, #cache_state{name = Name, timer_ref = TimerRef}) -> segmented_cache_helpers:erase_cache_config(Name), case TimerRef of