Skip to content

Commit

Permalink
Cleanup and corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Aug 20, 2024
1 parent c1c2ceb commit 2b9aeda
Showing 1 changed file with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public NamedList<Object> request(SolrRequest<?> request, String collection)
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

if (Objects.isNull(requestAttributes)) {
logger.warn("Unable to cache without originating request. RequestContextHolder.getRequestAttributes() is {}");
logger.warn("Unable to cache without originating request. RequestContextHolder.getRequestAttributes() is {}", requestAttributes);
return client.request(request, collection);
}

Expand All @@ -140,6 +140,7 @@ public NamedList<Object> request(SolrRequest<?> request, String collection)
String key = String.format("%s%s/lookup_table", StringUtils.removeEnd(index.getCacheLocation(), FORWARD_SLASH), cachePath);

File lookupFile = getOrCreateLookupFile(key);

Map<String, String> innerMap = getOrCreateInnerMapForLookupFile(key, lookupFile);

long start = System.currentTimeMillis();
Expand Down Expand Up @@ -171,25 +172,23 @@ public NamedList<Object> request(SolrRequest<?> request, String collection)

File file = new File(filename);

long startFoundCache, startQueryToSolr;

if (file.exists()) { // cache response branch
startFoundCache = System.currentTimeMillis();
long startTime = System.currentTimeMillis();

try {
response = JavaObjectStorageFileUtility.readObject(filename);
} catch (ClassNotFoundException | IOException e) {
throw new RuntimeException(String.format("%s:%s", e.getClass(), e.getMessage(), e));
throw new RuntimeException(String.format("%s:%s", e.getClass(), e.getMessage()), e);
}

logger.info("{}:{}: {} seconds", uuid, "RESPONSE CACHED READ", (System.currentTimeMillis() - startFoundCache) / (double) 1000);
logger.info("{}:{}: {} seconds", uuid, "RESPONSE CACHED READ", (System.currentTimeMillis() - startTime) / (double) 1000);
} else { // actual response branch
startQueryToSolr = System.currentTimeMillis();
long startTime = System.currentTimeMillis();
response = client.request(request, collection);

JavaObjectStorageFileUtility.writeObject(response, filename);

logger.info("{}:{}: {} seconds", uuid, "QUERY RESPONSE", (System.currentTimeMillis() - startQueryToSolr) / (double) 1000);
logger.info("{}:{}: {} seconds", uuid, "QUERY RESPONSE", (System.currentTimeMillis() - startTime) / (double) 1000);

long startUdateLookupTable = System.currentTimeMillis();

Expand All @@ -205,6 +204,12 @@ public NamedList<Object> request(SolrRequest<?> request, String collection)
return response;
}

/**
* Get lookup file by key or create lookup file and place in map.
*
* @param key
* @return lookup file
*/
private synchronized File getOrCreateLookupFile(String key) {
File lookupFile = this.lookup.get(key);

Expand All @@ -216,7 +221,15 @@ private synchronized File getOrCreateLookupFile(String key) {
return lookupFile;
}

private synchronized Map<String, String> getOrCreateInnerMapForLookupFile(String key, File lookupFile) throws StreamReadException, DatabindException, IOException {
/**
* Get lookup file by key or read lookup file provided is exists and is not a directory/
*
* @param key
* @param lookupFile lookup file or request cache
* @return inner map for cache
* @throws IOException if unable to read and map file
*/
private synchronized Map<String, String> getOrCreateInnerMapForLookupFile(String key, File lookupFile) throws IOException {
Map<String, String> innerMap = this.map.get(key);

if (innerMap == null) {
Expand All @@ -229,14 +242,13 @@ private synchronized Map<String, String> getOrCreateInnerMapForLookupFile(String
return innerMap;
}

/**
* Convert the request to an ObjectNode with at most knowledge of SolrRequest at this time.
*
* @param request SolrRequest
* @return ObjectNode
* @throws JsonMappingException when unable to map request parameters to JSON
* @throws JsonProcessingException when unable to deserialize JSON parameters of the SolrRequest
*/
/**
* Convert the request to an ObjectNode with at most knowledge of SolrRequest at this time.
*
* @param request SolrRequest
* @return ObjectNode for SolrRequest
* @throws JsonProcessingException when unable to deserialize JSON parameters of the SolrRequest
*/
private ObjectNode requestToObjectNode(SolrRequest<?> request) throws JsonProcessingException {
ObjectNode rootNode = objectMapper.createObjectNode();

Expand Down Expand Up @@ -288,10 +300,10 @@ private ObjectNode requestToObjectNode(SolrRequest<?> request) throws JsonProces
}

/**
* Convert the ObjectNode to a Map<String, Object>.
* Convert the ObjectNode to a Map<String, Object> without null values.
*
* @param rootNode ObjectNode from the SolrRequest
* @return claims without nulls
* @return claims without null values
*/
private Map<String, Object> requestObjectToClaims(ObjectNode rootNode) {
return ClaimBuilder
Expand Down

0 comments on commit 2b9aeda

Please sign in to comment.