1 #ifndef BLUB_PROCEDURAL_VOXEL_SIMPLE_CONTAINER_DATABASE_HPP
2 #define BLUB_PROCEDURAL_VOXEL_SIMPLE_CONTAINER_DATABASE_HPP
7 #include "blub/core/base64.hpp"
8 #include "blub/core/byteArray.hpp"
9 #include "blub/core/vector.hpp"
10 #include "blub/database/connection.hpp"
11 #include "blub/database/functions.hpp"
12 #include "blub/procedural/log/global.hpp"
13 #include "blub/procedural/voxel/simple/container/inMemory.hpp"
14 #include "blub/procedural/voxel/simple/container/utils/tile.hpp"
15 #include "blub/serialization/format/binary/input.hpp"
16 #include "blub/serialization/format/binary/output.hpp"
19 #include <boost/interprocess/streams/vectorstream.hpp>
34 template <
class voxelType>
35 class database :
public inMemory<voxelType>
38 typedef inMemory<voxelType> t_base;
39 typedef std::function<byteArray (const byteArray&)> t_funcCompress;
43 , m_databaseConnection(dbConn)
45 , m_writeToDatabase(true)
48 blub::BOUT(
"database::database()");
55 blub::BOUT(
"database::~database()");
68 m_databaseConnection <<
"select count(*) from voxel_tiles"
69 , blub::database::into(numData);
72 BLUB_PROCEDURAL_LOG_WARNING() <<
"numData == 0";
75 std::vector<int32> xList(numData);
76 std::vector<int32> yList(numData);
77 std::vector<int32> zList(numData);
78 m_databaseConnection <<
"select x, y, z from voxel_tiles"
79 , soci::into(xList), soci::into(yList), soci::into(zList);
80 BASSERT(xList.size() == yList.size());
81 BASSERT(xList.size() == zList.size());
83 for (uint32 ind = 0; ind < xList.size(); ++ind)
85 const vector3int32 id(xList[ind], yList[ind], zList[ind]);
86 typename t_base::t_utilsTile tile(getTileHolderFromDatabaseMaster(
id));
95 soci::indicator indicator = soci::i_null;
96 std::string selectData;
97 m_databaseConnection <<
"select data from voxel_tiles where x = :x and y = :y and z = :z"
98 , soci::into(selectData, indicator), blub::database::use(
id.x), blub::database::use(
id.y), blub::database::use(
id.z);
99 if (indicator == soci::i_ok)
101 byteArray decompressed(base64::decode(selectData));
102 if (m_funcDecompress)
104 decompressed = m_funcDecompress(decompressed);
112 typename t_base::t_utilsTile getTileHolderFromDatabaseMaster(
const blub::vector3int32&
id)
const
114 typename t_base::t_utilsTile result;
116 const byteArray &strToCast(getTileHolderDatabaseMaster(
id));
118 if (!strToCast.empty())
120 boost::interprocess::basic_ivectorstream<byteArray> dataContainer(strToCast);
124 format >> result.state;
125 if (result.state == utils::tileState::partitial)
129 format >> (*result.data.data());
138 void setWriteToDatabase(
const bool& en)
140 m_writeToDatabase = en;
142 void setCompressionCallback(
const t_funcCompress& compress,
const t_funcCompress& decompress)
144 m_funcCompress = compress;
145 m_funcDecompress = decompress;
150 virtual void setTileDatabaseMaster(
const vector3int32 &
id,
const blub::byteArray &toSet,
const bool &insert,
const bool &update,
const bool &
remove)
170 const byteArray *work(&toSet);
171 byteArray toSetCompressed;
174 toSetCompressed = m_funcCompress(toSet);
175 work = &toSetCompressed;
182 m_databaseConnection <<
"insert into voxel_tiles(x, y, z, data) values(:x, :y, :z, :data)"
183 , blub::database::use(
id.x), blub::database::use(
id.y), blub::database::use(
id.z)
184 , blub::database::use(toSetBase64);
189 m_databaseConnection <<
"delete data from voxel_tiles where x = :x and y = :y and z = :z"
190 , blub::database::use(
id.x), blub::database::use(
id.y), blub::database::use(
id.z);
195 m_databaseConnection <<
"update voxel_tiles set data = :data where x = :x and y = :y and z = :z"
196 , blub::database::use(toSetBase64), blub::database::use(
id.x), blub::database::use(
id.y), blub::database::use(
id.z);
202 const typename t_base::t_utilsTile &oldOne,
203 const typename t_base::t_utilsTile &toSet)
override
207 if (m_loading || !m_writeToDatabase)
211 std::stringstream dataContainer;
214 format << toSet.state;
215 if (toSet.state == utils::tileState::partitial)
217 BASSERT(!toSet.data.isNull());
218 format << *toSet.data.data();
223 setTileDatabaseMaster(
id, strCasted,
224 oldOne.state == utils::tileState::empty,
225 (toSet.state != utils::tileState::empty) && (oldOne.state != utils::tileState::empty),
226 toSet.state == utils::tileState::empty);
231 blub::database::connection &m_databaseConnection;
233 bool m_writeToDatabase;
234 t_funcCompress m_funcCompress;
235 t_funcCompress m_funcDecompress;
250 #endif // BLUB_PROCEDURAL_VOXEL_SIMPLE_CONTAINER_DATABASE_HPP
void setTileToContainerMaster(const typename t_base::t_tileId id, const typename t_base::t_utilsTile &oldOne, const typename t_base::t_utilsTile &toSet) override
setTileToContainerMaster replaces a tile. Call method by one thread at a time. Write-lock class befor...
Definition: database.hpp:201
static t_tilePtr createTileFull(const bool &full)
createTileFull creates a tile in which all voxel are maximum or minimum.
Definition: base.hpp:416
void lockForEditMaster() override
Definition: base.hpp:193
void unlockForEditMaster() override
Definition: base.hpp:176
Definition: portable_binary_oarchive.hpp:63
Definition: string.hpp:22
Definition: dispatcher.hpp:29
Definition: byteArray.hpp:17
void setTileMaster(const blub::vector3int32 &id, const typename t_base::t_utilsTile &toSet) override
setTileMaster sets a tile to an id. Call by one thread at a time.
Definition: inMemory.hpp:125
Definition: portable_binary_iarchive.hpp:68
Definition: deadlineTimer.hpp:10
blub::async::strand m_master
m_master The master synchronises jobs for the worker-thread and writes to class member. The master calls all methods which end with *Master.
Definition: base.hpp:143
virtual void setTileToContainerMaster(const typename t_base::t_tileId id, const typename t_base::t_utilsTile &oldOne, const typename t_base::t_utilsTile &toSet)
setTileToContainerMaster replaces a tile. Call method by one thread at a time. Write-lock class befor...
Definition: inMemory.hpp:112