1 /** 2 Copyright: Copyright (c) 2016-2018 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module voxelman.world.db.lmdbworlddb; 7 8 import voxelman.log; 9 import lmdb; 10 11 import std..string : fromStringz, toStringz; 12 struct LmdbWorldDb 13 { 14 private MDB_env* env; 15 private MDB_txn* txn; 16 private uint txnFlags; 17 private MDB_dbi dbi; 18 19 // filename needs to have /0 after end 20 void open(string filename, size_t mapSize = 1 * 1024 * 1024 * 1024) { 21 assert(env is null); 22 assert(txn is null); 23 checked!mdb_env_create(&env); 24 25 checked!mdb_env_set_mapsize(env, mapSize); 26 27 checked!mdb_env_open(env, filename.toStringz, 28 MDB_NOSUBDIR| 29 MDB_NOMETASYNC| 30 MDB_NOSYNC| 31 MDB_WRITEMAP| 32 MDB_NOLOCK| 33 MDB_NOMEMINIT, 34 //rwx_rwx_rwx 35 0b110_110_110); 36 } 37 38 static string libVersion() @nogc { 39 return cast(string)mdb_version(null, null, null).fromStringz; 40 } 41 42 void close() @nogc { 43 mdb_env_close(env); 44 env = null; 45 txn = null; 46 } 47 48 void beginTxn(uint flags = 0) @nogc { 49 checked!mdb_txn_begin(env, null, flags, &txn); 50 checked!mdb_dbi_open(txn, null/*main DB*/, 0/*flags*/, &dbi); 51 checked!mdb_set_compare(txn, dbi, &mdb_cmp_long); 52 } 53 54 void abortTxn() @nogc { 55 mdb_txn_abort(txn); 56 } 57 58 void commitTxn() @nogc { 59 checked!mdb_txn_commit(txn); 60 } 61 62 void put(ubyte[] key, ubyte[] value) @nogc { 63 checked!mdb_put(txn, dbi, &key, &value, 0); 64 } 65 66 ubyte[] get(ubyte[] key) @nogc { 67 ubyte[] value; 68 checked!mdb_get(txn, dbi, &key, &value); 69 return value; 70 } 71 72 void del(ubyte[] key) @nogc { 73 checked!mdb_del(txn, dbi, &key, null); 74 } 75 76 void dropDB() @nogc { 77 checked!mdb_drop(txn, dbi, 0); 78 checked!mdb_drop(txn, dbi, 1); 79 } 80 }