├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── src ├── lrocksdb.c ├── lrocksdb.h ├── lrocksdb_backup_engine.c ├── lrocksdb_backup_engine.h ├── lrocksdb_db.h ├── lrocksdb_helpers.c ├── lrocksdb_helpers.h ├── lrocksdb_iter.c ├── lrocksdb_iter.h ├── lrocksdb_options.c ├── lrocksdb_options.h ├── lrocksdb_types.h ├── lrocksdb_writebatch.c └── lrocksdb_writebatch.h └── test ├── test_backup_engine.lua ├── test_iterator.lua ├── test_rocksdb.lua └── test_writebatch.lua /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | *.su 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | UNAME := $(shell uname) 2 | all-tests := $(basename $(wildcard test/*.lua)) 3 | 4 | LIB_PATH= /usr/local/lib64 5 | INC_PATH= /usr/local/include 6 | BIN_PATH= /usr/bin 7 | LUA_LIB= -L$(LIB_PATH) -llua 8 | LUA_INC= -I$(INC_PATH) -I$(INC_PATH)/lua 9 | ROCKSDB_INC= 10 | ROCKSDB_LIB= -lrocksdb 11 | EXTRACFLAGS= -std=c99 -undefined -fPIC 12 | 13 | # change these based on your installation 14 | ifeq ($(UNAME), Darwin) 15 | LIB_PATH= /usr/local/lib 16 | INC_PATH= /usr/local/include 17 | BIN_PATH= /usr/local/bin 18 | LUA_LIB= -L$(LIB_PATH) -llua5.1 19 | LUA_INC= -I$(INC_PATH) -I$(INC_PATH)/lua-5.1 20 | EXTRACFLAGS= -std=c99 -undefined dynamic_lookup -fPIC 21 | endif 22 | 23 | 24 | INC= $(LUA_INC) $(ROCKSDB_INC) 25 | LIB= $(LUA_LIB) $(ROCKSDB_LIB) 26 | WARN= -Wall 27 | CFLAGS= -O2 $(WARN) $(INC) 28 | 29 | MYNAME= rocksdb 30 | MYLIB= $(MYNAME) 31 | T= $(MYLIB).so 32 | OBJS= src/l$(MYLIB).o \ 33 | src/l$(MYLIB)_helpers.o \ 34 | src/l$(MYLIB)_options.o \ 35 | src/l$(MYLIB)_backup_engine.o \ 36 | src/l$(MYLIB)_writebatch.o \ 37 | src/l$(MYLIB)_iter.o 38 | 39 | all: $T 40 | 41 | %.o: %.c 42 | $(CC) $(CFLAGS) -fPIC -c -o $@ $< 43 | 44 | $T: $(OBJS) 45 | $(CC) $(CFLAGS) $(LIB) $(EXTRACFLAGS) -o $@ -shared $(OBJS) 46 | 47 | clean: 48 | rm -f $T $(OBJS) 49 | 50 | tests: $(all-tests) 51 | 52 | test/test_%: $T 53 | lua $@.lua 54 | 55 | test: clean all tests 56 | 57 | install: $(TARGET) 58 | 59 | 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | lua-rocksdb 2 | = 3 | Lua binding for RocksDB 4 | 5 | Installation 6 | === 7 | * Install RocksDB (make shared_lib, make static_lib), download from: https://github.com/facebook/rocksdb 8 | * Update the Makefile if needed to point at the correct include/lib paths 9 | 10 | Example 11 | === 12 | 13 | ```lua 14 | local rocksdb = require("rocksdb") 15 | local options = rocksdb.options({ 16 | create_if_missing = true 17 | }) 18 | local db = rocksdb.open(options, "/tmp/test.rocksdb") 19 | local writeoptions = rocksdb.writeoptions() 20 | local readoptions = rocksdb.readoptions() 21 | db:put(writeoptions, "key", "value") 22 | print(db:get(readoptions, "key")) 23 | db:close() 24 | ``` 25 | 26 | Doc 27 | === 28 | 29 | rocksdb.options() 30 | ===== 31 | Creates an options object, the options are passed as a table. 32 | Currently only basic types (bool, int, uint64) types are supported. 33 | 34 | rocksdb.readoptions() 35 | ===== 36 | Creates a readoptions object. 37 | 38 | rocksdb.writeoptions() 39 | ===== 40 | Create a writeoptions object. 41 | 42 | rocksdb.restoreoptions(table) 43 | ===== 44 | Create a restoreoptions object. 45 | 46 | * **table** options map, supported: 47 | * **keep_log_files** - number of log files to keep 48 | 49 | rocksdb.open(options, db_path) 50 | ===== 51 | * **options** - object (created using rocksdb.options()) 52 | * **db_path** - string. path 53 | * returns *rocksdb* object (referenced below as **db**) 54 | 55 | db:put(writeoptions, key, value) 56 | ===== 57 | Execute a put command, key-value (both of string type). 58 | 59 | * **writeoptions** - write options object (created using rocksdb.writeoptions()) 60 | * **key** - string 61 | * **value** - string 62 | 63 | rocksdb:get(readoptions, key) 64 | ===== 65 | Gets a value from the DB. 66 | 67 | * **readoptions** - read options object (created using rocksdb.readoptions()) 68 | * **key** - string 69 | 70 | rocksdb:write(writeoptions, writebatch) 71 | ===== 72 | Executes _write_ operation on a batch. 73 | 74 | * **writeoptions** - created using rocksdb.writeoptions() 75 | * **writebatch** - created using rocksdb.writebatch() 76 | 77 | rocksdb:iterator(readoptions) 78 | ===== 79 | Returns an iterator object. 80 | 81 | 82 | rocksdb:close() 83 | ===== 84 | Closes the instance (it doesn't destroy the DB!) 85 | 86 | rocksdb.backup_engine(options, backup_path) 87 | ===== 88 | Creates a backup engine object. 89 | 90 | * **options** options object (created using rocksdb.options()) 91 | * **backup_path** - string - backup path 92 | * returns *backup_engine* object 93 | 94 | backup_engine:create_new_backup(db) 95 | ===== 96 | Creates a back from *db*. 97 | 98 | * **db** db object (rocksdb.open()) 99 | * returns true on success, error on failure 100 | 101 | backup_engine:purge_old_backups(number) 102 | ===== 103 | Purge *number* of old backups. 104 | 105 | backup_engine:get_backup_info_count() 106 | ===== 107 | Returns the number of the available backups. 108 | 109 | backup_engine:get_backup_info(index) 110 | ===== 111 | Returns info of a backup at *index* (**note** indices start at 1, to keep with Lua 1-based array). 112 | * returns **table** 113 | 114 | _keys_ 115 | 116 | * **id** 117 | * **timestamp** 118 | * **size** 119 | * **number_files** 120 | 121 | 122 | backup_engine:restore_db_from_latest_backup(db_path, wal_dir, restoreoptions) 123 | ===== 124 | Restores DB from the latest backup. 125 | 126 | * **db_path** (string) 127 | * **wal_dir** (string) 128 | * **restoreoptions** (created using rocksdb.restoreoptions()) 129 | Returns an error on failure 130 | 131 | backup_engine:close() 132 | ===== 133 | Close/free the backup_engine instance 134 | 135 | rocksdb.writebatch() 136 | ===== 137 | Creates a *writebatch* instance. 138 | 139 | writebatch:put(key, value) 140 | ===== 141 | Puts a *key* with its *value* into the batch (both are strings). 142 | 143 | writebatch:count() 144 | ===== 145 | Returns the *count* of key-values pairs in the batch. 146 | 147 | writebatch:clear() 148 | ===== 149 | Clears the writebatch. 150 | 151 | writebatch:destroy() 152 | ===== 153 | Frees the writebatch instance. 154 | 155 | 156 | iterator:valid() 157 | ===== 158 | Returns _true_ if the iterator is valid, _false_ otherwise. 159 | 160 | iterator:seek_to_first(), iterator:seek_to_last() 161 | ===== 162 | Seek to first/last iterm. 163 | 164 | 165 | iterator:seek(key) 166 | ===== 167 | Seek specific _key_. 168 | 169 | iterator:next(), iterator:prev() 170 | ===== 171 | Move to next/prev item. 172 | 173 | iterator:key(), iterator:value() 174 | ===== 175 | Returns _string_ key/value. 176 | 177 | iterator:get_error() 178 | ===== 179 | Returns _string_ error message of the iterator. 180 | 181 | iterator:destroy() 182 | ===== 183 | Frees the iterator instance. 184 | -------------------------------------------------------------------------------- /src/lrocksdb.c: -------------------------------------------------------------------------------- 1 | #include "lrocksdb.h" 2 | 3 | LUALIB_API int lrocksdb_reg(lua_State *L) { 4 | lrocksdb_createmeta(L, "db", lrocksdb_db_reg); 5 | return 1; 6 | } 7 | 8 | lrocksdb_t *lrocksdb_get_db(lua_State *L, int index) { 9 | lrocksdb_t *o = (lrocksdb_t *) luaL_checkudata(L, index, "db"); 10 | luaL_argcheck(L, o != NULL && o->db != NULL, index, "db expected"); 11 | return o; 12 | } 13 | 14 | 15 | LUALIB_API int lrocksdb_open(lua_State *L) { 16 | lrocksdb_options_t *o = lrocksdb_get_options(L, 1); 17 | const char *path = luaL_checkstring(L, 2); 18 | char *err = NULL; 19 | rocksdb_t *db = rocksdb_open(o->options, path, &err); 20 | 21 | if(err) { 22 | luaL_error(L, err); 23 | free(err); 24 | return 0; 25 | } 26 | 27 | lrocksdb_t *d = (lrocksdb_t *) lua_newuserdata(L, sizeof(lrocksdb_t)); 28 | d->db = db; 29 | d->options = o; 30 | d->open = 1; 31 | lrocksdb_setmeta(L, "db"); 32 | return 1; 33 | } 34 | 35 | LUALIB_API int lrocksdb_open_for_read_only(lua_State *L) { 36 | lrocksdb_options_t *o = lrocksdb_get_options(L, 1); 37 | const char *name = luaL_checkstring(L, 2); 38 | unsigned char error_if_log_file_exist = lua_toboolean(L, 3); 39 | char *err = NULL; 40 | rocksdb_t *db = rocksdb_open_for_read_only(o->options, name, 41 | error_if_log_file_exist, &err); 42 | 43 | if(err) { 44 | luaL_error(L, err); 45 | free(err); 46 | return 0; 47 | } 48 | 49 | lrocksdb_t *d = (lrocksdb_t *) lua_newuserdata(L, sizeof(lrocksdb_t)); 50 | d->db = db; 51 | d->options = o; 52 | d->open = 1; 53 | d->read_only = 1; 54 | lrocksdb_setmeta(L, "db"); 55 | return 1; 56 | } 57 | 58 | LUALIB_API int lrocksdb_put(lua_State *L) { 59 | lrocksdb_t *d = lrocksdb_get_db(L, 1); 60 | lrocksdb_writeoptions_t *wo = lrocksdb_get_writeoptions(L, 2); 61 | size_t key_len, value_len; 62 | const char *key, *value; 63 | char *err = NULL; 64 | 65 | key = luaL_checklstring(L, 3, &key_len); 66 | value = luaL_checklstring(L, 4, &value_len); 67 | rocksdb_put(d->db, wo->writeoptions, key, key_len, value, value_len, &err); 68 | if(err) { 69 | luaL_error(L, err); 70 | free(err); 71 | return 0; 72 | } 73 | return 1; 74 | } 75 | 76 | LUALIB_API int lrocksdb_get(lua_State *L) { 77 | lrocksdb_t *d = lrocksdb_get_db(L, 1); 78 | lrocksdb_assert(L, d->open, "db is closed"); 79 | lrocksdb_readoptions_t *ro = lrocksdb_get_readoptions(L, 2); 80 | size_t key_len, value_len; 81 | const char *key = luaL_checklstring(L, 3, &key_len); 82 | char *err = NULL; 83 | char *value = rocksdb_get(d->db, ro->readoptions, key, key_len, &value_len, &err); 84 | if(err) { 85 | luaL_error(L, err); 86 | free(err); 87 | return 0; 88 | } 89 | if(value != NULL) { 90 | lua_pushlstring(L, value, value_len); 91 | free(value); 92 | } 93 | else { 94 | lua_pushnil(L); 95 | } 96 | return 1; 97 | } 98 | 99 | LUALIB_API int lrocksdb_delete(lua_State *L) { 100 | lrocksdb_t *d = lrocksdb_get_db(L, 1); 101 | lrocksdb_writeoptions_t *wo = lrocksdb_get_writeoptions(L, 2); 102 | size_t key_len; 103 | const char *key; 104 | char *err = NULL; 105 | 106 | key = luaL_checklstring(L, 3, &key_len); 107 | rocksdb_delete(d->db, wo->writeoptions, key, key_len, &err); 108 | if(err) { 109 | luaL_error(L, err); 110 | free(err); 111 | return 0; 112 | } 113 | lua_pushnil(L); 114 | return 1; 115 | } 116 | 117 | LUALIB_API int lrocksdb_write(lua_State *L) { 118 | lrocksdb_t *d = lrocksdb_get_db(L, 1); 119 | lrocksdb_writeoptions_t *wo = lrocksdb_get_writeoptions(L, 2); 120 | lrocksdb_writebatch_t *rb = lrocksdb_get_writebatch(L, 3); 121 | char *err = NULL; 122 | rocksdb_write(d->db, wo->writeoptions, rb->writebatch, &err); 123 | if(err) { 124 | luaL_error(L, err); 125 | free(err); 126 | return 0; 127 | } 128 | lua_pushboolean(L, 1); 129 | return 1; 130 | } 131 | 132 | LUALIB_API int lrocksdb_close(lua_State *L) { 133 | lrocksdb_t *d = lrocksdb_get_db(L, 1); 134 | rocksdb_close(d->db); 135 | d->open = 0; 136 | return 1; 137 | } 138 | 139 | LUALIB_API int lrocksdb_create_iterator(lua_State *L) { 140 | lrocksdb_t *d = lrocksdb_get_db(L, 1); 141 | lrocksdb_readoptions_t *ro = lrocksdb_get_readoptions(L, 2); 142 | lrocksdb_iterator_t *i = (lrocksdb_iterator_t *) 143 | lua_newuserdata(L, sizeof(lrocksdb_iterator_t)); 144 | i->iter = rocksdb_create_iterator(d->db, ro->readoptions); 145 | lrocksdb_setmeta(L, "iterator"); 146 | return 1; 147 | } 148 | 149 | LUALIB_API int lrocksdb_property_value(lua_State *L) { 150 | lrocksdb_t *d = lrocksdb_get_db(L, 1); 151 | const char* propname = luaL_checkstring(L, 2); 152 | char *propvalue = rocksdb_property_value(d->db, propname); 153 | if(propvalue != NULL) { 154 | lua_pushstring(L, propvalue); 155 | free(propvalue); 156 | } 157 | else { 158 | lua_pushnil(L); 159 | } 160 | return 1; 161 | } 162 | 163 | LUALIB_API int luaopen_rocksdb(lua_State *L) { 164 | lua_newtable(L); 165 | 166 | /* register classes */ 167 | for(int i = 0; lrocksdb_regs[i].name != NULL; i++) { 168 | lrocksdb_regs[i].func(L); 169 | } 170 | 171 | luaL_setfuncs(L, lrocksdb_funcs, 0); 172 | 173 | lua_pushliteral(L, LROCKSDB_VERSION); 174 | lua_setfield(L, -2, "_VERSION"); 175 | lua_pushliteral(L, LROCKSDB_COPYRIGHT); 176 | lua_setfield(L, -2, "_COPYRIGHT"); 177 | lua_pushliteral(L, LROCKSDB_VERSION); 178 | lua_setfield(L, -2, "_DESCRIPTION"); 179 | 180 | return 1; 181 | } 182 | 183 | -------------------------------------------------------------------------------- /src/lrocksdb.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_H 2 | #define LROCKSDB_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "lauxlib.h" 8 | 9 | #include "rocksdb/c.h" 10 | #include "lrocksdb_helpers.h" 11 | #include "lrocksdb_types.h" 12 | #include "lrocksdb_db.h" 13 | #include "lrocksdb_options.h" 14 | #include "lrocksdb_backup_engine.h" 15 | #include "lrocksdb_writebatch.h" 16 | #include "lrocksdb_iter.h" 17 | 18 | #define LROCKSDB_VERSION "lua-rocksdb 0.0.1" 19 | #define LROCKSDB_COPYRIGHT "Copyright (C) 2016, Zaher Marzuq" 20 | #define LROCKSDB_DESCRIPTION "RocksDB binding for Lua" 21 | 22 | static const struct luaL_Reg lrocksdb_db_reg[] = { 23 | { "put", lrocksdb_put }, 24 | { "get", lrocksdb_get }, 25 | { "close", lrocksdb_close }, 26 | { "delete", lrocksdb_delete }, 27 | { "write", lrocksdb_write }, 28 | { "iterator", lrocksdb_create_iterator }, 29 | { "property_value", lrocksdb_property_value }, 30 | { NULL, NULL } 31 | }; 32 | 33 | static const struct luaL_Reg lrocksdb_regs[] = { 34 | { "db", lrocksdb_reg }, 35 | { "options", lrocksdb_options_reg }, 36 | { "writeoptions", lrocksdb_writeoptions_reg }, 37 | { "readoptions", lrocksdb_readoptions_reg }, 38 | { "backup_engine", lrocksdb_backup_engine_reg }, 39 | { "writebatch", lrocksdb_writebatch_reg }, 40 | { "restoreoptions", lrocksdb_restoreoptions_reg }, 41 | { "iterator", lrocksdb_iter_reg }, 42 | { NULL, NULL } 43 | }; 44 | 45 | static const struct luaL_Reg lrocksdb_funcs[] = { 46 | { "open", lrocksdb_open }, 47 | { "open_for_read_only", lrocksdb_open_for_read_only }, 48 | { "options", lrocksdb_options_create }, 49 | { "writeoptions", lrocksdb_writeoptions_create }, 50 | { "readoptions", lrocksdb_readoptions_create }, 51 | { "backup_engine", lrocksdb_backup_engine_open }, 52 | { "writebatch", lrocksdb_writebatch_create }, 53 | { "restoreoptions", lrocksdb_restoreoptions_create }, 54 | { NULL, NULL } 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/lrocksdb_backup_engine.c: -------------------------------------------------------------------------------- 1 | #include "lrocksdb_backup_engine.h" 2 | 3 | LUALIB_API int lrocksdb_backup_engine_reg(lua_State *L) { 4 | lrocksdb_createmeta(L, "backup_engine", backup_engine_reg); 5 | return 1; 6 | } 7 | 8 | lrocksdb_backup_engine_t *lrocksdb_get_backup_engine(lua_State *L, int index) { 9 | lrocksdb_backup_engine_t *o = (lrocksdb_backup_engine_t*) 10 | luaL_checkudata(L, index, "backup_engine"); 11 | luaL_argcheck(L, o != NULL && o->backup_engine!= NULL, index, "backup_engine expected"); 12 | return o; 13 | } 14 | 15 | LUALIB_API int lrocksdb_backup_engine_open(lua_State *L) { 16 | lrocksdb_options_t *o = lrocksdb_get_options(L, 1); 17 | const char *path = luaL_checkstring(L, 2); 18 | char *err = NULL; 19 | lrocksdb_backup_engine_t *b = (lrocksdb_backup_engine_t *) 20 | lua_newuserdata(L, sizeof(lrocksdb_backup_engine_t)); 21 | b->backup_engine = rocksdb_backup_engine_open(o->options, path, &err); 22 | if(err) { 23 | luaL_error(L, err); 24 | free(err); 25 | return 0; 26 | } 27 | lrocksdb_setmeta(L, "backup_engine"); 28 | return 1; 29 | } 30 | 31 | LUALIB_API int lrocksdb_backup_engine_create_new_backup(lua_State *L) { 32 | lrocksdb_backup_engine_t *be = lrocksdb_get_backup_engine(L, 1); 33 | lrocksdb_t *db = lrocksdb_get_db(L, 2); 34 | char *err = NULL; 35 | rocksdb_backup_engine_create_new_backup(be->backup_engine, db->db, &err); 36 | if(err) { 37 | luaL_error(L, err); 38 | free(err); 39 | return 0; 40 | } 41 | return 1; 42 | } 43 | 44 | LUALIB_API int lrocksdb_backup_engine_purge_old_backups(lua_State *L) { 45 | lrocksdb_backup_engine_t *be = lrocksdb_get_backup_engine(L, 1); 46 | uint32_t num_backups_to_keep = luaL_checknumber(L, 2); 47 | char *err = NULL; 48 | rocksdb_backup_engine_purge_old_backups(be->backup_engine, num_backups_to_keep, &err); 49 | if(err) { 50 | luaL_error(L, err); 51 | free(err); 52 | return 0; 53 | } 54 | return 1; 55 | } 56 | 57 | LUALIB_API int lrocksdb_backup_engine_restore_db_from_latest_backup(lua_State *L) { 58 | lrocksdb_backup_engine_t *be = lrocksdb_get_backup_engine(L, 1); 59 | const char* db_dir = luaL_checkstring(L, 2); 60 | const char* wal_dir = luaL_checkstring(L, 3); 61 | lrocksdb_restoreoptions_t *ro = lrocksdb_get_restoreoptions(L, 4); 62 | char *err = NULL; 63 | rocksdb_backup_engine_restore_db_from_latest_backup(be->backup_engine, db_dir, 64 | wal_dir, ro->restoreoptions, &err); 65 | if(err != NULL) { 66 | luaL_error(L, err); 67 | free(err); 68 | return 0; 69 | } 70 | return 1; 71 | } 72 | 73 | LUALIB_API int lrocksdb_backup_engine_get_backup_info_count(lua_State *L) { 74 | lrocksdb_backup_engine_t *be = lrocksdb_get_backup_engine(L, 1); 75 | const rocksdb_backup_engine_info_t 76 | *info = rocksdb_backup_engine_get_backup_info(be->backup_engine); 77 | if(info) { 78 | int count = rocksdb_backup_engine_info_count(info); 79 | lua_pushnumber(L, count); 80 | rocksdb_backup_engine_info_destroy(info); 81 | } 82 | return 1; 83 | } 84 | 85 | LUALIB_API int lrocksdb_backup_engine_get_backup_info(lua_State *L) { 86 | lrocksdb_backup_engine_t *be = lrocksdb_get_backup_engine(L, 1); 87 | int index = luaL_checkint(L, 2) - 1; //keeping with Lua indices start at 1 88 | const rocksdb_backup_engine_info_t 89 | *info = rocksdb_backup_engine_get_backup_info(be->backup_engine); 90 | int count = rocksdb_backup_engine_info_count(info); 91 | if(index < 0 || index >= count) { 92 | luaL_error(L, "index out of range"); 93 | rocksdb_backup_engine_info_destroy(info); 94 | } 95 | int64_t timestamp = rocksdb_backup_engine_info_timestamp(info, index); 96 | uint32_t id = rocksdb_backup_engine_info_backup_id(info, index); 97 | uint64_t size = rocksdb_backup_engine_info_size(info, index); 98 | uint32_t number_files = rocksdb_backup_engine_info_number_files(info, index); 99 | rocksdb_backup_engine_info_destroy(info); 100 | lua_newtable(L); 101 | lua_pushnumber(L, timestamp); 102 | lua_setfield(L, -2, "timestamp"); 103 | lua_pushnumber(L, id); 104 | lua_setfield(L, -2, "id"); 105 | lua_pushnumber(L, size); 106 | lua_setfield(L, -2, "size"); 107 | lua_pushnumber(L, number_files); 108 | lua_setfield(L, -2, "number_files"); 109 | return 1; 110 | } 111 | 112 | LUALIB_API int lrocksdb_backup_engine_close(lua_State *L) { 113 | lrocksdb_backup_engine_t *be = lrocksdb_get_backup_engine(L, 1); 114 | if(be->backup_engine != NULL) { 115 | rocksdb_backup_engine_close(be->backup_engine); 116 | be->backup_engine = NULL; 117 | } 118 | return 1; 119 | } 120 | 121 | -------------------------------------------------------------------------------- /src/lrocksdb_backup_engine.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_BACKUP_ENGINE_H 2 | #define LROCKSDB_BACKUP_ENGINE_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "lauxlib.h" 8 | #include "lrocksdb_types.h" 9 | #include "lrocksdb_helpers.h" 10 | #include "lrocksdb_options.h" 11 | #include "lrocksdb_db.h" 12 | 13 | lrocksdb_backup_engine_t *lrocksdb_get_backup_engine(lua_State *L, int index); 14 | LUALIB_API int lrocksdb_backup_engine_reg(lua_State *L); 15 | LUALIB_API int lrocksdb_backup_engine_open(lua_State *L); 16 | LUALIB_API int lrocksdb_backup_engine_create_new_backup(lua_State *L); 17 | LUALIB_API int lrocksdb_backup_engine_purge_old_backups(lua_State *L); 18 | LUALIB_API int lrocksdb_backup_engine_restore_db_from_latest_backup(lua_State *L); 19 | LUALIB_API int lrocksdb_backup_engine_get_backup_info_count(lua_State *L); 20 | LUALIB_API int lrocksdb_backup_engine_get_backup_info(lua_State *L); 21 | LUALIB_API int lrocksdb_backup_engine_close(lua_State *L); 22 | 23 | static const struct luaL_Reg backup_engine_reg[] = { 24 | { "create_new_backup", lrocksdb_backup_engine_create_new_backup }, 25 | { "purge_old_backups", lrocksdb_backup_engine_purge_old_backups }, 26 | { "restore_db_from_latest_backup", lrocksdb_backup_engine_restore_db_from_latest_backup }, 27 | { "get_backup_info_count", lrocksdb_backup_engine_get_backup_info_count }, 28 | { "get_backup_info", lrocksdb_backup_engine_get_backup_info }, 29 | { "close", lrocksdb_backup_engine_close }, 30 | { "__gc", lrocksdb_backup_engine_close }, 31 | { NULL, NULL } 32 | }; 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /src/lrocksdb_db.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_DB_H 2 | #define LROCKSDB_DB_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "lauxlib.h" 8 | 9 | #include "lrocksdb_helpers.h" 10 | #include "lrocksdb_types.h" 11 | #include "lrocksdb_options.h" 12 | 13 | lrocksdb_t *lrocksdb_get_db(lua_State *L, int index); 14 | LUALIB_API int lrocksdb_reg(lua_State *L); 15 | LUALIB_API int lrocksdb_open(lua_State *L); 16 | LUALIB_API int lrocksdb_put(lua_State *L); 17 | LUALIB_API int lrocksdb_get(lua_State *L); 18 | LUALIB_API int lrocksdb_close(lua_State *L); 19 | LUALIB_API int lrocksdb_open_for_read_only(lua_State *L); 20 | LUALIB_API int lrocksdb_delete(lua_State *L); 21 | LUALIB_API int lrocksdb_write(lua_State *L); 22 | LUALIB_API int lrocksdb_create_iterator(lua_State *L); 23 | LUALIB_API int lrocksdb_property_value(lua_State *L); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/lrocksdb_helpers.c: -------------------------------------------------------------------------------- 1 | #include "lrocksdb_helpers.h" 2 | 3 | #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 4 | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { 5 | luaL_checkstack(L, nup+1, "too many upvalues"); 6 | for (; l->name != NULL; l++) { /* fill the table with given functions */ 7 | int i; 8 | lua_pushstring(L, l->name); 9 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ 10 | lua_pushvalue(L, -(nup+1)); 11 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ 12 | lua_settable(L, -(nup + 3)); 13 | } 14 | lua_pop(L, nup); /* remove upvalues */ 15 | } 16 | #endif 17 | 18 | LUALIB_API int lrocksdb_createmeta(lua_State *L, const char *name, const luaL_Reg *methods) { 19 | if (!luaL_newmetatable(L, name)) { 20 | return 0; 21 | } 22 | 23 | lua_pushstring(L, "__index"); 24 | lua_newtable(L); 25 | lua_pushstring(L, "class"); 26 | lua_pushstring(L, name); 27 | lua_rawset(L, -3); 28 | for (; methods->name; methods++) { 29 | lua_pushstring(L, methods->name); 30 | lua_pushcfunction(L, methods->func); 31 | lua_rawset(L, methods->name[0] == '_' ? -5: -3); 32 | } 33 | lua_rawset(L, -3); 34 | lua_pop(L, 1); 35 | return 1; 36 | } 37 | 38 | 39 | LUALIB_API void lrocksdb_setmeta(lua_State *L, const char *name) { 40 | luaL_getmetatable(L, name); 41 | lua_setmetatable(L, -2); 42 | } 43 | 44 | 45 | void lrocksdb_assert(lua_State *L, int cond, const char *msg) { 46 | if(!cond) { 47 | luaL_error(L, msg); 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/lrocksdb_helpers.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_HELPERS_H 2 | #define LROCKSDB_HELPERS_H 3 | #include "lauxlib.h" 4 | #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 5 | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); 6 | #endif 7 | LUALIB_API int lrocksdb_createmeta(lua_State *L, const char *name, const luaL_Reg *methods); 8 | LUALIB_API void lrocksdb_setmeta(lua_State *L, const char *name); 9 | void lrocksdb_assert(lua_State *L, int cond, const char *msg); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/lrocksdb_iter.c: -------------------------------------------------------------------------------- 1 | #include "lrocksdb_iter.h" 2 | 3 | LUALIB_API int lrocksdb_iter_reg(lua_State *L) { 4 | lrocksdb_createmeta(L, "iterator", iter_reg); 5 | return 1; 6 | } 7 | 8 | lrocksdb_iterator_t *lrocksdb_get_iter(lua_State *L, int index) { 9 | lrocksdb_iterator_t *i = (lrocksdb_iterator_t *) luaL_checkudata(L, index, "iterator"); 10 | luaL_argcheck(L, i != NULL && i->iter != NULL, index, "iterator expected"); 11 | return i; 12 | } 13 | 14 | LUALIB_API int lrocksdb_iter_valid(lua_State *L) { 15 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 16 | unsigned char valid = rocksdb_iter_valid(i->iter); 17 | lua_pushboolean(L, valid); 18 | return 1; 19 | } 20 | 21 | LUALIB_API int lrocksdb_iter_seek_to_first(lua_State *L) { 22 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 23 | rocksdb_iter_seek_to_first(i->iter); 24 | return 1; 25 | } 26 | 27 | LUALIB_API int lrocksdb_iter_seek_to_last(lua_State *L) { 28 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 29 | rocksdb_iter_seek_to_last(i->iter); 30 | return 1; 31 | } 32 | 33 | LUALIB_API int lrocksdb_iter_seek(lua_State *L) { 34 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 35 | size_t klen; 36 | const char* k = luaL_checklstring(L, 2, &klen); 37 | rocksdb_iter_seek(i->iter, k, klen); 38 | return 1; 39 | } 40 | 41 | LUALIB_API int lrocksdb_iter_next(lua_State *L) { 42 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 43 | rocksdb_iter_next(i->iter); 44 | return 1; 45 | } 46 | 47 | LUALIB_API int lrocksdb_iter_prev(lua_State *L) { 48 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 49 | rocksdb_iter_prev(i->iter); 50 | return 1; 51 | } 52 | LUALIB_API int lrocksdb_iter_key(lua_State *L) { 53 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 54 | lrocksdb_assert(L, rocksdb_iter_valid(i->iter), "invalid iterator"); 55 | size_t klen; 56 | const char* key = rocksdb_iter_key(i->iter, &klen); 57 | if(key != NULL) { 58 | lua_pushlstring(L, key, klen); 59 | } 60 | else { 61 | lua_pushnil(L); 62 | } 63 | return 1; 64 | } 65 | 66 | LUALIB_API int lrocksdb_iter_value(lua_State *L) { 67 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 68 | lrocksdb_assert(L, rocksdb_iter_valid(i->iter), "invalid iterator"); 69 | size_t vlen; 70 | const char *value = rocksdb_iter_value(i->iter, &vlen); 71 | if(value != NULL) { 72 | lua_pushlstring(L, value, vlen); 73 | } 74 | else { 75 | lua_pushnil(L); 76 | } 77 | return 1; 78 | } 79 | LUALIB_API int lrocksdb_iter_get_error(lua_State *L) { 80 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 81 | char *err = NULL; 82 | rocksdb_iter_get_error(i->iter, &err); 83 | if(err != NULL) { 84 | lua_pushstring(L, err); 85 | free(err); 86 | } 87 | else { 88 | lua_pushnil(L); 89 | } 90 | return 1; 91 | } 92 | 93 | LUALIB_API int lrocksdb_iter_destroy(lua_State *L) { 94 | lrocksdb_iterator_t *i = lrocksdb_get_iter(L, 1); 95 | if(i->iter != NULL) { 96 | rocksdb_iter_destroy(i->iter); 97 | i->iter = NULL; 98 | } 99 | return 1; 100 | } 101 | -------------------------------------------------------------------------------- /src/lrocksdb_iter.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_ITER_H 2 | #define LROCKSDB_ITER_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "lauxlib.h" 8 | #include "lrocksdb_types.h" 9 | #include "lrocksdb_helpers.h" 10 | #include "lrocksdb_options.h" 11 | #include "lrocksdb_db.h" 12 | 13 | lrocksdb_iterator_t *lrocksdb_get_iter(lua_State *L, int index); 14 | LUALIB_API int lrocksdb_iter_reg(lua_State *L); 15 | LUALIB_API int lrocksdb_iter_valid(lua_State *L); 16 | LUALIB_API int lrocksdb_iter_seek_to_first(lua_State *L); 17 | LUALIB_API int lrocksdb_iter_seek_to_last(lua_State *L); 18 | LUALIB_API int lrocksdb_iter_seek(lua_State *L); 19 | LUALIB_API int lrocksdb_iter_next(lua_State *L); 20 | LUALIB_API int lrocksdb_iter_prev(lua_State *L); 21 | LUALIB_API int lrocksdb_iter_key(lua_State *L); 22 | LUALIB_API int lrocksdb_iter_value(lua_State *L); 23 | LUALIB_API int lrocksdb_iter_get_error(lua_State *L); 24 | LUALIB_API int lrocksdb_iter_destroy(lua_State *L); 25 | 26 | static const struct luaL_Reg iter_reg[] = { 27 | { "valid", lrocksdb_iter_valid }, 28 | { "seek_to_first", lrocksdb_iter_seek_to_first }, 29 | { "seek_to_last", lrocksdb_iter_seek_to_last }, 30 | { "seek", lrocksdb_iter_seek }, 31 | { "next", lrocksdb_iter_next }, 32 | { "prev", lrocksdb_iter_prev }, 33 | { "key", lrocksdb_iter_key }, 34 | { "value", lrocksdb_iter_value }, 35 | { "get_error", lrocksdb_iter_get_error }, 36 | { "destroy", lrocksdb_iter_destroy }, 37 | { "__gec", lrocksdb_iter_destroy }, 38 | { NULL, NULL } 39 | }; 40 | 41 | #endif 42 | 43 | -------------------------------------------------------------------------------- /src/lrocksdb_options.c: -------------------------------------------------------------------------------- 1 | #include "lrocksdb_options.h" 2 | 3 | LUALIB_API int lrocksdb_options_reg(lua_State *L) { 4 | lrocksdb_createmeta(L, "options", options_reg); 5 | return 1; 6 | } 7 | 8 | LUALIB_API int lrocksdb_writeoptions_reg(lua_State *L) { 9 | lrocksdb_createmeta(L, "writeoptions", writeoptions_reg); 10 | return 1; 11 | } 12 | 13 | LUALIB_API int lrocksdb_readoptions_reg(lua_State *L) { 14 | lrocksdb_createmeta(L, "readoptions", readoptions_reg); 15 | return 1; 16 | } 17 | 18 | LUALIB_API int lrocksdb_restoreoptions_reg(lua_State *L) { 19 | lrocksdb_createmeta(L, "restoreoptions", restoreoptions_reg); 20 | return 1; 21 | } 22 | 23 | void lrocksdb_options_set_from_table(lua_State *L, int index, rocksdb_options_t *opt) { 24 | int opt_int; 25 | uint64_t opt_uint64; 26 | unsigned char opt_bool; 27 | 28 | lua_pushvalue(L, index); 29 | lua_pushnil(L); 30 | while (lua_next(L, -2)) 31 | { 32 | lua_pushvalue(L, -2); 33 | const char *key = lua_tostring(L, -1); 34 | /* int options */ 35 | if(strcmp(key, "increase_parallelism") == 0) { 36 | opt_int = luaL_checkint(L, -2); 37 | rocksdb_options_increase_parallelism(opt, opt_int); 38 | } 39 | else if(strcmp(key, "info_log_level") == 0) { 40 | opt_int = luaL_checkint(L, -2); 41 | rocksdb_options_set_info_log_level(opt, opt_int); 42 | } 43 | else if(strcmp(key, "max_open_files") == 0) { 44 | opt_int = luaL_checkint(L, -2); 45 | rocksdb_options_set_max_open_files(opt, opt_int); 46 | } 47 | else if(strcmp(key, "num_levels") == 0) { 48 | opt_int = luaL_checkint(L, -2); 49 | rocksdb_options_set_num_levels(opt, opt_int); 50 | } 51 | else if(strcmp(key, "level0_file_num_compaction_trigger") == 0) { 52 | opt_int = luaL_checkint(L, -2); 53 | rocksdb_options_set_level0_file_num_compaction_trigger(opt, opt_int); 54 | } 55 | else if(strcmp(key, "level0_slowdown_writes_trigger") == 0) { 56 | opt_int = luaL_checkint(L, -2); 57 | rocksdb_options_set_level0_slowdown_writes_trigger(opt, opt_int); 58 | } 59 | else if(strcmp(key, "level0_stop_writes_trigger") == 0) { 60 | opt_int = luaL_checkint(L, -2); 61 | rocksdb_options_set_level0_stop_writes_trigger(opt, opt_int); 62 | } 63 | else if(strcmp(key, "max_mem_compaction_level") == 0) { 64 | opt_int = luaL_checkint(L, -2); 65 | rocksdb_options_set_max_mem_compaction_level(opt, opt_int); 66 | } 67 | else if(strcmp(key, "target_file_size_multiplier") == 0) { 68 | opt_int = luaL_checkint(L, -2); 69 | rocksdb_options_set_target_file_size_multiplier(opt, opt_int); 70 | } 71 | else if(strcmp(key, "max_write_buffer_number") == 0) { 72 | opt_int = luaL_checkint(L, -2); 73 | rocksdb_options_set_max_write_buffer_number(opt, opt_int); 74 | } 75 | else if(strcmp(key, "min_write_buffer_number_to_merge") == 0) { 76 | opt_int = luaL_checkint(L, -2); 77 | rocksdb_options_set_min_write_buffer_number_to_merge(opt, opt_int); 78 | } 79 | else if(strcmp(key, "max_write_buffer_number_to_maintain") == 0) { 80 | opt_int = luaL_checkint(L, -2); 81 | rocksdb_options_set_max_write_buffer_number_to_maintain(opt, opt_int); 82 | } 83 | else if(strcmp(key, "max_background_compactions") == 0) { 84 | opt_int = luaL_checkint(L, -2); 85 | rocksdb_options_set_max_background_compactions(opt, opt_int); 86 | } 87 | else if(strcmp(key, "max_background_flushes") == 0) { 88 | opt_int = luaL_checkint(L, -2); 89 | rocksdb_options_set_max_background_flushes(opt, opt_int); 90 | } 91 | else if(strcmp(key, "table_cache_numshardbits") == 0) { 92 | opt_int = luaL_checkint(L, -2); 93 | rocksdb_options_set_table_cache_numshardbits(opt, opt_int); 94 | } 95 | else if(strcmp(key, "table_cache_remove_scan_count_limit") == 0) { 96 | opt_int = luaL_checkint(L, -2); 97 | rocksdb_options_set_table_cache_remove_scan_count_limit(opt, opt_int); 98 | } 99 | else if(strcmp(key, "use_fsync") == 0) { 100 | opt_int = luaL_checkint(L, -2); 101 | rocksdb_options_set_use_fsync(opt, opt_int); 102 | } 103 | else if(strcmp(key, "access_hint_on_compaction_start") == 0) { 104 | opt_int = luaL_checkint(L, -2); 105 | rocksdb_options_set_access_hint_on_compaction_start(opt, opt_int); 106 | } 107 | else if(strcmp(key, "disable_data_sync") == 0) { 108 | opt_int = luaL_checkint(L, -2); 109 | rocksdb_options_set_disable_data_sync(opt, opt_int); 110 | } 111 | else if(strcmp(key, "disable_auto_compactions") == 0) { 112 | opt_int = luaL_checkint(L, -2); 113 | rocksdb_options_set_disable_auto_compactions(opt, opt_int); 114 | } 115 | else if(strcmp(key, "report_bg_io_stats") == 0) { 116 | opt_int = luaL_checkint(L, -2); 117 | rocksdb_options_set_report_bg_io_stats(opt, opt_int); 118 | } 119 | else if(strcmp(key, "compression") == 0) { 120 | opt_int = luaL_checkint(L, -2); 121 | rocksdb_options_set_compression(opt, opt_int); 122 | } 123 | else if(strcmp(key, "compaction_style") == 0) { 124 | opt_int = luaL_checkint(L, -2); 125 | rocksdb_options_set_compaction_style(opt, opt_int); 126 | } 127 | /* bool options */ 128 | else if(strcmp(key, "create_if_missing") == 0) { 129 | opt_bool = lua_toboolean(L, -2); 130 | rocksdb_options_set_create_if_missing(opt, opt_bool); 131 | } 132 | else if(strcmp(key, "create_missing_column_families") == 0) { 133 | opt_bool = lua_toboolean(L, -2); 134 | rocksdb_options_set_create_missing_column_families(opt, opt_bool); 135 | } 136 | else if(strcmp(key, "error_if_exists") == 0) { 137 | opt_bool = lua_toboolean(L, -2); 138 | rocksdb_options_set_error_if_exists(opt, opt_bool); 139 | } 140 | else if(strcmp(key, "paranoid_checks") == 0) { 141 | opt_bool = lua_toboolean(L, -2); 142 | rocksdb_options_set_paranoid_checks(opt, opt_bool); 143 | } 144 | else if(strcmp(key, "purge_redundant_kvs_while_flush") == 0) { 145 | opt_bool = lua_toboolean(L, -2); 146 | rocksdb_options_set_purge_redundant_kvs_while_flush(opt, opt_bool); 147 | } 148 | else if(strcmp(key, "allow_os_buffer") == 0) { 149 | opt_bool = lua_toboolean(L, -2); 150 | rocksdb_options_set_allow_os_buffer(opt, opt_bool); 151 | } 152 | else if(strcmp(key, "allow_mmap_reads") == 0) { 153 | opt_bool = lua_toboolean(L, -2); 154 | rocksdb_options_set_allow_mmap_reads(opt, opt_bool); 155 | } 156 | else if(strcmp(key, "allow_mmap_writes") == 0) { 157 | opt_bool = lua_toboolean(L, -2); 158 | rocksdb_options_set_allow_mmap_writes(opt, opt_bool); 159 | } 160 | else if(strcmp(key, "is_fd_close_on_exec") == 0) { 161 | opt_bool = lua_toboolean(L, -2); 162 | rocksdb_options_set_is_fd_close_on_exec(opt, opt_bool); 163 | } 164 | else if(strcmp(key, "skip_log_error_on_recovery") == 0) { 165 | opt_bool = lua_toboolean(L, -2); 166 | rocksdb_options_set_skip_log_error_on_recovery(opt, opt_bool); 167 | } 168 | else if(strcmp(key, "advise_random_on_open") == 0) { 169 | opt_bool = lua_toboolean(L, -2); 170 | rocksdb_options_set_advise_random_on_open(opt, opt_bool); 171 | } 172 | else if(strcmp(key, "use_adaptive_mutex") == 0) { 173 | opt_bool = lua_toboolean(L, -2); 174 | rocksdb_options_set_use_adaptive_mutex(opt, opt_bool); 175 | } 176 | else if(strcmp(key, "verify_checksums_in_compaction") == 0) { 177 | opt_bool = lua_toboolean(L, -2); 178 | rocksdb_options_set_verify_checksums_in_compaction(opt, opt_bool); 179 | } 180 | else if(strcmp(key, "inplace_update_support") == 0) { 181 | opt_bool = lua_toboolean(L, -2); 182 | rocksdb_options_set_inplace_update_support(opt, opt_bool); 183 | } 184 | /* uint64 options */ 185 | else if(strcmp(key, "optimize_for_point_lookup") == 0) { 186 | opt_uint64 = luaL_checknumber(L, -2); 187 | rocksdb_options_optimize_for_point_lookup(opt, opt_uint64); 188 | } 189 | else if(strcmp(key, "optimize_level_style_compaction") == 0) { 190 | opt_uint64 = luaL_checknumber(L, -2); 191 | rocksdb_options_optimize_level_style_compaction(opt, opt_uint64); 192 | } 193 | else if(strcmp(key, "optimize_universal_style_compaction") == 0) { 194 | opt_uint64 = luaL_checknumber(L, -2); 195 | rocksdb_options_optimize_universal_style_compaction(opt, opt_uint64); 196 | } 197 | else if(strcmp(key, "max_total_wal_size") == 0) { 198 | opt_uint64 = luaL_checknumber(L, -2); 199 | rocksdb_options_set_max_total_wal_size(opt, opt_uint64); 200 | } 201 | else if(strcmp(key, "target_file_size_base") == 0) { 202 | opt_uint64 = luaL_checknumber(L, -2); 203 | rocksdb_options_set_target_file_size_base(opt, opt_uint64); 204 | } 205 | else if(strcmp(key, "max_bytes_for_level_base") == 0) { 206 | opt_uint64 = luaL_checknumber(L, -2); 207 | rocksdb_options_set_max_bytes_for_level_base(opt, opt_uint64); 208 | } 209 | else if(strcmp(key, "WAL_ttl_seconds") == 0) { 210 | opt_uint64 = luaL_checknumber(L, -2); 211 | rocksdb_options_set_WAL_ttl_seconds(opt, opt_uint64); 212 | } 213 | else if(strcmp(key, "WAL_size_limit_MB") == 0) { 214 | opt_uint64 = luaL_checknumber(L, -2); 215 | rocksdb_options_set_WAL_size_limit_MB(opt, opt_uint64); 216 | } 217 | else if(strcmp(key, "bytes_per_sync") == 0) { 218 | opt_uint64 = luaL_checknumber(L, -2); 219 | rocksdb_options_set_bytes_per_sync(opt, opt_uint64); 220 | } 221 | else if(strcmp(key, "max_sequential_skip_in_iterations") == 0) { 222 | opt_uint64 = luaL_checknumber(L, -2); 223 | rocksdb_options_set_max_sequential_skip_in_iterations(opt, opt_uint64); 224 | } 225 | else if(strcmp(key, "delete_obsolete_files_period_micros") == 0) { 226 | opt_uint64 = luaL_checknumber(L, -2); 227 | rocksdb_options_set_delete_obsolete_files_period_micros(opt, opt_uint64); 228 | } 229 | lua_pop(L, 2); 230 | } 231 | lua_pop(L, 1); 232 | } 233 | 234 | lrocksdb_options_t *lrocksdb_get_options(lua_State *L, int index) { 235 | lrocksdb_options_t *o = (lrocksdb_options_t *) luaL_checkudata(L, index, "options"); 236 | luaL_argcheck(L, o != NULL && o->options != NULL, index, "options expected"); 237 | return o; 238 | } 239 | 240 | LUALIB_API int lrocksdb_options_create(lua_State *L) { 241 | lrocksdb_options_t *o = (lrocksdb_options_t *) lua_newuserdata(L, sizeof(lrocksdb_options_t)); 242 | o->options = rocksdb_options_create(); 243 | lrocksdb_setmeta(L, "options"); 244 | if(lua_istable(L, 1)) { 245 | lrocksdb_options_set_from_table(L, 1, o->options); 246 | } 247 | return 1; 248 | } 249 | 250 | LUALIB_API int lrocksdb_options_set(lua_State *L) { 251 | lrocksdb_options_t *o = lrocksdb_get_options(L, 1); 252 | if(lua_istable(L, 1)) { 253 | lrocksdb_options_set_from_table(L, 1, o->options); 254 | } 255 | return 0; 256 | } 257 | 258 | LUALIB_API int lrocksdb_options_destroy(lua_State *L) { 259 | lrocksdb_options_t *o = lrocksdb_get_options(L, 1); 260 | if(o->options != NULL) { 261 | rocksdb_options_destroy(o->options); 262 | o->options = NULL; 263 | o = NULL; 264 | } 265 | return 0; 266 | } 267 | 268 | /* write options */ 269 | lrocksdb_writeoptions_t *lrocksdb_get_writeoptions(lua_State *L, int index) { 270 | lrocksdb_writeoptions_t *o = (lrocksdb_writeoptions_t *) luaL_checkudata(L, index, "writeoptions"); 271 | luaL_argcheck(L, o != NULL && o->writeoptions != NULL, index, "writeoptions expected"); 272 | return o; 273 | } 274 | 275 | void lrocksdb_writeoptions_set_from_table(lua_State *L, int index, rocksdb_writeoptions_t *opt) { 276 | lua_pushvalue(L, index); 277 | lua_pushnil(L); 278 | while (lua_next(L, -2)) 279 | { 280 | lua_pushvalue(L, -2); 281 | //TODO: const char *key = lua_tostring(L, -1); 282 | lua_pop(L, 2); 283 | } 284 | lua_pop(L, 1); 285 | } 286 | 287 | LUALIB_API int lrocksdb_writeoptions_create(lua_State *L) { 288 | lrocksdb_writeoptions_t *o = (lrocksdb_writeoptions_t *) lua_newuserdata(L, sizeof(lrocksdb_writeoptions_t)); 289 | o->writeoptions = rocksdb_writeoptions_create(); 290 | lrocksdb_setmeta(L, "writeoptions"); 291 | if(lua_istable(L, 1)) { 292 | lrocksdb_writeoptions_set_from_table(L, 1, o->writeoptions); 293 | } 294 | return 1; 295 | } 296 | 297 | LUALIB_API int lrocksdb_writeoptions_destroy(lua_State *L) { 298 | lrocksdb_writeoptions_t *wo = lrocksdb_get_writeoptions(L, 1); 299 | if(wo != NULL && wo->writeoptions != NULL) { 300 | rocksdb_writeoptions_destroy(wo->writeoptions); 301 | wo->writeoptions = NULL; 302 | wo = NULL; 303 | } 304 | return 0; 305 | } 306 | /* read options */ 307 | lrocksdb_readoptions_t *lrocksdb_get_readoptions(lua_State *L, int index) { 308 | lrocksdb_readoptions_t *o = (lrocksdb_readoptions_t *) luaL_checkudata(L, index, "readoptions"); 309 | luaL_argcheck(L, o != NULL && o->readoptions != NULL, index, "readoptions expected"); 310 | return o; 311 | } 312 | 313 | void lrocksdb_readoptions_set_from_table(lua_State *L, int index, rocksdb_readoptions_t *opt) { 314 | lua_pushvalue(L, index); 315 | lua_pushnil(L); 316 | while (lua_next(L, -2)) 317 | { 318 | lua_pushvalue(L, -2); 319 | //TODO: const char *key = lua_tostring(L, -1); 320 | lua_pop(L, 2); 321 | } 322 | lua_pop(L, 1); 323 | } 324 | 325 | LUALIB_API int lrocksdb_readoptions_create(lua_State *L) { 326 | lrocksdb_readoptions_t *o = (lrocksdb_readoptions_t *) lua_newuserdata(L, sizeof(lrocksdb_readoptions_t)); 327 | o->readoptions = rocksdb_readoptions_create(); 328 | lrocksdb_setmeta(L, "readoptions"); 329 | if(lua_istable(L, 1)) { 330 | lrocksdb_readoptions_set_from_table(L, 1, o->readoptions); 331 | } 332 | return 1; 333 | } 334 | 335 | LUALIB_API int lrocksdb_readoptions_destroy(lua_State *L) { 336 | lrocksdb_readoptions_t *ro = lrocksdb_get_readoptions(L, 1); 337 | if(ro != NULL && ro->readoptions != NULL) { 338 | rocksdb_readoptions_destroy(ro->readoptions); 339 | ro->readoptions = NULL; 340 | ro = NULL; 341 | } 342 | return 1; 343 | } 344 | 345 | 346 | /* restore options */ 347 | lrocksdb_restoreoptions_t *lrocksdb_get_restoreoptions(lua_State *L, int index) { 348 | lrocksdb_restoreoptions_t *o = (lrocksdb_restoreoptions_t *) luaL_checkudata(L, index, "restoreoptions"); 349 | luaL_argcheck(L, o != NULL && o->restoreoptions != NULL, index, "restoreoptions expected"); 350 | return o; 351 | } 352 | 353 | void lrocksdb_restoreoptions_set_from_table(lua_State *L, int index, rocksdb_restore_options_t *opt) { 354 | lua_pushvalue(L, index); 355 | lua_pushnil(L); 356 | while (lua_next(L, -2)) 357 | { 358 | lua_pushvalue(L, -2); 359 | const char *key = lua_tostring(L, -1); 360 | int value = luaL_checkint(L, -2); 361 | if(strcmp(key, "keep_log_files") == 0) { 362 | rocksdb_restore_options_set_keep_log_files(opt, value); 363 | } 364 | lua_pop(L, 2); 365 | } 366 | lua_pop(L, 1); 367 | } 368 | 369 | LUALIB_API int lrocksdb_restoreoptions_create(lua_State *L) { 370 | lrocksdb_restoreoptions_t *o = (lrocksdb_restoreoptions_t*) lua_newuserdata(L, 371 | sizeof(lrocksdb_restoreoptions_t)); 372 | o->restoreoptions = rocksdb_restore_options_create(); 373 | lrocksdb_setmeta(L, "restoreoptions"); 374 | if(lua_istable(L, 1)) { 375 | lrocksdb_restoreoptions_set_from_table(L, 1, o->restoreoptions); 376 | } 377 | return 1; 378 | } 379 | 380 | LUALIB_API int lrocksdb_restoreoptions_destroy(lua_State *L) { 381 | lrocksdb_restoreoptions_t *o = lrocksdb_get_restoreoptions(L, 1); 382 | if(o != NULL && o->restoreoptions != NULL) { 383 | rocksdb_restore_options_destroy(o->restoreoptions); 384 | o->restoreoptions = NULL; 385 | o = NULL; 386 | } 387 | return 1; 388 | } 389 | 390 | 391 | 392 | -------------------------------------------------------------------------------- /src/lrocksdb_options.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_OPTIONS_H 2 | #define LROCKSDB_OPTIONS_H 3 | #include 4 | #include "lauxlib.h" 5 | #include "lrocksdb_types.h" 6 | #include "lrocksdb_helpers.h" 7 | 8 | lrocksdb_options_t *lrocksdb_get_options(lua_State *L, int index); 9 | LUALIB_API int lrocksdb_options_reg(lua_State *L); 10 | LUALIB_API int lrocksdb_options_create(lua_State *L); 11 | LUALIB_API int lrocksdb_options_set(lua_State *L); 12 | LUALIB_API int lrocksdb_options_destroy(lua_State *L); 13 | 14 | /* read options */ 15 | lrocksdb_readoptions_t *lrocksdb_get_readoptions(lua_State *L, int index); 16 | LUALIB_API int lrocksdb_readoptions_reg(lua_State *L); 17 | LUALIB_API int lrocksdb_readoptions_create(lua_State *L); 18 | LUALIB_API int lrocksdb_readoptions_destroy(lua_State *L); 19 | 20 | /* write options */ 21 | lrocksdb_writeoptions_t *lrocksdb_get_writeoptions(lua_State *L, int index); 22 | LUALIB_API int lrocksdb_writeoptions_reg(lua_State *L); 23 | LUALIB_API int lrocksdb_writeoptions_create(lua_State *L); 24 | LUALIB_API int lrocksdb_writeoptions_destroy(lua_State *L); 25 | 26 | /* flush options */ 27 | LUALIB_API int lrocksdb_flushoptions_create(lua_State *L); 28 | LUALIB_API int lrocksdb_flushoptions_destroy(lua_State *L); 29 | LUALIB_API int lrocksdb_flushoptions_set_wait(lua_State *L); 30 | 31 | /* restore options */ 32 | lrocksdb_restoreoptions_t *lrocksdb_get_restoreoptions(lua_State *L, int index); 33 | LUALIB_API int lrocksdb_restoreoptions_reg(lua_State *L); 34 | LUALIB_API int lrocksdb_restoreoptions_create(lua_State *L); 35 | LUALIB_API int lrocksdb_restoreoptions_destroy(lua_State *L); 36 | 37 | static const struct luaL_Reg options_reg[] = { 38 | { "set", lrocksdb_options_set }, 39 | { "destroy", lrocksdb_options_destroy }, 40 | { "__gc", lrocksdb_options_destroy }, 41 | { NULL, NULL } 42 | }; 43 | 44 | static const struct luaL_Reg writeoptions_reg[] = { 45 | { "__gc", lrocksdb_writeoptions_destroy }, 46 | { "destroy", lrocksdb_writeoptions_destroy }, 47 | { NULL, NULL } 48 | }; 49 | 50 | static const struct luaL_Reg readoptions_reg[] = { 51 | { "__gc", lrocksdb_readoptions_destroy }, 52 | { "destroy", lrocksdb_readoptions_destroy }, 53 | { NULL, NULL } 54 | }; 55 | 56 | static const struct luaL_Reg restoreoptions_reg[] = { 57 | { "__gc", lrocksdb_restoreoptions_destroy }, 58 | { "destroy", lrocksdb_restoreoptions_destroy }, 59 | { NULL, NULL } 60 | }; 61 | 62 | #endif 63 | 64 | -------------------------------------------------------------------------------- /src/lrocksdb_types.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_TYPES_H 2 | #define LROCKSDB_TYPES_H 3 | #include "rocksdb/c.h" 4 | 5 | typedef struct { 6 | rocksdb_options_t *options; 7 | } lrocksdb_options_t; 8 | 9 | typedef struct { 10 | rocksdb_writeoptions_t *writeoptions; 11 | } lrocksdb_writeoptions_t; 12 | 13 | typedef struct { 14 | rocksdb_readoptions_t *readoptions; 15 | } lrocksdb_readoptions_t; 16 | 17 | typedef struct { 18 | rocksdb_restore_options_t *restoreoptions; 19 | } lrocksdb_restoreoptions_t; 20 | 21 | typedef struct { 22 | rocksdb_t *db; 23 | lrocksdb_options_t *options; 24 | unsigned char open; 25 | unsigned char read_only; 26 | } lrocksdb_t; 27 | 28 | typedef struct { 29 | rocksdb_backup_engine_t *backup_engine; 30 | } lrocksdb_backup_engine_t; 31 | 32 | typedef struct { 33 | rocksdb_writebatch_t *writebatch; 34 | } lrocksdb_writebatch_t; 35 | 36 | typedef struct { 37 | rocksdb_iterator_t *iter; 38 | } lrocksdb_iterator_t; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/lrocksdb_writebatch.c: -------------------------------------------------------------------------------- 1 | #include "lrocksdb_writebatch.h" 2 | 3 | LUALIB_API int lrocksdb_writebatch_reg(lua_State *L) { 4 | lrocksdb_createmeta(L, "writebatch", writebatch_reg); 5 | return 1; 6 | } 7 | 8 | lrocksdb_writebatch_t *lrocksdb_get_writebatch(lua_State *L, int index) { 9 | lrocksdb_writebatch_t *o = (lrocksdb_writebatch_t*) 10 | luaL_checkudata(L, index, "writebatch"); 11 | luaL_argcheck(L, o != NULL && o->writebatch!= NULL, index, "writebatch expected"); 12 | return o; 13 | } 14 | 15 | LUALIB_API int lrocksdb_writebatch_create(lua_State *L) { 16 | lrocksdb_writebatch_t *rb = 17 | (lrocksdb_writebatch_t *) lua_newuserdata(L, sizeof(lrocksdb_writebatch_t)); 18 | rb->writebatch = rocksdb_writebatch_create(); 19 | lrocksdb_setmeta(L, "writebatch"); 20 | return 1; 21 | } 22 | 23 | LUALIB_API int lrocksdb_writebatch_destroy(lua_State *L) { 24 | lrocksdb_writebatch_t *rb = lrocksdb_get_writebatch(L, 1); 25 | if(rb->writebatch != NULL) { 26 | rocksdb_writebatch_destroy(rb->writebatch); 27 | rb->writebatch = NULL; 28 | } 29 | return 0; 30 | } 31 | 32 | LUALIB_API int lrocksdb_writebatch_clear(lua_State *L) { 33 | lrocksdb_writebatch_t *rb = lrocksdb_get_writebatch(L, 1); 34 | rocksdb_writebatch_clear(rb->writebatch); 35 | return 0; 36 | } 37 | 38 | LUALIB_API int lrocksdb_writebatch_count(lua_State *L) { 39 | lrocksdb_writebatch_t *rb = lrocksdb_get_writebatch(L, 1); 40 | int count = rocksdb_writebatch_count(rb->writebatch); 41 | lua_pushnumber(L, count); 42 | return 1; 43 | } 44 | 45 | LUALIB_API int lrocksdb_writebatch_put(lua_State *L) { 46 | lrocksdb_writebatch_t *rb = lrocksdb_get_writebatch(L, 1); 47 | size_t klen, vlen; 48 | const char *key = luaL_checklstring(L, 2, &klen); 49 | const char *val = luaL_checklstring(L, 3, &vlen); 50 | rocksdb_writebatch_put(rb->writebatch, key, klen, val, vlen); 51 | return 0; 52 | } 53 | 54 | LUALIB_API int lrocksdb_writebatch_merge(lua_State *L) { 55 | lrocksdb_writebatch_t *rb = lrocksdb_get_writebatch(L, 1); 56 | size_t klen, vlen; 57 | const char *key = luaL_checklstring(L, 2, &klen); 58 | const char *val = luaL_checklstring(L, 3, &vlen); 59 | rocksdb_writebatch_merge(rb->writebatch, key, klen, val, vlen); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /src/lrocksdb_writebatch.h: -------------------------------------------------------------------------------- 1 | #ifndef LROCKSDB_WRITEBATCH_H 2 | #define LROCKSDB_WRITEBATCH_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "lauxlib.h" 8 | #include "lrocksdb_types.h" 9 | #include "lrocksdb_helpers.h" 10 | #include "lrocksdb_options.h" 11 | #include "lrocksdb_db.h" 12 | 13 | lrocksdb_writebatch_t *lrocksdb_get_writebatch(lua_State *L, int index); 14 | LUALIB_API int lrocksdb_writebatch_reg(lua_State *L); 15 | LUALIB_API int lrocksdb_writebatch_create(lua_State *L); 16 | LUALIB_API int lrocksdb_writebatch_put(lua_State *L); 17 | LUALIB_API int lrocksdb_writebatch_clear(lua_State *L); 18 | LUALIB_API int lrocksdb_writebatch_count(lua_State *L); 19 | LUALIB_API int lrocksdb_writebatch_merge(lua_State *L); 20 | LUALIB_API int lrocksdb_writebatch_destroy(lua_State *L); 21 | 22 | static const struct luaL_Reg writebatch_reg[] = { 23 | { "put", lrocksdb_writebatch_put }, 24 | { "clear", lrocksdb_writebatch_clear }, 25 | { "count", lrocksdb_writebatch_count }, 26 | { "merge", lrocksdb_writebatch_merge }, 27 | { "destroy", lrocksdb_writebatch_destroy }, 28 | { "__gc", lrocksdb_writebatch_destroy }, 29 | { NULL, NULL } 30 | }; 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /test/test_backup_engine.lua: -------------------------------------------------------------------------------- 1 | print("***** start: backup_engine test *****") 2 | local rocksdb = require("rocksdb") 3 | local format = string.format 4 | local db_path = "/tmp/rocksdb.test" 5 | 6 | local options = rocksdb.options({ 7 | increase_parallelism = 1, 8 | create_if_missing = true 9 | }) 10 | 11 | assert(options.class == "options") 12 | local db = rocksdb.open(options, db_path) 13 | local backup_engine = rocksdb.backup_engine(options, db_path.."-backup") 14 | assert(backup_engine.class == "backup_engine") 15 | assert(backup_engine:create_new_backup(db)) 16 | assert(backup_engine:purge_old_backups(1)) 17 | local count = backup_engine:get_backup_info_count() 18 | print("count: "..count) 19 | local info = backup_engine:get_backup_info(count) 20 | for k,v in pairs(info) do 21 | print(k..": "..tostring(v)) 22 | end 23 | 24 | local restoreoptions = rocksdb.restoreoptions({ 25 | keep_log_files = 1 26 | }) 27 | backup_engine:restore_db_from_latest_backup(db_path, db_path, restoreoptions); 28 | 29 | backup_engine:close() 30 | print("***** done: backup_engine test *****") 31 | 32 | -------------------------------------------------------------------------------- /test/test_iterator.lua: -------------------------------------------------------------------------------- 1 | local rocksdb = require("rocksdb") 2 | local format = string.format 3 | 4 | local options = rocksdb.options({ 5 | increase_parallelism = 1, 6 | create_if_missing = true 7 | }) 8 | 9 | assert(options.class == "options") 10 | 11 | local db = rocksdb.open(options, "/tmp/rocksdb.test") 12 | 13 | local readoptions = rocksdb.readoptions() 14 | local writeoptions = rocksdb.writeoptions() 15 | 16 | local key, value, expected_value 17 | 18 | for i = 0, 1000 do 19 | key = format("lrocks_db_key:%d", i) 20 | value = format("lrocks_db_value:%d", i) 21 | db:put(writeoptions, key, value) 22 | end 23 | 24 | local iterator = db:iterator(readoptions) 25 | assert(iterator:valid() == false) 26 | print("error", iterator:get_error()) 27 | iterator:seek_to_first() 28 | assert(iterator:valid() == true) 29 | 30 | while iterator:valid() do 31 | key = iterator:key() 32 | value = iterator:value() 33 | iterator:next() 34 | end 35 | 36 | iterator:destroy() 37 | db:close() 38 | readoptions:destroy() 39 | writeoptions:destroy() 40 | options:destroy() 41 | 42 | -------------------------------------------------------------------------------- /test/test_rocksdb.lua: -------------------------------------------------------------------------------- 1 | local rocksdb = require("rocksdb") 2 | local format = string.format 3 | 4 | for k,v in pairs(rocksdb) do 5 | print(k..": "..tostring(v)) 6 | end 7 | 8 | local options = rocksdb.options({ 9 | increase_parallelism = 1, 10 | create_if_missing = true 11 | }) 12 | 13 | assert(options.class == "options") 14 | 15 | local db = rocksdb.open(options, "/tmp/rocksdb.test") 16 | 17 | local writeoptions = rocksdb.writeoptions() 18 | assert(writeoptions.class == "writeoptions") 19 | 20 | local readoptions = rocksdb.readoptions() 21 | assert(readoptions.class == "readoptions") 22 | 23 | local key, value, expected_value 24 | 25 | print("start: put") 26 | for i = 0, 1000 do 27 | key = format("lrocks_db_key:%d", i) 28 | value = format("lrocks_db_value:%d", i) 29 | db:put(writeoptions, key, value) 30 | end 31 | print("done: put") 32 | 33 | print("start: get") 34 | for i = 0, 1000 do 35 | key = format("lrocks_db_key:%d", i) 36 | expected_value = format("lrocks_db_value:%d", i) 37 | value = db:get(readoptions, key) 38 | assert(value == expected_value) 39 | end 40 | print("done: get") 41 | print("delete: start", key) 42 | key = "lrocks_db_key:delete_me" 43 | db:put(writeoptions, key, "delete") 44 | db:delete(writeoptions, key) 45 | value = db:get(readoptions, key) 46 | print("delete: end", key, type(value), value, "-") 47 | assert(value == nil) 48 | db:close() 49 | print("closed") 50 | local ok, res = pcall(db.get, db, readoptions, "testkey") 51 | assert(ok == false) 52 | collectgarbage() 53 | 54 | local read_only_db = rocksdb.open_for_read_only(options, "/tmp/rocksdb.test", false) 55 | assert(read_only_db) 56 | print("start.read_only_db: get") 57 | for i = 0, 1000 do 58 | key = format("lrocks_db_key:%d", i) 59 | expected_value = format("lrocks_db_value:%d", i) 60 | value = read_only_db:get(readoptions, key) 61 | assert(value == expected_value) 62 | end 63 | print("done.read_only_db: get") 64 | 65 | ok, res = pcall(read_only_db.put, read_only_db, writeoptions, "test", "test") 66 | assert(ok == false) 67 | 68 | read_only_db:close() 69 | 70 | readoptions:destroy() 71 | writeoptions:destroy() 72 | options:destroy() 73 | 74 | -------------------------------------------------------------------------------- /test/test_writebatch.lua: -------------------------------------------------------------------------------- 1 | print("***** start: writebatch test *****") 2 | local rocksdb = require("rocksdb") 3 | local format = string.format 4 | local db_path = "/tmp/rocksdb.test" 5 | 6 | local options = rocksdb.options({ 7 | increase_parallelism = 1, 8 | create_if_missing = true 9 | }) 10 | 11 | assert(options.class == "options") 12 | local db = rocksdb.open(options, db_path) 13 | local writeoptions = rocksdb.writeoptions() 14 | local readoptions = rocksdb.readoptions() 15 | local writebatch = rocksdb.writebatch() 16 | local key, val 17 | for i = 1,10 do 18 | key = format("writebatch:key:%d", i) 19 | val = format("writebatch:val:%d", i) 20 | writebatch:put(key, val) 21 | assert(writebatch:count() == i) 22 | end 23 | db:write(writeoptions, writebatch) 24 | writebatch:clear() 25 | assert(writebatch:count() == 0) 26 | writebatch:destroy() 27 | 28 | local dbval = db:get(readoptions, key) 29 | assert(dbval == val) 30 | db:close() 31 | print("***** done: writebatch test *****") 32 | 33 | --------------------------------------------------------------------------------