You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ma...@apache.org on 2020/09/06 23:59:01 UTC

[lucene-solr] branch reference_impl updated: @766 More thread safe.

This is an automated email from the ASF dual-hosted git repository.

markrmiller pushed a commit to branch reference_impl
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/reference_impl by this push:
     new 7a1e118  @766 More thread safe.
7a1e118 is described below

commit 7a1e1187706a1ce27689d7a851b570b0ca218e3f
Author: markrmiller@gmail.com <ma...@gmail.com>
AuthorDate: Sun Sep 6 18:58:13 2020 -0500

    @766 More thread safe.
---
 .../apache/lucene/search/suggest/tst/TSTLookup.java    | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java
index 7948f44..e4896ed 100644
--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java
+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java
@@ -21,6 +21,8 @@ import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.LongAccumulator;
 
 import org.apache.lucene.search.suggest.InputIterator;
 import org.apache.lucene.search.suggest.Lookup;
@@ -39,11 +41,11 @@ import org.apache.lucene.util.RamUsageEstimator;
  * @see TSTAutocomplete
  */
 public class TSTLookup extends Lookup {
-  TernaryTreeNode root = new TernaryTreeNode();
-  TSTAutocomplete autocomplete = new TSTAutocomplete();
+  volatile TernaryTreeNode root = new TernaryTreeNode();
+  final TSTAutocomplete autocomplete = new TSTAutocomplete();
 
   /** Number of entries the lookup was built with */
-  private long count = 0;
+  private AtomicLong count = new AtomicLong();
 
   private final Directory tempDir;
   private final String tempFileNamePrefix;
@@ -120,7 +122,7 @@ public class TSTLookup extends Lookup {
 
     // make sure it's sorted and the comparator uses UTF16 sort order
     iterator = new SortedInputIterator(tempDir, tempFileNamePrefix, iterator, utf8SortedAsUTF16SortOrder);
-    count = 0;
+    count.set(0);
     ArrayList<String> tokens = new ArrayList<>();
     ArrayList<Number> vals = new ArrayList<>();
     BytesRef spare;
@@ -129,7 +131,7 @@ public class TSTLookup extends Lookup {
       charsSpare.copyUTF8Bytes(spare);
       tokens.add(charsSpare.toString());
       vals.add(Long.valueOf(iterator.weight()));
-      count++;
+      count.incrementAndGet();
     }
     autocomplete.balancedTree(tokens.toArray(), vals.toArray(), 0, tokens.size() - 1, root);
   }
@@ -263,14 +265,14 @@ public class TSTLookup extends Lookup {
 
   @Override
   public synchronized boolean store(DataOutput output) throws IOException {
-    output.writeVLong(count);
+    output.writeVLong(count.incrementAndGet());
     writeRecursively(output, root);
     return true;
   }
 
   @Override
   public synchronized boolean load(DataInput input) throws IOException {
-    count = input.readVLong();
+    count.set(input.readVLong());
     root = new TernaryTreeNode();
     readRecursively(input, root);
     return true;
@@ -288,6 +290,6 @@ public class TSTLookup extends Lookup {
   
   @Override
   public long getCount() {
-    return count;
+    return count.get();
   }
 }