|
| 1 | +''' |
| 2 | + Copyright (c) 2019 Timothy Savannah under terms of LGPLv3. All Rights Reserved. |
| 3 | +
|
| 4 | + See LICENSE (https://gnu.org/licenses/lgpl-3.0.txt) for more information. |
| 5 | +
|
| 6 | + See: https://github.com/kata198/AdvancedHTMLParser for full information |
| 7 | +
|
| 8 | +
|
| 9 | + ==INTERNAL== |
| 10 | +
|
| 11 | + xpath._cache.py - Internal module for caching recent XPath expression parsings |
| 12 | +''' |
| 13 | +# vim: set ts=4 sw=4 st=4 expandtab : |
| 14 | + |
| 15 | +import threading |
| 16 | + |
| 17 | +from hashlib import sha1 |
| 18 | + |
| 19 | +from ..compat import ensureStringEncoded |
| 20 | + |
| 21 | +__all__ = ('XPathExpressionCache', 'XPathExpressionCacheType', ) |
| 22 | + |
| 23 | +# MAX_CACHED_EXPRESSIONS - The maximum number of cached expressions before we perform a clean-up of the cache |
| 24 | +MAX_CACHED_EXPRESSIONS = 10 |
| 25 | + |
| 26 | +# CLEAR_AT_ONE_TIME - The number of cached expressions that we clear from the cache upon exceeding #MAX_CACHED_EXPRESSIONS |
| 27 | +CLEAR_AT_ONE_TIME = 3 |
| 28 | + |
| 29 | +class XPathExpressionCacheType(object): |
| 30 | + ''' |
| 31 | + XPathExpressionCacheType - The type of the XPath Expression Cache. |
| 32 | +
|
| 33 | + This is meant to be used as a singleton, the instance being "XPathExpressionCache" |
| 34 | + ''' |
| 35 | + |
| 36 | + def __init__(self): |
| 37 | + ''' |
| 38 | + __init__ - Create this object |
| 39 | + ''' |
| 40 | + |
| 41 | + self.cachedCompiledExpressions = {} |
| 42 | + self.recentCachedExpressionStrs = [] |
| 43 | + |
| 44 | + self.cacheLock = threading.Lock() |
| 45 | + |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def getKeyForExpressionStr(expressionStr): |
| 49 | + ''' |
| 50 | + getKeyForExpressionStr - Get a unique hash "key" for a given expression str, |
| 51 | +
|
| 52 | + as will be used to cache the compiled expression. |
| 53 | +
|
| 54 | +
|
| 55 | + @param expressionStr <str/unicode/bytes> - The XPath expression str |
| 56 | +
|
| 57 | +
|
| 58 | + @return <str> - The key |
| 59 | + ''' |
| 60 | + expressionStr = ensureStringEncoded(expressionStr) |
| 61 | + |
| 62 | + return sha1(expressionStr).hexdigest() |
| 63 | + |
| 64 | + |
| 65 | + def getCachedExpression(self, expressionStr): |
| 66 | + ''' |
| 67 | + getCachedExpression - Try to get a cached XPathExpression object for a given key |
| 68 | +
|
| 69 | +
|
| 70 | + @param expressionStr <str> - The XPath expression str |
| 71 | +
|
| 72 | +
|
| 73 | + @return <XPathExpression/None> - The XPathExpression object, if one was cached, otherwise None |
| 74 | + ''' |
| 75 | + key = self.getKeyForExpressionStr(expressionStr) |
| 76 | + |
| 77 | + self.cacheLock.acquire() |
| 78 | + xpathExpressionObj = self.cachedCompiledExpressions.get(key, None) |
| 79 | + |
| 80 | + if xpathExpressionObj is None: |
| 81 | + self.cacheLock.release() |
| 82 | + return None |
| 83 | + |
| 84 | + # We got a match, mark it as hot |
| 85 | + while True: |
| 86 | + # Ensure we remove all references, if multiple got in somehow |
| 87 | + try: |
| 88 | + self.recentCachedExpressionStrs.remove(key) |
| 89 | + except ValueError: |
| 90 | + break |
| 91 | + |
| 92 | + # Add single refernce to end (hot side) of list |
| 93 | + self.recentCachedExpressionStrs.append(key) |
| 94 | + |
| 95 | + self.cacheLock.release() |
| 96 | + |
| 97 | + # And return the expression obj |
| 98 | + return xpathExpressionObj |
| 99 | + |
| 100 | + |
| 101 | + def applyCachedExpressionIfAvailable(self, expressionStr, xpathExpressionObj): |
| 102 | + ''' |
| 103 | + applyCachedExpressionIfAvailable - Check if a cached compiled expression object is available, based on the xpath expression string, |
| 104 | +
|
| 105 | + and if it is, update the expression object's members with the cached version. |
| 106 | +
|
| 107 | +
|
| 108 | + @param expressionStr <str> - The XPath expression str |
| 109 | +
|
| 110 | + @param xpathExpressionObj <xpath.expression.XPathExpression> - The expression object |
| 111 | +
|
| 112 | +
|
| 113 | + @return <bool> - True if did apply from cache, False if no match (expression needs to be compiled) |
| 114 | + ''' |
| 115 | + cachedExpression = self.getCachedExpression(expressionStr) |
| 116 | + if cachedExpression is None: |
| 117 | + return False |
| 118 | + |
| 119 | + xpathExpressionObj._copyOperationsFromXPathExpressionObj(cachedExpression) |
| 120 | + return True |
| 121 | + |
| 122 | + |
| 123 | + def setCachedExpression(self, expressionStr, xpathExpressionObj): |
| 124 | + ''' |
| 125 | + setCachedExpression - Sets the expression object to be cached under a given string |
| 126 | +
|
| 127 | +
|
| 128 | + @param expressionStr <str> - The XPath expression str |
| 129 | +
|
| 130 | + @param xpathExpressionObj <XPathExpression> - The XPathExpression object |
| 131 | + ''' |
| 132 | + key = self.getKeyForExpressionStr(expressionStr) |
| 133 | + self.cacheLock.acquire() |
| 134 | + try: |
| 135 | + while True: |
| 136 | + # Ensure we remove all references, if multiple got in somehow |
| 137 | + try: |
| 138 | + self.recentCachedExpressionStrs.remove(key) |
| 139 | + except ValueError: |
| 140 | + break |
| 141 | + |
| 142 | + self.cachedCompiledExpressions[key] = xpathExpressionObj |
| 143 | + self.recentCachedExpressionStrs.append(key) |
| 144 | + |
| 145 | + numCachedExpressionStrs = len(self.recentCachedExpressionStrs) |
| 146 | + if numCachedExpressionStrs > MAX_CACHED_EXPRESSIONS: |
| 147 | + |
| 148 | + numRemainingAfterClear = MAX_CACHED_EXPRESSIONS - CLEAR_AT_ONE_TIME |
| 149 | + |
| 150 | + # Gather and remove overflow |
| 151 | + keysToRemove = self.recentCachedExpressionStrs[ : len(self.recentCachedExpressionStrs) - numRemainingAfterClear ] |
| 152 | + for keyToRemove in keysToRemove: |
| 153 | + try: |
| 154 | + del self.cachedCompiledExpressions[keyToRemove] |
| 155 | + except: |
| 156 | + pass |
| 157 | + |
| 158 | + # Retain references to remaining |
| 159 | + self.recentCachedExpressionStrs = self.recentCachedExpressionStrs[ -1 * numRemainingAfterClear : ] |
| 160 | + |
| 161 | + except Exception as exc: |
| 162 | + self.cacheLock.release() |
| 163 | + raise exc |
| 164 | + |
| 165 | + self.cacheLock.release() |
| 166 | + |
| 167 | +# XPathExpressionCache - The singleton instance of the XPath Expression Cache. Use this instead of creating a new XPathExpressionCacheType() |
| 168 | +XPathExpressionCache = XPathExpressionCacheType() |
| 169 | + |
| 170 | + |
| 171 | +# vim: set ts=4 sw=4 st=4 expandtab : |
0 commit comments