jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/381484 )
Change subject: fix checkstyle / spotbugs warnings on tools module ...................................................................... fix checkstyle / spotbugs warnings on tools module Change-Id: I6d173267a09ded9245e3c304e66fce53ca18e4f1 --- M tools/src/main/java/org/wikidata/query/rdf/tool/CliUtils.java M tools/src/main/java/org/wikidata/query/rdf/tool/Munge.java M tools/src/main/java/org/wikidata/query/rdf/tool/Update.java M tools/src/main/java/org/wikidata/query/rdf/tool/Updater.java M tools/src/main/java/org/wikidata/query/rdf/tool/change/Change.java M tools/src/main/java/org/wikidata/query/rdf/tool/change/RecentChangesPoller.java M tools/src/main/java/org/wikidata/query/rdf/tool/rdf/Munger.java M tools/src/main/java/org/wikidata/query/rdf/tool/rdf/RdfRepository.java M tools/src/main/java/org/wikidata/query/rdf/tool/rdf/UpdateBuilder.java M tools/src/main/java/org/wikidata/query/rdf/tool/wikibase/WikibaseRepository.java M tools/src/test/java/org/wikidata/query/rdf/tool/AbstractUpdaterIntegrationTestBase.java 11 files changed, 85 insertions(+), 62 deletions(-) Approvals: Smalyshev: Looks good to me, approved jenkins-bot: Verified diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/CliUtils.java b/tools/src/main/java/org/wikidata/query/rdf/tool/CliUtils.java index 0205033..1dad22c 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/CliUtils.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/CliUtils.java @@ -1,12 +1,12 @@ package org.wikidata.query.rdf.tool; +import static java.nio.file.Files.createDirectories; +import static java.nio.file.Files.newInputStream; +import static java.nio.file.Files.newOutputStream; import static org.wikidata.query.rdf.tool.StreamUtils.utf8; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -14,10 +14,9 @@ import java.io.Reader; import java.io.Writer; import java.net.URI; +import java.nio.file.Paths; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; - -import com.google.common.io.Files; /** * Utilities for command line scripts. @@ -44,7 +43,7 @@ } InputStream stream; if (!uri.contains(":/")) { - stream = new BufferedInputStream(new FileInputStream(uri)); + stream = new BufferedInputStream(newInputStream(Paths.get(uri))); } else { stream = URI.create(uri).toURL().openStream(); } @@ -74,8 +73,8 @@ if (out.equals("-")) { return ForbiddenOk.systemDotOut(); } - Files.createParentDirs(new File(out)); - OutputStream stream = new BufferedOutputStream(new FileOutputStream(out)); + createDirectories(Paths.get(out)); + OutputStream stream = new BufferedOutputStream(newOutputStream(Paths.get(out))); if (out.endsWith(".gz")) { stream = new GZIPOutputStream(stream); } diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/Munge.java b/tools/src/main/java/org/wikidata/query/rdf/tool/Munge.java index 3e235db..85e5d13 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/Munge.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/Munge.java @@ -1,5 +1,6 @@ package org.wikidata.query.rdf.tool; +import static java.lang.Boolean.FALSE; import static org.wikidata.query.rdf.tool.options.OptionsUtils.handleOptions; import static org.wikidata.query.rdf.tool.options.OptionsUtils.mungerFromOptions; import static org.wikidata.query.rdf.tool.StreamUtils.utf8; @@ -286,6 +287,7 @@ } @Override + @SuppressFBWarnings(value = "STT_STRING_PARSING_A_FIELD", justification = "low priority to fix") public void handleStatement(Statement statement) throws RDFHandlerException { lastStatement = statement; String subject = statement.getSubject().stringValue(); @@ -499,6 +501,9 @@ } @Override + @SuppressFBWarnings( + value = "EXS_EXCEPTION_SOFTENING_NO_CHECKED", + justification = "Hiding IOException is suspicious, but seems to be the usual pattern in this project") protected Writer buildWriter(long chunk) { String file = String.format(Locale.ROOT, pattern, chunk); log.info("Switching to {}", file); @@ -526,6 +531,9 @@ } @Override + @SuppressFBWarnings( + value = "EXS_EXCEPTION_SOFTENING_NO_CHECKED", + justification = "Hiding IOException is suspicious, but seems to be the usual pattern in this project") protected Writer buildWriter(long chunk) { PipedInputStream toQueue = new PipedInputStream(); try { @@ -607,7 +615,7 @@ private void setHandlerFromLastWriter() throws RDFHandlerException { final RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, lastWriter); final WriterConfig config = writer.getWriterConfig(); - config.set(BasicWriterSettings.PRETTY_PRINT, false); + config.set(BasicWriterSettings.PRETTY_PRINT, FALSE); handler = new PrefixRecordingRdfHandler(writer, prefixes); for (Map.Entry<String, String> prefix : prefixes.entrySet()) { handler.handleNamespace(prefix.getKey(), prefix.getValue()); diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/Update.java b/tools/src/main/java/org/wikidata/query/rdf/tool/Update.java index 2776f73..9ea247e 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/Update.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/Update.java @@ -72,6 +72,8 @@ } catch (Exception e) { log.error("Error during updater run.", e); throw e; + } finally { + updater.close(); } } diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/Updater.java b/tools/src/main/java/org/wikidata/query/rdf/tool/Updater.java index 9bfa117..3957132 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/Updater.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/Updater.java @@ -41,7 +41,7 @@ */ // TODO fan out complexity @SuppressWarnings("checkstyle:classfanoutcomplexity") -public class Updater<B extends Change.Batch> implements Runnable { +public class Updater<B extends Change.Batch> implements Runnable, AutoCloseable { private static final Logger log = LoggerFactory.getLogger(Updater.class); /** @@ -163,6 +163,11 @@ } } + @Override + public void close() { + executor.shutdown(); + } + /** * Handle the changes in a batch. * diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/change/Change.java b/tools/src/main/java/org/wikidata/query/rdf/tool/change/Change.java index d8ca8d4..ce48b99 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/change/Change.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/change/Change.java @@ -17,6 +17,7 @@ /** * A change in an entity in Wikibase. */ +@SuppressFBWarnings("FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY") public class Change implements Comparable<Change> { /** * Entity that changed. @@ -106,8 +107,8 @@ b.append('@').append(revision); } if (timestamp != null) { - b.append("@").append(WikibaseRepository.outputDateFormat().format(timestamp)); - b.append("|").append(rcid); + b.append('@').append(WikibaseRepository.outputDateFormat().format(timestamp)); + b.append('|').append(rcid); } return b.toString(); } diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/change/RecentChangesPoller.java b/tools/src/main/java/org/wikidata/query/rdf/tool/change/RecentChangesPoller.java index 25f1e06..85b6208 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/change/RecentChangesPoller.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/change/RecentChangesPoller.java @@ -28,6 +28,7 @@ * of the previous poll, or, if there isn't a continue, then they start one * second after the last first start time. */ +@SuppressFBWarnings("FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY") public class RecentChangesPoller implements Change.Source<RecentChangesPoller.Batch> { private static final Logger log = LoggerFactory.getLogger(RecentChangesPoller.class); @@ -183,7 +184,7 @@ /** * Batch implementation for this poller. */ - public final class Batch extends Change.Batch.AbstractDefaultImplementation { + public static final class Batch extends Change.Batch.AbstractDefaultImplementation { /** * The date where we last left off. */ @@ -241,7 +242,7 @@ public String leftOffHuman() { if (lastContinue != null) { return WikibaseRepository.inputDateFormat().format(leftOffDate) - + " (next: " + lastContinue.get("rccontinue").toString() + ")"; + + " (next: " + lastContinue.get("rccontinue") + ")"; } else { return WikibaseRepository.inputDateFormat().format(leftOffDate); } @@ -252,6 +253,7 @@ * @param another * @return */ + @SuppressFBWarnings(value = "OCP_OVERLY_CONCRETE_PARAMETER", justification = "Type seems semantically correct") public Batch merge(Batch another) { final ImmutableList<Change> newChanges = new ImmutableList.Builder<Change>() .addAll(another.changes()) diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/Munger.java b/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/Munger.java index aca9281..180b533 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/Munger.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/Munger.java @@ -51,6 +51,8 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.ListMultimap; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + /** * Munges RDF from Wikibase into a more queryable format. Note that this is * tightly coupled with Wikibase's export format. @@ -194,7 +196,7 @@ * @param version Version to handle. * @param handler Handler. */ - public void addFormatHandler(String version, FormatHandler handler) { + public final void addFormatHandler(String version, FormatHandler handler) { formatHandlers.put(version, handler); } @@ -876,6 +878,7 @@ /** * Fetch the object from the current statement as a Literal. */ + @SuppressFBWarnings(value = "LEST_LOST_EXCEPTION_STACK_TRACE", justification = "Cause is really not needed here.") private Literal objectAsLiteral() { try { return (Literal) statement.getObject(); diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/RdfRepository.java b/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/RdfRepository.java index eb621aa..05e6994 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/RdfRepository.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/RdfRepository.java @@ -285,7 +285,7 @@ try { return Resources.toString(url, Charsets.UTF_8); } catch (IOException e) { - throw new FatalException("Can't load " + url); + throw new FatalException("Can't load " + url, e); } } @@ -419,7 +419,7 @@ * @return Number of triples modified. */ public int syncFromChanges(Collection<Change> changes, boolean verifyResult) { - if (changes.size() == 0) { + if (changes.isEmpty()) { // no changes, we're done return 0; } @@ -432,7 +432,7 @@ List<Statement> insertStatements = new ArrayList<>(); List<Statement> entityStatements = new ArrayList<>(); - Set<String> valueList = new HashSet<>(); + Set<String> valueSet = new HashSet<>(); for (final Change change : changes) { if (change.getStatements() == null) { @@ -442,7 +442,7 @@ entityIds.add(change.entityId()); insertStatements.addAll(change.getStatements()); entityStatements.addAll(filtered(change.getStatements()).withSubject(uris.entity() + change.entityId())); - valueList.addAll(change.getCleanupList()); + valueSet.addAll(change.getCleanupList()); } if (entityIds.isEmpty()) { @@ -465,9 +465,9 @@ aboutStatements.removeAll(filtered(insertStatements).withSubjectStarts(uris.reference())); b.bindValues("aboutStatements", aboutStatements); - if (!valueList.isEmpty()) { + if (!valueSet.isEmpty()) { UpdateBuilder cleanup = new UpdateBuilder(cleanUnused); - cleanup.bindUris("values", valueList); + cleanup.bindUris("values", valueSet); b.bind("cleanupQuery", cleanup.toString()); } else { b.bind("cleanupQuery", ""); @@ -578,7 +578,8 @@ UpdateBuilder b = new UpdateBuilder(getRevisions); StringBuilder values = new StringBuilder(); for (Change entry: candidates) { - values.append("( <" + uris.entity() + entry.entityId() + "> " + entry.revision() + " )\n"); + values.append("( <").append(uris.entity()).append(entry.entityId()).append("> ") + .append(entry.revision()).append(" )\n"); } b.bind("values", values.toString()); b.bindUri("schema:version", SchemaDotOrg.VERSION); @@ -603,6 +604,7 @@ * * @return the date or null if we have nowhere to start from */ + @SuppressFBWarnings(value = "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS", justification = "prefix() is called with different StringBuilders") public Date fetchLeftOffTime() { log.info("Checking for left off time from the updater"); StringBuilder b = SchemaDotOrg.prefix(new StringBuilder()); @@ -816,6 +818,7 @@ } @Override + @SuppressFBWarnings(value = "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS", justification = "more readable with 2 calls") public Integer parse(ContentResponse entity) throws IOException { Integer mutationCount = null; for (String line : entity.getContentAsString().split("\\r?\\n")) { diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/UpdateBuilder.java b/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/UpdateBuilder.java index 88e1d7f..c37ca00 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/UpdateBuilder.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/rdf/UpdateBuilder.java @@ -140,7 +140,7 @@ } else if (!l.getDatatype().equals(XMLSchema.STRING)) { sb.append("^^<"); sb.append(l.getDatatype()); - sb.append(">"); + sb.append('>'); } return sb.toString(); diff --git a/tools/src/main/java/org/wikidata/query/rdf/tool/wikibase/WikibaseRepository.java b/tools/src/main/java/org/wikidata/query/rdf/tool/wikibase/WikibaseRepository.java index c72c068..81b15be 100644 --- a/tools/src/main/java/org/wikidata/query/rdf/tool/wikibase/WikibaseRepository.java +++ b/tools/src/main/java/org/wikidata/query/rdf/tool/wikibase/WikibaseRepository.java @@ -146,45 +146,39 @@ * @return */ private static HttpRequestRetryHandler getRetryHandler(final int max) { - HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { - @Override - public boolean retryRequest(IOException exception, int executionCount, - HttpContext context) { - log.debug("Exception: {} in attempt {}", exception, executionCount); - if (executionCount >= max) { - // Do not retry if over max retry count - return false; - } - if (exception instanceof InterruptedIOException) { - // Timeout - return true; - } - if (exception instanceof UnknownHostException) { - // Unknown host - return false; - } - if (exception instanceof ConnectTimeoutException) { - // Connection refused - return true; - } - if (exception instanceof SSLException) { - // SSL handshake exception - return false; - } - - HttpClientContext clientContext = HttpClientContext.adapt(context); - HttpRequest request = clientContext.getRequest(); - boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); - if (idempotent) { - // Retry if the request is considered idempotent - return true; - } - + return (exception, executionCount, context) -> { + log.debug("Exception in attempt {}", executionCount, exception); + if (executionCount >= max) { + // Do not retry if over max retry count + return false; + } + if (exception instanceof InterruptedIOException) { + // Timeout + return true; + } + if (exception instanceof UnknownHostException) { + // Unknown host + return false; + } + if (exception instanceof ConnectTimeoutException) { + // Connection refused + return true; + } + if (exception instanceof SSLException) { + // SSL handshake exception return false; } + HttpClientContext clientContext = HttpClientContext.adapt(context); + HttpRequest request = clientContext.getRequest(); + boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); + if (idempotent) { + // Retry if the request is considered idempotent + return true; + } + + return false; }; - return myRetryHandler; } /** @@ -473,7 +467,7 @@ * @param continueObject Continue object from the last request * @param batchSize maximum number of results we want back from wikibase */ - public URI recentChanges(Date startTime, JSONObject continueObject, int batchSize) { + public URI recentChanges(Date startTime, Map continueObject, int batchSize) { URIBuilder builder = apiBuilder(); builder.addParameter("action", "query"); builder.addParameter("list", "recentchanges"); @@ -649,6 +643,11 @@ /** * Convert a DateFormat to always output in utc. + * + * Note that the DateFormat passed as a parameter is modified AND returned. + * + * @param df the DateFormat to modify + * @return the DateFormat that was passed as a parameter (useful for method chaining) */ private static DateFormat utc(DateFormat df) { df.setTimeZone(TimeZone.getTimeZone("UTC")); @@ -674,6 +673,7 @@ * @return Timestamp as date * @throws java.text.ParseException When data is in is wrong format */ + @SuppressFBWarnings(value = "STT_STRING_PARSING_A_FIELD", justification = "low priority to fix") public Change getChangeFromContinue(Map<String, Object> nextContinue) throws java.text.ParseException { if (nextContinue == null) { return null; diff --git a/tools/src/test/java/org/wikidata/query/rdf/tool/AbstractUpdaterIntegrationTestBase.java b/tools/src/test/java/org/wikidata/query/rdf/tool/AbstractUpdaterIntegrationTestBase.java index 9e5189a..331de1d 100644 --- a/tools/src/test/java/org/wikidata/query/rdf/tool/AbstractUpdaterIntegrationTestBase.java +++ b/tools/src/test/java/org/wikidata/query/rdf/tool/AbstractUpdaterIntegrationTestBase.java @@ -32,9 +32,9 @@ ExecutorService executorService = new ThreadPoolExecutor(0, 10, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); WikibaseUris uris = new WikibaseUris("www.wikidata.org"); - Updater<?> updater = new Updater<>(source, wikibaseRepository, rdfRepository(), munger, executorService, 0, uris, false); - updater.run(); - executorService.shutdown(); + try (Updater<?> updater = new Updater<>(source, wikibaseRepository, rdfRepository(), munger, executorService, 0, uris, false)) { + updater.run(); + } } /** -- To view, visit https://gerrit.wikimedia.org/r/381484 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I6d173267a09ded9245e3c304e66fce53ca18e4f1 Gerrit-PatchSet: 7 Gerrit-Project: wikidata/query/rdf Gerrit-Branch: master Gerrit-Owner: Gehel <guillaume.leder...@wikimedia.org> Gerrit-Reviewer: Gehel <guillaume.leder...@wikimedia.org> Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits