changeset 513:935e8c7e0b18

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Aug 2013 17:11:45 +0200
parents 3b735fdf320b
children a8be42bcf2bb
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/ServerIndex.cpp
diffstat 3 files changed, 31 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Fri Aug 16 15:39:53 2013 +0200
+++ b/OrthancServer/DatabaseWrapper.cpp	Fri Aug 16 17:11:45 2013 +0200
@@ -949,4 +949,13 @@
   {
     db_.Execute("DELETE FROM " + tableName);    
   }
+
+
+  bool DatabaseWrapper::IsExistingResource(int64_t internalId)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
+                        "SELECT * FROM Resources WHERE internalId=?");
+    s.BindInt(0, internalId);
+    return s.Step();
+  }
 }
--- a/OrthancServer/DatabaseWrapper.h	Fri Aug 16 15:39:53 2013 +0200
+++ b/OrthancServer/DatabaseWrapper.h	Fri Aug 16 17:11:45 2013 +0200
@@ -223,5 +223,7 @@
     uint64_t IncrementGlobalSequence(GlobalProperty property);
 
     void ClearTable(const std::string& tableName);
+
+    bool IsExistingResource(int64_t internalId);
   };
 }
--- a/OrthancServer/ServerIndex.cpp	Fri Aug 16 15:39:53 2013 +0200
+++ b/OrthancServer/ServerIndex.cpp	Fri Aug 16 17:11:45 2013 +0200
@@ -1416,6 +1416,7 @@
       boost::this_thread::sleep(boost::posix_time::seconds(1));
 
       boost::mutex::scoped_lock lock(that->mutex_);
+
       while (!that->unstableResources_.IsEmpty() &&
              that->unstableResources_.GetOldestPayload().GetAge() > static_cast<unsigned int>(stableAge))
       {
@@ -1425,25 +1426,29 @@
         UnstableResourcePayload payload;
         int64_t id = that->unstableResources_.RemoveOldest(payload);
 
-        switch (payload.type_)
+        // Ensure that the resource is still existing before logging the change
+        if (that->db_->IsExistingResource(id))
         {
-          case Orthanc::ResourceType_Patient:
-            that->db_->LogChange(ChangeType_StablePatient, id, ResourceType_Patient);
-            break;
-
-          case Orthanc::ResourceType_Study:
-            that->db_->LogChange(ChangeType_StableStudy, id, ResourceType_Study);
-            break;
+          switch (payload.type_)
+          {
+            case Orthanc::ResourceType_Patient:
+              that->db_->LogChange(ChangeType_StablePatient, id, ResourceType_Patient);
+              break;
 
-          case Orthanc::ResourceType_Series:
-            that->db_->LogChange(ChangeType_StableSeries, id, ResourceType_Series);
-            break;
+            case Orthanc::ResourceType_Study:
+              that->db_->LogChange(ChangeType_StableStudy, id, ResourceType_Study);
+              break;
 
-          default:
-            throw OrthancException(ErrorCode_InternalError);
+            case Orthanc::ResourceType_Series:
+              that->db_->LogChange(ChangeType_StableSeries, id, ResourceType_Series);
+              break;
+
+            default:
+              throw OrthancException(ErrorCode_InternalError);
+          }
+
+          //LOG(INFO) << "Stable resource: " << EnumerationToString(payload.type_) << " " << id;
         }
-
-        //LOG(INFO) << "Stable resource: " << EnumerationToString(payload.type_) << " " << id;
       }
     }