comparison OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp @ 4594:d494b4f1103e db-changes

removed the global database mutex from ServerIndex and StatelessDatabaseOperations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Mar 2021 12:43:43 +0100
parents 36bbf3169a27
children cc64385593ef
comparison
equal deleted inserted replaced
4593:60a860942c5e 4594:d494b4f1103e
301 { 301 {
302 childrenPublicIds.push_back(s.ColumnString(0)); 302 childrenPublicIds.push_back(s.ColumnString(0));
303 } 303 }
304 } 304 }
305 305
306 IDatabaseListener& listener_; 306 boost::mutex::scoped_lock lock_;
307 SignalRemainingAncestor& signalRemainingAncestor_; 307 IDatabaseListener& listener_;
308 SignalRemainingAncestor& signalRemainingAncestor_;
308 309
309 public: 310 public:
310 TransactionBase(SQLite::Connection& db, 311 TransactionBase(boost::mutex& mutex,
312 SQLite::Connection& db,
311 IDatabaseListener& listener, 313 IDatabaseListener& listener,
312 SignalRemainingAncestor& signalRemainingAncestor) : 314 SignalRemainingAncestor& signalRemainingAncestor) :
313 UnitTestsTransaction(db), 315 UnitTestsTransaction(db),
316 lock_(mutex),
314 listener_(listener), 317 listener_(listener),
315 signalRemainingAncestor_(signalRemainingAncestor) 318 signalRemainingAncestor_(signalRemainingAncestor)
316 { 319 {
317 } 320 }
318 321
1159 int64_t initialDiskSize_; 1162 int64_t initialDiskSize_;
1160 1163
1161 public: 1164 public:
1162 ReadWriteTransaction(SQLiteDatabaseWrapper& that, 1165 ReadWriteTransaction(SQLiteDatabaseWrapper& that,
1163 IDatabaseListener& listener) : 1166 IDatabaseListener& listener) :
1164 TransactionBase(that.db_, listener, *that.signalRemainingAncestor_), 1167 TransactionBase(that.mutex_, that.db_, listener, *that.signalRemainingAncestor_),
1165 that_(that), 1168 that_(that),
1166 transaction_(new SQLite::Transaction(that_.db_)) 1169 transaction_(new SQLite::Transaction(that_.db_))
1167 { 1170 {
1168 if (that_.activeTransaction_ != NULL) 1171 if (that_.activeTransaction_ != NULL)
1169 { 1172 {
1213 SQLiteDatabaseWrapper& that_; 1216 SQLiteDatabaseWrapper& that_;
1214 1217
1215 public: 1218 public:
1216 ReadOnlyTransaction(SQLiteDatabaseWrapper& that, 1219 ReadOnlyTransaction(SQLiteDatabaseWrapper& that,
1217 IDatabaseListener& listener) : 1220 IDatabaseListener& listener) :
1218 TransactionBase(that.db_, listener, *that.signalRemainingAncestor_), 1221 TransactionBase(that.mutex_, that.db_, listener, *that.signalRemainingAncestor_),
1219 that_(that) 1222 that_(that)
1220 { 1223 {
1221 if (that_.activeTransaction_ != NULL) 1224 if (that_.activeTransaction_ != NULL)
1222 { 1225 {
1223 throw OrthancException(ErrorCode_InternalError); 1226 throw OrthancException(ErrorCode_InternalError);
1272 } 1275 }
1273 1276
1274 1277
1275 void SQLiteDatabaseWrapper::Open() 1278 void SQLiteDatabaseWrapper::Open()
1276 { 1279 {
1277 if (signalRemainingAncestor_ != NULL) 1280 {
1278 { 1281 boost::mutex::scoped_lock lock(mutex_);
1279 throw OrthancException(ErrorCode_BadSequenceOfCalls); // Cannot open twice 1282
1280 } 1283 if (signalRemainingAncestor_ != NULL)
1284 {
1285 throw OrthancException(ErrorCode_BadSequenceOfCalls); // Cannot open twice
1286 }
1281 1287
1282 signalRemainingAncestor_ = dynamic_cast<SignalRemainingAncestor*>(db_.Register(new SignalRemainingAncestor)); 1288 signalRemainingAncestor_ = dynamic_cast<SignalRemainingAncestor*>(db_.Register(new SignalRemainingAncestor));
1283 db_.Register(new SignalFileDeleted(*this)); 1289 db_.Register(new SignalFileDeleted(*this));
1284 db_.Register(new SignalResourceDeleted(*this)); 1290 db_.Register(new SignalResourceDeleted(*this));
1285 1291
1286 db_.Execute("PRAGMA ENCODING=\"UTF-8\";"); 1292 db_.Execute("PRAGMA ENCODING=\"UTF-8\";");
1287 1293
1288 // Performance tuning of SQLite with PRAGMAs 1294 // Performance tuning of SQLite with PRAGMAs
1289 // http://www.sqlite.org/pragma.html 1295 // http://www.sqlite.org/pragma.html
1290 db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;"); 1296 db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;");
1291 db_.Execute("PRAGMA JOURNAL_MODE=WAL;"); 1297 db_.Execute("PRAGMA JOURNAL_MODE=WAL;");
1292 db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;"); 1298 db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;");
1293 db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;"); 1299 db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;");
1294 //db_.Execute("PRAGMA TEMP_STORE=memory"); 1300 //db_.Execute("PRAGMA TEMP_STORE=memory");
1295 1301
1296 // Make "LIKE" case-sensitive in SQLite 1302 // Make "LIKE" case-sensitive in SQLite
1297 db_.Execute("PRAGMA case_sensitive_like = true;"); 1303 db_.Execute("PRAGMA case_sensitive_like = true;");
1304 }
1298 1305
1299 VoidDatabaseListener listener; 1306 VoidDatabaseListener listener;
1300 1307
1301 { 1308 {
1302 std::unique_ptr<ITransaction> transaction(StartTransaction(TransactionType_ReadOnly, listener)); 1309 std::unique_ptr<ITransaction> transaction(StartTransaction(TransactionType_ReadOnly, listener));
1349 transaction->Commit(0); 1356 transaction->Commit(0);
1350 } 1357 }
1351 } 1358 }
1352 1359
1353 1360
1361 void SQLiteDatabaseWrapper::Close()
1362 {
1363 boost::mutex::scoped_lock lock(mutex_);
1364 db_.Close();
1365 }
1366
1367
1354 static void ExecuteUpgradeScript(SQLite::Connection& db, 1368 static void ExecuteUpgradeScript(SQLite::Connection& db,
1355 ServerResources::FileResourceId script) 1369 ServerResources::FileResourceId script)
1356 { 1370 {
1357 std::string upgrade; 1371 std::string upgrade;
1358 ServerResources::GetFileResource(upgrade, script); 1372 ServerResources::GetFileResource(upgrade, script);
1363 1377
1364 1378
1365 void SQLiteDatabaseWrapper::Upgrade(unsigned int targetVersion, 1379 void SQLiteDatabaseWrapper::Upgrade(unsigned int targetVersion,
1366 IStorageArea& storageArea) 1380 IStorageArea& storageArea)
1367 { 1381 {
1382 boost::mutex::scoped_lock lock(mutex_);
1383
1368 if (targetVersion != 6) 1384 if (targetVersion != 6)
1369 { 1385 {
1370 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); 1386 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
1371 } 1387 }
1372 1388
1439 throw OrthancException(ErrorCode_InternalError); 1455 throw OrthancException(ErrorCode_InternalError);
1440 } 1456 }
1441 } 1457 }
1442 1458
1443 1459
1460 void SQLiteDatabaseWrapper::FlushToDisk()
1461 {
1462 boost::mutex::scoped_lock lock(mutex_);
1463 db_.FlushToDisk();
1464 }
1465
1466
1444 int64_t SQLiteDatabaseWrapper::UnitTestsTransaction::CreateResource(const std::string& publicId, 1467 int64_t SQLiteDatabaseWrapper::UnitTestsTransaction::CreateResource(const std::string& publicId,
1445 ResourceType type) 1468 ResourceType type)
1446 { 1469 {
1447 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)"); 1470 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)");
1448 s.BindInt(0, type); 1471 s.BindInt(0, type);