comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java @ 1030:c07d9f9a738c

Removed bugs that existed in the caching mechanism (issue264, issue268). gnv-artifacts/trunk@1067 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 06 May 2010 08:32:56 +0000
parents dfd02f8d3602
children 9981452c7e75
comparison
equal deleted inserted replaced
1029:a5e0384fe464 1030:c07d9f9a738c
11 import java.util.HashMap; 11 import java.util.HashMap;
12 import java.util.Iterator; 12 import java.util.Iterator;
13 import java.util.List; 13 import java.util.List;
14 import java.util.Locale; 14 import java.util.Locale;
15 import java.util.Map; 15 import java.util.Map;
16 import java.util.Set;
17 import java.util.TreeMap;
16 18
17 import javax.xml.xpath.XPathConstants; 19 import javax.xml.xpath.XPathConstants;
18 20
19 import net.sf.ehcache.Cache; 21 import net.sf.ehcache.Cache;
20 22
241 while (it.hasNext()) { 243 while (it.hasNext()) {
242 InputData tmpItem = it.next(); 244 InputData tmpItem = it.next();
243 InputValue inputValue = this.inputValues.get(tmpItem.getName()); 245 InputValue inputValue = this.inputValues.get(tmpItem.getName());
244 if (inputValue != null) { 246 if (inputValue != null) {
245 if (this.inputData == null) { 247 if (this.inputData == null) {
246 this.inputData = new HashMap<String, InputData>( 248 this.inputData = new TreeMap<String, InputData>();
247 inputData.size());
248 } 249 }
249 250
250 boolean valid = InputValidator.isInputValid(tmpItem.getValue(), 251 boolean valid = InputValidator.isInputValid(tmpItem.getValue(),
251 inputValue.getType()); 252 inputValue.getType());
252 if (valid) { 253 if (valid) {
346 while (it.hasNext()) { 347 while (it.hasNext()) {
347 InputData tmpItem = it.next(); 348 InputData tmpItem = it.next();
348 InputValue inputValue = this.inputValues.get(tmpItem.getName()); 349 InputValue inputValue = this.inputValues.get(tmpItem.getName());
349 if (inputValue != null) { 350 if (inputValue != null) {
350 if (this.inputData == null) { 351 if (this.inputData == null) {
351 this.inputData = new HashMap<String, InputData>( 352 this.inputData = new TreeMap<String, InputData>();
352 inputData.size());
353 } 353 }
354 354
355 boolean valid = InputValidator.isInputValid(tmpItem.getValue(), 355 boolean valid = InputValidator.isInputValid(tmpItem.getValue(),
356 inputValue.getType()); 356 inputValue.getType());
357 if (valid) { 357 if (valid) {
460 while (it.hasNext()) { 460 while (it.hasNext()) {
461 String value = it.next(); 461 String value = it.next();
462 InputData data = this.inputData.get(value); 462 InputData data = this.inputData.get(value);
463 if (data != null 463 if (data != null
464 && this.inputValues.containsKey(data.getName())) { 464 && this.inputValues.containsKey(data.getName())) {
465
465 int size = this.inputValues.get(data.getName()) 466 int size = this.inputValues.get(data.getName())
466 .usedInQueries(); 467 .usedInQueries();
467 String type = this.inputValues.get(data.getName()) 468 String type = this.inputValues.get(data.getName())
468 .getType(); 469 .getType();
469 String requestValue = data.getValue(); 470 String requestValue = data.getValue();
693 Node staticNode, 694 Node staticNode,
694 CallContext context, 695 CallContext context,
695 String uuid) 696 String uuid)
696 { 697 {
697 State parent = getParent(); 698 State parent = getParent();
698 if (parent != null && parent instanceof StateBase) { 699 if (parent instanceof StateBase) {
699 ((StateBase) parent).describeStatic( 700 ((StateBase) parent).describeStatic(
700 artCreator, creator, document, staticNode, context, uuid); 701 artCreator, creator, document, staticNode, context, uuid);
701 } 702 }
702 703
703 CallMeta callMeta = context.getMeta(); 704 CallMeta callMeta = context.getMeta();
710 XMLUtils.ElementCreator creator, 711 XMLUtils.ElementCreator creator,
711 Document document, 712 Document document,
712 Node staticNode, 713 Node staticNode,
713 CallMeta callMeta 714 CallMeta callMeta
714 ) { 715 ) {
715 InputData data = inputData.get(dataName); 716 InputData data = dataName!= null ? inputData.get(dataName) : null;
716 717
717 if (data == null) { 718 if (data == null) {
718 return; 719 return;
719 } 720 }
720 721
935 node.appendChild(groupNode); 936 node.appendChild(groupNode);
936 } 937 }
937 938
938 939
939 protected void setHash(String uuid) { 940 protected void setHash(String uuid) {
940 this.hash = uuid + 941 String newHash = uuid + HASH_ID_SEPARATOR + id + HASH_ID_SEPARATOR;
941 HASH_ID_SEPARATOR + 942 Set keys = inputData.keySet();
942 id + 943
943 HASH_ID_SEPARATOR + 944 int nhash = 0;
944 inputData.hashCode(); 945 int shift = 0;
945 } 946
946 947 for (Object o: keys) {
947 948 nhash ^= inputData.get(o).hashCode() << shift;
948 protected String getHash() { 949 shift += 2;
950 }
951
952 log.info("### OLD HASH: " + hash);
953 log.info("### NEW HASH: " + (newHash + nhash));
954
955 this.hash = newHash + nhash;
956 }
957
958
959 protected String getHash(String uuid) {
949 return this.hash; 960 return this.hash;
950 } 961 }
951 962
952 963
953 public List<Object> getDescibeData(String uuid) { 964 public List<Object> getDescibeData(String uuid) {
954 CacheFactory factory = CacheFactory.getInstance(); 965 CacheFactory factory = CacheFactory.getInstance();
955 if (factory.isInitialized()) { 966 if (factory.isInitialized()) {
956 // we use a cache 967 // we use a cache
957 log.debug("Using cache.");
958 Cache cache = factory.getCache(); 968 Cache cache = factory.getCache();
959 String key = getHash(); 969 String key = getHash(uuid);
970
971 log.debug("Using cache - key: " + key);
960 972
961 net.sf.ehcache.Element value = cache.get(key); 973 net.sf.ehcache.Element value = cache.get(key);
962 if (value != null) { 974 if (value != null) {
963 // element already in cache, so return it. 975 // element already in cache, so return it.
964 log.debug("Found element in cache."); 976 log.debug("Found element in cache.");
971 try { 983 try {
972 String[] filterValues = generateFilterValuesFromInputData(); 984 String[] filterValues = generateFilterValuesFromInputData();
973 List<Object> data = queryDatabase(filterValues, uuid); 985 List<Object> data = queryDatabase(filterValues, uuid);
974 986
975 cache.put(new net.sf.ehcache.Element(key, data)); 987 cache.put(new net.sf.ehcache.Element(key, data));
988
976 return data; 989 return data;
977 } 990 }
978 catch (QueryException qe) { 991 catch (QueryException qe) {
979 log.error(qe, qe); 992 log.error(qe, qe);
980 } 993 }

http://dive4elements.wald.intevation.org