ctkDicomObjectLocatorCache.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.apache.org/licenses/LICENSE-2.0.txt
  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. 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. 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. hasCachedData = true;
  100. if (!uuids.contains(objectDescriptor.descriptorUUID))
  101. {
  102. return false;
  103. }
  104. }
  105. // Loop over series
  106. foreach(const ctkDicomAppHosting::Series& series, study.series)
  107. {
  108. // Loop over series level object descriptors
  109. foreach(const ctkDicomAppHosting::ObjectDescriptor& objectDescriptor, series.objectDescriptors)
  110. {
  111. hasCachedData = true;
  112. if (!uuids.contains(objectDescriptor.descriptorUUID))
  113. {
  114. return false;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return hasCachedData;
  121. }
  122. //----------------------------------------------------------------------------
  123. bool ctkDicomObjectLocatorCache::find(const QString& objectUuid,
  124. ctkDicomAppHosting::ObjectLocator& objectLocator)const
  125. {
  126. Q_D(const ctkDicomObjectLocatorCache);
  127. ObjectLocatorCacheItem item;
  128. bool found = d->find(objectUuid, item);
  129. if (!found)
  130. {
  131. return false;
  132. }
  133. objectLocator = item.ObjectLocator;
  134. return true;
  135. }
  136. //----------------------------------------------------------------------------
  137. void ctkDicomObjectLocatorCache::insert(const QString& objectUuid,
  138. const ctkDicomAppHosting::ObjectLocator& objectLocator,
  139. bool temporary)
  140. {
  141. Q_D(ctkDicomObjectLocatorCache);
  142. ObjectLocatorCacheItem item;
  143. d->find(objectUuid, item);
  144. if(d->ObjectLocatorMap.contains(objectUuid))
  145. {
  146. Q_ASSERT(objectLocator == item.ObjectLocator); // ObjectLocator are expected to match
  147. item.RefCount++;
  148. d->ObjectLocatorMap.insert(objectUuid, item);
  149. return;
  150. }
  151. item.ObjectLocator = objectLocator;
  152. d->ObjectLocatorMap.insert(objectUuid, item);
  153. if (temporary)
  154. {
  155. d->TemporaryObjectLocatorSet.insert(objectUuid);
  156. }
  157. }
  158. //----------------------------------------------------------------------------
  159. bool ctkDicomObjectLocatorCache::remove(const QString& objectUuid)
  160. {
  161. Q_D(ctkDicomObjectLocatorCache);
  162. ObjectLocatorCacheItem item;
  163. bool found = d->find(objectUuid, item);
  164. if (!found)
  165. {
  166. return false;
  167. }
  168. Q_ASSERT(item.RefCount > 0);
  169. item.RefCount--;
  170. d->ObjectLocatorMap.insert(objectUuid, item);
  171. if (item.RefCount == 0)
  172. {
  173. if (d->TemporaryObjectLocatorSet.contains(objectUuid))
  174. {
  175. // Not implemented - Delete the object
  176. qDebug() << "ctkDicomObjectLocatorCache::remove - RefCount [1] - Temporary [True] - Not implemented";
  177. bool removed = d->TemporaryObjectLocatorSet.remove(objectUuid);
  178. Q_ASSERT(removed);
  179. }
  180. int removed = d->ObjectLocatorMap.remove(objectUuid);
  181. Q_ASSERT(removed == 1);
  182. }
  183. return true;
  184. }
  185. //----------------------------------------------------------------------------
  186. QList<ctkDicomAppHosting::ObjectLocator> ctkDicomObjectLocatorCache::getData(const QList<QUuid>& objectUUIDs)
  187. {
  188. QList<ctkDicomAppHosting::ObjectLocator> objectLocators;
  189. foreach(const QUuid& uuid, objectUUIDs)
  190. {
  191. ctkDicomAppHosting::ObjectLocator objectLocator;
  192. bool found = this->find(uuid.toString(), objectLocator);
  193. if (!found)
  194. {
  195. // Use the empty objectLocator
  196. // TODO Source should be set to NULL
  197. }
  198. objectLocators << objectLocator;
  199. }
  200. return objectLocators;
  201. }