Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 2127:0c7847b8e85e
Speed up the join of calculation results and the km annotations.
flys-artifacts/trunk@3698 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 17 Jan 2012 18:26:45 +0000 |
parents | 97a25b54eea3 |
children | a4bdf7d8527e |
comparison
equal
deleted
inserted
replaced
2126:d626ae185305 | 2127:0c7847b8e85e |
---|---|
5 | 5 |
6 import org.apache.log4j.Logger; | 6 import org.apache.log4j.Logger; |
7 | 7 |
8 import de.intevation.flys.model.Annotation; | 8 import de.intevation.flys.model.Annotation; |
9 import de.intevation.flys.model.Position; | 9 import de.intevation.flys.model.Position; |
10 import de.intevation.flys.model.Range; | |
10 | 11 |
11 import de.intevation.flys.artifacts.cache.CacheFactory; | 12 import de.intevation.flys.artifacts.cache.CacheFactory; |
12 import de.intevation.flys.artifacts.model.AnnotationsFactory; | 13 import de.intevation.flys.artifacts.model.AnnotationsFactory; |
13 | 14 |
15 import java.util.Map; | |
16 import java.util.TreeMap; | |
17 import java.util.Comparator; | |
18 import java.util.Iterator; | |
19 | |
20 import java.io.Serializable; | |
21 | |
14 | 22 |
15 public class LocationProvider { | 23 public class LocationProvider { |
16 | 24 |
25 public static final double EPSILON = 1e-5; | |
26 | |
27 public static class KmComparator | |
28 implements Serializable, Comparator<Double> | |
29 { | |
30 @Override | |
31 public int compare(Double a, Double b) { | |
32 double diff = a - b; | |
33 if (diff < -EPSILON) return -1; | |
34 if (diff > +EPSILON) return +1; | |
35 return 0; | |
36 } | |
37 } // class KmComparator | |
38 | |
17 public static final String CACHE_KEY = "location-provider"; | 39 public static final String CACHE_KEY = "location-provider"; |
40 | |
41 public static final String PREFIX = "lp-"; | |
18 | 42 |
19 | 43 |
20 private static final Logger logger = | 44 private static final Logger logger = |
21 Logger.getLogger(LocationProvider.class); | 45 Logger.getLogger(LocationProvider.class); |
22 | 46 |
23 | 47 |
24 private LocationProvider() { | 48 private LocationProvider() { |
25 } | 49 } |
26 | 50 |
51 public static String getLocation(String river, double km) { | |
27 | 52 |
28 public static String getLocation(String river, double km) { | 53 Double KM = Double.valueOf(km); |
29 return getLocation(getLocationHash(river, km), river, km); | 54 |
55 return getKmMap(river, KM).get(KM); | |
30 } | 56 } |
31 | 57 |
32 | 58 protected static Map<Double, String> getKmMap(String river, double km) { |
33 public static String getLocation(String hash, String river, double km) { | |
34 logger.debug("Fetch location for '" + river + "' at '" + km + "'"); | |
35 | 59 |
36 Cache cache = CacheFactory.getCache(CACHE_KEY); | 60 Cache cache = CacheFactory.getCache(CACHE_KEY); |
37 | 61 |
38 if (cache != null) { | 62 if (cache == null) { |
39 return getCachedLocation(cache, hash, river, km); | 63 return uncachedKmMap(river, km); |
40 } | 64 } |
41 else { | 65 |
42 logger.info("No Cache for Locations configured."); | 66 String key = PREFIX + river; |
43 return getUncachedLocation(river, km); | 67 |
68 Element element = cache.get(key); | |
69 | |
70 if (element != null) { | |
71 return (Map<Double, String>)element.getValue(); | |
44 } | 72 } |
73 | |
74 Map<Double, String> map = uncachedKmMap(river, null); | |
75 | |
76 cache.put(new Element(key, map)); | |
77 | |
78 return map; | |
45 } | 79 } |
46 | 80 |
47 | 81 protected static Map<Double, String> uncachedKmMap( |
48 protected static String getCachedLocation( | |
49 Cache cache, | |
50 String hash, | |
51 String river, | 82 String river, |
52 double km | 83 Double queryKm |
53 ) { | 84 ) { |
54 logger.debug("Fetch location from cache."); | 85 Map<Double, String> map = |
55 | 86 new TreeMap<Double, String>(new KmComparator()); |
56 Element element = cache.get(hash); | 87 |
57 | 88 if (queryKm != null) { |
58 if (element == null) { | 89 Annotation annotation = |
59 logger.debug("Element is not in cache yet."); | 90 AnnotationsFactory.getAnnotation(river, queryKm); |
60 | 91 if (annotation != null) { |
61 String location = getUncachedLocation(river, km); | 92 map.put(queryKm, annotation.getPosition().getValue()); |
62 element = new Element(hash, location); | 93 } |
63 cache.put(element); | 94 return map; |
64 } | 95 } |
65 | 96 |
66 return (String) element.getValue(); | 97 for (Iterator<Annotation> iter = |
67 } | 98 AnnotationsFactory.getAnnotationsIterator(river); |
68 | 99 iter.hasNext(); |
69 | 100 ) { |
70 protected static String getUncachedLocation(String river, double km) { | 101 Annotation annotation = iter.next(); |
71 logger.debug("Fetch location from backend."); | 102 Position pos = annotation.getPosition(); |
72 | 103 Range range = annotation.getRange(); |
73 Annotation annotation = AnnotationsFactory.getAnnotation(river, km); | 104 Double km = range.getA().doubleValue(); |
74 | 105 map.put(km, pos.getValue()); |
75 if (annotation != null) { | |
76 logger.debug("Found an annotation."); | |
77 Position pos = annotation.getPosition(); | |
78 return pos.getValue(); | |
79 } | 106 } |
80 | 107 |
81 return ""; | 108 return map; |
82 } | |
83 | |
84 | |
85 protected static String getLocationHash(String river, double km) { | |
86 return river + "#" + km; | |
87 } | 109 } |
88 } | 110 } |
89 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 111 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |