 |
Fledge
An open source edge computing platform for industrial users
|
1 #ifndef _READINGS_CATALOGUE_H
2 #define _READINGS_CATALOGUE_H
13 #include "connection.h"
16 #define OVERFLOW_TABLE_ID 0 // Table ID to use for the overflow table
30 std::map<std::thread::id, unsigned long>
32 std::mutex m_boundaryLock;
47 int nReadingsPerDb = 14;
48 int nDbPreallocate = 3;
49 int nDbLeftFreeBeforeAllocate = 1;
50 int nDbToAllocate = 2;
59 TableReference(
int dbId,
int tableId) : m_dbId(dbId), m_tableId(tableId)
153 void getAllDbs(std::vector<int> &dbIdList);
154 void getNewDbs(std::vector<int> &dbIdList);
158 int getNReadingsAvailable()
const {
return m_nReadingsAvailable;}
159 long getIncGlobalId() {
return m_ReadingsGlobalId.fetch_add(1); }
161 long getGlobalId() {
return m_ReadingsGlobalId;};
174 std::string
sqlConstructMultiDb(std::string &sqlCmdBase, std::vector<std::string> &assetCodes,
bool considerExclusion=
false);
175 std::string
sqlConstructOverflow(std::string &sqlCmdBase, std::vector<std::string> &assetCodes,
bool considerExclusion=
false,
bool groupBy =
false);
176 int purgeAllReadings(sqlite3 *dbHandle,
const char *sqlCmdBase,
char **errMsg = NULL,
unsigned long *rowsAffected = NULL);
180 bool attachDb(sqlite3 *dbHandle, std::string &path, std::string &alias,
int dbId);
181 void detachDb(sqlite3 *dbHandle, std::string &alias);
186 int SQLExec(sqlite3 *dbHandle,
const char *sqlCmd,
char **errMsg = NULL);
188 int getMaxAttached() {
return m_attachLimit; };
195 enum NEW_DB_OPERATION {
197 NEW_DB_ATTACH_REQUEST,
211 typedef struct ReadingAvailable {
215 } tyReadingsAvailable;
219 bool createNewDB(sqlite3 *dbHandle,
int newDbId,
int startId, NEW_DB_OPERATION attachAllDb);
220 int getUsedTablesDbId(
int dbId);
221 int getNReadingsAllocate()
const {
return m_storageConfigCurrent.nReadingsPerDb;}
222 bool createReadingsTables(sqlite3 *dbHandle,
int dbId,
int idStartFrom,
int nTables);
223 bool isReadingAvailable()
const;
224 void allocateReadingAvailable();
225 tyReadingsAvailable evaluateLastReadingAvailable(sqlite3 *dbHandle,
int dbId);
226 long calculateGlobalId (sqlite3 *dbHandle);
227 std::string generateDbFilePath(
int dbId);
229 void raiseError(
const char *operation,
const char *reason,...);
230 int SQLStep(sqlite3_stmt *statement);
231 bool enableWAL(std::string &dbPathReadings);
233 bool configurationRetrieve(sqlite3 *dbHandle);
234 void prepareAllDbs();
235 bool applyStorageConfigChanges(sqlite3 *dbHandle);
236 void dbFileDelete(std::string dbPath);
237 void dbsRemove(
int startId,
int endId);
238 void storeReadingsConfiguration (sqlite3 *dbHandle);
239 ACTION changesLogicDBs(
int dbIdCurrent ,
int dbIdLast,
int nDbPreallocateCurrent,
int nDbPreallocateRequest,
int nDbLeftFreeBeforeAllocate);
240 ACTION changesLogicTables(
int maxUsed ,
int Current,
int Request);
241 int retrieveDbIdFromTableId(
int tableId);
243 void configChangeAddDb(sqlite3 *dbHandle);
244 void configChangeRemoveDb(sqlite3 *dbHandle);
245 void configChangeAddTables(sqlite3 *dbHandle ,
int startId,
int endId);
246 void configChangeRemoveTables(sqlite3 *dbHandle ,
int startId,
int endId);
248 int calcMaxReadingUsed();
249 void dropReadingsTables(sqlite3 *dbHandle,
int dbId,
int idStart,
int idEnd);
261 m_nReadingsAvailable = 0;
262 std::map <std::string, TableReference> m_AssetReadingCatalogue={
267 std::map <std::string, std::pair<int, int>> m_EmptyAssetReadingCatalogue={
273 int m_maxOverflowUsed;
275 std::mutex m_emptyReadingTableMutex;
293 void lock() {m_dbLock.lock();}
294 void unlock() {m_dbLock.unlock();}
int extractDbIdFromName(std::string tableName)
Extract the database id from the table name.
Definition: readings_catalogue.cpp:2707
This class handles per thread started transaction boundaries:
Definition: readings_catalogue.h:21
bool attachDb(sqlite3 *dbHandle, std::string &path, std::string &alias, int dbId)
Attach a database to the database connection passed to the call.
Definition: readings_catalogue.cpp:703
int getMaxReadingsId(int dbId)
Retrieve the maximum table id for the provided database id.
Definition: readings_catalogue.cpp:2321
void ClearThreadTransaction(std::thread::id)
Remove committed TRANSACTION for the given thread.
Definition: readings_catalogue.cpp:2897
int getReadingsCount()
Returns the number of readings in use.
Definition: readings_catalogue.cpp:2341
bool createReadingsOverflowTable(sqlite3 *dbHandle, int dbId)
Create the overflow reading tables in the given database id.
Definition: readings_catalogue.cpp:1830
int SQLExec(sqlite3 *dbHandle, const char *sqlCmd, char **errMsg=NULL)
SQLIte wrapper to retry statements when the database is locked.
Definition: readings_catalogue.cpp:2815
void detachDb(sqlite3 *dbHandle, std::string &alias)
Detach a database from a connection.
Definition: readings_catalogue.cpp:735
bool loadEmptyAssetReadingCatalogue(bool clean=true)
Loads the empty reading table catalogue.
Definition: readings_catalogue.cpp:2229
bool storeGlobalId()
Stores the global id into SQlite.
Definition: readings_catalogue.cpp:208
long getMinGlobalId(sqlite3 *dbHandle)
Calculates the minimum id from the readings tables executing a min(id) on each table.
Definition: readings_catalogue.cpp:343
void SetThreadTransactionStart(std::thread::id tid, unsigned long id)
Set BEGIN of a transaction for a given thread, reading id.
Definition: readings_catalogue.cpp:2926
void getNewDbs(std::vector< int > &dbIdList)
Retrieve the list of newly created databases.
Definition: readings_catalogue.cpp:644
Class used to store table references.
Definition: readings_catalogue.h:57
std::string generateDbNameFromTableId(int tableId)
Identifies SQLIte database name from the given table id.
Definition: readings_catalogue.cpp:2787
std::string generateDbFileName(int dbId)
Generates a SQLITE database file name from the database id.
Definition: readings_catalogue.cpp:2667
tyReadingReference getReadingReference(Connection *connection, const char *asset_code)
Allocates a reading table to the given asset_code.
Definition: readings_catalogue.cpp:2018
Definition: readings_catalogue.h:44
Definition: connection.h:32
std::string sqlConstructMultiDb(std::string &sqlCmdBase, std::vector< std::string > &assetCodes, bool considerExclusion=false)
Constructs a sql command from the given one consisting of a set of UNION ALL commands considering all...
Definition: readings_catalogue.cpp:2474
int getReadingPosition(int dbId, int tableId)
Returns the position in the array of the specific readings Id considering the database id and the tab...
Definition: readings_catalogue.cpp:2354
Definition: readings_catalogue.h:130
Used to synchronize the attach database operation.
Definition: readings_catalogue.h:284
bool connectionAttachAllDbs(sqlite3 *dbHandle)
Attaches all the defined SQlite database to all the connections and enable the WAL.
Definition: readings_catalogue.cpp:802
bool latestDbUpdate(sqlite3 *dbHandle, int newDbId)
Stores on the persistent storage the id of the last created database.
Definition: readings_catalogue.cpp:1571
int preallocateNewDbsRange(int dbIdStart, int dbIdEnd)
Create a set of databases.
Definition: readings_catalogue.cpp:568
bool attachDbsToAllConnections()
Attaches all the defined SQlite database to all the connections and enable the WAL.
Definition: readings_catalogue.cpp:838
std::string generateReadingsName(int dbId, int tableId)
Generates the name of the reading table from the given table id as: Prefix + db Id + reading Id.
Definition: readings_catalogue.cpp:2736
void preallocateReadingsTables(int dbId)
Creates all the required readings tables considering the tables already defined in the database and t...
Definition: readings_catalogue.cpp:1493
bool evaluateGlobalId()
Retrieves the global id stored in SQLite and if it is not possible it calculates the value from the r...
Definition: readings_catalogue.cpp:127
int extractReadingsIdFromName(std::string tableName)
Extracts the readings id from the table name.
Definition: readings_catalogue.cpp:2679
bool loadAssetReadingCatalogue()
Loads the reading catalogue stored in SQLite into an in memory structure.
Definition: readings_catalogue.cpp:433
int purgeAllReadings(sqlite3 *dbHandle, const char *sqlCmdBase, char **errMsg=NULL, unsigned long *rowsAffected=NULL)
Delete the content of all the active readings tables using the provided sql command sqlCmdBase.
Definition: readings_catalogue.cpp:2400
unsigned long GetMinReadingId()
Fetch the minimum safe global reading id among all UNCOMMITTED per thread transactions.
Definition: readings_catalogue.cpp:2948
std::string sqlConstructOverflow(std::string &sqlCmdBase, std::vector< std::string > &assetCodes, bool considerExclusion=false, bool groupBy=false)
Add union all clauses for all the overflow tables based on tempalted SQL that is passed in and a set ...
Definition: readings_catalogue.cpp:2584
bool connectionAttachDbList(sqlite3 *dbHandle, std::vector< int > &dbIdList)
Attaches all the defined SQlite database to all the connections and enable the WAL.
Definition: readings_catalogue.cpp:760
tyReadingReference getEmptyReadingTableReference(std::string &asset)
Get Empty Reading Table.
Definition: readings_catalogue.cpp:2295
Implements the handling of multiples readings tables stored among multiple SQLite databases.
Definition: readings_catalogue.h:127
std::string generateDbName(int tableId)
Generates a SQLIte database name from the database id.
Definition: readings_catalogue.cpp:2655
void setUsedDbId(int dbId)
Add the newly create db to the list.
Definition: readings_catalogue.cpp:510
void getAllDbs(std::vector< int > &dbIdList)
Generates a list of all the used databases.
Definition: readings_catalogue.cpp:603
std::string generateDbAlias(int dbId)
Generates a SQLite db alias from the database id.
Definition: readings_catalogue.cpp:2642
void multipleReadingsInit(STORAGE_CONFIGURATION &storageConfig)
Setup the multiple readings databases/tables feature.
Definition: readings_catalogue.cpp:882