Attempts to load the file from disk
()
| 194 | * Attempts to load the file from disk |
| 195 | */ |
| 196 | private void loadFromFile() { |
| 197 | // load from disk if the caller gave us a file |
| 198 | if (file_location != null && !file_location.isEmpty()) { |
| 199 | final File file = new File(file_location); |
| 200 | if (!file.exists()) { |
| 201 | LOG.warn("Query override file " + file_location + " does not exist"); |
| 202 | return; |
| 203 | } |
| 204 | try { |
| 205 | final String raw_json = Files.toString(file, Const.UTF8_CHARSET); |
| 206 | if (raw_json != null && !raw_json.isEmpty()) { |
| 207 | final Set<QueryLimitOverrideItem> cached_items = |
| 208 | JSON.parseToObject(raw_json, TR_OVERRIDES); |
| 209 | |
| 210 | // iterate so we only change bits that are different. |
| 211 | for (final QueryLimitOverrideItem override : cached_items) { |
| 212 | QueryLimitOverrideItem existing = overrides.get(override.getRegex()); |
| 213 | if (existing == null || !existing.equals(override)) { |
| 214 | overrides.put(override.getRegex(), override); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // reverse quadratic, woot! Ugly but if the limit file is so big that |
| 219 | // this takes over 60 seconds or starts blocking queries on modifications |
| 220 | // to the map then something is really wrong. |
| 221 | final Iterator<Entry<String, QueryLimitOverrideItem>> iterator = |
| 222 | overrides.entrySet().iterator(); |
| 223 | while (iterator.hasNext()) { |
| 224 | final Entry<String, QueryLimitOverrideItem> entry = iterator.next(); |
| 225 | boolean matched = false; |
| 226 | for (final QueryLimitOverrideItem override : cached_items) { |
| 227 | if (override.getRegex().equals(entry.getKey())) { |
| 228 | matched = true; |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | if (!matched) { |
| 233 | iterator.remove(); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | LOG.info("Successfully loaded query overrides: " + this); |
| 238 | } catch (Exception e) { |
| 239 | LOG.error("Failed to read cache file for query limit override: " + |
| 240 | this, e); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** A simple class for ser/des of the items along with some validation */ |
| 246 | public static class QueryLimitOverrideItem { |