epotyom commented on code in PR #13568:
URL: https://github.com/apache/lucene/pull/13568#discussion_r1693218171


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/misc/LongValueFacetCutter.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.sandbox.facet.misc;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.lucene.facet.taxonomy.FacetLabel;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.internal.hppc.IntLongHashMap;
+import org.apache.lucene.internal.hppc.LongCursor;
+import org.apache.lucene.internal.hppc.LongHashSet;
+import org.apache.lucene.internal.hppc.LongIntHashMap;
+import org.apache.lucene.sandbox.facet.cutters.FacetCutter;
+import org.apache.lucene.sandbox.facet.cutters.LeafFacetCutter;
+import org.apache.lucene.sandbox.facet.labels.OrdLabelBiMap;
+
+/**
+ * {@link FacetCutter} and {@link OrdLabelBiMap} for distinct long values.
+ *
+ * <p>TODO: This class is quite inefficient. Will optimise later. TODO: add 
support for other value
+ * sources e.g: LongValues
+ */
+public class LongValueFacetCutter implements FacetCutter, OrdLabelBiMap {
+  private final String field;
+  // TODO: consider alternatives if this is a bottleneck
+  private final LongIntHashMap valueToOrdMap;
+  private final IntLongHashMap ordToValueMap;
+  private final AtomicInteger maxOrdinal;
+
+  /**
+   * Constructor.
+   *
+   * @param field field name to read long values from.
+   */
+  public LongValueFacetCutter(String field) {
+    this.field = field;
+    valueToOrdMap =
+        new LongIntHashMap() {
+          @Override
+          public synchronized boolean putIfAbsent(long key, int value) {
+            return super.putIfAbsent(key, value);
+          }
+        };
+    ordToValueMap = new IntLongHashMap();
+    maxOrdinal = new AtomicInteger(-1);
+  }
+
+  @Override
+  public LeafFacetCutter createLeafCutter(LeafReaderContext context) throws 
IOException {
+    SortedNumericDocValues docValues = 
DocValues.getSortedNumeric(context.reader(), field);
+    return new LeafFacetCutter() {
+      int currDoc = -1;
+      final LongHashSet valuesForDoc = new LongHashSet();
+      private Iterator<LongCursor> valuesCursor;
+
+      @Override
+      public boolean advanceExact(int doc) throws IOException {
+        if (doc < currDoc) {
+          return false;
+        }
+        if (doc == currDoc) {
+          return true;
+        }
+        valuesForDoc.clear();
+        if (docValues.advanceExact(doc)) {
+          int numValues = docValues.docValueCount();
+          for (int i = 0; i < numValues; i++) {
+            long value = docValues.nextValue();
+            valueToOrdMap.putIfAbsent(value, maxOrdinal.incrementAndGet());

Review Comment:
   Oh, I think this class also has to do some operations atomically, e.g. 
updates valueToOrd and ordToValue maps. I'll fix that. Hmm, other option is 
that we don't need the second map until the first time try to read from it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to