cabol / shards

Partitioned ETS tables for Erlang and Elixir

Home Page:https://hexdocs.pm/shards

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

update_counter spec is not the same as ETS.

pggalaviz opened this issue · comments

The current shards spec expects an integer result when calling update_counter/3, however ETS allows to pass a list of operations:

%%  From Erlang docs
update_counter(Tab, Key, X3 :: [UpdateOp]) -> [Result]

This unfortunately is messing with dialyzer since I'm calling something like this in distributed mode:

case Shards.update_counter(@table, key, [{2, increment}, {4, 1, 0, now}]) do
   [count, _] when count < limit ->
      {:ok, count}

   # key not found, insert it
   {:badrpc, {:EXIT, {:badarg, _trace}}} -> 
      true = Shards.insert(@table, {key, increment, now, now})
      {:ok, increment}

   _other ->
      {:error, :invalid}
end

From dialyzer I get:

The pattern
[_count, _]

can never match the type
integer()
________________________________________________________________________________
The pattern
{:badrpc, {:EXIT, {:badarg, __trace}}}

can never match the type
integer()

Is this just a matter of updating the function spec?

@pggalaviz you're right, there is an issue with the spec, it should be integer() | [integer()]. I will fix it. Thanks!

@pggalaviz it is fixed, please try again and let me know if it works for you now, so we can close the issue. Stay tuned!

Working fine now!
I still get the {:badrpc, {:EXIT, {:badarg, __trace}}} error message if a key is not found, dialyzer complains as it's not part of the function's spec.
But I guess this is out of shards boundary?

Thanks for the quick fix and for sharing your work!

Actually, checking out the code in shards_dist, there is an issue with the specs too, the case when rpc returns an error {:badrpc, Reason} is not being considered. Thanks for report it!

I'll open a new issue with this and will work on it as soon as possible!

Check out: Issue #43

I will close this issue for now, but feel free to re open it if the error persist!!