/** * Construct the cache key. * Current implementation includes all the fields of the druidQuery besides the context. * * @param druidQuery The druid query. * * @return The cache key as a String. * @throws JsonProcessingException if the druid query cannot be mapped to Json */ protected String getKey(DruidAggregationQuery<?> druidQuery) throws JsonProcessingException { JsonNode root = mapper.valueToTree(druidQuery); Utils.canonicalize(root, mapper, false); return writer.writeValueAsString(root); } }
/** * Construct the cache key. * Current implementation includes all the fields of the druidQuery besides the context. * * @param druidQuery The druid query. * * @return The cache key as a String. * @throws JsonProcessingException if the druid query cannot be serialized to JSON */ protected String getKey(DruidAggregationQuery<?> druidQuery) throws JsonProcessingException { JsonNode root = mapper.valueToTree(druidQuery); Utils.canonicalize(root , mapper, false); return writer.writeValueAsString(root); } }
/** * Construct the cache key. * Current implementation includes all the fields of the druidQuery besides the context. * * @param druidQuery The druid query. * * @return The cache key as a String. * @throws JsonProcessingException if the druid query cannot be serialized to JSON */ private String getKey(DruidAggregationQuery<?> druidQuery) throws JsonProcessingException { JsonNode root = mapper.valueToTree(druidQuery); Utils.canonicalize(root, mapper, false); return writer.writeValueAsString(root); } }
/** * Given a JsonObjectNode, order the fields and recursively and replace context blocks with empty nodes. * * This method is recursive. * * @param node The root of the tree of json nodes. * @param mapper The object mapper that creates and empty node. * @param preserveContext Boolean indicating whether context should be omitted. */ public static void canonicalize(JsonNode node, ObjectMapper mapper, boolean preserveContext) { if (node.isObject()) { ObjectNode objectNode = ((ObjectNode) node); if (objectNode.has("context") && !preserveContext) { objectNode.replace("context", mapper.createObjectNode()); } Iterator<Map.Entry<String, JsonNode>> iterator = objectNode.fields(); // collect and sort the entries TreeMap<String, JsonNode> fieldMap = new TreeMap<>(); while (iterator.hasNext()) { Map.Entry<String, JsonNode> entry = iterator.next(); fieldMap.put(entry.getKey(), entry.getValue()); // canonicalize all child nodes canonicalize(entry.getValue(), mapper, preserveContext); } // remove the existing entries objectNode.removeAll(); // replace the entries in sorted order objectNode.setAll(fieldMap); } }