Files
geef/src/geef_index.erl
Carlos Martín Nieto 4a2f2e8ab2 Namespace the records
This makes it useful for users of the bindings. The naming is the same
as the module to allow elixir to treat the records in much the same
way as its native module/record hybrid.
2013-05-13 11:05:59 +02:00

124 lines
3.7 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @author Carlos Martin Nieto <cmn@dwim.me>
%%% @copyright (C) 2013, Carlos Martin Nieto
%%% @doc
%%%
%%% @end
%%% Created : 25 Apr 2013 by Carlos Martin Nieto <cmn@dwim.me>
%%%-------------------------------------------------------------------
-module(geef_index).
-behaviour(gen_server).
%% API
-export([start_link/1]).
-export([new/0, write/1, write_tree/1, write_tree/2, clear/1, stop/1, read_tree/2, add/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {handle}).
-include("geef_records.hrl").
%%%===================================================================
%%% API
%%%===================================================================
%% @doc Create a new index. This index is empty and not associated to
%% any repository.
-spec new() -> pid().
new() ->
{ok, Handle} = geef_nif:index_new(),
start_link(Handle).
%% @doc Write out the index file. This is only possible for indices
%% that are associated with a repository.
-spec write(pid()) -> ok | {error, term()}.
write(Pid) ->
gen_server:call(Pid, write).
%% @doc Write out the index's contents to its the repository.
-spec write_tree(pid()) -> {ok, geef_oid()} | {error, term()}.
write_tree(Pid) ->
gen_server:call(Pid, write_tree).
%% @doc Write out the index's contents to the given repository
-spec write_tree(pid(), pid()) -> {ok, geef_oid()} | {error, term()}.
write_tree(Pid, Repo) ->
gen_server:call(Pid, {write_tree, Repo}).
read_tree(Pid, #geef_object{type=tree, handle=TreeHandle}) ->
gen_server:call(Pid, {read_tree, TreeHandle}).
%% @doc Add an entry to the index
-spec add(pid(), geef_index_entry()) -> ok | {error, term()}.
add(Pid, Entry) ->
gen_server:call(Pid, {add, Entry}).
%% @doc Clear the contents of the index.
-spec clear(pid()) -> ok.
clear(Pid) ->
gen_server:call(Pid, clear).
-spec stop(pid()) -> ok.
stop(Pid) ->
gen_server:call(Pid, stop).
%% @private
start_link(Handle) ->
gen_server:start_link(?MODULE, Handle, []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%% @private
init(Handle) ->
{ok, #state{handle=Handle}}.
%% @private
handle_call(write, _From, State = #state{handle=Handle}) ->
Reply = geef_nif:index_write(Handle),
{reply, Reply, State};
handle_call(write_tree, _From, State = #state{handle=Handle}) ->
{ok, Oid} = geef_nif:index_write_tree(Handle),
Reply = #geef_oid{oid=Oid},
{reply, Reply, State};
handle_call({write_tree, Repo}, _From, State = #state{handle=Handle}) ->
RepoHandle = geef_repo:handle(Repo),
{ok, Oid} = geef_nif:index_write_tree(Handle, RepoHandle),
Reply = {ok, #geef_oid{oid=Oid}},
{reply, Reply, State};
handle_call(clear, _From, State = #state{handle=Handle}) ->
Reply = geef_nif:index_clear(Handle),
{reply, Reply, State};
handle_call(stop, _From, State) ->
{stop, normal, ok, State};
handle_call({read_tree, TreeHandle}, _From, State = #state{handle=Handle}) ->
Reply = geef_nif:index_read_tree(Handle, TreeHandle),
{reply, Reply, State};
handle_call({add, Entry}, _From, State = #state{handle=Handle}) ->
Reply = geef_nif:index_add(Handle, Entry),
{reply, Reply, 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
%%%===================================================================