ref: store the target in the record

There's no need to create a new object every time the user asks for
the target, so store it the same way we do for the type.
This commit is contained in:
Carlos Martín Nieto
2013-05-02 03:53:07 +02:00
parent 0127730f6b
commit 9d7139d604
2 changed files with 11 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
-record(ref, {handle, type :: atom()}).
-record(ref, {handle, type :: atom(), target :: binary() | oid()}).
-record(repo, {handle}).
-record(odb, {handle}).
-record(revwalk, {handle}).

View File

@@ -7,7 +7,14 @@
-spec new(term()) -> ref().
new(Handle) ->
Type = geef_nif:reference_type(Handle),
#ref{handle=Handle, type=Type}.
Bin = geef_nif:reference_target(Handle),
Target = case Type of
symbolic ->
Bin;
oid ->
#oid{oid=Bin}
end,
#ref{handle=Handle, type=Type, target=Target}.
-spec lookup(pid(), iolist()) -> {ok, ref()} | {error, term()}.
lookup(Repo, Refname) ->
@@ -28,11 +35,8 @@ resolve(#ref{handle=Handle}) ->
end.
-spec target(ref()) -> binary() | oid().
target(#ref{handle=Handle,type=symbolic}) ->
geef_nif:reference_target(Handle);
target(#ref{handle=Handle,type=oid}) ->
Oid = geef_nif:reference_target(Handle),
#oid{oid=Oid}.
target(#ref{target=Target}) ->
Target.
-spec name_to_id(repo(), iolist()) -> {ok, oid()} | {error, binary()}.
name_to_id(#repo{handle=Handle}, Name) ->