Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/CrossSectionKMService.java @ 2120:f021080cb409
Use improved caching for cross section lines data.
flys-artifacts/trunk@3690 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 16 Jan 2012 17:38:39 +0000 |
parents | e0b081105a82 |
children |
comparison
equal
deleted
inserted
replaced
2119:dc28ea60b53d | 2120:f021080cb409 |
---|---|
74 logger.debug("CrossSectionKMService.doProcess"); | 74 logger.debug("CrossSectionKMService.doProcess"); |
75 | 75 |
76 NodeList crossSectionNodes = | 76 NodeList crossSectionNodes = |
77 data.getElementsByTagName("art:cross-section"); | 77 data.getElementsByTagName("art:cross-section"); |
78 | 78 |
79 Cache cache = CacheFactory.getCache(CACHE_NAME); | |
80 | |
81 Document document = XMLUtils.newDocument(); | 79 Document document = XMLUtils.newDocument(); |
82 | 80 |
83 Element all = document.createElement("cross-sections"); | 81 Element all = document.createElement("cross-sections"); |
84 | 82 |
85 for (int i = 0, CS = crossSectionNodes.getLength(); i < CS; ++i) { | 83 for (int i = 0, CS = crossSectionNodes.getLength(); i < CS; ++i) { |
109 catch (NumberFormatException nfe) { | 107 catch (NumberFormatException nfe) { |
110 logger.debug("converting number failed", nfe); | 108 logger.debug("converting number failed", nfe); |
111 continue; | 109 continue; |
112 } | 110 } |
113 | 111 |
114 NavigableMap<Double, Integer> map; | 112 NavigableMap<Double, Integer> map = getKms(crossSectionId); |
115 | |
116 if (cache == null) { | |
117 map = getUncached(crossSectionId); | |
118 } | |
119 else { | |
120 net.sf.ehcache.Element element = cache.get(crossSectionId); | |
121 if (element == null) { | |
122 map = getUncached(crossSectionId); | |
123 if (map != null) { | |
124 element = new net.sf.ehcache.Element( | |
125 crossSectionId, map); | |
126 cache.put(element); | |
127 } | |
128 } | |
129 else { | |
130 map = (NavigableMap<Double, Integer>)element.getValue(); | |
131 } | |
132 } | |
133 | 113 |
134 if (map == null) { | 114 if (map == null) { |
135 logger.debug("cannot find cross section " + crossSectionId); | 115 logger.debug("cannot find cross section " + crossSectionId); |
136 continue; | 116 continue; |
137 } | 117 } |
157 document.appendChild(all); | 137 document.appendChild(all); |
158 | 138 |
159 return document; | 139 return document; |
160 } | 140 } |
161 | 141 |
142 public static NavigableMap<Double, Integer> getKms(int crossSectionId) { | |
143 | |
144 Cache cache = CacheFactory.getCache(CACHE_NAME); | |
145 | |
146 if (cache == null) { | |
147 return getUncached(crossSectionId); | |
148 } | |
149 | |
150 NavigableMap<Double, Integer> map; | |
151 | |
152 net.sf.ehcache.Element element = cache.get(crossSectionId); | |
153 if (element == null) { | |
154 map = getUncached(crossSectionId); | |
155 if (map != null) { | |
156 element = new net.sf.ehcache.Element( | |
157 crossSectionId, map); | |
158 cache.put(element); | |
159 } | |
160 } | |
161 else { | |
162 map = (NavigableMap<Double, Integer>)element.getValue(); | |
163 } | |
164 | |
165 return map; | |
166 } | |
167 | |
162 | 168 |
163 /** | 169 /** |
164 * @param km the kilometer from which to start searching for other | 170 * @param km the kilometer from which to start searching for other |
165 * measurements | 171 * measurements |
166 * @param N number of neighboring measurements to find. | 172 * @param N number of neighboring measurements to find. |
171 int N | 177 int N |
172 ) { | 178 ) { |
173 Deque<Map.Entry<Double, Integer>> result = | 179 Deque<Map.Entry<Double, Integer>> result = |
174 new ArrayDeque<Map.Entry<Double, Integer>>(2*N); | 180 new ArrayDeque<Map.Entry<Double, Integer>>(2*N); |
175 | 181 |
176 if(map.get(km) != null) { | 182 Integer v = map.get(km); |
177 result.add(new AbstractMap.SimpleEntry<Double, Integer>(km,map.get(km))); | 183 |
178 | 184 if (v != null) { |
185 result.add(new AbstractMap.SimpleEntry<Double, Integer>(km, v)); | |
179 } | 186 } |
180 | 187 |
181 int i = 0; | 188 int i = 0; |
182 for (Map.Entry<Double, Integer> entry: | 189 for (Map.Entry<Double, Integer> entry: |
183 map.headMap(km, false).descendingMap().entrySet()) { | 190 map.headMap(km, false).descendingMap().entrySet()) { |