comparison OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp @ 5522:dd430a1b21fe pg-transactions

simplifying StatelessDatabaseOperations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jan 2024 09:41:06 +0100
parents 4dd50c4b985a
children d82cc7c9720a
comparison
equal deleted inserted replaced
5521:0d0f8788884a 5522:dd430a1b21fe
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_.GetDatabaseCapabilities()); 610 ReadOnlyTransaction t(transaction.GetDatabaseTransaction(), transaction.GetContext());
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_.GetDatabaseCapabilities()); 621 ReadWriteTransaction t(transaction.GetDatabaseTransaction(), transaction.GetContext());
622 writeOperations->Apply(t); 622 writeOperations->Apply(t);
623 } 623 }
624 transaction.Commit(); 624 transaction.Commit();
625 } 625 }
626 626
719 { 719 {
720 class Operations : public ReadOnlyOperationsT6< 720 class Operations : public ReadOnlyOperationsT6<
721 bool&, ExpandedResource&, const std::string&, ResourceType, const std::set<DicomTag>&, ExpandResourceFlags> 721 bool&, ExpandedResource&, const std::string&, ResourceType, const std::set<DicomTag>&, ExpandResourceFlags>
722 { 722 {
723 private: 723 private:
724 724 bool hasLabelsSupport_;
725
725 static bool LookupStringMetadata(std::string& result, 726 static bool LookupStringMetadata(std::string& result,
726 const std::map<MetadataType, std::string>& metadata, 727 const std::map<MetadataType, std::string>& metadata,
727 MetadataType type) 728 MetadataType type)
728 { 729 {
729 std::map<MetadataType, std::string>::const_iterator found = metadata.find(type); 730 std::map<MetadataType, std::string>::const_iterator found = metadata.find(type);
761 } 762 }
762 } 763 }
763 764
764 765
765 public: 766 public:
767 Operations(bool hasLabelsSupport) :
768 hasLabelsSupport_(hasLabelsSupport)
769 {
770 }
771
766 virtual void ApplyTuple(ReadOnlyTransaction& transaction, 772 virtual void ApplyTuple(ReadOnlyTransaction& transaction,
767 const Tuple& tuple) ORTHANC_OVERRIDE 773 const Tuple& tuple) ORTHANC_OVERRIDE
768 { 774 {
769 // Lookup for the requested resource 775 // Lookup for the requested resource
770 int64_t internalId; 776 int64_t internalId;
937 } 943 }
938 } 944 }
939 } 945 }
940 946
941 if ((expandFlags & ExpandResourceFlags_IncludeLabels) && 947 if ((expandFlags & ExpandResourceFlags_IncludeLabels) &&
942 transaction.GetDatabaseCapabilities().HasLabelsSupport()) 948 hasLabelsSupport_)
943 { 949 {
944 transaction.ListLabels(target.labels_, internalId); 950 transaction.ListLabels(target.labels_, internalId);
945 } 951 }
946 952
947 std::string tmp; 953 std::string tmp;
976 } 982 }
977 } 983 }
978 }; 984 };
979 985
980 bool found; 986 bool found;
981 Operations operations; 987 Operations operations(db_.GetDatabaseCapabilities().HasLabelsSupport());
982 operations.Apply(*this, found, target, publicId, level, requestedTags, expandFlags); 988 operations.Apply(*this, found, target, publicId, level, requestedTags, expandFlags);
983 return found; 989 return found;
984 } 990 }
985 991
986 992
2469 { 2475 {
2470 private: 2476 private:
2471 uint64_t newValue_; 2477 uint64_t newValue_;
2472 GlobalProperty sequence_; 2478 GlobalProperty sequence_;
2473 bool shared_; 2479 bool shared_;
2480 bool hasAtomicIncrementGlobalProperty_;
2474 2481
2475 public: 2482 public:
2476 Operations(GlobalProperty sequence, 2483 Operations(GlobalProperty sequence,
2477 bool shared) : 2484 bool shared,
2485 bool hasAtomicIncrementGlobalProperty) :
2478 newValue_(0), // Dummy initialization 2486 newValue_(0), // Dummy initialization
2479 sequence_(sequence), 2487 sequence_(sequence),
2480 shared_(shared) 2488 shared_(shared),
2489 hasAtomicIncrementGlobalProperty_(hasAtomicIncrementGlobalProperty)
2481 { 2490 {
2482 } 2491 }
2483 2492
2484 uint64_t GetNewValue() const 2493 uint64_t GetNewValue() const
2485 { 2494 {
2486 return newValue_; 2495 return newValue_;
2487 } 2496 }
2488 2497
2489 virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE 2498 virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE
2490 { 2499 {
2491 if (transaction.GetDatabaseCapabilities().HasAtomicIncrementGlobalProperty()) 2500 if (hasAtomicIncrementGlobalProperty_)
2492 { 2501 {
2493 newValue_ = static_cast<uint64_t>(transaction.IncrementGlobalProperty(sequence_, shared_, 1)); 2502 newValue_ = static_cast<uint64_t>(transaction.IncrementGlobalProperty(sequence_, shared_, 1));
2494 } 2503 }
2495 else 2504 else
2496 { 2505 {
2522 transaction.SetGlobalProperty(sequence_, shared_, boost::lexical_cast<std::string>(newValue_)); 2531 transaction.SetGlobalProperty(sequence_, shared_, boost::lexical_cast<std::string>(newValue_));
2523 } 2532 }
2524 } 2533 }
2525 }; 2534 };
2526 2535
2527 Operations operations(sequence, shared); 2536 Operations operations(sequence, shared, GetDatabaseCapabilities().HasAtomicIncrementGlobalProperty());
2528 Apply(operations); 2537 Apply(operations);
2529 assert(operations.GetNewValue() != 0); 2538 assert(operations.GetNewValue() != 0);
2530 return operations.GetNewValue(); 2539 return operations.GetNewValue();
2531 } 2540 }
2532 2541