From 1b9162e8a263e55a13d3d074b9ac38c1c34024bc Mon Sep 17 00:00:00 2001 From: yankun <1939810907@qq.com> Date: Thu, 16 Feb 2023 21:26:19 +0800 Subject: [PATCH] docs: bulk api docs (#5). --- README.md | 19 +++++++++++++++++++ docs/README.zh_cn.md | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 8e950bd..8901a40 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,25 @@ assert bloom3.contains('hello') ``` +there are some bulk api for python to reduce ffi cost between python and rust + +```python +bloom = BloomFilter(100_000_000, 0.01) +inserts = [1, 2, 3, 4, 5, 6, 7, 9, 18, 68, 90, 100] +checks = [1, 2, 3, 4, 5, 6, 7, 9, 18, 68, 90, 100, 190, 290, 390] +results = [True, True, True, True, True, True, True, True, True, True, True, True, False, False, False] + +bloom.add_int_batch(inserts) +contains = bloom.contains_int_batch(checks) +assert contains == results + +bloom.add_str_batch(list(map(lambda x: str(x), inserts))) +assert bloom.contains_str_batch(list(map(lambda x: str(x), checks))) == results + +bloom.add_bytes_batch(list(map(lambda x: bytes(x), inserts))) +assert bloom.contains_bytes_batch(list(map(lambda x: bytes(x), checks))) == results +``` + more examples at [py_tests](py_tests/test_bloom.py). ### Rust diff --git a/docs/README.zh_cn.md b/docs/README.zh_cn.md index ad1a191..ad96b6a 100644 --- a/docs/README.zh_cn.md +++ b/docs/README.zh_cn.md @@ -104,6 +104,25 @@ assert bloom3.contains('hello') ``` +由于python与rust之间的数据转换有一定的性能开销,所以`fastbloom`提供了一些批量操作api用于减少ffi开销 + +```python +bloom = BloomFilter(100_000_000, 0.01) +inserts = [1, 2, 3, 4, 5, 6, 7, 9, 18, 68, 90, 100] +checks = [1, 2, 3, 4, 5, 6, 7, 9, 18, 68, 90, 100, 190, 290, 390] +results = [True, True, True, True, True, True, True, True, True, True, True, True, False, False, False] + +bloom.add_int_batch(inserts) +contains = bloom.contains_int_batch(checks) +assert contains == results + +bloom.add_str_batch(list(map(lambda x: str(x), inserts))) +assert bloom.contains_str_batch(list(map(lambda x: str(x), checks))) == results + +bloom.add_bytes_batch(list(map(lambda x: bytes(x), inserts))) +assert bloom.contains_bytes_batch(list(map(lambda x: bytes(x), checks))) == results +``` + 更多例子参考 [py_tests](py_tests/test_bloom.py). ### Rust