geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ash...@apache.org
Subject [1/3] incubator-geode git commit: GEODE-11: Add classes to manage shard results
Date Fri, 04 Sep 2015 21:15:22 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-11 a379b0c18 -> a0155ef97


GEODE-11: Add classes to manage shard results

Lucene ScoreDoc and TopDocs classes are based on int docIds. This implementation
needs Object type keys. So new equivalent classes are needed.


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/3c659bfa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/3c659bfa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/3c659bfa

Branch: refs/heads/feature/GEODE-11
Commit: 3c659bfad33a48cb4bec584452a2e5b7dce098ea
Parents: a379b0c
Author: Ashvin Agrawal <ashvin@apache.org>
Authored: Tue Sep 1 21:13:16 2015 -0700
Committer: Ashvin Agrawal <ashvin@apache.org>
Committed: Fri Sep 4 13:18:06 2015 -0700

----------------------------------------------------------------------
 .../lucene/internal/distributed/EntryScore.java |  22 ++++
 .../lucene/internal/distributed/TopEntries.java |  78 ++++++++++++++
 .../LuceneQueryFunctionJUnitTest.java           |   6 +-
 .../distributed/TopEntriesJUnitTest.java        | 106 +++++++++++++++++++
 4 files changed, 209 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3c659bfa/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/EntryScore.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/EntryScore.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/EntryScore.java
new file mode 100644
index 0000000..6903dce
--- /dev/null
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/EntryScore.java
@@ -0,0 +1,22 @@
+package com.gemstone.gemfire.cache.lucene.internal.distributed;
+
+/**
+ * Holds one entry matching search query and its metadata
+ */
+public class EntryScore {
+  // Key of the entry matching search query
+  final Object key;
+
+  // The score of this document for the query.
+  final float score;
+
+  public EntryScore(Object key, float score) {
+    this.key = key;
+    this.score = score;
+  }
+
+  @Override
+  public String toString() {
+    return "key=" + key + " score=" + score;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3c659bfa/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntries.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntries.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntries.java
new file mode 100644
index 0000000..3a07d3f
--- /dev/null
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntries.java
@@ -0,0 +1,78 @@
+package com.gemstone.gemfire.cache.lucene.internal.distributed;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory;
+
+/**
+ * Holds a ordered collection of entries matching a search query.
+ */
+public class TopEntries {
+  // ordered collection of entries
+  final private List<EntryScore> hits = new ArrayList<>();
+
+  // the maximum number of entries stored in this
+  final int limit;
+
+  // comparator to order entryScore instances
+  final Comparator<EntryScore> comparator = new EntryScoreComparator();
+
+  public TopEntries() {
+    this(LuceneQueryFactory.DEFAULT_LIMIT);
+  }
+
+  public TopEntries(int limit) {
+    if (limit < 0) {
+      throw new IllegalArgumentException();
+    }
+    this.limit = limit;
+  }
+
+  /**
+   * Adds an entry to the collection. The new entry must have a lower score than all previous
entries added to the
+   * collection. The new entry will be ignored if the limit is already reached.
+   * 
+   * @param entry
+   */
+  public void addHit(EntryScore entry) {
+    if (hits.size() > 0) {
+      EntryScore lastEntry = hits.get(hits.size() - 1);
+      if (comparator.compare(lastEntry, entry) < 0) {
+        throw new IllegalArgumentException();
+      }
+    }
+
+    if (hits.size() >= limit) {
+      return;
+    }
+
+    hits.add(entry);
+  }
+
+  /**
+   * @return count of entries in the collection
+   */
+  public int size() {
+    return hits.size();
+  }
+
+  /**
+   * @return The entries collection managed by this instance
+   */
+  public List<EntryScore> getHits() {
+    return hits;
+  }
+
+  /**
+   * Compares scores of two entries using natural ordering. I.e. it returns -1 if the first
entry's score is less than
+   * the second one.
+   */
+  class EntryScoreComparator implements Comparator<EntryScore> {
+    @Override
+    public int compare(EntryScore o1, EntryScore o2) {
+      return Float.compare(o1.score, o2.score);
+    }
+  };
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3c659bfa/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneQueryFunctionJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneQueryFunctionJUnitTest.java
b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneQueryFunctionJUnitTest.java
index c23dffe..94e088f 100644
--- a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneQueryFunctionJUnitTest.java
+++ b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneQueryFunctionJUnitTest.java
@@ -46,7 +46,7 @@ public class LuceneQueryFunctionJUnitTest {
 
     final AtomicReference<LuceneQueryResults> result = new AtomicReference<>();
 
-    QueryMocks m = new QueryMocks();
+    final QueryMocks m = new QueryMocks();
     mocker.checking(new Expectations() {
       {
         oneOf(m.mockContext).getDataSet();
@@ -105,7 +105,7 @@ public class LuceneQueryFunctionJUnitTest {
 
   @Test
   public void testIndexRepoQueryFails() throws Exception {
-    QueryMocks m = new QueryMocks();
+    final QueryMocks m = new QueryMocks();
     mocker.checking(new Expectations() {
       {
         oneOf(m.mockContext).getDataSet();
@@ -133,7 +133,7 @@ public class LuceneQueryFunctionJUnitTest {
 
   @Test
   public void testBucketNotFound() throws Exception {
-    QueryMocks m = new QueryMocks();
+    final QueryMocks m = new QueryMocks();
     mocker.checking(new Expectations() {
       {
         oneOf(m.mockContext).getDataSet();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3c659bfa/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesJUnitTest.java
b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesJUnitTest.java
new file mode 100644
index 0000000..b799a00
--- /dev/null
+++ b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesJUnitTest.java
@@ -0,0 +1,106 @@
+package com.gemstone.gemfire.cache.lucene.internal.distributed;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.jmock.Mockery;
+import org.jmock.lib.concurrent.Synchroniser;
+import org.jmock.lib.legacy.ClassImposteriser;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class TopEntriesJUnitTest {
+  Mockery mockContext;
+
+  EntryScore r1_1 = new EntryScore("3", .9f);
+  EntryScore r1_2 = new EntryScore("1", .8f);
+  EntryScore r2_1 = new EntryScore("2", 0.85f);
+  EntryScore r2_2 = new EntryScore("4", 0.1f);
+
+  @Test
+  public void testPopulateTopEntries() {
+    TopEntries hits = new TopEntries();
+    hits.addHit(r1_1);
+    hits.addHit(r2_1);
+    hits.addHit(r1_2);
+    hits.addHit(r2_2);
+    
+    assertEquals(4, hits.size());
+    verifyResultOrder(hits.getHits(), r1_1, r2_1, r1_2, r2_2);
+  }
+
+  @Test
+  public void putSameScoreEntries() {
+    TopEntries hits = new TopEntries();
+    EntryScore r1 = new EntryScore("1", .8f);
+    EntryScore r2 = new EntryScore("2", .8f);
+    hits.addHit(r1);
+    hits.addHit(r2);
+    
+    assertEquals(2, hits.size());
+    verifyResultOrder(hits.getHits(), r1, r2);
+  }
+  
+  @Test
+  public void testInitialization() {
+    TopEntries hits = new TopEntries();
+    assertEquals(LuceneQueryFactory.DEFAULT_LIMIT, hits.limit);
+
+    hits = new TopEntries(123);
+    assertEquals(123, hits.limit);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testInvalidLimit() {
+    new TopEntries(-1);
+  }
+  
+  @Test
+  public void enforceLimit() throws Exception {
+    TopEntries hits = new TopEntries(3);
+    hits.addHit(r1_1);
+    hits.addHit(r2_1);
+    hits.addHit(r1_2);
+    hits.addHit(r2_2);
+
+    assertEquals(3, hits.size());
+    verifyResultOrder(hits.getHits(), r1_1, r2_1, r1_2);
+  }
+
+  public static void verifyResultOrder(Collection<EntryScore> list, EntryScore... expectedEntries)
{
+    Iterator<EntryScore> iter = list.iterator();
+    for (EntryScore expectedEntry : expectedEntries) {
+      if (!iter.hasNext()) {
+        fail();
+      }
+      EntryScore toVerify = iter.next();
+      assertEquals(expectedEntry.key, toVerify.key);
+      assertEquals(expectedEntry.score, toVerify.score, .0f);
+    }
+  }
+
+  @Before
+  public void setupMock() {
+    mockContext = new Mockery() {
+      {
+        setImposteriser(ClassImposteriser.INSTANCE);
+        setThreadingPolicy(new Synchroniser());
+      }
+    };
+  }
+
+  @After
+  public void validateMock() {
+    mockContext.assertIsSatisfied();
+    mockContext = null;
+  }
+}


Mime
View raw message