threaddb  1.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 
13 #include "threaddbC.h"
14 #include <stdexcept>
15 #include <string>
16 #include <limits>
17 
18 #define threadDB_throw(a)\
19  if((a)) \
20  throw std::runtime_error(ThreadDB_GetErrorMessage(m_pThreadDB));
21 
22 namespace tdb
23 {
25 
30  class ReadInfo
31  {
32  public:
33  ReadInfo(threadDB_ReadInfo* pReadInfo_p, void* pThreadDB_p) :
34  m_pReadInfo(pReadInfo_p),
35  m_pThreadDB(pThreadDB_p)
36  {
37  }
39  {
40  // Note: the return code is ignored to prevent that an exception is generated within the destructor
41  ThreadDB_Close(&m_pReadInfo, m_pThreadDB);
42  }
44  {
45  return m_pReadInfo;
46  }
47 
48  private:
49  threadDB_ReadInfo* m_pReadInfo;
50  void* m_pThreadDB;
51  };
52 
57  class database
58  {
59  public:
60 
65  database(size_t PackageSize_p, size_t PackageCacheLimit_p) :
66  m_pThreadDB(0)
67  {
68  if (ThreadDB_Create(&m_pThreadDB, PackageSize_p, PackageCacheLimit_p))
69  {
70  throw std::runtime_error("Error creating the database.");
71  }
72  }
73 
78  database(const std::string& rIndexFileUTF8_p, size_t PackageCacheLimit_p) :
79  m_pThreadDB(0)
80  {
81  if (ThreadDB_Import(&m_pThreadDB, rIndexFileUTF8_p.c_str(), PackageCacheLimit_p))
82  {
83  throw std::runtime_error("Error creating the database.");
84  }
85  }
86 
92  {
93  ThreadDB_Destroy(m_pThreadDB);
94  }
95 
100  const char* GetVersionInfo() const
101  {
102  return ThreadDB_GetVersionInfo(m_pThreadDB);
103  }
104 
109  void Save(const char* pIndexFileUTF8_p)
110  {
111  threadDB_throw(ThreadDB_Save(pIndexFileUTF8_p, m_pThreadDB));
112  }
113 
118  const char* NewThread(const char* pDataFolder_p, size_t MaxFileSize_p = std::numeric_limits<size_t>::max())
119  {
120  const char* pFileName_p = 0;
121  threadDB_throw(ThreadDB_NewThread(&pFileName_p,pDataFolder_p, MaxFileSize_p, m_pThreadDB));
122  return pFileName_p;
123  }
124 
129  uint64_t NewPackage()
130  {
131  uint64_t packageID = 0;
132  threadDB_throw(ThreadDB_NewPackage(&packageID,m_pThreadDB));
133  return packageID;
134  }
135 
140  size_t GetPackageCount() const
141  {
142  return ThreadDB_GetPackageCount(m_pThreadDB);
143  }
144 
149  size_t GetFileCount() const
150  {
151  return ThreadDB_GetFileCount(m_pThreadDB);
152  }
153 
158  const char* GetDatabaseFilename(size_t FileIndex_p) const
159  {
160  return ThreadDB_GetDatabaseFileName(FileIndex_p, m_pThreadDB);
161  }
162 
167  void RelocateFileTo(size_t FileID_p, const char* pFilePathUTF8_p, threadDB_RelocationType RelocationType_p) const
168  {
169  threadDB_throw(ThreadDB_RelocateFileTo(FileID_p, pFilePathUTF8_p, RelocationType_p, m_pThreadDB));
170  }
171 
176  void Store(uint64_t Package_p, size_t Size_p, const char pData_p[], threadDB_ItemInfo* pItemHandle_p = 0)
177  {
178  threadDB_throw(ThreadDB_Store(Package_p, Size_p, pData_p, pItemHandle_p, m_pThreadDB));
179  }
180 
185  void Synchronize()
186  {
187  threadDB_throw(ThreadDB_Synchronize(m_pThreadDB));
188  }
189 
196  tdb::ReadInfo Open(size_t Package_p)
197  {
198  threadDB_ReadInfo* pReadInfo = 0;
199  threadDB_throw(ThreadDB_Open(&pReadInfo,Package_p, m_pThreadDB));
200  return tdb::ReadInfo(pReadInfo, m_pThreadDB);
201  }
202 
209  size_t Recover(size_t Size_p, char pData_p[], tdb::ReadInfo& rReadInfo_p)
210  {
211  size_t readBytes = 0;
212  threadDB_throw(ThreadDB_Recover(&readBytes,Size_p, pData_p, rReadInfo_p.Get(), m_pThreadDB));
213  return readBytes;
214  }
215 
222  threadDB_ItemInfo Recover(size_t Size_p, char pData_p[], const threadDB_ItemInfo& rItemHandle_p)
223  {
224  threadDB_ItemInfo itemHandle = rItemHandle_p;
225  threadDB_throw(ThreadDB_RecoverItem(Size_p, pData_p, &itemHandle, m_pThreadDB));
226  return itemHandle;
227  }
228 
233  void Replace(size_t Size_p, const char pData_p[], threadDB_ItemInfo& rItemHandle_p)
234  {
235  threadDB_throw(ThreadDB_Replace(Size_p, pData_p, &rItemHandle_p, m_pThreadDB));
236  }
237 
238  private:
239  void* m_pThreadDB;
240  };
241 }
242 
tdb::database::GetFileCount
size_t GetFileCount() const
C++ wrapper to retrieve the number of registered temporary database files see ThreadDB_GetFileCount.
Definition: threaddbCPP.h:149
tdb::ReadInfo
C++ wrapper class to the threadDB_ReadInfo control structure.
Definition: threaddbCPP.h:30
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::database::Replace
void Replace(size_t Size_p, const char pData_p[], threadDB_ItemInfo &rItemHandle_p)
C++ wrapper for replacing the contents of a data item see ThreadDB_Replace.
Definition: threaddbCPP.h:233
ThreadDB_GetVersionInfo
const DLLEXPORT_ char * ThreadDB_GetVersionInfo(void *pThreadDB_p)
Interface for retrieving the package version info.
tdb::ReadInfo::~ReadInfo
~ReadInfo()
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::GetPackageCount
size_t GetPackageCount() const
C++ wrapper to retrieve the number of registered packages see ThreadDB_GetPackageCount.
Definition: threaddbCPP.h:140
tdb::ReadInfo::ReadInfo
ReadInfo(threadDB_ReadInfo *pReadInfo_p, void *pThreadDB_p)
Definition: threaddbCPP.h:33
tdb::database::~database
~database()
C++ wrapper to destruct a threadDB database object see ThreadDB_Destroy.
Definition: threaddbCPP.h:91
ThreadDB_GetPackageCount
DLLEXPORT_ size_t ThreadDB_GetPackageCount(void *pThreadDB_p)
Returns the number of currently registered packages.
tdb::database::database
database(size_t PackageSize_p, size_t PackageCacheLimit_p)
C++ wrapper to construct a threadDB database object see ThreadDB_Create.
Definition: threaddbCPP.h:65
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 disc see ThreadDB_RelocateFileTo.
Definition: threaddbCPP.h:167
tdb::ItemInfo
threadDB_ItemInfo ItemInfo
Definition: threaddbCPP.h:24
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:176
ThreadDB_GetFileCount
DLLEXPORT_ size_t ThreadDB_GetFileCount(void *pThreadDB_p)
Returns the number of currently registered temporary database files.
ThreadDB_Save
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Save(const char *pIndexFileUTF8_p, void *pThreadDB_p)
Interface to save the stored data.
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:118
tdb
Definition: threaddbCPP.h:22
ThreadDB_Open
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Open(threadDB_ReadInfo **pReadInfo_p, size_t Package_p, void *pThreadDB_p)
This operation opens a package for stream reading.
threaddbC.h
tdb::database::GetVersionInfo
const char * GetVersionInfo() const
C++ wrapper to retrieve the version information of the current installation see ThreadDB_GetVersionIn...
Definition: threaddbCPP.h:100
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.
ThreadDB_GetDatabaseFileName
const DLLEXPORT_ char * ThreadDB_GetDatabaseFileName(size_t FileIndex_p, void *pThreadDB_p)
Returns the filename of a currently registered temporary database files.
tdb::database::Open
tdb::ReadInfo Open(size_t Package_p)
C++ wrapper to open a package for stream reading see ThreadDB_Open.
Definition: threaddbCPP.h:196
ThreadDB_RecoverItem
DLLEXPORT_ threadDB_ReturnCode ThreadDB_RecoverItem(size_t Size_p, char pData_p[], threadDB_ItemInfo *pItemHandle_p, void *pThreadDB_p)
Random access operation to the data items stored in a package.
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 disc.
ThreadDB_Create
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Create(void **pThreadDB_p, size_t PackageSize_p, size_t PackageCacheLimit_p)
Interface for creating a new database object.
tdb::ReadInfo::Get
threadDB_ReadInfo * Get() const
Definition: threaddbCPP.h:43
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:109
tdb::database::Synchronize
void Synchronize()
C++ wrapper to synchronize the package buffers prior reading see ThreadDB_Synchronize.
Definition: threaddbCPP.h:185
tdb::database
C++ wrapper class of the threadDB file mapped memory container extension.
Definition: threaddbCPP.h:57
threadDB_ReadInfo
Stream reading control information.
Definition: threaddbTypes.h:74
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_Recover.
Definition: threaddbCPP.h:209
threadDB_ItemInfo
Random access managment information.
Definition: threaddbTypes.h:56
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:129
tdb::database::database
database(const std::string &rIndexFileUTF8_p, size_t PackageCacheLimit_p)
C++ wrapper to construct a threadDB database object from file see ThreadDB_Import.
Definition: threaddbCPP.h:78
tdb::database::Recover
threadDB_ItemInfo Recover(size_t Size_p, char pData_p[], const threadDB_ItemInfo &rItemHandle_p)
C++ wrapper for random reading of data items see ThreadDB_RecoverItem.
Definition: threaddbCPP.h:222
ThreadDB_Recover
DLLEXPORT_ threadDB_ReturnCode ThreadDB_Recover(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.
threadDB_throw
#define threadDB_throw(a)
Definition: threaddbCPP.h:18
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:92
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:158