在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):marcopompili/lua-leveldb开源软件地址(OpenSource Url):https://github.com/marcopompili/lua-leveldb开源编程语言(OpenSource Language):C++ 60.9%开源软件介绍(OpenSource Introduction):lua-leveldbLua bindings for Google's LevelDB key/store database. From the GoogleCode page: "The LevelDB library provides a persistent key value store. Keys and values are arbitrary byte arrays. The keys are ordered within the key value store according to a user-specified comparator function." I wrote this extension because I needed a fast embeddable key/value database for TCP/IP packet analysis. I had to analyze big files in pcap format and divide the traffic using some specific rules. This simple Lua extension allows you to access LevelDB from Lua in a quite simple way. Lua is also used as extension language for WireShark. Most of the basic options and functions are supported right now, but still not the full set of operations permitted by LevelDB. Take a look at the examples below. InstallationUsing Luarocks (Recommended)You can install lua-leveldb with the Luarocks packaging system (if you got Lua >= 5.1):
Or if you want to build the latest Luarock locally use the make script like this:
Or if you want to build the latest binary locally use
ManuallyThe library is packed as a Luarock extension, check out luarocks.org.
Manual (system-wide) installation (on Debian 8):
Note - Library name collision with leveldb.soWhen LevelDB libraries are installed, there is a file with name SupportThe extension still not support the full set of operations permitted by the LevelDB library. These are the current possible operation:
Basic ExampleThis is a simple example about how to use the Lua extension for Google's LevelDB, just putting some data and getting it back: leveldb = require 'lualeveldb'
opt = leveldb.options()
opt.createIfMissing = true
opt.errorIfExists = false
local test_key = 'key1'
local test_val = 'value1'
print ('opening test.db')
testdb = leveldb.open(opt, 'test.db')
if leveldb.check(testdb)
then
if testdb:put(test_key, test_val)
then
print ('key1: '.. testdb:get(test_key))
end
end
leveldb.close(testdb)
testdb = leveldb.open(opt, 'test.db')
testdb:put('key2', 123456)
print ('key2: ' .. testdb:get('key2'))
leveldb.close(testdb) Iterator ExampleAn example using iterators: leveldb = require 'lualeveldb'
local opt = leveldb.options()
opt.createIfMissing = true
opt.errorIfExists = false
-- db example
local db = leveldb.open(opt, 'iterator.db')
assert(db:put('key1s', 'value1'))
assert(db:put('key1n', 1))
assert(db:put('key2s', 'value2'))
assert(db:put('key2n', 2))
assert(db:put('key3s', 'value3'))
assert(db:put('key3n', 3.14))
local iter = db:iterator()
iter:seekToFirst()
while(iter:valid())
do
local key = iter:key()
local value = iter:value();
print(iter:key() .. ' => ' .. '(' .. type(value) .. ') ' .. tostring(value))
iter:next()
end
iter:del()
leveldb.close(db)
Using the Bloom filterleveldb = require 'lualeveldb'
opt = leveldb.options()
opt.createIfMissing = true
opt.errorIfExists = false
-- setting the bloom filter into the options
leveldb.bloomFilterPolicy(opt, 10)
print(opt) SerializationHere an examples of dealing with serialization. leveldb = require 'lualeveldb'
require 'serialization'
--[[
Lua >=5.3.3 extended the format option "%q" to work also with
numbers (plus nil and Booleans), again writing them in a
proper way to be read back by Lua.
]]--
function serialize(o)
local t = type(o)
if t == "number"
or t == "string"
or t == "boolean"
or t == "nil"
then
return string.format("%q", o)
else
error("cannot serialize a: " .. type(o))
end
end
function deserialize(s)
local n = tonumber(s)
if n ~= nil then
return n
elseif s == 'true' then
return true
elseif s == 'false' then
return false
else
return s
end
end
local opt = leveldb.options()
opt.createIfMissing = true
opt.errorIfExists = false
local db = leveldb.open(opt, 'types.db')
-- serialization example: simple serialization can be used for storing values, tables too.
assert(db:put("string", "Oh hai Mark!"))
assert(db:put("number", serialize(123456)))
assert(db:put("boolean", serialize(true)))
local function check(val, expected)
assert(type(deserialize(val)) == expected, "expected " .. expected .. ", found: " .. type(val))
end
check(db:get("string"), "string")
check(db:get("number"), "number")
check(db:get("boolean"), "boolean")
print("all type tests are fine!")
leveldb.close(db) Example filesExamples above can be found in the NotesLua-leveldb is compatible with Lua 5.1 and newer. Versions
LicenseThe lua-leveldb code is licensed under the terms of the MIT license. This means that lua-leveldb is free software and can be used for both academic and commercial purposes at absolutely no cost, check the LICENSE file for details. Note that LevelDB library itself (included for convenience) is licensed under BSD-3. DonateLike the software? Feeling generous? Buy me a beer! :P Contacts
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论