Introduce geef_index

This commit is contained in:
Carlos Martín Nieto
2013-04-25 12:42:44 +02:00
parent 65227fe85b
commit 0a71085a40
7 changed files with 252 additions and 2 deletions

109
src/geef_index.erl Normal file
View File

@@ -0,0 +1,109 @@
%%%-------------------------------------------------------------------
%%% @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]).
%% 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, 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, oid()} | {error, term()}.
write_tree(Pid, Repo) ->
gen_server:call(Pid, {write_tree, Repo}).
%% @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 = #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 = #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}.
%% @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
%%%===================================================================

View File

@@ -22,6 +22,9 @@
% revision walker
-export([revwalk_new/1, revwalk_push/3, revwalk_next/1, revwalk_sorting/2, revwalk_reset/1]).
% index
-export([index_new/0, index_write/1, index_write_tree/1, index_write_tree/2, index_clear/1]).
-on_load(load_enif/0).
-define(NIF_FN, nif_error(?LINE)).
@@ -132,6 +135,21 @@ revwalk_sorting(_Walk, _Sort) ->
revwalk_reset(_Walk) ->
?NIF_FN.
index_new() ->
?NIF_FN.
index_write(_Handle) ->
?NIF_FN.
index_write_tree(_Handle) ->
?NIF_FN.
index_write_tree(_Handle, _RepoHandle) ->
?NIF_FN.
index_clear(_Handle) ->
?NIF_FN.
nif_error(Line) ->
erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}).

View File

@@ -30,7 +30,7 @@ lookup(Repo, Id, Type) ->
end.
id(#object{handle=Handle}) ->
case geef:object_id(Handle) of
case geef_nif:object_id(Handle) of
{ok, Oid} ->
#oid{oid=Oid};
Other ->

View File

@@ -16,7 +16,7 @@
%% API
-export([open/1, init/2, path/1, workdir/1, odb/1, is_bare/1, references/1, discover/1,
lookup_object/2, lookup_reference/2, revwalk/1, stop/1]).
lookup_object/2, lookup_reference/2, revwalk/1, stop/1, handle/1]).
-include("geef_records.hrl").
-record(state, {handle}).
@@ -92,6 +92,11 @@ revwalk(Pid) ->
stop(Pid) ->
gen_server:call(Pid, stop).
%% @private
%% @doc Get the underlying repo resource6
handle(Pid) ->
gen_server:call(Pid, handle).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================