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