Permalink
Browse files

Use iolist as an input data. Some cosmetic changes in README.

  • Loading branch information...
1 parent 6757d56 commit 79320aa63d5c45334160e3c7075dddca775a084d @thekvs committed Jun 19, 2011
Showing with 21 additions and 11 deletions.
  1. +2 −2 README.md
  2. +6 −4 c_src/esnappy_nif.cpp
  3. +13 −5 src/esnappy.erl
View
@@ -2,7 +2,7 @@
The **esnappy** library provides Erlang bindings to Google's
[Snappy compression library](http://code.google.com/p/snappy/).
-It uses separate thread for compression/decompression so it won't
+It uses separate OS thread for compression/decompression so it won't
screw up Erlang's VM scheduler while processing large data chunks.
## Erlang Version
@@ -50,4 +50,4 @@ true
832898
</pre>
-Note the difference in execution time 46692 (Snappy) vs. 493585 (zlib).
+Note the difference in execution time **46692** (Snappy) vs. **493585** (zlib).
View
@@ -151,7 +151,7 @@ init_task(task_type_t type, ERL_NIF_TERM ref, ErlNifPid pid,
term = enif_make_copy(task->env, orig_term);
- if (!enif_inspect_binary(task->env, term, &task->data)) {
+ if (!enif_inspect_iolist_as_binary(task->env, term, &task->data)) {
cleanup_task(&task);
goto done;
}
@@ -370,9 +370,10 @@ snappy_compress_impl(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
ERL_NIF_LATIN1));
}
- if (!enif_is_binary(env, argv[3])) {
+ if (!(enif_is_binary(env, argv[3]) ||
+ enif_is_list(env, argv[3]))) {
return enif_make_tuple2(env, atom_error,
- enif_make_string(env, "Forth arg. is not a binary",
+ enif_make_string(env, "Forth arg. is not a binary or a list",
ERL_NIF_LATIN1));
}
@@ -418,7 +419,8 @@ snappy_decompress_impl(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
ERL_NIF_LATIN1));
}
- if (!enif_is_binary(env, argv[3])) {
+ if (!(enif_is_binary(env, argv[3]) ||
+ enif_is_list(env, argv[3]))) {
return enif_make_tuple2(env, atom_error,
enif_make_string(env, "Forth arg. is not a binary",
ERL_NIF_LATIN1));
View
@@ -25,18 +25,18 @@ init() ->
end, atom_to_list(?MODULE) ++ "_nif"),
erlang:load_nif(SoName, 0).
-compress_impl(_Ctx, _Ref, _Self, _Data) ->
+compress_impl(_Ctx, _Ref, _Self, _IoList) ->
erlang:nif_error(not_loaded).
-decompress_impl(_Ctx, _Ref, _Self, _Data) ->
+decompress_impl(_Ctx, _Ref, _Self, _IoList) ->
erlang:nif_error(not_loaded).
create_ctx() ->
erlang:nif_error(not_loaded).
-compress(Ctx, UncompressedData) ->
+compress(Ctx, RawData) ->
Ref = make_ref(),
- ok = compress_impl(Ctx, Ref, self(), UncompressedData),
+ ok = compress_impl(Ctx, Ref, self(), RawData),
receive
{ok, Ref, CompressedData} ->
{ok, CompressedData};
@@ -68,6 +68,14 @@ esnappy_test() ->
{ok, Data} = file:read_file("../test/text.txt"),
{ok, CompressedData} = compress(Ctx, Data),
{ok, UncompressedData} = decompress(Ctx, CompressedData),
- true = (Data =:= UncompressedData).
+ true = (Data =:= UncompressedData andalso
+ size(CompressedData) < size(Data)).
+
+esnappy_iolist_test() ->
+ {ok, Ctx} = create_ctx(),
+ RawData = ["fshgggggggggggggggggg", <<"weqeqweqw">>],
+ {ok, CompressedData} = compress(Ctx, RawData),
+ {ok, UncompressedData} = decompress(Ctx, CompressedData),
+ true = (list_to_binary(RawData) =:= UncompressedData).
-endif.

0 comments on commit 79320aa

Please sign in to comment.