comparison OrthancServer/ServerIndex.cpp @ 1177:5b2d8c280ac2 db-changes

Plugins can monitor changes through callbacks
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 24 Sep 2014 17:37:44 +0200
parents 1ea4094d077c
children 6b9b02a16e99
comparison
equal deleted inserted replaced
1176:f24e04838054 1177:5b2d8c280ac2
59 class ServerIndexListener : public IServerIndexListener 59 class ServerIndexListener : public IServerIndexListener
60 { 60 {
61 private: 61 private:
62 struct FileToRemove 62 struct FileToRemove
63 { 63 {
64 private:
64 std::string uuid_; 65 std::string uuid_;
65 FileContentType type_; 66 FileContentType type_;
66 67
68 public:
67 FileToRemove(const FileInfo& info) : uuid_(info.GetUuid()), 69 FileToRemove(const FileInfo& info) : uuid_(info.GetUuid()),
68 type_(info.GetContentType()) 70 type_(info.GetContentType())
69 { 71 {
72 }
73
74 const std::string& GetUuid() const
75 {
76 return uuid_;
77 }
78
79 FileContentType GetContentType() const
80 {
81 return type_;
82 }
83 };
84
85 struct Change
86 {
87 private:
88 ChangeType changeType_;
89 ResourceType resourceType_;
90 std::string publicId_;
91
92 public:
93 Change(ChangeType changeType,
94 ResourceType resourceType,
95 const std::string& publicId) :
96 changeType_(changeType),
97 resourceType_(resourceType),
98 publicId_(publicId)
99 {
100 }
101
102 ChangeType GetChangeType() const
103 {
104 return changeType_;
105 }
106
107 ResourceType GetResourceType() const
108 {
109 return resourceType_;
110 }
111
112 const std::string& GetPublicId() const
113 {
114 return publicId_;
70 } 115 }
71 }; 116 };
72 117
73 ServerContext& context_; 118 ServerContext& context_;
74 bool hasRemainingLevel_; 119 bool hasRemainingLevel_;
75 ResourceType remainingType_; 120 ResourceType remainingType_;
76 std::string remainingPublicId_; 121 std::string remainingPublicId_;
77 std::list<FileToRemove> pendingFilesToRemove_; 122 std::list<FileToRemove> pendingFilesToRemove_;
123 std::list<Change> pendingChanges_;
78 uint64_t sizeOfFilesToRemove_; 124 uint64_t sizeOfFilesToRemove_;
79 125
80 public: 126 public:
81 ServerIndexListener(ServerContext& context) : 127 ServerIndexListener(ServerContext& context) :
82 context_(context) 128 context_(context)
103 { 149 {
104 for (std::list<FileToRemove>::iterator 150 for (std::list<FileToRemove>::iterator
105 it = pendingFilesToRemove_.begin(); 151 it = pendingFilesToRemove_.begin();
106 it != pendingFilesToRemove_.end(); ++it) 152 it != pendingFilesToRemove_.end(); ++it)
107 { 153 {
108 context_.RemoveFile(it->uuid_, it->type_); 154 context_.RemoveFile(it->GetUuid(), it->GetContentType());
109 } 155 }
110 } 156 }
111 157
112 virtual void SignalRemainingAncestor(ResourceType parentType, 158 virtual void SignalRemainingAncestor(ResourceType parentType,
113 const std::string& publicId) 159 const std::string& publicId)
135 assert(Toolbox::IsUuid(info.GetUuid())); 181 assert(Toolbox::IsUuid(info.GetUuid()));
136 pendingFilesToRemove_.push_back(FileToRemove(info)); 182 pendingFilesToRemove_.push_back(FileToRemove(info));
137 sizeOfFilesToRemove_ += info.GetCompressedSize(); 183 sizeOfFilesToRemove_ += info.GetCompressedSize();
138 } 184 }
139 185
140 virtual void SignalResourceDeleted(ResourceType type, 186 virtual void SignalChange(ChangeType changeType,
141 const std::string& publicId) 187 ResourceType resourceType,
142 { 188 const std::string& publicId)
143 LOG(INFO) << "Resource " << publicId << " of type " << EnumerationToString(type) << " is deleted"; 189 {
144 } 190 LOG(INFO) << "Change related to resource " << publicId << " of type "
145 191 << EnumerationToString(resourceType) << ": " << EnumerationToString(changeType);
192
193 pendingChanges_.push_back(Change(changeType, resourceType, publicId));
194 }
146 195
147 bool HasRemainingLevel() const 196 bool HasRemainingLevel() const
148 { 197 {
149 return hasRemainingLevel_; 198 return hasRemainingLevel_;
150 } 199 }
205 } 254 }
206 } 255 }
207 }; 256 };
208 257
209 258
210 struct ServerIndex::UnstableResourcePayload 259 class ServerIndex::UnstableResourcePayload
211 { 260 {
212 Orthanc::ResourceType type_; 261 private:
262 ResourceType type_;
263 std::string publicId_;
213 boost::posix_time::ptime time_; 264 boost::posix_time::ptime time_;
214 265
215 UnstableResourcePayload() : type_(Orthanc::ResourceType_Instance) 266 public:
216 { 267 UnstableResourcePayload() : type_(ResourceType_Instance)
217 } 268 {
218 269 }
219 UnstableResourcePayload(Orthanc::ResourceType type) : type_(type) 270
271 UnstableResourcePayload(Orthanc::ResourceType type,
272 const std::string& publicId) :
273 type_(type),
274 publicId_(publicId)
220 { 275 {
221 time_ = boost::posix_time::second_clock::local_time(); 276 time_ = boost::posix_time::second_clock::local_time();
222 } 277 }
223 278
224 unsigned int GetAge() const 279 unsigned int GetAge() const
225 { 280 {
226 return (boost::posix_time::second_clock::local_time() - time_).total_seconds(); 281 return (boost::posix_time::second_clock::local_time() - time_).total_seconds();
282 }
283
284 ResourceType GetResourceType() const
285 {
286 return type_;
287 }
288
289 const std::string& GetPublicId() const
290 {
291 return publicId_;
227 } 292 }
228 }; 293 };
229 294
230 295
231 bool ServerIndex::DeleteResource(Json::Value& target, 296 bool ServerIndex::DeleteResource(Json::Value& target,
602 } 667 }
603 668
604 SeriesStatus seriesStatus = GetSeriesStatus(series); 669 SeriesStatus seriesStatus = GetSeriesStatus(series);
605 if (seriesStatus == SeriesStatus_Complete) 670 if (seriesStatus == SeriesStatus_Complete)
606 { 671 {
607 db_->LogChange(ChangeType_CompletedSeries, series, ResourceType_Series); 672 db_->LogChange(ChangeType_CompletedSeries, series, ResourceType_Series, hasher.HashSeries());
608 } 673 }
609 674
610 // Mark the parent resources of this instance as unstable 675 // Mark the parent resources of this instance as unstable
611 MarkAsUnstable(series, ResourceType_Series); 676 MarkAsUnstable(series, ResourceType_Series, hasher.HashSeries());
612 MarkAsUnstable(study, ResourceType_Study); 677 MarkAsUnstable(study, ResourceType_Study, hasher.HashStudy());
613 MarkAsUnstable(patient, ResourceType_Patient); 678 MarkAsUnstable(patient, ResourceType_Patient, hasher.HashPatient());
614 679
615 t.Commit(instanceSize); 680 t.Commit(instanceSize);
616 681
617 return StoreStatus_Success; 682 return StoreStatus_Success;
618 } 683 }
1391 if (!db_->LookupResource(publicId, id, type)) 1456 if (!db_->LookupResource(publicId, id, type))
1392 { 1457 {
1393 throw OrthancException(ErrorCode_UnknownResource); 1458 throw OrthancException(ErrorCode_UnknownResource);
1394 } 1459 }
1395 1460
1396 db_->LogChange(changeType, id, type); 1461 db_->LogChange(changeType, id, type, publicId);
1397 1462
1398 transaction->Commit(); 1463 transaction->Commit();
1399 } 1464 }
1400 1465
1401 1466
1588 int64_t id = that->unstableResources_.RemoveOldest(payload); 1653 int64_t id = that->unstableResources_.RemoveOldest(payload);
1589 1654
1590 // Ensure that the resource is still existing before logging the change 1655 // Ensure that the resource is still existing before logging the change
1591 if (that->db_->IsExistingResource(id)) 1656 if (that->db_->IsExistingResource(id))
1592 { 1657 {
1593 switch (payload.type_) 1658 switch (payload.GetResourceType())
1594 { 1659 {
1595 case Orthanc::ResourceType_Patient: 1660 case ResourceType_Patient:
1596 that->db_->LogChange(ChangeType_StablePatient, id, ResourceType_Patient); 1661 that->db_->LogChange(ChangeType_StablePatient, id, ResourceType_Patient, payload.GetPublicId());
1597 break; 1662 break;
1598 1663
1599 case Orthanc::ResourceType_Study: 1664 case ResourceType_Study:
1600 that->db_->LogChange(ChangeType_StableStudy, id, ResourceType_Study); 1665 that->db_->LogChange(ChangeType_StableStudy, id, ResourceType_Study, payload.GetPublicId());
1601 break; 1666 break;
1602 1667
1603 case Orthanc::ResourceType_Series: 1668 case ResourceType_Series:
1604 that->db_->LogChange(ChangeType_StableSeries, id, ResourceType_Series); 1669 that->db_->LogChange(ChangeType_StableSeries, id, ResourceType_Series, payload.GetPublicId());
1605 break; 1670 break;
1606 1671
1607 default: 1672 default:
1608 throw OrthancException(ErrorCode_InternalError); 1673 throw OrthancException(ErrorCode_InternalError);
1609 } 1674 }
1616 LOG(INFO) << "Closing the monitor thread for stable resources"; 1681 LOG(INFO) << "Closing the monitor thread for stable resources";
1617 } 1682 }
1618 1683
1619 1684
1620 void ServerIndex::MarkAsUnstable(int64_t id, 1685 void ServerIndex::MarkAsUnstable(int64_t id,
1621 Orthanc::ResourceType type) 1686 Orthanc::ResourceType type,
1687 const std::string& publicId)
1622 { 1688 {
1623 // WARNING: Before calling this method, "mutex_" must be locked. 1689 // WARNING: Before calling this method, "mutex_" must be locked.
1624 1690
1625 assert(type == Orthanc::ResourceType_Patient || 1691 assert(type == Orthanc::ResourceType_Patient ||
1626 type == Orthanc::ResourceType_Study || 1692 type == Orthanc::ResourceType_Study ||
1627 type == Orthanc::ResourceType_Series); 1693 type == Orthanc::ResourceType_Series);
1628 1694
1629 unstableResources_.AddOrMakeMostRecent(id, type); 1695 UnstableResourcePayload payload(type, publicId);
1696 unstableResources_.AddOrMakeMostRecent(id, payload);
1630 //LOG(INFO) << "Unstable resource: " << EnumerationToString(type) << " " << id; 1697 //LOG(INFO) << "Unstable resource: " << EnumerationToString(type) << " " << id;
1631 } 1698 }
1632 1699
1633 1700
1634 1701
1785 target[key] = value; 1852 target[key] = value;
1786 } 1853 }
1787 1854
1788 return true; 1855 return true;
1789 } 1856 }
1790
1791
1792 } 1857 }