Current section
Files
Jump to
Current section
Files
src/radish.erl
-module(radish).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([connect/2, get/2, set/4, set_new/4, set_existing/4, del/2, incr/2, incr_by/3, incr_by_float/3, decr/2, decr_by/3, exists/2, keys/2]).
-spec execute(mug:socket(), bitstring()) -> {ok, radish@resp:value()} |
{error, radish@error:error()}.
execute(Socket, Cmd) ->
_pipe = Cmd,
_pipe@1 = radish@tcp:execute(Socket, _pipe, 1024),
_pipe@2 = gleam@result:map_error(
_pipe@1,
fun(Tcp_error) -> {tcp_error, Tcp_error} end
),
_pipe@3 = gleam@result:map(_pipe@2, fun radish@decoder:decode/1),
_pipe@4 = gleam@result:flatten(_pipe@3),
_pipe@5 = gleam@result:map(_pipe@4, fun(Value) -> case Value of
{simple_error, Error} ->
{error, {server_error, Error}};
{bulk_error, Error} ->
{error, {server_error, Error}};
Value@1 ->
{ok, Value@1}
end end),
gleam@result:flatten(_pipe@5).
-spec connect(binary(), integer()) -> {ok, mug:socket()} | {error, mug:error()}.
connect(Host, Port) ->
_pipe = radish@tcp:connect(Host, Port, 1024),
gleam@result:map(
_pipe,
fun(Socket) ->
_pipe@1 = radish@command:hello(3),
execute(Socket, _pipe@1),
Socket
end
).
-spec get(mug:socket(), binary()) -> {ok, binary()} |
{error, radish@error:error()}.
get(Socket, Key) ->
_pipe = radish@command:get(Key),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value) -> case Value of
{simple_string, Str} ->
{ok, Str};
{bulk_string, Str} ->
{ok, Str};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec set(mug:socket(), binary(), binary(), gleam@option:option(integer())) -> {ok,
binary()} |
{error, radish@error:error()}.
set(Socket, Key, Value, Ttl) ->
case Ttl of
none ->
radish@command:set(Key, Value, []);
{some, Ttl@1} ->
radish@command:set(Key, Value, [{px, Ttl@1}])
end,
_pipe = radish@command:set(Key, Value, []),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value@1) -> case Value@1 of
{simple_string, Str} ->
{ok, Str};
{bulk_string, Str} ->
{ok, Str};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec set_new(mug:socket(), binary(), binary(), gleam@option:option(integer())) -> {ok,
binary()} |
{error, radish@error:error()}.
set_new(Socket, Key, Value, Ttl) ->
case Ttl of
none ->
radish@command:set(Key, Value, [nx]);
{some, Ttl@1} ->
radish@command:set(Key, Value, [nx, {px, Ttl@1}])
end,
_pipe = radish@command:set(Key, Value, [nx]),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value@1) -> case Value@1 of
{simple_string, Str} ->
{ok, Str};
{bulk_string, Str} ->
{ok, Str};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec set_existing(
mug:socket(),
binary(),
binary(),
gleam@option:option(integer())
) -> {ok, binary()} | {error, radish@error:error()}.
set_existing(Socket, Key, Value, Ttl) ->
_pipe = case Ttl of
none ->
radish@command:set(Key, Value, [xx, get]);
{some, Ttl@1} ->
radish@command:set(Key, Value, [xx, get, {px, Ttl@1}])
end,
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value@1) -> case Value@1 of
{simple_string, Str} ->
{ok, Str};
{bulk_string, Str} ->
{ok, Str};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec del(mug:socket(), list(binary())) -> {ok, integer()} |
{error, radish@error:error()}.
del(Socket, Keys) ->
_pipe = radish@command:del(Keys),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value) -> case Value of
{integer, N} ->
{ok, N};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec incr(mug:socket(), binary()) -> {ok, integer()} |
{error, radish@error:error()}.
incr(Socket, Key) ->
_pipe = radish@command:incr(Key),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value) -> case Value of
{integer, New} ->
{ok, New};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec incr_by(mug:socket(), binary(), integer()) -> {ok, integer()} |
{error, radish@error:error()}.
incr_by(Socket, Key, Value) ->
_pipe = radish@command:incr_by(Key, Value),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value@1) -> case Value@1 of
{integer, New} ->
{ok, New};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec incr_by_float(mug:socket(), binary(), float()) -> {ok, float()} |
{error, radish@error:error()}.
incr_by_float(Socket, Key, Value) ->
_pipe = radish@command:incr_by_float(Key, Value),
_pipe@1 = execute(Socket, _pipe),
_pipe@3 = gleam@result:map(_pipe@1, fun(Value@1) -> case Value@1 of
{bulk_string, New} ->
_pipe@2 = gleam@float:parse(New),
gleam@result:replace_error(_pipe@2, resp_error);
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@3).
-spec decr(mug:socket(), binary()) -> {ok, integer()} |
{error, radish@error:error()}.
decr(Socket, Key) ->
_pipe = radish@command:incr(Key),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value) -> case Value of
{integer, New} ->
{ok, New};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec decr_by(mug:socket(), binary(), integer()) -> {ok, integer()} |
{error, radish@error:error()}.
decr_by(Socket, Key, Value) ->
_pipe = radish@command:incr_by(Key, Value),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value@1) -> case Value@1 of
{integer, New} ->
{ok, New};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec exists(mug:socket(), list(binary())) -> {ok, integer()} |
{error, radish@error:error()}.
exists(Socket, Keys) ->
_pipe = radish@command:exists(Keys),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value) -> case Value of
{integer, N} ->
{ok, N};
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).
-spec keys(mug:socket(), binary()) -> {ok, list(binary())} |
{error, radish@error:error()}.
keys(Socket, Pattern) ->
_pipe = radish@command:keys(Pattern),
_pipe@1 = execute(Socket, _pipe),
_pipe@2 = gleam@result:map(_pipe@1, fun(Value) -> case Value of
{array, Array} ->
gleam@list:try_map(Array, fun(Item) -> case Item of
{bulk_string, Value@1} ->
{ok, Value@1};
_ ->
{error, resp_error}
end end);
_ ->
{error, resp_error}
end end),
gleam@result:flatten(_pipe@2).