comparison Plugin/ViewerPrefetchPolicy.cpp @ 0:02f7a0400a91

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Feb 2015 13:45:35 +0100
parents
children 46ec13a1177c
comparison
equal deleted inserted replaced
-1:000000000000 0:02f7a0400a91
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "ViewerPrefetchPolicy.h"
22
23 #include "ViewerToolbox.h"
24 #include "Cache/CacheScheduler.h"
25
26 #include <json/value.h>
27 #include <json/reader.h>
28
29
30
31 static const Json::Value::ArrayIndex PREFETCH_FORWARD = 10;
32 static const Json::Value::ArrayIndex PREFETCH_BACKWARD = 3;
33
34
35 namespace OrthancPlugins
36 {
37 void ViewerPrefetchPolicy::ApplySeries(std::list<CacheIndex>& toPrefetch,
38 CacheScheduler& cache,
39 const std::string& series,
40 const std::string& content)
41 {
42 Json::Value json;
43 Json::Reader reader;
44 if (!reader.parse(content, json) ||
45 !json.isMember("SortedInstances"))
46 {
47 return;
48 }
49
50 const Json::Value& instances = json["SortedInstances"];
51 if (instances.type() != Json::arrayValue)
52 {
53 return;
54 }
55
56 for (Json::Value::ArrayIndex i = 0;
57 i < instances.size() && i < PREFETCH_FORWARD;
58 i++)
59 {
60 std::string item = "jpeg95-" + instances[i].asString();
61 toPrefetch.push_back(CacheIndex(CacheBundle_DecodedImage, item));
62 }
63 }
64
65
66 void ViewerPrefetchPolicy::ApplyInstance(std::list<CacheIndex>& toPrefetch,
67 CacheScheduler& cache,
68 const std::string& path)
69 {
70 size_t separator = path.find('-');
71 if (separator == std::string::npos)
72 {
73 return;
74 }
75
76 std::string compression = path.substr(0, separator + 1);
77 std::string instanceId = path.substr(separator + 1);
78
79 Json::Value instance;
80 if (!GetJsonFromOrthanc(instance, context_, "/instances/" + instanceId) ||
81 !instance.isMember("ParentSeries"))
82 {
83 return;
84 }
85
86 std::string tmp;
87 if (!cache.Access(tmp, CacheBundle_SeriesInformation, instance["ParentSeries"].asString()))
88 {
89 return;
90 }
91
92 Json::Value series;
93 Json::Reader reader;
94 if (!reader.parse(tmp, series) ||
95 !series.isMember("SortedInstances"))
96 {
97 return;
98 }
99
100 const Json::Value& instances = series["SortedInstances"];
101 if (instances.type() != Json::arrayValue)
102 {
103 return;
104 }
105
106 Json::Value::ArrayIndex position = 0;
107 while (position < instances.size())
108 {
109 if (instances[position] == instanceId)
110 {
111 break;
112 }
113
114 position++;
115 }
116
117 if (position == instances.size())
118 {
119 return;
120 }
121
122 for (Json::Value::ArrayIndex i = position;
123 i < instances.size() && i < position + PREFETCH_FORWARD;
124 i++)
125 {
126 std::string item = compression + instances[i].asString();
127 toPrefetch.push_back(CacheIndex(CacheBundle_DecodedImage, item));
128 }
129
130 for (Json::Value::ArrayIndex i = position;
131 i >= 0 && i > position - PREFETCH_BACKWARD; )
132 {
133 i--;
134 std::string item = compression + instances[i].asString();
135 toPrefetch.push_back(CacheIndex(CacheBundle_DecodedImage, item));
136 }
137 }
138
139
140 void ViewerPrefetchPolicy::Apply(std::list<CacheIndex>& toPrefetch,
141 CacheScheduler& cache,
142 const CacheIndex& accessed,
143 const std::string& content)
144 {
145 switch (accessed.GetBundle())
146 {
147 case CacheBundle_SeriesInformation:
148 ApplySeries(toPrefetch, cache, accessed.GetItem(), content);
149 return;
150
151 case CacheBundle_DecodedImage:
152 ApplyInstance(toPrefetch, cache, accessed.GetItem());
153 return;
154
155 default:
156 return;
157 }
158 }
159 }
160