| threaddb 2.0 A file mapped memory container extension

threaddbCPP.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2019 by The ThreadDB Project
3 All Rights Reserved.
4
5 ThreadDB undergoes the BSD License 2.0. You should have received a copy along with this program; if not, write to the ThreadDB Project.
6 To obtain a full unlimited version contact thethreaddbproject(at)gmail.com.
7
8 threaddbCpp.h - C++ Interface to the ThreadDB memory container database
9 */
10
11 #pragma once
12
19 #include "threaddbC.h"
20 #include <stdexcept>
21 #include <string>
22 #include <limits>
23
24 #define threadDB_throw(a)\
25 if((a)) \
26 throw std::runtime_error(ThreadDB_GetErrorMessage(m_pThreadDB));
27
28 namespace tdb
29 {
30
35 class LinkInfo : public threadDB_LinkInfo
36 {
37 public:
38 LinkInfo() : threadDB_LinkInfo({ 0, std::numeric_limits<uint32_t>::max(), 0 })
39 {
40 }
41
42 ~LinkInfo()
43 {
44 }
45 };
46
47
52 class ItemInfo : public threadDB_ItemInfo
53 {
54 public:
55 ItemInfo() : threadDB_ItemInfo({ 0, std::numeric_limits<uint32_t>::max(), 0, std::numeric_limits<uint32_t>::max() })
56 {
57 }
58
59 ~ItemInfo()
60 {
61 }
62
63 bool Unregistered() const { return m_PackageSize != std::numeric_limits<uint32_t>::max(); }
64 };
65
70 class ReadInfo
71 {
72 public:
73 ReadInfo(threadDB_ReadInfo* pReadInfo_p, void* pThreadDB_p) :
74 m_pReadInfo(pReadInfo_p),
75 m_pThreadDB(pThreadDB_p)
76 {
77 }
78 ~ReadInfo()
79 {
80 // Note: the return code is ignored to prevent that an exception is generated within the destructor
81 ThreadDB_Close(&m_pReadInfo, m_pThreadDB);
82 }
83 threadDB_ReadInfo* Get() const
84 {
85 return m_pReadInfo;
86 }
87
88 private:
89 threadDB_ReadInfo* m_pReadInfo;
90 void* m_pThreadDB;
91 };
92
97 class database
98 {
99 public:
100
105 database(size_t PackageSize_p = 4096, size_t PackageCacheLimit_p = std::numeric_limits<size_t>::max(), const std::string& rPackagesPath_p = "") :
106 m_pThreadDB(0)
107 {
108 if (ThreadDB_Create(&m_pThreadDB, PackageSize_p, PackageCacheLimit_p, rPackagesPath_p.empty()?0:rPackagesPath_p.c_str()))
109 {
110 throw std::runtime_error("Error creating the database.");
111 }
112 }
113
118 database(const std::string& rIndexFileUTF8_p, size_t PackageCacheLimit_p = std::numeric_limits<size_t>::max()) :
119 m_pThreadDB(0)
120 {
121 if (ThreadDB_Import(&m_pThreadDB, rIndexFileUTF8_p.c_str(), PackageCacheLimit_p))
122 {
123 throw std::runtime_error("Error creating the database.");
124 }
125 }
126
131 ~database()
132 {
133 ThreadDB_Destroy(m_pThreadDB);
134 }
135
140 const char* GetVersionInfo() const
141 {
142 return ThreadDB_GetVersionInfo(m_pThreadDB);
143 }
144
149 void Save(const char* pIndexFileUTF8_p)
150 {
151 threadDB_throw(ThreadDB_Save(pIndexFileUTF8_p, m_pThreadDB));
152 }
153
158 const char* NewThread(const char* pDataFolder_p, size_t MaxFileSize_p = std::numeric_limits<size_t>::max())
159 {
160 const char* pFileName_p = 0;
161 threadDB_throw(ThreadDB_NewThread(&pFileName_p,pDataFolder_p, MaxFileSize_p, m_pThreadDB));
162 return pFileName_p;
163 }
164
169 uint64_t NewPackage()
170 {
171 uint64_t packageID = 0;
172 threadDB_throw(ThreadDB_NewPackage(&packageID,m_pThreadDB));
173 return packageID;
174 }
175
180 size_t GetThreadCount() const
181 {
182 return ThreadDB_GetThreadCount(m_pThreadDB);
183 }
184
189 size_t GetPackageCount() const
190 {
191 return ThreadDB_GetPackageCount(m_pThreadDB);
192 }
193
198 size_t GetFileCount() const
199 {
200 return ThreadDB_GetFileCount(m_pThreadDB);
201 }
202
207 size_t GetPackageSize() const
208 {
209 return ThreadDB_GetPackageSize(m_pThreadDB);
210 }
211
216 const char* GetDatabaseFilename(size_t FileIndex_p) const
217 {
218 return ThreadDB_GetDatabaseFileName(FileIndex_p, m_pThreadDB);
219 }
220
225 void RelocateFileTo(size_t FileID_p, const char* pFilePathUTF8_p, threadDB_RelocationType RelocationType_p) const
226 {
227 threadDB_throw(ThreadDB_RelocateFileTo(FileID_p, pFilePathUTF8_p, RelocationType_p, m_pThreadDB));
228 }
229
234 void Store(uint64_t Package_p, size_t Size_p, const char pData_p[], threadDB_ItemInfo* pItemHandle_p = 0)
235 {
236 threadDB_throw(ThreadDB_Store(Package_p, Size_p, pData_p, pItemHandle_p, m_pThreadDB));
237 }
238
243 void Synchronize(uint64_t Package_p)
244 {
245 threadDB_throw(ThreadDB_SynchronizePackage(Package_p, m_pThreadDB));
246 }
247
252 void Synchronize()
253 {
254 threadDB_throw(ThreadDB_Synchronize(m_pThreadDB));
255 }
256
263 tdb::ReadInfo Open(uint64_t Package_p)
264 {
265 threadDB_ReadInfo* pReadInfo = 0;
266 threadDB_throw(ThreadDB_Open(&pReadInfo, Package_p, m_pThreadDB));
267 return tdb::ReadInfo(pReadInfo, m_pThreadDB);
268 }
269
276 tdb::ReadInfo Open(const tdb::ItemInfo& rItemHandle_p)
277 {
278 threadDB_ReadInfo* pReadInfo = 0;
279 threadDB_throw(ThreadDB_OpenItem(&pReadInfo, &rItemHandle_p, m_pThreadDB));
280 return tdb::ReadInfo(pReadInfo, m_pThreadDB);
281 }
282
289 size_t Recover(size_t Size_p, char pData_p[], tdb::ReadInfo& rReadInfo_p)
290 {
291 size_t readBytes = 0;
292 threadDB_throw(ThreadDB_RecoverContinous(&readBytes,Size_p, pData_p, rReadInfo_p.Get(), m_pThreadDB));
293 return readBytes;
294 }
295
302 bool End(const tdb::ReadInfo& rReadInfo_p) const
303 {
304 return ThreadDB_PackageEnd(rReadInfo_p.Get(), m_pThreadDB);
305 }
306
313 void Recover(uint32_t Size_p, char pData_p[], const tdb::ItemInfo& rItemHandle_p, uint64_t Package_p)
314 {
315 threadDB_throw(ThreadDB_RecoverRandom(Size_p, pData_p, &rItemHandle_p, Package_p, m_pThreadDB));
316 }
317
322 void Replace(size_t Size_p, const char pData_p[], tdb::ItemInfo& rItemHandle_p)
323 {
324 threadDB_throw(ThreadDB_Replace(Size_p, pData_p, &rItemHandle_p, m_pThreadDB));
325 }
326
327 private:
328 database(const database&) = delete;
329 void operator=(const database&) = delete;
330
331 void* m_pThreadDB;
332 };
333 }
334
tdb::database::GetThreadCount
size_t GetThreadCount() const
C++ wrapper to retrieve the number of running threads see ThreadDB_GetThreadCount.
Definition: threaddbCPP.h:180
ThreadDB_OpenItem
DLLEXPORT_ threadDB_ReturnCode ThreadDB_OpenItem(threadDB_ReadInfo **pReadInfo_p, const threadDB_ItemInfo *pItemHandle_p, void *pThreadDB_p)
This operation opens a package for stream reading at the given item cursor.
tdb::database::GetFileCount
size_t GetFileCount() const
C++ wrapper to retrieve the number of registered temporary database files see ThreadDB_GetFileCount.
Definition: threaddbCPP.h:198
tdb::ReadInfo
C++ wrapper class to the threadDB_ReadInfo control structure.
Definition: threaddbCPP.h:70
ThreadDB_GetThreadCount
DLLEXPORT_ size_t ThreadDB_GetThreadCount(void *pThreadDB_p)
Returns the number of currently running threads.
tdb::ItemInfo::Unregistered
bool Unregistered() const
Definition: threaddbCPP.h:63
ThreadDB_Store
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Store(uint64_t Package_p, size_t Size_p, const char pData_p[], threadDB_ItemInfo *pItemHandle_p, void *pThreadDB_p)
Stores database package items in the specified package.
tdb::ItemInfo
C++ wrapper class to the threadDB_ItemInfo control structure.
Definition: threaddbCPP.h:52
ThreadDB_GetVersionInfo
const DLLEXPORT_ char * ThreadDB_GetVersionInfo(void *pThreadDB_p)
Interface for retrieving the package version info.
tdb::ReadInfo::~ReadInfo
~ReadInfo()
Definition: threaddbCPP.h:78
tdb::LinkInfo::LinkInfo
LinkInfo()
Definition: threaddbCPP.h:38
ThreadDB_Synchronize
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Synchronize(void *pThreadDB_p)
Synchronizes the internal package buffers with the temporary database files.
tdb::database::database
database(const std::string &rIndexFileUTF8_p, size_t PackageCacheLimit_p=std::numeric_limits< size_t >::max())
C++ wrapper to construct a threadDB database object from file see ThreadDB_Import.
Definition: threaddbCPP.h:118
tdb::database::GetPackageCount
size_t GetPackageCount() const
C++ wrapper to retrieve the number of registered packages see ThreadDB_GetPackageCount.
Definition: threaddbCPP.h:189
tdb::ReadInfo::ReadInfo
ReadInfo(threadDB_ReadInfo *pReadInfo_p, void *pThreadDB_p)
Definition: threaddbCPP.h:73
tdb::database::~database
~database()
C++ wrapper to destruct a threadDB database object see ThreadDB_Destroy.
Definition: threaddbCPP.h:131
ThreadDB_GetPackageCount
DLLEXPORT_ size_t ThreadDB_GetPackageCount(void *pThreadDB_p)
Returns the number of currently registered packages.
tdb::database::RelocateFileTo
void RelocateFileTo(size_t FileID_p, const char *pFilePathUTF8_p, threadDB_RelocationType RelocationType_p) const
C++ wrapper to move a file to a different location or disk see ThreadDB_RelocateFileTo.
Definition: threaddbCPP.h:225
ThreadDB_RecoverContinous
DLLEXPORT_ threadDB_ReturnCode ThreadDB_RecoverContinous(size_t *pReadBytes_p, size_t Size_p, char pData_p[], threadDB_ReadInfo *pReadInfo_p, void *pThreadDB_p)
Stream reading operation through the data items stored in a package.
tdb::database::Store
void Store(uint64_t Package_p, size_t Size_p, const char pData_p[], threadDB_ItemInfo *pItemHandle_p=0)
C++ wrapper to store a data item in the selected package see ThreadDB_Store.
Definition: threaddbCPP.h:234
ThreadDB_GetFileCount
DLLEXPORT_ size_t ThreadDB_GetFileCount(void *pThreadDB_p)
Returns the number of currently registered temporary database files.
threadDB_LinkInfo
Data item entry index.
Definition: threaddbTypes.h:46
ThreadDB_Save
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Save(const char *pIndexFileUTF8_p, void *pThreadDB_p)
Interface to save the stored data.
tdb::ItemInfo::~ItemInfo
~ItemInfo()
Definition: threaddbCPP.h:59
tdb::database::NewThread
const char * NewThread(const char *pDataFolder_p, size_t MaxFileSize_p=std::numeric_limits< size_t >::max())
C++ wrapper to create and register a threadDB worker thread see ThreadDB_NewThread.
Definition: threaddbCPP.h:158
tdb
Definition: threaddbCPP.h:28
threaddbC.h
List of interface methods to the database.
tdb::database::database
database(size_t PackageSize_p=4096, size_t PackageCacheLimit_p=std::numeric_limits< size_t >::max(), const std::string &rPackagesPath_p="")
C++ wrapper to construct a threadDB database object see ThreadDB_Create.
Definition: threaddbCPP.h:105
tdb::database::End
bool End(const tdb::ReadInfo &rReadInfo_p) const
C++ wrapper for stream end indicator see ThreadDB_PackageEnd.
Definition: threaddbCPP.h:302
tdb::database::GetVersionInfo
const char * GetVersionInfo() const
C++ wrapper to retrieve the version information of the current installation see ThreadDB_GetVersionIn...
Definition: threaddbCPP.h:140
ThreadDB_Replace
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Replace(size_t Size_p, const char pData_p[], const threadDB_ItemInfo *pItemHandle_p, void *pThreadDB_p)
Replace operation to modify the contents of a stored data item.
tdb::database::Synchronize
void Synchronize(uint64_t Package_p)
C++ wrapper to synchronize the package buffers prior reading see ThreadDB_Synchronize.
Definition: threaddbCPP.h:243
tdb::database::Recover
void Recover(uint32_t Size_p, char pData_p[], const tdb::ItemInfo &rItemHandle_p, uint64_t Package_p)
C++ wrapper for random reading of data items see ThreadDB_RecoverRandom.
Definition: threaddbCPP.h:313
ThreadDB_GetPackageSize
DLLEXPORT_ size_t ThreadDB_GetPackageSize(void *pThreadDB_p)
Returns the package buffer size.
ThreadDB_GetDatabaseFileName
const DLLEXPORT_ char * ThreadDB_GetDatabaseFileName(size_t FileIndex_p, void *pThreadDB_p)
Returns the filename of a currently registered temporary database files.
ThreadDB_RecoverRandom
DLLEXPORT_ threadDB_ReturnCode ThreadDB_RecoverRandom(uint32_t Size_p, char pData_p[], const threadDB_ItemInfo *pItemHandle_p, uint64_t Package_p, void *pThreadDB_p)
Random access operation to the data items stored in a package.
tdb::database::GetPackageSize
size_t GetPackageSize() const
C++ wrapper to retrieve the size of the internally allocated package buffers see ThreadDB_GetPackageS...
Definition: threaddbCPP.h:207
ThreadDB_RelocateFileTo
DLLEXPORT_ threadDB_ReturnCode ThreadDB_RelocateFileTo(size_t FileID_p, const char *pFilePathUTF8_p, threadDB_RelocationType RelocationType_p, void *pThreadDB_p)
Renames and copies/moves a temporary database file to a different location or disk.
tdb::ReadInfo::Get
threadDB_ReadInfo * Get() const
Definition: threaddbCPP.h:83
tdb::database::Save
void Save(const char *pIndexFileUTF8_p)
C++ wrapper to save the contents of a threadDB database see ThreadDB_Save.
Definition: threaddbCPP.h:149
tdb::database::Synchronize
void Synchronize()
C++ wrapper to synchronize the package buffers prior reading see ThreadDB_Synchronize.
Definition: threaddbCPP.h:252
tdb::database
C++ wrapper class of the threadDB file mapped memory container extension.
Definition: threaddbCPP.h:97
threadDB_ReadInfo
Stream reading control information.
Definition: threaddbTypes.h:81
ThreadDB_Close
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Close(threadDB_ReadInfo **pReadInfo_p, void *pThreadDB_p)
This operation closes a package after stream reading has finished.
ThreadDB_Destroy
DLLEXPORT_ void ThreadDB_Destroy(void *pThreadDB_p)
Interface to destroy an existing database object.
tdb::database::Recover
size_t Recover(size_t Size_p, char pData_p[], tdb::ReadInfo &rReadInfo_p)
C++ wrapper for stream reading of data items see ThreadDB_RecoverContinous.
Definition: threaddbCPP.h:289
threadDB_ItemInfo
Random access managment information.
Definition: threaddbTypes.h:63
tdb::database::Open
tdb::ReadInfo Open(const tdb::ItemInfo &rItemHandle_p)
C++ wrapper to open a package for stream reading see ThreadDB_OpenItem.
Definition: threaddbCPP.h:276
ThreadDB_Import
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Import(void **pThreadDB_p, const char *pIndexFileUTF8_p, size_t PackageCacheLimit_p)
Interface for creating a new database object based on a database index file.
ThreadDB_NewThread
DLLEXPORT_ threadDB_ReturnCode ThreadDB_NewThread(const char **pFileName_p, const char *pFolder_p, size_t MaxFileSize_p, void *pThreadDB_p)
Interface to create and register a new worker thread.
tdb::database::NewPackage
uint64_t NewPackage()
C++ wrapper to create and register a threadDB data package see ThreadDB_NewPackage.
Definition: threaddbCPP.h:169
ThreadDB_PackageEnd
DLLEXPORT_ bool ThreadDB_PackageEnd(threadDB_ReadInfo *pReadInfo_p, void *pThreadDB_p)
Stream reading operation through the data items stored in a package.
tdb::LinkInfo
C++ wrapper class to the threadDB_LinkInfo control structure.
Definition: threaddbCPP.h:35
ThreadDB_Create
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Create(void **pThreadDB_p, size_t PackageSize_p, size_t PackageCacheLimit_p, const char *pPackagesFolder_p)
Interface for creating a new database object.
ThreadDB_SynchronizePackage
DLLEXPORT_ threadDB_ReturnCode ThreadDB_SynchronizePackage(uint64_t Package_p, void *pThreadDB_p)
Synchronizes the internal package buffers with the temporary database files.
ThreadDB_Open
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Open(threadDB_ReadInfo **pReadInfo_p, uint64_t Package_p, void *pThreadDB_p)
This operation opens a package for stream reading.
threadDB_throw
#define threadDB_throw(a)
Definition: threaddbCPP.h:24
tdb::LinkInfo::~LinkInfo
~LinkInfo()
Definition: threaddbCPP.h:42
tdb::database::Open
tdb::ReadInfo Open(uint64_t Package_p)
C++ wrapper to open a package for stream reading see ThreadDB_Open.
Definition: threaddbCPP.h:263
tdb::database::Replace
void Replace(size_t Size_p, const char pData_p[], tdb::ItemInfo &rItemHandle_p)
C++ wrapper for replacing the contents of a data item see ThreadDB_Replace.
Definition: threaddbCPP.h:322
tdb::ItemInfo::ItemInfo
ItemInfo()
Definition: threaddbCPP.h:55
threadDB_ItemInfo::m_PackageSize
volatile uint32_t m_PackageSize
Size of the data record in bytes.
Definition: threaddbTypes.h:67
ThreadDB_NewPackage
DLLEXPORT_ threadDB_ReturnCode ThreadDB_NewPackage(uint64_t *pPackageID_p, void *pThreadDB_p)
Registers a new data item package.
threadDB_RelocationType
threadDB_RelocationType
Type of action to be performed when relocating a temporary database file.
Definition: threaddbTypes.h:98
tdb::database::GetDatabaseFilename
const char * GetDatabaseFilename(size_t FileIndex_p) const
C++ wrapper to derive the name of the temporary database file with index FileIndex_p ThreadDB_GetData...
Definition: threaddbCPP.h:216