Project

Profile

Help

Revision 0f5be6eb

Added by Michael Kay over 12 years ago

Fix bug 2770579 - anyURI cache grows indefinitely when multithreading

View differences:

latest9.1/bj/net/sf/saxon/sort/LRUCache.java
54 54
        map.clear();
55 55
    }
56 56

  
57

  
58 57
}
59 58

  
60 59
//
latest9.1/bj/net/sf/saxon/value/AnyURIValue.java
3 3
import net.sf.saxon.functions.EscapeURI;
4 4
import net.sf.saxon.om.StandardNames;
5 5
import net.sf.saxon.sort.LRUCache;
6
import net.sf.saxon.trans.XPathException;
7 6
import net.sf.saxon.type.AtomicType;
8 7
import net.sf.saxon.type.BuiltInAtomicType;
9 8
import net.sf.saxon.type.ConversionResult;
......
13 12
import java.net.URISyntaxException;
14 13

  
15 14

  
16

  
17 15
/**
18 16
* An XPath value of type xs:anyURI.
19 17
 *
......
38 36
     * valid only after escaping, as otherwise an exception occurs during the validation process
39 37
     */
40 38

  
41
    private static LRUCache cache = new LRUCache(20);
39
    private static ThreadLocal caches = new ThreadLocal();
40
    //private static LRUCache cache = new LRUCache(20);
42 41

  
43 42
    /**
44 43
    * Constructor
......
87 86

  
88 87
    public static boolean isValidURI(CharSequence value) {
89 88

  
89
        LRUCache cache = (LRUCache)caches.get();
90
        if (cache == null) {
91
            cache = new LRUCache(10);
92
            caches.set(cache);
93
        }
94

  
90 95
        if (cache.get(value) != null) {
91 96
            return true;
92 97
        }
......
160 165
        }
161 166
    }
162 167

  
163
    /**
164
    * Convert to Java object (for passing to external functions)
165
    * @param target the Java class to which conversion is required
166
    * @return the result of the conversion
167
    * @throws XPathException if conversion to this target type is not possible
168
    */
169

  
170
//    public Object convertAtomicToJava(Class target, XPathContext context) throws XPathException {
171
//        if (target==Object.class) {
172
//            return value;
173
//        } else if (target.isAssignableFrom(StringValue.class)) {
174
//            return this;
175
//        } else if (target==URI.class) {
176
//            try {
177
//                return new URI(value.toString());
178
//            } catch (URISyntaxException err) {
179
//                throw new XPathException("The anyURI value '" + value + "' is not an acceptable Java URI");
180
//            }
181
//        } else if (target==URL.class) {
182
//            try {
183
//                return new URL(value.toString());
184
//            } catch (MalformedURLException err) {
185
//                throw new XPathException("The anyURI value '" + value + "' is not an acceptable Java URL");
186
//            }
187
//        } else if (target==String.class) {
188
//            return value;
189
//        } else if (target==CharSequence.class) {
190
//            return value;
191
//        } else {
192
//             Object o = super.convertSequenceToJava(target, context);
193
//            if (o == null) {
194
//                throw new XPathException("Conversion of anyURI to " + target.getName() +
195
//                        " is not supported");
196
//            }
197
//            return o;
168
//    public static void main(String[] args) {
169
//        ExecutorService executor = Executors.newFixedThreadPool(10);
170
//        for (int i = 0; i < 100; i++) {
171
//            executor.execute(new Runnable() {
172
//                public void run() {
173
//                    for (int i=0; i<1000000; i++) {
174
//                        String uri = "http://a.com/aaa" + i;
175
//                        boolean b = AnyURIValue.isValidURI(uri);
176
//                        if (i % 1000 == 0) {
177
//                            System.err.println("Memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
178
//                        }
179
//                    }
180
//                }
181
//            });
198 182
//        }
199 183
//    }
200 184

  
201

  
202 185
}
203 186

  
204 187
//

Also available in: Unified diff