Git Product home page Git Product logo

lua-resty-lmdb's Introduction

lua-resty-lmdb

This module allows OpenResty applications to use the LMDB (Lightning Memory-Mapped Database) inside the Nginx worker process. It has two parts, a core module built into Nginx that controls the life cycle of the database environment, and a FFI based Lua binding for interacting with the module to access/change data.

Table of Contents

APIs

resty.lmdb

get

syntax: value, err = lmdb.get(key, db?)

context: any context except init_by_lua*

Gets the value corresponding to key from LMDB database db. If db is omitted, it defaults to "_default".

If the key does not exist, nil will be returned.

In case of error, nil and a string describing the error will be returned instead.

Back to TOC

set

syntax: ok, err = lmdb.set(key, value, db?)

context: any context except init_by_lua*

Sets the value corresponding to key to value inside LMDB database db. If db is omitted, it defaults to "_default".

Setting a key's value to nil will remove that key from the corresponding database.

In case of error, nil and a string describing the error will be returned instead.

Back to TOC

get_env_info

syntax: status, err = lmdb.get_env_info()

context: any context except init_by_lua*

Get the LMDB database runtime information. status table struct as below.

{
    "page_size": 4096,
    "max_readers":126,
    "num_readers": 1,
    "allocated_pages": 2,
    "in_use_pages": 0,
    "entries": 0,
    "map_size": 10485760 # in bytes
}

In case of error, nil and a string describing the error will be returned instead.

Back to TOC

db_drop

syntax: ok, err = lmdb.db_drop(delele?, db?)

context: any context except init_by_lua*

Clears the contents of database db. If delete is true, then the database handle is also dropped.

In case of error, nil and a string describing the error will be returned instead.

Back to TOC

prefix

syntax: for key, value in lmdb.prefix(prefix) do

context: any context

Returns all key and their associated value for keys starting with prefix. For example, if the database contains:

key1: value1
key11: value11
key2: value2

Then a call of lmdb.prefix("key") will yield key1, key11 and key2 respectively.

In case of errors while fetching from LMDB, key will be nil and value will be a string describing the error. The caller must anticipate this happening and check each return value carefully before consuming.

Warning on transaction safety: Since the number of keys that could potentially be returned with this method could be very large, this method does not return all results inside a single transaction as this will be very expensive. Instead, this method gets keys from LMDB in batches using different read transaction. Therefore, it is possible that the database content has changed between batches. We may introduce a mechanism for detecting this case in the future, but for now there is a small opportunity for this to happen and you should guard your application for concurrent writes if this is a huge concern. This function makes best effort to detect when database content definitely changed between iterations, in this case nil, "DB content changed while iterating" will be returned from the iterator.

Back to TOC

resty.lmdb.transaction

reset

syntax: txn:reset()

context: any context

Resets a transaction object. Removes all existing transactions and results (if any) from the object but keeps the table's capacity. After this call the transaction can be reused as if it was a new transaction returned by transaction.begin().

Back to TOC

get

syntax: txn:get(key, db?)

context: any context

Appends a get operation in the transactions table. If db is omitted, it defaults to "_default". The output table contains the following fields:

  • value: Value for key, or nil if key is not found

Back to TOC

set

syntax: txn:set(key, value, db?)

context: any context

Appends a set operation in the transactions table. If db is omitted, it defaults to "_default". The output able contains the following fields:

  • result: Always true for successful transaction commits

Back to TOC

db_open

syntax: txn:db_open(create, db?)

context: any context

Appends a db_open operation in the transactions table. If db is omitted, it defaults to "_default". This operation does not return anything in case of successful transaction commits.

Back to TOC

db_drop

syntax: txn:db_drop(delete, db?)

context: any context

Appends a db_drop operation in the transactions table. If db is omitted, it defaults to "_default". This operation does not return anything in case of successful transaction commits.

Back to TOC

commit

syntax: local res, err = txn:commit()

context: any context except init_by_lua*

Commits all operations currently inside the transactions table using a single LMDB transaction. Since LMDB transaction exhibits ACID (atomicity, consistency, isolation, durability) properties, this method will either commit all operations at once or fail without causing side effects.

In case of successful transaction commit, true will be returned. Output value of each operation can be accessed like this: txn[3].value. Please note that Lua table index starts at 1 and the order corresponds to the order operations were appended into the transaction. So the first operation's output will be inside txn[1] and second operation's result will be inside txn[2].

In case of any error during the transaction, it will be rolled back and nil and an string describing the reason of the failure will be returned instead. Accessing the output value from the txn table when commit() returned an error is undefined.

Back to TOC

resty.lmdb.prefix

page

syntax: res, err = prefix.page(start, prefix, db?)

context: any context

Return all keys >= start and starts with prefix. If db is omitted, it defaults to "_default".

The return value of this function is a table res where res[1].key and res[1].value corresponds to the first key and value, res[2].key and res[2].value corresponds to the second and etc. If no keys matched the provided criteria, then an empty table will be returned.

In case of errors, nil and an string describing the reason of the failure will be returned.

Back to TOC

Directives

lmdb_environment_path

syntax: lmdb_environment_path path;

context: main

Set the directory in which the LMDB database files reside.

Back to TOC

lmdb_max_databases

syntax: lmdb_max_databases number;

context: main

Set the maximum number of named databases, the default value is 1.

Back to TOC

lmdb_map_size

syntax: lmdb_map_size number;

context: main

Set the size of the memory map, the default value is 1048576(1MB).

Back to TOC

lmdb_validation_tag

syntax: lmdb_validation_tag value;

default: none

context: main

Set a content validation tag into LMDB. When LMDB starts, it will check the tag value, if the value is different from the directive value, the content of LMDB will be cleaned up.

When this directive is not set, tag validation is disabled.

Back to TOC

Copyright and license

Copyright (c) 2021-2022 Kong, Inc.

Licensed under the Apache License, Version 2.0 <LICENSE or https://www.apache.org/licenses/LICENSE-2.0>. Files in the project may not be copied, modified, or distributed except according to those terms.

Back to TOC

lua-resty-lmdb's People

Contributors

add-sp avatar bungle avatar chronolaw avatar curiositycasualty avatar dndx avatar fffonion avatar gszr avatar oowl avatar saisatishkarra avatar wheelerlaw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-resty-lmdb's Issues

dynamic module not binary compatible

(real quick thank you all for making this. i am trying to use it in a hobby project)

I incorporated the changes from #18 to the latest commit and attempted to build it as a dynamic module to drop into my existing nginx install from apt. after some fiddling i got it to build but nginx complains that the resulting .so was not binary compatible.

i'm using the same configuration options as shown in nginx -V which indicates it is built with the --with-compat flag and i have nginx source cloned from the official git mirror with the same version tag checked out (1.18.0) and i have done a make clean in both the nginx source directory and the lmdb submodule here before building with make modules

the only thing i can think of that is different is that i used a different version of openssl by accident and removed the the unnecesary other dynamic modules and the gzip and pcre modules because i didnt think them relevant. but by this point i realized i was out of my depth and aimlessly grasping.

any suggestions for how to proceed? or is this kind of question a bit out of scope?

lua cannot read lmdb create by python or c

I create a lmdb by python(pip3 install lmdb), set key and value, when I use lua to read it by the key, I get a nil data.
The python edition lmdb can be used by c edition(just use the c language to read it).

If I use lua in nginx to set key and value, the lmdb can read by lua.
Python and c cannot read it by the key with lua edition.

My key and value just like:
"key": "value"
"qwer": "1234"
Very simple strings

more:
python: 3.6.8
lua-resty-lmdb: v1.4.2
nginx: openresty-1.25.3.1

resty/lmdb/transaction.lua:245 lib/libluajit-5.1.so.2: undefined symbol: ngx_lua_resty_lmdb_ffi_execute

I want to use lmdb in my project
I make the lmdb and get liblmdb.so
I use lmdb.lua and transaction.lua in my project
but get the err undefined symbol: ngx_lua_resty_lmdb_ffi_execute
I think this may be my luajit problem, but how can I solve it

nginx version: openresty/1.19.3.1rc0

./configure --prefix=/data/pkg/openresty --user=nginx --group=nginx --with-pcre-jit --with-http_v2_module --with-http_sub_module --with-http_realip_module --with-http_stub_status_module --with-luajit --add-module=$otherbundles/nginx-sticky-module-ng --add-module=$otherbundles/nginx_upstream_check_module --add-module=$otherbundles/ngx_stream_upstream_check_module --with-openssl=$otherbundles/openssl/$OPENSSL_VERSION --with-pcre=$otherbundles/pcre/pcre-8.40 --with-zlib=$otherbundles/zlib/zlib-1.2.10

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.