comparison OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp @ 4587:888868a5dc4e db-changes

ServerIndex now uses StatelessDatabaseOperations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Mar 2021 15:59:03 +0100
parents 1d96fe7e054e
children bec74e29f86b
comparison
equal deleted inserted replaced
4586:1d96fe7e054e 4587:888868a5dc4e
666 666
667 667
668 StatelessDatabaseOperations::StatelessDatabaseOperations(IDatabaseWrapper& db) : 668 StatelessDatabaseOperations::StatelessDatabaseOperations(IDatabaseWrapper& db) :
669 db_(db), 669 db_(db),
670 maxRetries_(10), 670 maxRetries_(10),
671 mainDicomTagsRegistry_(new MainDicomTagsRegistry) 671 mainDicomTagsRegistry_(new MainDicomTagsRegistry),
672 { 672 hasFlushToDisk_(db.HasFlushToDisk())
673 {
674 }
675
676
677 void StatelessDatabaseOperations::FlushToDisk()
678 {
679 boost::mutex::scoped_lock lock(databaseMutex_);
680
681 try
682 {
683 db_.FlushToDisk();
684 }
685 catch (OrthancException&)
686 {
687 LOG(ERROR) << "Cannot flush the SQLite database to the disk (is your filesystem full?)";
688 }
673 } 689 }
674 690
675 691
676 void StatelessDatabaseOperations::SetTransactionContextFactory(ITransactionContextFactory* factory) 692 void StatelessDatabaseOperations::SetTransactionContextFactory(ITransactionContextFactory* factory)
677 { 693 {
2413 Operations operations(publicId, type); 2429 Operations operations(publicId, type);
2414 Apply(operations); 2430 Apply(operations);
2415 } 2431 }
2416 2432
2417 2433
2418 void StatelessDatabaseOperations::LogChange(ChangeType changeType, 2434 void StatelessDatabaseOperations::LogChange(int64_t internalId,
2435 ChangeType changeType,
2419 const std::string& publicId, 2436 const std::string& publicId,
2420 ResourceType level) 2437 ResourceType level)
2421 { 2438 {
2422 class Operations : public IReadWriteOperations 2439 class Operations : public IReadWriteOperations
2423 { 2440 {
2424 private: 2441 private:
2442 int64_t internalId_;
2425 ChangeType changeType_; 2443 ChangeType changeType_;
2426 const std::string& publicId_; 2444 const std::string& publicId_;
2427 ResourceType level_; 2445 ResourceType level_;
2428 2446
2429 public: 2447 public:
2430 Operations(ChangeType changeType, 2448 Operations(int64_t internalId,
2449 ChangeType changeType,
2431 const std::string& publicId, 2450 const std::string& publicId,
2432 ResourceType level) : 2451 ResourceType level) :
2452 internalId_(internalId),
2433 changeType_(changeType), 2453 changeType_(changeType),
2434 publicId_(publicId), 2454 publicId_(publicId),
2435 level_(level) 2455 level_(level)
2436 { 2456 {
2437 } 2457 }
2438 2458
2439 virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE 2459 virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE
2440 { 2460 {
2441 int64_t id; 2461 int64_t id;
2442 ResourceType type; 2462 ResourceType type;
2443 if (transaction.LookupResource(id, type, publicId_)) 2463 if (transaction.LookupResource(id, type, publicId_) &&
2444 { 2464 id == internalId_)
2445 // Make sure that the resource is still existing. Ignore if 2465 {
2446 // the resource has been deleted, because this function 2466 /**
2447 // might e.g. be called from 2467 * Make sure that the resource is still existing, with the
2448 // "StatelessDatabaseOperations::UnstableResourcesMonitorThread()" (for 2468 * same internal ID, which indicates the absence of bouncing
2449 // which a deleted resource not an error case) 2469 * (if deleting then recreating the same resource). Don't
2470 * throw an exception if the resource has been deleted,
2471 * because this function might e.g. be called from
2472 * "StatelessDatabaseOperations::UnstableResourcesMonitorThread()"
2473 * (for which a deleted resource is *not* an error case).
2474 **/
2450 if (type == level_) 2475 if (type == level_)
2451 { 2476 {
2452 transaction.LogChange(id, changeType_, type, publicId_); 2477 transaction.LogChange(id, changeType_, type, publicId_);
2453 } 2478 }
2454 else 2479 else
2458 } 2483 }
2459 } 2484 }
2460 } 2485 }
2461 }; 2486 };
2462 2487
2463 Operations operations(changeType, publicId, level); 2488 Operations operations(internalId, changeType, publicId, level);
2464 Apply(operations); 2489 Apply(operations);
2465 } 2490 }
2466 2491
2467 2492
2468 void StatelessDatabaseOperations::ReconstructInstance(const ParsedDicomFile& dicom) 2493 void StatelessDatabaseOperations::ReconstructInstance(const ParsedDicomFile& dicom)