Add a way to get a blob's size and content
This commit is contained in:
10
src/geef.erl
10
src/geef.erl
@@ -11,7 +11,7 @@
|
||||
-export([oid_fmt/1, oid_parse/1]).
|
||||
|
||||
% objects
|
||||
-export([object_lookup/2, object_id/1, commit_tree_id/1, commit_tree/1, tree_bypath/2]).
|
||||
-export([object_lookup/2, object_id/1, commit_tree_id/1, commit_tree/1, tree_bypath/2, blob_size/1, blob_content/1]).
|
||||
|
||||
-on_load(load_enif/0).
|
||||
|
||||
@@ -89,6 +89,14 @@ commit_tree(_Handle) ->
|
||||
tree_bypath(_TreeHandle, _Path) ->
|
||||
nif_error(?LINE).
|
||||
|
||||
-spec blob_size(term) -> {ok, integer()} | error.
|
||||
blob_size(_ObjHandle) ->
|
||||
nif_error(?LINE).
|
||||
|
||||
-spec blob_content(term) -> {ok, binary()} | error.
|
||||
blob_content(_ObjHandle) ->
|
||||
nif_error(?LINE).
|
||||
|
||||
nif_error(Line) ->
|
||||
erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}).
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
-module(geef_blob).
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-endif.
|
||||
|
||||
-include("geef_records.hrl").
|
||||
|
||||
-export([lookup/2, id/1]).
|
||||
-export([lookup/2, id/1, size/1, content/1]).
|
||||
|
||||
-spec lookup(repo(), oid() | iolist()) -> object().
|
||||
lookup(Repo, Id) ->
|
||||
@@ -13,3 +16,21 @@ lookup(Repo, Id) ->
|
||||
-spec id(object()) -> oid().
|
||||
id(Obj = #object{type=blob}) ->
|
||||
geef_object:id(Obj).
|
||||
|
||||
size(#object{type=blob, handle=Handle}) ->
|
||||
geef:blob_size(Handle).
|
||||
|
||||
content(#object{type=blob, handle=Handle}) ->
|
||||
{ok, Content} = geef:blob_content(Handle),
|
||||
Content.
|
||||
|
||||
|
||||
-ifdef(TEST).
|
||||
|
||||
blob_size_test() ->
|
||||
{ok, Repo} = geef_repo:open(".."),
|
||||
Blob = lookup(Repo, "80d5c15a040c93a4f98f4496a05ebf30cdd58650"),
|
||||
?assertMatch(889, ?MODULE:size(Blob)),
|
||||
?assertMatch(889, erlang:size(content(Blob))).
|
||||
|
||||
-endif.
|
||||
|
||||
Reference in New Issue
Block a user