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.worlddb; 7 8 import std.experimental.allocator.mallocator; 9 import voxelman.world.db.lmdbworlddb; 10 import lmdb; 11 12 private enum Table : ulong 13 { 14 world, 15 region, 16 chunk, 17 } 18 19 final class WorldDb 20 { 21 LmdbWorldDb db; 22 private ubyte[] buffer; 23 24 //----------------------------------------------- 25 void open(string filename) { 26 version(Windows) mdb_load_libs(); 27 db.open(filename); 28 } 29 void close() { 30 db.close(); 31 if (buffer !is null) Mallocator.instance.deallocate(buffer); 32 } 33 34 ubyte[] tempBuffer() @property { 35 if (buffer is null) buffer = cast(ubyte[])Mallocator.instance.allocate(4096*64); 36 37 return buffer; 38 } 39 40 //----------------------------------------------- 41 void put(ubyte[16] key, ubyte[] value) { 42 db.put(key, value); 43 } 44 ubyte[] get(ubyte[16] key) { 45 return db.get(key); 46 } 47 void del(ubyte[16] key) { 48 return db.del(key); 49 } 50 51 //----------------------------------------------- 52 void beginTxn() { 53 db.beginTxn(); 54 } 55 void abortTxn() { 56 db.abortTxn(); 57 } 58 void commitTxn() { 59 db.commitTxn(); 60 } 61 } 62 63 ubyte[16] formChunkKey(ulong chunkPos) { 64 ubyte[16] res; 65 (*cast(ulong[2]*)res.ptr)[0] = chunkPos; 66 (*cast(ulong[2]*)res.ptr)[1] = Table.chunk; 67 return res; 68 } 69 70 ubyte[16] formWorldKey(uint key) { 71 ubyte[16] res; 72 (*cast(ulong[2]*)res.ptr)[0] = key; 73 (*cast(ulong[2]*)res.ptr)[1] = Table.world; 74 return res; 75 }