当前位置: 首页 > 软件库 > 数据库相关 > >

redis-lua-scaling-bloom-filter

LUA Redis scripts for a scaling bloom filter
授权协议 MIT License
开发语言 C/C++
所属分类 数据库相关
软件类型 开源软件
地区 不详
投 递 者 虞祯
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

redis-lua-scaling-bloom-filter

add.lua, cas.lua and check.lua are three lua scripts for a scaling bloom filter for Redis

layer-add.lua and later-check.lua are two lua scripts for a scaling layered bloom filter for Redis

The scripts are to be executed using the EVAL command in Redis.

These scripts will probably not work on Redis cluster since the keys used inside the script aren't all passed as arguments!

The layered filter has a maximum number of 32 layers. You can modify this in the source.

add.lua, cas.lua and layer-add.lua

The add.lua script adds a new element to the filter. It will create the filter when it doesn't exist yet.

cas.lua does a Check And Set, this will not add the element if it already exist.cas.lua will return 0 if the element is added, or 1 if the element was already in the filter.Since we use a scaling filter adding an element using add.lua might cause the elementto exist in multiple parts of the filter at the same time. cas.lua prevents this.Using only cas.lua the :count key of the filter will accurately count the number of elements added to the filter.Only using cas.lua will also lower the number of false positives by a small amount (less duplicates in the filter means less bits set).

layer-add.lua does a similar thing to cas.lua since this is necessary for the layer part to work(need to check all the filters in a layer to see if it already exists in the layer).layer-add.lua will return the layer number the element was added to.

These scripts expects 4 arguments.

  1. The base name of the keys to use.
  2. The initial size of the bloom filter (in number of elements).
  3. The probability of false positives.
  4. The element to add to the filter.

For example the following call would add "something" to a filter named testwhich will initially be able to hold 10000 elements with a probability of false positives of 1%.

eval "add.lua source here" 0 test 10000 0.01 something

check.lua and layer-check.lua

The check.lua and layer-check.lua scripts check if an element is contained in the bloom filter.

layer-check.lua returns the layer the element was found in.

These scripts expects 4 arguments.

  1. The base name of the keys to use.
  2. The initial size of the bloom filter (in number of elements).
  3. The probability of false positives.
  4. The element to check for.

For example the following call would check if "something" is part of the filter named testwhich will initially be able to hold 10000 elements with a probability of false positives of 1%.

eval "check.lua source here" 0 test 10000 0.01 something

Tests

$ npm install redis srand
$ node add.js
$ node cas.js
$ node check.js
$ # or/and
$ node layer-add.js
$ node layer-check.js

add.js and layer-add.js will add elements to a filter named test and then check if the elements are part of the filter.

check.js and layer-check.js will test random elements against the filter build by add.js or layer-add.js to find the probability of false positives.

Both script assume Redis is running on the default port.

Benchmark

You can run ./benchmark.sh and ./layer-benchmark.sh to see how fast the scripts are.

This script assumes Redis is running on the default port and redis-cli and redis-benchmark are installed.

This is the outputs on my 2.3GHz 2012 MacBook Pro Retina:

add.lua
====== evalsha ab31647b3931a68b3b93a7354a297ed273349d39 0 HSwVBmHECt 1000000 0.01 :rand:000000000000 ======
  200000 requests completed in 8.27 seconds
  20 parallel clients
  3 bytes payload
  keep alive: 1

94.57% <= 1 milliseconds
100.00% <= 2 milliseconds
24175.03 requests per second


check.lua
====== evalsha 437a3b0c6a452b5f7a1f10487974c002d41f4a04 0 HSwVBmHECt 1000000 0.01 :rand:000000000000 ======
  200000 requests completed in 8.54 seconds
  20 parallel clients
  3 bytes payload
  keep alive: 1

92.52% <= 1 milliseconds
100.00% <= 8 milliseconds
23419.20 requests per second


layer-add.lua
====== evalsha 7ae29948e3096dd064c22fcd8b628a5c77394b0c 0 ooPb5enskU 1000000 0.01 :rand:000000000000 ======
  20000 requests completed in 12.61 seconds
  20 parallel clients
  3 bytes payload
  keep alive: 1

55.53% <= 12 milliseconds
75.42% <= 13 milliseconds
83.71% <= 14 milliseconds
91.48% <= 15 milliseconds
97.76% <= 16 milliseconds
99.90% <= 24 milliseconds
100.00% <= 24 milliseconds
1586.04 requests per second


layer-check.lua
====== evalsha c1386438944daedfc4b5c06f79eadb6a83d4b4ea 0 ooPb5enskU 1000000 0.01 :rand:000000000000 ======
  20000 requests completed in 11.13 seconds
  20 parallel clients
  3 bytes payload
  keep alive: 1

0.00% <= 9 milliseconds
74.12% <= 11 milliseconds
80.43% <= 12 milliseconds
83.93% <= 13 milliseconds
97.43% <= 14 milliseconds
99.89% <= 15 milliseconds
100.00% <= 15 milliseconds
1797.59 requests per second
  • 布隆过滤器 布隆过滤器是用于判断一个元素是否在集合中。通过一个位数组和N个hash函数实现。(某样东西一定不存在或者可能存在.) 优点: 空间效率高,所占空间小。 查询时间短。 自带去重 缺点: 元素添加到集合中后,不能被删除。 有一定的误判率 布谷鸟过滤器解决了布隆过滤器无法删除的问题 布隆过滤器原理 当一个元素加入布隆过滤器中的时候,会进行如下操作: 使用布隆过滤器中的哈希函数对元素值进行计算

 相关资料
  • redis-lua 是 Redis 的 Lua 语言的客户端开发包。 示例代码: require 'redis'local redis = Redis.connect('127.0.0.1', 6379)local response = redis:ping() -- trueredis:set('usr:nrk', 10)redis:set('usr:nobody', 5)l

  • rld 是一个非交互的调试工具,用于调试 Redis 的 Lua 脚本,这里有篇详细介绍的文章。 rld 特性包括: 易于安装,只有 6kB 可打印输出到本地和远端 跟踪执行的代码行 先进的数值变化的自动监控机制报告 报告函数调用、返回和参数可进行实时检查 基本使用: Load rld.lua to Redis once (e.g. redis-cli --eval rld.lua). Add t

  • Nginx与Lua编写脚本的基本构建块是指令执行顺序的图 Nginx 教程 基础 Nginx编译安装 Nginx.conf详解 Location 详解 Nginx基础知识 Nginx高性能WEB服务器详解 Nginx高并发系统内核优化和PHP7配置文件优化 Nginx和PHP-FPM启动脚本 Nginx的11个Phases agentzh 的 Nginx 教程 Nginx 陷阱和常见错误 TCP和

  • 在应用布隆效果时,使节点的某些部分中的像素发光。 包javafx.scene.effect名为Bloom的类表示bloom效果。 该类包含两个属性,它们是 - input - 此属性的类型为Effect,它表示bloom效果的输入。 threshold - 此属性的类型为double; 这表示节点的像素的亮度的阈值。 所有那些亮度大于等于该值的像素都会发光。 阈值的范围是0.0到1.0。 例子 (

  • Bloom 是用于自然语言处理的大语言模型,包含 1760 亿个参数,支持 46 种自然语言(包括中文)和 13 种编程语言,可以用来回答问题、翻译文本、从文件中提取信息片段,还能像 GitHub Copilot 一样用于生成代码。 BLOOM 模型的最大优势是它的易获取性,任何个人或机构都可以从 Hugging Face 免费获得 1760 亿个参数的完整模型。用户有多个语种可选,然后将需求输入

  • 主要内容:第一个Lua脚本命令,为什么使用Lua脚本,常用脚本命令,基本命令应用从 Redis 2.6 版本开始,Redis 使用内置的 Lua 解释器执行脚本,这意味着我们可以直接在 Redis 客户端执行Lua 脚本 ,于此同时 Redis 还非常贴心地提供了用于编写 Lua 脚本的 命令。 第一个Lua脚本命令 Lua 是一种轻量小巧、开源的脚本语言,用标准 C语言编写。其设计目的就是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。它被广泛的应用于:游戏开发