Convert geef_odb to a gen_server
This commit is contained in:
103
src/geef_odb.erl
103
src/geef_odb.erl
@@ -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).
|
-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).
|
-ifdef(TEST).
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
-endif.
|
-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 write(pid(), iolist(), atom()) -> {ok, geef_oid()} | {error, term}.
|
||||||
|
write(Pid, Contents, Type) ->
|
||||||
-spec exists(geef_odb(), geef_oid() | iolist()) -> boolean().
|
gen_server:call(Pid, {write, Contents, Type}).
|
||||||
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.
|
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
-record(geef_ref, {handle, name :: binary(), type :: atom(), target :: binary() | geef_oid()}).
|
-record(geef_ref, {handle, name :: binary(), type :: atom(), target :: binary() | geef_oid()}).
|
||||||
-record(geef_odb, {handle}).
|
|
||||||
-record(geef_oid, {oid}).
|
-record(geef_oid, {oid}).
|
||||||
-record(geef_object, {type :: atom(), handle}).
|
-record(geef_object, {type :: atom(), handle}).
|
||||||
-record(geef_index_entry, {mode, id :: geef_oid(), path :: iolist()}).
|
-record(geef_index_entry, {mode, id :: geef_oid(), path :: iolist()}).
|
||||||
|
|
||||||
-type geef_ref() :: #geef_ref{}.
|
-type geef_ref() :: #geef_ref{}.
|
||||||
-type geef_odb() :: #geef_odb{}.
|
|
||||||
-type geef_oid() :: #geef_oid{}.
|
-type geef_oid() :: #geef_oid{}.
|
||||||
-type geef_object() :: #geef_object{}.
|
-type geef_object() :: #geef_object{}.
|
||||||
-type geef_index_entry() :: #geef_index_entry{}.
|
-type geef_index_entry() :: #geef_index_entry{}.
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ workdir(Pid) ->
|
|||||||
|
|
||||||
%% @doc The repository's current object database. This encompasses all
|
%% @doc The repository's current object database. This encompasses all
|
||||||
%% the configured backends.
|
%% the configured backends.
|
||||||
-spec odb(pid()) -> {ok, geef_odb()} | {error, term}.
|
-spec odb(pid()) -> {ok, pid()} | {error, term}.
|
||||||
odb(Pid) ->
|
odb(Pid) ->
|
||||||
gen_server:call(Pid, odb).
|
gen_server:call(Pid, odb).
|
||||||
|
|
||||||
@@ -176,7 +176,7 @@ start_link(Handle) ->
|
|||||||
handle_odb(Handle) ->
|
handle_odb(Handle) ->
|
||||||
case geef_nif:repository_get_odb(Handle) of
|
case geef_nif:repository_get_odb(Handle) of
|
||||||
{ok, OdbHandle} ->
|
{ok, OdbHandle} ->
|
||||||
{ok, #geef_odb{handle=OdbHandle}};
|
geef_odb:start_link(OdbHandle);
|
||||||
Other ->
|
Other ->
|
||||||
Other
|
Other
|
||||||
end.
|
end.
|
||||||
|
|||||||
Reference in New Issue
Block a user