From 58bf0cf45cb1b1404ee464c5a82aea745a0f7f16 Mon Sep 17 00:00:00 2001 From: Adam Miller Date: Fri, 28 May 2021 23:10:11 +0000 Subject: [PATCH] Refactor Trough client URL cache put. Limit TroughContentDigestHistory query. --- .../recrawl/TroughContentDigestHistory.java | 2 +- .../java/org/archive/trough/TroughClient.java | 33 ++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/contrib/src/main/java/org/archive/modules/recrawl/TroughContentDigestHistory.java b/contrib/src/main/java/org/archive/modules/recrawl/TroughContentDigestHistory.java index 424b9780..f8b3cc1b 100644 --- a/contrib/src/main/java/org/archive/modules/recrawl/TroughContentDigestHistory.java +++ b/contrib/src/main/java/org/archive/modules/recrawl/TroughContentDigestHistory.java @@ -197,7 +197,7 @@ public class TroughContentDigestHistory extends AbstractContentDigestHistory imp } if(!memoryDedupHit) { try { - String sql = "select * from dedup where digest_key = %s"; + String sql = "select * from dedup where digest_key = %s limit 1"; List> results = troughClient().read(getSegmentId(), sql, new String[]{persistKeyFor(curi)}); if (!results.isEmpty()) { Map hist = new HashMap(); diff --git a/contrib/src/main/java/org/archive/trough/TroughClient.java b/contrib/src/main/java/org/archive/trough/TroughClient.java index 15d3e58d..726f3802 100644 --- a/contrib/src/main/java/org/archive/trough/TroughClient.java +++ b/contrib/src/main/java/org/archive/trough/TroughClient.java @@ -318,8 +318,20 @@ public class TroughClient { protected String readUrl(String segmentId) throws TroughException { if (readUrlCache.get(segmentId) == null) { - String url = readUrlNoCache(segmentId); - readUrlCache.put(segmentId, url); + try { + readUrlCache.computeIfAbsent(segmentId, k -> { + try { + return readUrlNoCache(k); + } catch (TroughException e) { + throw new RuntimeException(e); + } + }); + } + catch(RuntimeException e) { + if(e.getCause() instanceof TroughException){ + throw (TroughException)e.getCause(); + } + } logger.info("segment " + segmentId + " read url is " + readUrlCache.get(segmentId)); } return readUrlCache.get(segmentId); @@ -379,9 +391,20 @@ public class TroughClient { protected String writeUrl(String segmentId, String schemaId) throws IOException { if (writeUrlCache.get(segmentId) == null) { - String url = writeUrlNoCache(segmentId, schemaId); - writeUrlCache.put(segmentId, url); - + try { + writeUrlCache.computeIfAbsent(segmentId, k -> { + try { + return writeUrlNoCache(k, schemaId); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + catch(RuntimeException e) { + if(e.getCause() instanceof IOException){ + throw (IOException)e.getCause(); + } + } logger.info("segment " + segmentId + " write url is " + writeUrlCache.get(segmentId)); } return writeUrlCache.get(segmentId);