couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1098691 - in /couchdb/trunk/src/snappy: snappy.erl snappy_nif.cc
Date Mon, 02 May 2011 17:51:29 GMT
Author: davisp
Date: Mon May  2 17:51:28 2011
New Revision: 1098691

URL: http://svn.apache.org/viewvc?rev=1098691&view=rev
Log:
Refactored snappy NIF.


Modified:
    couchdb/trunk/src/snappy/snappy.erl
    couchdb/trunk/src/snappy/snappy_nif.cc

Modified: couchdb/trunk/src/snappy/snappy.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/snappy/snappy.erl?rev=1098691&r1=1098690&r2=1098691&view=diff
==============================================================================
--- couchdb/trunk/src/snappy/snappy.erl (original)
+++ couchdb/trunk/src/snappy/snappy.erl Mon May  2 17:51:28 2011
@@ -16,8 +16,7 @@
 -module(snappy).
 
 -export([compress/1, decompress/1]).
--export([get_uncompressed_length/1]).
--export([is_valid_compressed_buffer/1]).
+-export([uncompressed_length/1, is_valid/1]).
 
 -on_load(init/0).
 
@@ -49,9 +48,9 @@ decompress(_IoList) ->
     exit(snappy_nif_not_loaded).
 
 
-get_uncompressed_length(_IoList) ->
+uncompressed_length(_IoList) ->
     exit(snappy_nif_not_loaded).
 
 
-is_valid_compressed_buffer(_IoList) ->
+is_valid(_IoList) ->
     exit(snappy_nif_not_loaded).

Modified: couchdb/trunk/src/snappy/snappy_nif.cc
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/snappy/snappy_nif.cc?rev=1098691&r1=1098690&r2=1098691&view=diff
==============================================================================
--- couchdb/trunk/src/snappy/snappy_nif.cc (original)
+++ couchdb/trunk/src/snappy/snappy_nif.cc Mon May  2 17:51:28 2011
@@ -26,206 +26,240 @@
 #error OTP R13B03 not supported. Upgrade to R13B04 or later.
 #endif
 
+#ifdef __cplusplus
+#define BEGIN_C extern "C" {
+#define END_C }
+#else
+#define BEGIN_C
+#define END_C
+#endif
 
-class SnappyNifSink : public snappy::Sink {
-public:
-    SnappyNifSink(ErlNifEnv* e) : env(e), length(0)  {
-        if (!enif_alloc_binary_compat(env, 0, &bin)) {
-            enif_release_binary_compat(env, &bin);
-            throw std::bad_alloc();
-        }
+#define SC_PTR(c) reinterpret_cast<char *>(c)
+
+class SnappyNifSink : public snappy::Sink
+{
+    public:
+        SnappyNifSink(ErlNifEnv* e);
+        ~SnappyNifSink();
+        
+        void Append(const char* data, size_t n);
+        char* GetAppendBuffer(size_t len, char* scratch);
+        ErlNifBinary& getBin();
+
+    private:
+        ErlNifEnv* env;
+        ErlNifBinary bin;
+        size_t length;
+};
+
+SnappyNifSink::SnappyNifSink(ErlNifEnv* e) : env(e), length(0)
+{
+    if(!enif_alloc_binary_compat(env, 0, &bin)) {
+        env = NULL;
+        throw std::bad_alloc();
     }
+}
 
-    void Append(const char *data, size_t n) {
-        if (data != reinterpret_cast<const char *>(bin.data + length)) {
-            memcpy(bin.data + length, data, n);
-        }
-        length += n;
+SnappyNifSink::~SnappyNifSink()
+{
+    if(env != NULL) {
+        enif_release_binary_compat(env, &bin);
     }
+}
 
-    char* GetAppendBuffer(size_t len, char* scratch) {
-        if ((length + len) > bin.size) {
-            size_t sz = (len * 4) < 8192 ? 8192 : (len * 4);
+void
+SnappyNifSink::Append(const char *data, size_t n)
+{
+    if(data != (SC_PTR(bin.data) + length)) {
+        memcpy(bin.data + length, data, n);
+    }
+    length += n;
+}
 
-            if (!enif_realloc_binary_compat(env, &bin, bin.size + sz)) {
-                enif_release_binary_compat(env, &bin);
-                throw std::bad_alloc();
-            }
-        }
+char*
+SnappyNifSink::GetAppendBuffer(size_t len, char* scratch)
+{
+    size_t sz;
+    
+    if((length + len) > bin.size) {
+        sz = (len * 4) < 8192 ? 8192 : (len * 4);
 
-        return reinterpret_cast<char *>(bin.data + length);
+        if(!enif_realloc_binary_compat(env, &bin, bin.size + sz)) {
+            throw std::bad_alloc();
+        }
     }
 
-    ErlNifBinary& getBin() {
-        if (bin.size > length) {
-            if (!enif_realloc_binary_compat(env, &bin, length)) {
-                // shouldn't happen
-                enif_release_binary_compat(env, &bin);
-                throw std::bad_alloc();
-            }
+    return SC_PTR(bin.data) + length;
+}
+
+ErlNifBinary&
+SnappyNifSink::getBin()
+{
+    if(bin.size > length) {
+        if(!enif_realloc_binary_compat(env, &bin, length)) {
+            throw std::bad_alloc();
         }
-        return bin;
     }
+    return bin;
+}
+
+
+BEGIN_C
 
-private:
-    ErlNifEnv* env;
-    ErlNifBinary bin;
-    size_t length;
-};
 
+ERL_NIF_TERM
+make_atom(ErlNifEnv* env, const char* name)
+{
+    ERL_NIF_TERM ret;
+    if(enif_make_existing_atom(env, name, &ret, ERL_NIF_LATIN1)) {
+        return ret;
+    }
+    return enif_make_atom(env, name);
+}
 
-extern "C" {
 
-    ERL_NIF_TERM snappy_compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
-        ErlNifBinary input;
+ERL_NIF_TERM
+make_ok(ErlNifEnv* env, ERL_NIF_TERM mesg)
+{
+    ERL_NIF_TERM ok = make_atom(env, "ok");
+    return enif_make_tuple2(env, ok, mesg);   
+}
 
-        if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
-            return enif_make_badarg(env);
-        }
 
-        try {
-            snappy::ByteArraySource source(reinterpret_cast<const char *>(input.data),
-                                           input.size);
-            SnappyNifSink sink(env);
+ERL_NIF_TERM
+make_error(ErlNifEnv* env, const char* mesg)
+{
+    ERL_NIF_TERM error = make_atom(env, "error");
+    return enif_make_tuple2(env, error, make_atom(env, mesg));
+}
 
-            snappy::Compress(&source, &sink);
 
-            return enif_make_tuple(env, 2,
-                                   enif_make_atom(env, "ok"),
-                                   enif_make_binary(env, &sink.getBin()));
-        } catch(std::bad_alloc e) {
-            return enif_make_tuple(env, 2,
-                                   enif_make_atom(env, "error"),
-                                   enif_make_atom(env, "insufficient_memory"));
-        } catch(...) {
-            return enif_make_tuple(env, 2,
-                                   enif_make_atom(env, "error"),
-                                   enif_make_atom(env, "unknown"));
-        }
+ERL_NIF_TERM
+snappy_compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+    ErlNifBinary input;
+
+    if(!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
+        return enif_make_badarg(env);
+    }
+
+    try {
+        snappy::ByteArraySource source(SC_PTR(input.data), input.size);
+        SnappyNifSink sink(env);
+        snappy::Compress(&source, &sink);
+        return make_ok(env, enif_make_binary(env, &sink.getBin()));
+    } catch(std::bad_alloc e) {
+        return make_error(env, "insufficient_memory");
+    } catch(...) {
+        return make_error(env, "unknown");
     }
+}
 
 
-    ERL_NIF_TERM snappy_decompress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
-        ErlNifBinary input;
+ERL_NIF_TERM
+snappy_decompress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+    ErlNifBinary bin;
+    ErlNifBinary ret;
+    size_t len;
+
+    if(!enif_inspect_iolist_as_binary(env, argv[0], &bin)) {
+        return enif_make_badarg(env);
+    }
 
-        if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
-            return enif_make_badarg(env);
+    try {
+        if(!snappy::GetUncompressedLength(SC_PTR(bin.data), bin.size, &len)) {
+            return make_error(env, "data_not_compressed");
         }
 
-        try {
-            size_t len = -1;
-            bool isCompressed = snappy::GetUncompressedLength(
-                reinterpret_cast<const char *>(input.data), input.size, &len);
+        if(!enif_alloc_binary_compat(env, len, &ret)) {
+            return make_error(env, "insufficient_memory");
+        }
 
-            if (!isCompressed) {
-                return enif_make_tuple(env, 2,
-                                       enif_make_atom(env, "error"),
-                                       enif_make_atom(env, "not_compressed_data"));
-            }
+        if(!snappy::RawUncompress(SC_PTR(bin.data), bin.size,
+                                            SC_PTR(ret.data))) {
+            return make_error(env, "corrupted_data");
+        }
 
-            ErlNifBinary retBin;
+        return make_ok(env, enif_make_binary(env, &ret));
+    } catch(...) {
+        return make_error(env, "unknown");
+    }
+}
 
-            if (!enif_alloc_binary_compat(env, len, &retBin)) {
-                return enif_make_tuple(env, 2,
-                                       enif_make_atom(env, "error"),
-                                       enif_make_atom(env, "insufficient_memory"));
-            }
 
-            bool valid = snappy::RawUncompress(reinterpret_cast<const char *>(input.data),
-                                               input.size,
-                                               reinterpret_cast<char *>(retBin.data));
+ERL_NIF_TERM
+snappy_uncompressed_length(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+    ErlNifBinary bin;
+    size_t len;
 
-            if (!valid) {
-                return enif_make_tuple(env, 2,
-                                       enif_make_atom(env, "error"),
-                                       enif_make_atom(env, "corrupted_data"));
-            }
+    if(!enif_inspect_iolist_as_binary(env, argv[0], &bin)) {
+        return enif_make_badarg(env);
+    }
 
-            return enif_make_tuple(env, 2,
-                                   enif_make_atom(env, "ok"),
-                                   enif_make_binary(env, &retBin));
-        } catch(...) {
-            return enif_make_tuple(env, 2,
-                                   enif_make_atom(env, "error"),
-                                   enif_make_atom(env, "unknown"));
+    try {
+        if(!snappy::GetUncompressedLength(SC_PTR(bin.data), bin.size, &len)) {
+            return make_error(env, "data_not_compressed");
         }
+        return make_ok(env, enif_make_ulong(env, len));
+    } catch(...) {
+        return make_error(env, "unknown");
     }
+}
 
 
-    ERL_NIF_TERM snappy_get_uncompressed_length(ErlNifEnv* env, int argc, const ERL_NIF_TERM
argv[]) {
-        ErlNifBinary input;
-
-        if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
-            return enif_make_badarg(env);
-        }
+ERL_NIF_TERM
+snappy_is_valid(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+    ErlNifBinary bin;
 
-        try {
-            size_t len = -1;
-            bool isCompressed = snappy::GetUncompressedLength(
-                reinterpret_cast<const char *>(input.data), input.size, &len);
+    if (!enif_inspect_iolist_as_binary(env, argv[0], &bin)) {
+        return enif_make_badarg(env);
+    }
 
-            if (isCompressed) {
-                return enif_make_tuple(env, 2,
-                                       enif_make_atom(env, "ok"),
-                                       enif_make_ulong(env, len));
-            } else {
-                return enif_make_tuple(env, 2,
-                                       enif_make_atom(env, "error"),
-                                       enif_make_atom(env, "not_compressed_data"));
-            }
-        } catch(...) {
-            return enif_make_tuple(env, 2,
-                                   enif_make_atom(env, "error"),
-                                   enif_make_atom(env, "unknown"));
+    try {
+        if(snappy::IsValidCompressedBuffer(SC_PTR(bin.data), bin.size)) {
+            return make_atom(env, "true");
+        } else {
+            return make_atom(env, "false");
         }
+    } catch(...) {
+        return make_error(env, "unknown");
     }
+}
 
 
-    ERL_NIF_TERM snappy_is_valid_compressed_buffer(ErlNifEnv* env, int argc, const ERL_NIF_TERM
argv[]) {
-        ErlNifBinary input;
-
-        if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
-            return enif_make_badarg(env);
-        }
-
-        try {
-            bool valid = snappy::IsValidCompressedBuffer(
-                reinterpret_cast<const char *>(input.data), input.size);
+int
+on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
+{
+    return 0;
+}
 
-            if (valid) {
-                return enif_make_atom(env, "true");
-            } else {
-                return enif_make_atom(env, "false");
-            }
-        } catch(...) {
-            return enif_make_tuple(env, 2,
-                                   enif_make_atom(env, "error"),
-                                   enif_make_atom(env, "unknown"));
-        }
-    }
 
+int
+on_reload(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
+{
+    return 0;
+}
 
 
-    int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM info) {
-        return 0;
-    }
+int
+on_upgrade(ErlNifEnv* env, void** priv, void** old_priv, ERL_NIF_TERM info)
+{
+    return 0;
+}
 
 
-    int on_reload(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM info) {
-        return 0;
-    }
+static ErlNifFunc nif_functions[] = {
+    {"compress", 1, snappy_compress},
+    {"decompress", 1, snappy_decompress},
+    {"uncompressed_length", 1, snappy_uncompressed_length},
+    {"is_valid", 1, snappy_is_valid}
+};
 
-    int on_upgrade(ErlNifEnv* env, void** priv_data, void** old_data, ERL_NIF_TERM info)
{
-        return 0;
-    }
 
+ERL_NIF_INIT(snappy, nif_functions, &on_load, &on_reload, &on_upgrade, NULL);
 
-    static ErlNifFunc nif_functions[] = {
-        {"compress", 1, snappy_compress},
-        {"decompress", 1, snappy_decompress},
-        {"get_uncompressed_length", 1, snappy_get_uncompressed_length},
-        {"is_valid_compressed_buffer", 1, snappy_is_valid_compressed_buffer}
-    };
 
-    ERL_NIF_INIT(snappy, nif_functions, &on_load, &on_reload, &on_upgrade, NULL);
-}
+END_C
\ No newline at end of file



Mime
View raw message