├── LICENSE ├── README.md ├── lib └── resty │ └── xxhash.lua └── lua-resty-xxhash-dev-1.rockspec /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Aapo Talvensaari 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lua-resty-xxhash 2 | 3 | `lua-resty-xxhash` contains a LuaJIT FFI-bindings to xxHash, an Extremely fast non-cryptographic hash algorithm. 4 | 5 | ## Installation 6 | 7 | Just place [`xxhash.lua`](https://github.com/bungle/lua-resty-xxhash/blob/master/lib/resty/xxhash.lua) somewhere in your `package.path`, preferably under `resty` directory. If you are using OpenResty, the default location would be `/usr/local/openresty/lualib/resty`. 8 | 9 | ### Compiling and Installing xxHash C-library 10 | 11 | 1. First clone this repo: https://github.com/Cyan4973/xxHash 12 | 2. Run `gcc xxhash.c -O3 -o libxxhash.dylib -shared -fPIC` (on Linux) or `gcc xxhash.c -O3 -o libxxhash.dylib -shared` (OS X) 13 | 4. Place `libxxhash.so` somewhere in the default search path for dynamic libraries of your operating system (or modify `xxhash.lua` and point `ffi_load "xxhash"` with full path to `libxxhash.so`, e.g. `local lib = ffi_load "/usr/local/lib/lua/5.1/libxxhash.so"`). 14 | 15 | ### Using LuaRocks or MoonRocks 16 | 17 | If you are using LuaRocks >= 2.2: 18 | 19 | ```Shell 20 | $ luarocks install lua-resty-xxhash 21 | ``` 22 | 23 | If you are using LuaRocks < 2.2: 24 | 25 | ```Shell 26 | $ luarocks install --server=http://rocks.moonscript.org moonrocks 27 | $ moonrocks install lua-resty-xxhash 28 | ``` 29 | 30 | LuaRocks repository for `lua-resty-xxhash` is located here: https://luarocks.org/modules/bungle/lua-resty-xxhash. 31 | 32 | ## License 33 | 34 | `lua-resty-xxhash` uses two clause BSD license. 35 | 36 | ``` 37 | Copyright (c) 2015, Aapo Talvensaari 38 | All rights reserved. 39 | 40 | Redistribution and use in source and binary forms, with or without modification, 41 | are permitted provided that the following conditions are met: 42 | 43 | * Redistributions of source code must retain the above copyright notice, this 44 | list of conditions and the following disclaimer. 45 | 46 | * Redistributions in binary form must reproduce the above copyright notice, this 47 | list of conditions and the following disclaimer in the documentation and/or 48 | other materials provided with the distribution. 49 | 50 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 51 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 52 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 53 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 54 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 55 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 56 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 57 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 59 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60 | ``` 61 | -------------------------------------------------------------------------------- /lib/resty/xxhash.lua: -------------------------------------------------------------------------------- 1 | local ffi = require "ffi" 2 | local ffi_gc = ffi.gc 3 | local ffi_cdef = ffi.cdef 4 | local ffi_load = ffi.load 5 | local tonumber = tonumber 6 | local setmetatable = setmetatable 7 | 8 | ffi_cdef[[ 9 | typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; 10 | typedef struct { long long ll[ 6]; } XXH32_state_t; 11 | typedef struct { long long ll[11]; } XXH64_state_t; 12 | unsigned int XXH32 (const void* input, size_t length, unsigned seed); 13 | unsigned long long XXH64 (const void* input, size_t length, unsigned long long seed); 14 | XXH32_state_t* XXH32_createState(void); 15 | XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); 16 | XXH64_state_t* XXH64_createState(void); 17 | XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); 18 | XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned seed); 19 | XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length); 20 | unsigned int XXH32_digest (const XXH32_state_t* statePtr); 21 | XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed); 22 | XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); 23 | unsigned long long XXH64_digest (const XXH64_state_t* statePtr); 24 | ]] 25 | 26 | local lib = ffi_load "xxhash" 27 | 28 | local xxh32 = {} 29 | xxh32.__index = xxh32 30 | 31 | function xxh32:reset(seed) 32 | return lib.XXH32_reset(self.context, seed or 0) == 0 and self or nil 33 | end 34 | 35 | function xxh32:update(input) 36 | return lib.XXH32_update(self.context, input, #input) == 0 and self or nil 37 | end 38 | 39 | function xxh32:digest() 40 | return lib.XXH32_digest(self.context) 41 | end 42 | 43 | local xxh64 = {} 44 | xxh64.__index = xxh64 45 | 46 | function xxh64:reset(seed) 47 | return lib.XXH64_reset(self.context, seed or 0) == 0 and self or nil 48 | end 49 | 50 | function xxh64:update(input) 51 | return lib.XXH64_update(self.context, input, #input) == 0 and self or nil 52 | end 53 | 54 | function xxh64:digest() 55 | return tonumber(lib.XXH64_digest(self.context)) 56 | end 57 | 58 | local xxhash = {} 59 | 60 | function xxhash.new(seed, bits) 61 | local self = bits == 64 and setmetatable({ context = ffi_gc(lib.XXH64_createState(), lib.XXH64_freeState) }, xxh64) 62 | or setmetatable({ context = ffi_gc(lib.XXH32_createState(), lib.XXH32_freeState) }, xxh32) 63 | return self:reset(seed) and self or nil 64 | end 65 | 66 | function xxhash.hash32(input, seed) 67 | return lib.XXH32(input, #input, seed or 0) 68 | end 69 | 70 | function xxhash.hash64(input, seed) 71 | return tonumber(lib.XXH64(input, #input, seed or 0)) 72 | end 73 | 74 | local mt = {} 75 | 76 | function mt:__call(input, seed, bits) 77 | return bits == 64 and xxhash.hash64(input, seed) or xxhash.hash32(input, seed) 78 | end 79 | 80 | return setmetatable(xxhash, mt) -------------------------------------------------------------------------------- /lua-resty-xxhash-dev-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "lua-resty-xxhash" 2 | version = "dev-1" 3 | source = { 4 | url = "git://github.com/bungle/lua-resty-xxhash.git" 5 | } 6 | description = { 7 | summary = "LuaJIT FFI-bindings to xxHash, an Extremely fast non-cryptographic hash algorithm.", 8 | detailed = "lua-resty-xxhash contains a LuaJIT FFI-bindings to xxHash, an Extremely fast non-cryptographic hash algorithm.", 9 | homepage = "https://github.com/bungle/lua-resty-xxhash", 10 | maintainer = "Aapo Talvensaari ", 11 | license = "BSD" 12 | } 13 | dependencies = { 14 | "lua >= 5.1" 15 | } 16 | build = { 17 | type = "builtin", 18 | modules = { 19 | ["resty.xxhash"] = "lib/resty/xxhash.lua" 20 | } 21 | } 22 | --------------------------------------------------------------------------------