From e072fbf2dbaeb86177d775b1b4877daf80521a62 Mon Sep 17 00:00:00 2001 From: MartinWitt Date: Sat, 11 Mar 2023 15:31:37 +0000 Subject: [PATCH 1/3] Refactor bad smells: - ToArrayCallWithZeroLengthArrayArgument The performance of the empty array version is the same, and sometimes even better, compared to the pre-sized version. Also, passing a pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray calls. This may result in extra nulls at the end of the array if the collection was concurrently shrunk during the operation.

See https://shipilev.net/blog/2016/arrays-wisdom-ancients/ for more details. --- .../org/apache/gora/memory/store/MemStore.java | 2 +- .../java/org/apache/gora/util/StringUtils.java | 2 +- .../org/apache/gora/jcache/store/JCacheStore.java | 2 +- .../org/apache/gora/lucene/store/LuceneStore.java | 2 +- .../apache/gora/maven/plugin/AbstractGoraMojo.java | 2 +- .../gora/orientdb/store/OrientDBMapping.java | 2 +- .../gora/rethinkdb/store/RethinkDBMapping.java | 2 +- .../java/org/apache/gora/solr/store/SolrStore.java | 14 +++++++++++--- 8 files changed, 18 insertions(+), 10 deletions(-) diff --git a/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java b/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java index 151e7e7c6..50804519a 100644 --- a/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java +++ b/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java @@ -133,7 +133,7 @@ public long deleteByQuery(Query query) { excludedFields.add(field); } } - T newClonedObj = getPersistent(result.get(),excludedFields.toArray(new String[excludedFields.size()])); + T newClonedObj = getPersistent(result.get(),excludedFields.toArray(new String[0])); if (delete(result.getKey())) { put(result.getKey(),newClonedObj); deletedRows++; diff --git a/gora-core/src/main/java/org/apache/gora/util/StringUtils.java b/gora-core/src/main/java/org/apache/gora/util/StringUtils.java index 57e3ff864..07a822cde 100644 --- a/gora-core/src/main/java/org/apache/gora/util/StringUtils.java +++ b/gora-core/src/main/java/org/apache/gora/util/StringUtils.java @@ -42,7 +42,7 @@ public static String[] joinStringArrays(String[] arr1, String... arr2) { Collections.addAll(set, arr1); Collections.addAll(set, arr2); - return set.toArray(new String[set.size()]); + return set.toArray(new String[0]); } public static String join(List strs) { diff --git a/gora-jcache/src/main/java/org/apache/gora/jcache/store/JCacheStore.java b/gora-jcache/src/main/java/org/apache/gora/jcache/store/JCacheStore.java index f963dd81f..814722544 100644 --- a/gora-jcache/src/main/java/org/apache/gora/jcache/store/JCacheStore.java +++ b/gora-jcache/src/main/java/org/apache/gora/jcache/store/JCacheStore.java @@ -325,7 +325,7 @@ public long deleteByQuery(Query query) throws GoraException { } } T newClonedObj = getPersistent(result.get(), - excludedFields.toArray(new String[excludedFields.size()])); + excludedFields.toArray(new String[0])); if (delete(result.getKey())) { put(result.getKey(), newClonedObj); deletedRows++; diff --git a/gora-lucene/src/main/java/org/apache/gora/lucene/store/LuceneStore.java b/gora-lucene/src/main/java/org/apache/gora/lucene/store/LuceneStore.java index 73fb34030..72a7c20c7 100644 --- a/gora-lucene/src/main/java/org/apache/gora/lucene/store/LuceneStore.java +++ b/gora-lucene/src/main/java/org/apache/gora/lucene/store/LuceneStore.java @@ -405,7 +405,7 @@ private Object convertToIndexableFieldToAvroField(final Document doc, public T newInstance(Document doc, String[] fields) throws IOException { T persistent = newPersistent(); if (fields == null) { - fields = fieldMap.keySet().toArray(new String[fieldMap.size()]); + fields = fieldMap.keySet().toArray(new String[0]); } String pk = mapping.getPrimaryKey(); diff --git a/gora-maven-plugin/src/main/java/org/apache/gora/maven/plugin/AbstractGoraMojo.java b/gora-maven-plugin/src/main/java/org/apache/gora/maven/plugin/AbstractGoraMojo.java index 17cf9aa8c..4baea4519 100644 --- a/gora-maven-plugin/src/main/java/org/apache/gora/maven/plugin/AbstractGoraMojo.java +++ b/gora-maven-plugin/src/main/java/org/apache/gora/maven/plugin/AbstractGoraMojo.java @@ -89,7 +89,7 @@ protected void compile() throws IOException { } if (!changedFiles.isEmpty()) { try { - File[] schemaFile = changedFiles.toArray(new File[changedFiles.size()]); + File[] schemaFile = changedFiles.toArray(new File[0]); GoraCompiler.compileSchema(schemaFile, outputDirectory); } catch (SchemaParseException e) { if (e.getCause() != null && e.getCause() instanceof JsonParseException) { diff --git a/gora-orientdb/src/main/java/org/apache/gora/orientdb/store/OrientDBMapping.java b/gora-orientdb/src/main/java/org/apache/gora/orientdb/store/OrientDBMapping.java index ac06f0844..ef89b1f58 100644 --- a/gora-orientdb/src/main/java/org/apache/gora/orientdb/store/OrientDBMapping.java +++ b/gora-orientdb/src/main/java/org/apache/gora/orientdb/store/OrientDBMapping.java @@ -117,7 +117,7 @@ public void registerClassField(String classFieldName, * @return array of fields in string. */ public String[] getDocumentFields() { - return documentToClass.keySet().toArray(new String[documentToClass.keySet().size()]); + return documentToClass.keySet().toArray(new String[0]); } /** diff --git a/gora-rethinkdb/src/main/java/org/apache/gora/rethinkdb/store/RethinkDBMapping.java b/gora-rethinkdb/src/main/java/org/apache/gora/rethinkdb/store/RethinkDBMapping.java index 8e41d0c81..cb2d1a343 100644 --- a/gora-rethinkdb/src/main/java/org/apache/gora/rethinkdb/store/RethinkDBMapping.java +++ b/gora-rethinkdb/src/main/java/org/apache/gora/rethinkdb/store/RethinkDBMapping.java @@ -77,7 +77,7 @@ public void registerClassField(String classFieldName, } public String[] getDocumentFields() { - return documentToClass.keySet().toArray(new String[documentToClass.keySet().size()]); + return documentToClass.keySet().toArray(new String[0]); } public String getDocumentField(String field) { diff --git a/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java b/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java index 9a82d609a..50d98927a 100644 --- a/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java +++ b/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java @@ -154,9 +154,17 @@ public class SolrStore extends DataStoreBase private SolrMapping mapping; - private String SolrClientUrl, solrConfig, solrSchema, solrJServerImpl; + private String SolrClientUrl; - private SolrClient server, adminServer; + private String solrConfig; + + private String solrSchema; + + private String solrJServerImpl; + + private SolrClient server; + + private SolrClient adminServer; private boolean serverUserAuth; @@ -579,7 +587,7 @@ public T get(K key, String[] fields) throws GoraException { public T newInstance(SolrDocument doc, String[] fields) throws IOException { T persistent = newPersistent(); if (fields == null) { - fields = fieldMap.keySet().toArray(new String[fieldMap.size()]); + fields = fieldMap.keySet().toArray(new String[0]); } String pk = mapping.getPrimaryKey(); for (String f : fields) { From 1fa2d5c573f3e09bd9d2114400c94ce6ab4dffda Mon Sep 17 00:00:00 2001 From: MartinWitt Date: Sat, 11 Mar 2023 16:33:53 +0100 Subject: [PATCH 2/3] Update SolrStore.java --- .../java/org/apache/gora/solr/store/SolrStore.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java b/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java index 50d98927a..26c3a7262 100644 --- a/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java +++ b/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java @@ -154,17 +154,11 @@ public class SolrStore extends DataStoreBase private SolrMapping mapping; - private String SolrClientUrl; + private String SolrClientUrl, solrConfig, solrSchema, solrJServerImpl; - private String solrConfig; - private String solrSchema; - private String solrJServerImpl; - - private SolrClient server; - - private SolrClient adminServer; + private SolrClient server, adminServer; private boolean serverUserAuth; From 0a993ce065c808fb79c1748b52d0c0cb7828aa89 Mon Sep 17 00:00:00 2001 From: MartinWitt Date: Sat, 11 Mar 2023 16:34:06 +0100 Subject: [PATCH 3/3] Update SolrStore.java --- .../src/main/java/org/apache/gora/solr/store/SolrStore.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java b/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java index 26c3a7262..7a6fe81c3 100644 --- a/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java +++ b/gora-solr/src/main/java/org/apache/gora/solr/store/SolrStore.java @@ -156,8 +156,6 @@ public class SolrStore extends DataStoreBase private String SolrClientUrl, solrConfig, solrSchema, solrJServerImpl; - - private SolrClient server, adminServer; private boolean serverUserAuth;