Convert geef_odb to a gen_server

This commit is contained in:
Carlos Martín Nieto
2013-05-23 10:49:00 +02:00
parent 41113f3221
commit 7b1dacffc7
3 changed files with 88 additions and 21 deletions

View File

@@ -1,27 +1,96 @@
%%%-------------------------------------------------------------------
%%% @author Carlos Martin Nieto <cmn@dwim.me>
%%% @copyright (C) 2013, Carlos Martin Nieto
%%% @doc
%%%
%%% @end
%%% Created : 23 May 2013 by Carlos Martin Nieto <cmn@dwim.me>
%%%-------------------------------------------------------------------
-module(geef_odb).
-behaviour(gen_server).
%% API
-export([start_link/1, exists/2, write/3]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("geef_records.hrl").
-record(state, {handle}).
%%%===================================================================
%%% API
%%%===================================================================
%% @doc
%% Starts the server
-spec start_link(term()) -> {ok, pid()} | ignore | {error, term()}.
start_link(Handle) ->
gen_server:start_link(?MODULE, Handle, []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%% @private
init(Handle) ->
{ok, #state{handle=Handle}}.
%% @private
handle_call({exists, Oid}, _From, State = #state{handle=Handle}) ->
Reply = geef_nif:odb_object_exists(Handle, Oid),
{reply, Reply, State};
handle_call({write, Content, Type}, _From, State = #state{handle=Handle}) ->
Reply = case geef_nif:odb_write(Handle, Content, Type) of
{ok, Oid} ->
{ok, #geef_oid{oid=Oid}};
Other ->
Other
end,
{reply, Reply, State};
handle_call(stop, _From, State) ->
{stop, normal, ok, State}.
%% @private
handle_cast(_Msg, State) ->
{noreply, State}.
%% @private
handle_info(_Info, State) ->
{noreply, State}.
%% @private
terminate(_Reason, _State) ->
ok.
%% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-export([exists/2, write/3]).
-spec exists(pid(), geef_oid() | iolist()) -> boolean().
exists(Pid, #geef_oid{oid=Oid}) ->
gen_server:call(Pid, {exists, Oid});
exists(Pid, Sha) ->
#geef_oid{oid=Oid} = geef_oid:parse(Sha),
gen_server:call(Pid, {exists, Oid}).
-include("geef_records.hrl").
-spec exists(geef_odb(), geef_oid() | iolist()) -> boolean().
exists(#geef_odb{handle=Handle}, #geef_oid{oid=Oid}) ->
geef_nif:odb_object_exists(Handle, Oid);
exists(Odb = #geef_odb{}, Sha) ->
exists(Odb, geef_oid:parse(Sha)).
-spec write(geef_odb(), iolist(), atom()) -> {ok, geef_oid()} | {error, term}.
write(#geef_odb{handle=Handle}, Contents, Type) ->
case geef_nif:odb_write(Handle, Contents, Type) of
{ok, Oid} ->
{ok, #geef_oid{oid=Oid}};
Other ->
Other
end.
-spec write(pid(), iolist(), atom()) -> {ok, geef_oid()} | {error, term}.
write(Pid, Contents, Type) ->
gen_server:call(Pid, {write, Contents, Type}).
-ifdef(TEST).