ctkDicomObjectLocatorCache.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QHash>
  16. #include <QUuid>
  17. #include <QSet>
  18. #include <QDebug>
  19. // CTK includes
  20. #include "ctkDicomAppHostingTypes.h"
  21. #include "ctkDicomObjectLocatorCache.h"
  22. namespace
  23. {
  24. struct ObjectLocatorCacheItem
  25. {
  26. ObjectLocatorCacheItem():RefCount(1){}
  27. ctkDicomAppHosting::ObjectLocator ObjectLocator;
  28. int RefCount;
  29. };
  30. }
  31. class ctkDicomObjectLocatorCachePrivate
  32. {
  33. public:
  34. ctkDicomObjectLocatorCachePrivate();
  35. bool find(const QString& objectUuid, ObjectLocatorCacheItem& objectLocatorCacheItem)const;
  36. QHash<QString, ObjectLocatorCacheItem> ObjectLocatorMap;
  37. QSet<QString> TemporaryObjectLocatorSet;
  38. };
  39. //----------------------------------------------------------------------------
  40. // ctkDicomObjectLocatorCachePrivate methods
  41. //----------------------------------------------------------------------------
  42. ctkDicomObjectLocatorCachePrivate::ctkDicomObjectLocatorCachePrivate()
  43. {
  44. }
  45. //----------------------------------------------------------------------------
  46. bool ctkDicomObjectLocatorCachePrivate::find(const QString& objectUuid,
  47. ObjectLocatorCacheItem& objectLocatorCacheItem)const
  48. {
  49. if (!this->ObjectLocatorMap.contains(objectUuid))
  50. {
  51. return false;
  52. }
  53. objectLocatorCacheItem = this->ObjectLocatorMap[objectUuid];
  54. return true;
  55. }
  56. //----------------------------------------------------------------------------
  57. // ctkDicomObjectLocatorCache methods
  58. //----------------------------------------------------------------------------
  59. ctkDicomObjectLocatorCache::ctkDicomObjectLocatorCache() : d_ptr(new ctkDicomObjectLocatorCachePrivate())
  60. {
  61. }
  62. //----------------------------------------------------------------------------
  63. ctkDicomObjectLocatorCache::~ctkDicomObjectLocatorCache()
  64. {
  65. }
  66. //----------------------------------------------------------------------------
  67. bool ctkDicomObjectLocatorCache::isCached(const ctkDicomAppHosting::AvailableData& availableData)const
  68. {
  69. Q_D(const ctkDicomObjectLocatorCache);
  70. bool hasCachedData = false;
  71. QList<QString> uuids = d->ObjectLocatorMap.keys();
  72. // Loop over top level object descriptors
  73. foreach(const ctkDicomAppHosting::ObjectDescriptor& objectDescriptor, availableData.objectDescriptors)
  74. {
  75. if (!hasCachedData) { hasCachedData = true; }
  76. if (!uuids.contains(objectDescriptor.descriptorUUID))
  77. {
  78. return false;
  79. }
  80. }
  81. // Loop over patients
  82. foreach(const ctkDicomAppHosting::Patient& patient, availableData.patients)
  83. {
  84. // Loop over patient level object descriptors
  85. foreach(const ctkDicomAppHosting::ObjectDescriptor& objectDescriptor, patient.objectDescriptors)
  86. {
  87. if (!hasCachedData) { hasCachedData = true; }
  88. if (!uuids.contains(objectDescriptor.descriptorUUID))
  89. {
  90. return false;
  91. }
  92. }
  93. // Loop over studies
  94. foreach(const ctkDicomAppHosting::Study& study, patient.studies)
  95. {
  96. // Loop over study level object descriptors
  97. foreach(const ctkDicomAppHosting::ObjectDescriptor& objectDescriptor, study.objectDescriptors)
  98. {
  99. if (!uuids.contains(objectDescriptor.descriptorUUID))
  100. {
  101. return false;
  102. }
  103. }
  104. // Loop over series
  105. foreach(const ctkDicomAppHosting::Series& series, study.series)
  106. {
  107. // Loop over series level object descriptors
  108. foreach(const ctkDicomAppHosting::ObjectDescriptor& objectDescriptor, series.objectDescriptors)
  109. {
  110. if (!uuids.contains(objectDescriptor.descriptorUUID))
  111. {
  112. return false;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return hasCachedData;
  119. }
  120. //----------------------------------------------------------------------------
  121. bool ctkDicomObjectLocatorCache::find(const QString& objectUuid,
  122. ctkDicomAppHosting::ObjectLocator& objectLocator)const
  123. {
  124. Q_D(const ctkDicomObjectLocatorCache);
  125. ObjectLocatorCacheItem item;
  126. bool found = d->find(objectUuid, item);
  127. if (!found)
  128. {
  129. return false;
  130. }
  131. objectLocator = item.ObjectLocator;
  132. return true;
  133. }
  134. //----------------------------------------------------------------------------
  135. void ctkDicomObjectLocatorCache::insert(const QString& objectUuid,
  136. const ctkDicomAppHosting::ObjectLocator& objectLocator,
  137. bool temporary)
  138. {
  139. Q_D(ctkDicomObjectLocatorCache);
  140. ObjectLocatorCacheItem item;
  141. d->find(objectUuid, item);
  142. if(d->ObjectLocatorMap.contains(objectUuid))
  143. {
  144. Q_ASSERT(objectLocator == item.ObjectLocator); // ObjectLocator are expected to match
  145. item.RefCount++;
  146. d->ObjectLocatorMap.insert(objectUuid, item);
  147. return;
  148. }
  149. item.ObjectLocator = objectLocator;
  150. d->ObjectLocatorMap.insert(objectUuid, item);
  151. if (temporary)
  152. {
  153. d->TemporaryObjectLocatorSet.insert(objectUuid);
  154. }
  155. }
  156. //----------------------------------------------------------------------------
  157. bool ctkDicomObjectLocatorCache::remove(const QString& objectUuid)
  158. {
  159. Q_D(ctkDicomObjectLocatorCache);
  160. ObjectLocatorCacheItem item;
  161. bool found = d->find(objectUuid, item);
  162. if (!found)
  163. {
  164. return false;
  165. }
  166. Q_ASSERT(item.RefCount > 0);
  167. item.RefCount--;
  168. d->ObjectLocatorMap.insert(objectUuid, item);
  169. if (item.RefCount == 0)
  170. {
  171. if (d->TemporaryObjectLocatorSet.contains(objectUuid))
  172. {
  173. // Not implemented - Delete the object
  174. qDebug() << "ctkDicomObjectLocatorCache::remove - RefCount [1] - Temporary [True] - Not implemented";
  175. bool removed = d->TemporaryObjectLocatorSet.remove(objectUuid);
  176. Q_ASSERT(removed);
  177. }
  178. int removed = d->ObjectLocatorMap.remove(objectUuid);
  179. Q_ASSERT(removed == 1);
  180. }
  181. return true;
  182. }
  183. //----------------------------------------------------------------------------
  184. QList<ctkDicomAppHosting::ObjectLocator> ctkDicomObjectLocatorCache::getData(const QList<QUuid>& objectUUIDs)
  185. {
  186. QList<ctkDicomAppHosting::ObjectLocator> objectLocators;
  187. foreach(const QUuid& uuid, objectUUIDs)
  188. {
  189. ctkDicomAppHosting::ObjectLocator objectLocator;
  190. bool found = this->find(uuid, objectLocator);
  191. if (!found)
  192. {
  193. // Use the empty objectLocator
  194. // TODO Source should be set to NULL
  195. }
  196. objectLocators << objectLocator;
  197. }
  198. return objectLocators;
  199. }