summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-21 20:36:09 +0800
committerruki <[email protected]>2022-05-21 20:36:09 +0800
commitae7779636e29044be207d26d22a8cd27a223605d (patch)
treecc8da80bddf803579e4218e2102de84b9cc18986
parente4c3aa1c047d68a49de392307075b60f411e1950 (diff)
add bloom filter in core
-rw-r--r--core/src/xmake/bloom_filter/bloom_filter_close.c56
-rw-r--r--core/src/xmake/bloom_filter/bloom_filter_open.c58
-rw-r--r--core/src/xmake/bloom_filter/prefix.h31
-rw-r--r--core/src/xmake/engine.c15
-rw-r--r--core/src/xmake/makefile4
-rw-r--r--xmake/core/base/bloom_filter.lua6
6 files changed, 167 insertions, 3 deletions
diff --git a/core/src/xmake/bloom_filter/bloom_filter_close.c b/core/src/xmake/bloom_filter/bloom_filter_close.c
new file mode 100644
index 000000000..363acb2be
--- /dev/null
+++ b/core/src/xmake/bloom_filter/bloom_filter_close.c
@@ -0,0 +1,56 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file bloom_filter_close.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "bloom_filter_close"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_bloom_filter_close(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // is pointer?
+ if (!xm_lua_ispointer(lua, 1))
+ return 0;
+
+ // get the bloom filter
+ tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1);
+ tb_check_return_val(filter, 0);
+
+ // exit filter
+ tb_bloom_filter_exit(filter);
+
+ // save result: ok
+ lua_pushboolean(lua, tb_true);
+ return 1;
+}
+
diff --git a/core/src/xmake/bloom_filter/bloom_filter_open.c b/core/src/xmake/bloom_filter/bloom_filter_open.c
new file mode 100644
index 000000000..7370edcbe
--- /dev/null
+++ b/core/src/xmake/bloom_filter/bloom_filter_open.c
@@ -0,0 +1,58 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file bloom_filter_open.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "bloom_filter_open"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_bloom_filter_open(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get arguments
+ tb_int_t probability = (tb_int_t)lua_tointeger(lua, 1);
+ tb_int_t hash_count = (tb_int_t)lua_tointeger(lua, 2);
+ tb_int_t item_maxn = (tb_int_t)lua_tointeger(lua, 3);
+ if (hash_count > 16 || item_maxn < 0)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "invalid hash count(%p) and item maxn(%d)!", hash_count, item_maxn);
+ return 2;
+ }
+
+ // init the bloom filter
+ tb_bloom_filter_ref_t filter = tb_bloom_filter_init(probability, hash_count, item_maxn, tb_element_str(tb_true));
+ if (filter) xm_lua_pushpointer(lua, (tb_pointer_t)filter);
+ else lua_pushnil(lua);
+ return 1;
+}
+
diff --git a/core/src/xmake/bloom_filter/prefix.h b/core/src/xmake/bloom_filter/prefix.h
new file mode 100644
index 000000000..5d16c371c
--- /dev/null
+++ b/core/src/xmake/bloom_filter/prefix.h
@@ -0,0 +1,31 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except idata compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to idata writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file prefix.h
+ *
+ */
+#ifndef XM_BLOOM_FILTER_PREFIX_H
+#define XM_BLOOM_FILTER_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+
+#endif
+
+
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 3c0597977..992fb0cca 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -206,6 +206,10 @@ tb_int_t xm_lz4_decompress_stream_read(lua_State* lua);
tb_int_t xm_lz4_decompress_stream_write(lua_State* lua);
tb_int_t xm_lz4_decompress_stream_close(lua_State* lua);
+// the bloom filter functions
+tb_int_t xm_bloom_filter_open(lua_State* lua);
+tb_int_t xm_bloom_filter_close(lua_State* lua);
+
// the windows functions
#ifdef TB_CONFIG_OS_WINDOWS
tb_int_t xm_winos_cp_info(lua_State* lua);
@@ -441,6 +445,14 @@ static luaL_Reg const g_lz4_functions[] =
, { tb_null, tb_null }
};
+// the bloom filter functions
+static luaL_Reg const g_bloom_filter_functions[] =
+{
+ { "open", xm_bloom_filter_open }
+, { "close", xm_bloom_filter_close }
+, { tb_null, tb_null }
+};
+
// the string functions
static luaL_Reg const g_string_functions[] =
{
@@ -989,6 +1001,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
// bind lz4 functions
xm_lua_register(engine->lua, "lz4", g_lz4_functions);
+ // bind bloom filter functions
+ xm_lua_register(engine->lua, "bloom_filter", g_bloom_filter_functions);
+
// bind base64 functions
xm_lua_register(engine->lua, "base64", g_base64_functions);
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index d86df719e..bb1c21582 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -148,7 +148,9 @@ xmake_C_FILES += \
lz4/decompress_stream_open \
lz4/decompress_stream_read \
lz4/decompress_stream_write \
- lz4/decompress_stream_close
+ lz4/decompress_stream_close \
+ bloom_filter/bloom_filter_open \
+ bloom_filter/bloom_filter_close
iswin =
ifeq ($(PLAT),windows)
diff --git a/xmake/core/base/bloom_filter.lua b/xmake/core/base/bloom_filter.lua
index 781cfb4da..fd3bd3a9d 100644
--- a/xmake/core/base/bloom_filter.lua
+++ b/xmake/core/base/bloom_filter.lua
@@ -173,7 +173,7 @@ function _instance:__gc()
end
end
--- new a bloom filter, e.g. {probability = 0.001}
+-- new a bloom filter, e.g. {probability = 0.001, hash_count = 3, item_maxn = 1000000}
function bloom_filter.new(opt)
opt = opt or {}
local probability = opt.probability or 0.001
@@ -186,7 +186,9 @@ function bloom_filter.new(opt)
[0.000001] = bloom_filter.PROBABILITY_0_000001
}
probability = assert(maps[probability], "invalid probability(%f)", probability)
- local handle, errors = bloom_filter._open(probability)
+ local hash_count = opt.hash_count or 3
+ local item_maxn = opt.item_maxn or 1000000
+ local handle, errors = bloom_filter._open(probability, hash_count, item_maxn)
if handle then
return _instance.new(handle)
else