comparison OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp @ 5455:176bc05f85f4 pg-transactions

DB: new Capabilities class to manage future new methods from DB plugins + Added IncrementGlobalProperty
author Alain Mazy <am@osimis.io>
date Thu, 07 Dec 2023 12:04:11 +0100
parents e95caa87fed8
children 06eb8bc9f024
comparison
equal deleted inserted replaced
5454:099d45f49fe1 5455:176bc05f85f4
605 * global mutex that was protecting the database. 605 * global mutex that was protecting the database.
606 **/ 606 **/
607 607
608 Transaction transaction(db_, *factory_, TransactionType_ReadOnly); // TODO - Only if not "TransactionType_Implicit" 608 Transaction transaction(db_, *factory_, TransactionType_ReadOnly); // TODO - Only if not "TransactionType_Implicit"
609 { 609 {
610 ReadOnlyTransaction t(transaction.GetDatabaseTransaction(), transaction.GetContext(), db_.HasLabelsSupport()); 610 ReadOnlyTransaction t(transaction.GetDatabaseTransaction(), transaction.GetContext(), db_.GetDatabaseCapabilities());
611 readOperations->Apply(t); 611 readOperations->Apply(t);
612 } 612 }
613 transaction.Commit(); 613 transaction.Commit();
614 } 614 }
615 else 615 else
616 { 616 {
617 assert(writeOperations != NULL); 617 assert(writeOperations != NULL);
618 618
619 Transaction transaction(db_, *factory_, TransactionType_ReadWrite); 619 Transaction transaction(db_, *factory_, TransactionType_ReadWrite);
620 { 620 {
621 ReadWriteTransaction t(transaction.GetDatabaseTransaction(), transaction.GetContext(), db_.HasLabelsSupport()); 621 ReadWriteTransaction t(transaction.GetDatabaseTransaction(), transaction.GetContext(), db_.GetDatabaseCapabilities());
622 writeOperations->Apply(t); 622 writeOperations->Apply(t);
623 } 623 }
624 transaction.Commit(); 624 transaction.Commit();
625 } 625 }
626 626
652 652
653 653
654 StatelessDatabaseOperations::StatelessDatabaseOperations(IDatabaseWrapper& db) : 654 StatelessDatabaseOperations::StatelessDatabaseOperations(IDatabaseWrapper& db) :
655 db_(db), 655 db_(db),
656 mainDicomTagsRegistry_(new MainDicomTagsRegistry), 656 mainDicomTagsRegistry_(new MainDicomTagsRegistry),
657 hasFlushToDisk_(db.HasFlushToDisk()),
658 maxRetries_(0) 657 maxRetries_(0)
659 { 658 {
660 } 659 }
661 660
662 661
937 } 936 }
938 } 937 }
939 } 938 }
940 939
941 if ((expandFlags & ExpandResourceFlags_IncludeLabels) && 940 if ((expandFlags & ExpandResourceFlags_IncludeLabels) &&
942 transaction.HasLabelsSupport()) 941 transaction.GetDatabaseCapabilities().HasLabelsSupport())
943 { 942 {
944 transaction.ListLabels(target.labels_, internalId); 943 transaction.ListLabels(target.labels_, internalId);
945 } 944 }
946 945
947 std::string tmp; 946 std::string tmp;
1959 } 1958 }
1960 } 1959 }
1961 }; 1960 };
1962 1961
1963 if (!labels.empty() && 1962 if (!labels.empty() &&
1964 !db_.HasLabelsSupport()) 1963 !db_.GetDatabaseCapabilities().HasLabelsSupport())
1965 { 1964 {
1966 throw OrthancException(ErrorCode_NotImplemented, "The database backend doesn't support labels"); 1965 throw OrthancException(ErrorCode_NotImplemented, "The database backend doesn't support labels");
1967 } 1966 }
1968 1967
1969 for (std::set<std::string>::const_iterator it = labels.begin(); it != labels.end(); ++it) 1968 for (std::set<std::string>::const_iterator it = labels.begin(); it != labels.end(); ++it)
2429 return newValue_; 2428 return newValue_;
2430 } 2429 }
2431 2430
2432 virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE 2431 virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE
2433 { 2432 {
2434 std::string oldString; 2433 if (transaction.GetDatabaseCapabilities().HasAtomicIncrementGlobalProperty())
2435 2434 {
2436 if (transaction.LookupGlobalProperty(oldString, sequence_, shared_)) 2435 newValue_ = static_cast<uint64_t>(transaction.IncrementGlobalProperty(sequence_, shared_, 1));
2437 {
2438 uint64_t oldValue;
2439
2440 try
2441 {
2442 oldValue = boost::lexical_cast<uint64_t>(oldString);
2443 }
2444 catch (boost::bad_lexical_cast&)
2445 {
2446 LOG(ERROR) << "Cannot read the global sequence "
2447 << boost::lexical_cast<std::string>(sequence_) << ", resetting it";
2448 oldValue = 0;
2449 }
2450
2451 newValue_ = oldValue + 1;
2452 } 2436 }
2453 else 2437 else
2454 { 2438 {
2455 // Initialize the sequence at "1" 2439 std::string oldString;
2456 newValue_ = 1; 2440
2457 } 2441 if (transaction.LookupGlobalProperty(oldString, sequence_, shared_))
2458 2442 {
2459 transaction.SetGlobalProperty(sequence_, shared_, boost::lexical_cast<std::string>(newValue_)); 2443 uint64_t oldValue;
2444
2445 try
2446 {
2447 oldValue = boost::lexical_cast<uint64_t>(oldString);
2448 }
2449 catch (boost::bad_lexical_cast&)
2450 {
2451 LOG(ERROR) << "Cannot read the global sequence "
2452 << boost::lexical_cast<std::string>(sequence_) << ", resetting it";
2453 oldValue = 0;
2454 }
2455
2456 newValue_ = oldValue + 1;
2457 }
2458 else
2459 {
2460 // Initialize the sequence at "1"
2461 newValue_ = 1;
2462 }
2463
2464 transaction.SetGlobalProperty(sequence_, shared_, boost::lexical_cast<std::string>(newValue_));
2465 }
2460 } 2466 }
2461 }; 2467 };
2462 2468
2463 Operations operations(sequence, shared); 2469 Operations operations(sequence, shared);
2464 Apply(operations); 2470 Apply(operations);
3681 3687
3682 3688
3683 bool StatelessDatabaseOperations::HasLabelsSupport() 3689 bool StatelessDatabaseOperations::HasLabelsSupport()
3684 { 3690 {
3685 boost::shared_lock<boost::shared_mutex> lock(mutex_); 3691 boost::shared_lock<boost::shared_mutex> lock(mutex_);
3686 return db_.HasLabelsSupport(); 3692 return db_.GetDatabaseCapabilities().HasLabelsSupport();
3687 } 3693 }
3688 } 3694 }