Compare commits

2 Commits

Author SHA1 Message Date
niamtokik
d97d328fd2 clean a little bit 2021-04-19 19:34:36 +00:00
niamtokik
691a4824ae add few commands 2021-04-19 19:24:12 +00:00
3 changed files with 164 additions and 19 deletions

View File

@@ -5,20 +5,7 @@
-module(redis).
-export([encode/1, decode/1]).
-include_lib("eunit/include/eunit.hrl").
%-type encode_error() :: {error, bitstring() | binary()}.
-type encode_integer() :: integer().
-type encode_string() :: bitstring() | binary().
-type encode_bulk_string() :: {bulk_string, bitstring() | null} | list() | {error, binary()}.
-type encode_array() :: [encode_integer() |
encode_string() |
encode_bulk_string()].
-type encode_types() :: encode_integer() |
encode_string() |
encode_bulk_string() |
encode_array().
-type encode_return_ok() :: bitstring() | binary().
-type encode_return_error() :: {error, atom()}.
-include("redis.hrl").
%%-------------------------------------------------------------------
%% @doc
@@ -178,7 +165,8 @@ decode_test() ->
].
%%--------------------------------------------------------------------
%%
%% @doc
%% @end
%%--------------------------------------------------------------------
decode_string(Bitstring) ->
decode_string(Bitstring, <<>>).
@@ -195,7 +183,8 @@ decode_string(<<Char, Rest/bitstring>>, Buffer) ->
decode_string(Rest, <<Buffer/bitstring, Char>>).
%%--------------------------------------------------------------------
%%
%% @doc
%% @end
%%--------------------------------------------------------------------
decode_integer(Integer) ->
decode_integer(Integer, <<>>).
@@ -219,7 +208,8 @@ decode_integer(<<Char, Rest/bitstring>>, Buffer)
% end.
%%--------------------------------------------------------------------
%%
%% @doc
%% @end
%%--------------------------------------------------------------------
decode_bulk_string(<<"-1\r\n">>) ->
{ok, nil};
@@ -235,7 +225,8 @@ decode_bulk_string(Bitstring) ->
end.
%%--------------------------------------------------------------------
%%
%% @doc
%% @end
%%--------------------------------------------------------------------
decode_array(<<"0\r\n">>) ->
{ok, []};
@@ -258,7 +249,8 @@ decode_array(Bitstring, Size, Buffer) ->
end.
%%--------------------------------------------------------------------
%%
%% @doc
%% @end
%%--------------------------------------------------------------------
decode_error(Bitstring) ->
decode_error(Bitstring, <<>>).

13
src/redis.hrl Normal file
View File

@@ -0,0 +1,13 @@
%-type encode_error() :: {error, bitstring() | binary()}.
-type encode_integer() :: integer().
-type encode_string() :: bitstring() | binary().
-type encode_bulk_string() :: {bulk_string, bitstring() | null} | list() | {error, binary()}.
-type encode_array() :: [encode_integer() |
encode_string() |
encode_bulk_string()].
-type encode_types() :: encode_integer() |
encode_string() |
encode_bulk_string() |
encode_array().
-type encode_return_ok() :: bitstring() | binary().
-type encode_return_error() :: {error, atom()}.

140
src/redis_command.erl Normal file
View File

@@ -0,0 +1,140 @@
%%%-------------------------------------------------------------------
%%% @doc https://redis.io/commands#
%%%
%%% @todo copy
%%% @todo del
%%% @todo set
%%% @todo get
%%% @todo getdel
%%% @todo strlen
%%% @todo append
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(redis_command).
-compile({no_auto_import,[get/0]}).
-include("redis.hrl").
-export([copy/0, copy/2, copy/3]).
-export([del/0, del/1]).
-export([set/0, set/2, set/3]).
-export([get/0, get/1]).
%%--------------------------------------------------------------------
%% @doc copy/0.
%% @end
%%--------------------------------------------------------------------
-spec copy() -> Return when
Return :: encode_bulk_string().
copy() ->
{bulk_string, <<"COPY">>}.
%%--------------------------------------------------------------------
%% @doc copy/2.
%% @end
%%--------------------------------------------------------------------
-spec copy(Source, Destination) -> Return when
Source :: encode_bulk_string(),
Destination :: encode_bulk_string(),
Return :: any().
copy(Source, Destination) ->
copy(Source, Destination, []).
%%--------------------------------------------------------------------
%% @doc copy/3.
%% @end
%%--------------------------------------------------------------------
-spec copy(Source, Destination, Args) -> Return when
Source :: encode_bulk_string(),
Destination :: encode_bulk_string(),
Args :: list(),
Return :: any().
copy(Source, Destination, _Args) ->
Command = [copy(), Source, Destination],
redis:encode(Command).
%%--------------------------------------------------------------------
%% @doc del/0.
%% @end
%%--------------------------------------------------------------------
-spec del() -> Return when
Return :: encode_bulk_string().
del() -> {bulk_string, <<"DEL">>}.
%%--------------------------------------------------------------------
%% @doc del/1.
%% @end
%%--------------------------------------------------------------------
-spec del(Keys) -> Return when
Keys :: encode_bulk_string(),
Return :: any().
del(Keys) ->
Command = [del()|Keys],
redis:encode(Command).
%%--------------------------------------------------------------------
%% @doc set/0.
%% @end
%%--------------------------------------------------------------------
-spec set() -> Return when
Return :: encode_bulk_string().
set() ->
{bulk_string, <<"SET">>}.
%%--------------------------------------------------------------------
%% @doc set/0.
%% @end
%%--------------------------------------------------------------------
-spec set(Key, Value) -> Return when
Key :: encode_bulk_string(),
Value :: encode_types(),
Return :: any().
set(Key, Value) ->
set(Key, Value, []).
%%--------------------------------------------------------------------
%% @doc set/0.
%% @end
%%--------------------------------------------------------------------
-spec set(Key, Value, Args) -> Return when
Key :: encode_bulk_string(),
Value :: encode_types(),
Args :: list(),
Return :: any().
set(Key, Value, _Args) ->
Command = [set(), Key, Value],
redis:encode(Command).
%%--------------------------------------------------------------------
%% @doc get/0.
%% @end
%%--------------------------------------------------------------------
-spec get() -> Return when
Return :: encode_bulk_string().
get() ->
{bulk_string, <<"GET">>}.
%%--------------------------------------------------------------------
%% @doc get/0.
%% @end
%%--------------------------------------------------------------------
-spec get(Key) -> Return when
Key :: encode_bulk_string(),
Return :: any().
get(Key) ->
Command = [get(), Key],
redis:encode(Command).
%%--------------------------------------------------------------------
%% @doc getdel/0.
%% @end
%%--------------------------------------------------------------------