Giter Site home page Giter Site logo

lua-resty-bloomfilter's Introduction

安装

git clone https://github.com/Xiangqian5/lua-resty-bloomfilter.git

cd lua-resty-bloomfilter

sh build.sh

使用

example.lua
local bloomfilter = require("bloomfilter")
local str_format = string.format
local ngx = ngx

if not bloomfilter.initted() then
    local initted, err = bloomfilter.init()
    if initted == nil then
        ngx.log(ngx.ERR, err)
        return
    end
end

local bf = bloomfilter.new_bf(1200, 0.001)

local element = 4501310677070684
local is_changed, err = bloomfilter.put_uint64(bf, element)
if is_changed == nil then
    ngx.say(err)
    return
end

local length = bf.bitset.length
ngx.say(str_format("bitset.length=%s", length))

--you may want to store the serialized result to redis.
local bitset, err = bloomfilter.serialized(bf)
if bitset == nil then
    ngx.say(err)
    return
end

local buf = ""
buf = buf .. "["
for i=0, length*8+6-1, 1 do
    buf = buf .. str_format("%s ", bitset[i])
end
buf = buf:sub(1,-2) .. "]"
ngx.say(buf)

local bf, err = bloomfilter.load_bf(bitset, length*8+6)
if bf == nil then
    ngx.say(err)
    return
end

local ele = "4501310677070684"
local is_in, err = bloomfilter.might_contain_str_number(bf, ele)
if is_in == nil then
    ngx.say(err)
    return
end
ngx.say(str_format("is_in:%s", is_in))

local ele2 = 4501310677070684
is_in, err = bloomfilter.might_contain_number(bf, ele2)
if is_in == nil then
    ngx.say(err)
    return
end
ngx.say(str_format("is_in:%s", is_in))

性能比较

BloomFilter info:
seed:0
num_hash:10
bit_array_size:17280ULL
bit_count:10ULL
hash_func:cdata<void (*)()>: 0x7feb7adc7ff0

Api MightContainStrNumber is_in:1  loop:1          cost:0.010967254638672
Api MightContainNumber    is_in:1  loop:1          cost:0.0050067901611328
Api MightContainStrNumber is_in:1  loop:1000       cost:0.92315673828125
Api MightContainNumber    is_in:1  loop:1000       cost:0.18405914306641
Api MightContainStrNumber is_in:1  loop:3000       cost:2.2499561309814
Api MightContainNumber    is_in:1  loop:3000       cost:0.41890144348145
Api MightContainStrNumber is_in:1  loop:5000       cost:3.5860538482666
Api MightContainNumber    is_in:1  loop:5000       cost:0.7469654083252
Api MightContainStrNumber is_in:1  loop:10000      cost:7.2948932647705
Api MightContainNumber    is_in:1  loop:10000      cost:1.3251304626465
Api MightContainStrNumber is_in:1  loop:100000     cost:64.152956008911
Api MightContainNumber    is_in:1  loop:100000     cost:13.160943984985
Api MightContainStrNumber is_in:1  loop:1000000    cost:639.33396339417
Api MightContainNumber    is_in:1  loop:1000000    cost:130.28979301453

lua-resty-bloomfilter's People

Contributors

zombie-k avatar

Stargazers

 avatar Claudiu RAVEICA avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.