MCPcopy
hub / github.com/OpenTSDB/opentsdb / loadFromFile

Method loadFromFile

src/query/QueryLimitOverride.java:196–243  ·  view source on GitHub ↗

Attempts to load the file from disk

()

Source from the content-addressed store, hash-verified

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 {

Callers 2

QueryLimitOverrideMethod · 0.95
runMethod · 0.95

Calls 13

parseToObjectMethod · 0.95
equalsMethod · 0.95
isEmptyMethod · 0.80
putMethod · 0.80
errorMethod · 0.80
iteratorMethod · 0.65
hasNextMethod · 0.65
nextMethod · 0.65
removeMethod · 0.65
toStringMethod · 0.45
getMethod · 0.45
getRegexMethod · 0.45

Tested by

no test coverage detected