Merge "Add totalResources to index search"
diff --git a/Changes b/Changes
index ba791db..185fd0f 100644
--- a/Changes
+++ b/Changes
@@ -1,3 +1,6 @@
+0.61.3 2023-07-11
+    - Add totalResources to results (diewald)
+
 0.61.2 2023-04-05
     - [bugfix] Fix pagebreak retrieval (margaretha, diewald)
     - [feature] Support token lists for match infos (solved #88,
diff --git a/pom.xml b/pom.xml
index 0796235..ff80e83 100644
--- a/pom.xml
+++ b/pom.xml
@@ -35,7 +35,7 @@
 
   <groupId>de.ids_mannheim.korap</groupId>
   <artifactId>Krill</artifactId>
-  <version>0.61.2</version>
+  <version>0.61.3</version>
   <packaging>jar</packaging>
 
   <name>Krill</name>
diff --git a/src/main/java/de/ids_mannheim/korap/KrillIndex.java b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
index 6ec2c0e..6768bcf 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
@@ -459,7 +459,6 @@
                             if (this.reader.getRefCount() == 0) {
 
                                 // Retry update
-                                // System.err.println("Retry update!");
                                 break;
                             };
                             */
@@ -1416,7 +1415,8 @@
         };
         
         // Some initializations ...
-        int i = 0;
+        int i = 0; // matchcount
+        int j = 0; // matchdoccount
         int startIndex = kr.getStartIndex();
         int count = kr.getItemsPerPage();
         int hits = kr.getItemsPerPage() + startIndex;
@@ -1464,7 +1464,6 @@
 			if (DEBUG)
 				log.trace("Rewritten query is {}", query.toString());
 
-
             // Todo: run this in a separated thread
             for (LeafReaderContext atomic : this.reader().leaves()) {
 
@@ -1479,9 +1478,8 @@
                  */
                 final FixedBitSet bitset = collection.bits(atomic);
 
-				if (bitset.nextSetBit(0) == DocIdSetIterator.NO_MORE_DOCS) {
+				if (bitset.nextSetBit(0) == DocIdSetIterator.NO_MORE_DOCS)
 					continue;
-				};
 
                 final PositionsToOffset pto = snippets ? new PositionsToOffset(atomic, field) : null;
 				
@@ -1497,11 +1495,14 @@
 
                     if (DEBUG)
                         log.trace("Match Nr {}/{}", i, count);
-
+                   
                     // There are no more spans to find
                     if (!spans.next())
                         break;
 
+                    // Increment resource counter
+                    itemsPerResourceCounter++;
+                    
                     // Timeout!
                     if (tthread.getTime() > timeout) {
                         kr.setTimeExceeded(true);
@@ -1511,30 +1512,38 @@
 
                     localDocID = spans.doc();
 
-                    // Count hits per resource
-                    if (itemsPerResource > 0) {
+                    // IDS are identical
+                    if (localDocID == oldLocalDocID
+                        || oldLocalDocID == -1) {
 
-                        // IDS are identical
-                        if (localDocID == oldLocalDocID
-                                || oldLocalDocID == -1) {
-                            if (itemsPerResourceCounter++ >= itemsPerResource) {
+                        // Count hits per resource
+                        if (itemsPerResource > 0) {
+                            
+                            // End of resourcecounter is reached
+                            if (itemsPerResourceCounter > itemsPerResource) {
+
+                                // Skip to next resource
                                 if (spans.skipTo(localDocID + 1) != true) {
                                     break;
                                 }
-                                else {
-                                    itemsPerResourceCounter = 1;
-                                    localDocID = spans.doc();
-                                };
+
+                                itemsPerResourceCounter = 1;
+                                localDocID = spans.doc();
                             };
                         }
+                    }
 
-                        // Reset counter
-                        else
-                            itemsPerResourceCounter = 0;
+                    // localDoc is new
+                    else
+                        itemsPerResourceCounter = 1;
 
-                        oldLocalDocID = localDocID;
-                    };
 
+                    if (itemsPerResourceCounter == 1)
+                        j++;
+
+                    oldLocalDocID = localDocID;
+                    
+                    
                     // The next matches are not yet part of the result
                     if (startIndex > i)
                         continue;
@@ -1612,20 +1621,26 @@
                         break;
                     };
 
-                    // Count hits per resource
-                    if (itemsPerResource > 0) {
-                        localDocID = spans.doc();
+                    // Increment resource counter
+                    itemsPerResourceCounter++;
+                    
+                    localDocID = spans.doc();
 
-                        if (localDocID == DocIdSetIterator.NO_MORE_DOCS)
+                    if (localDocID == DocIdSetIterator.NO_MORE_DOCS)
+                        break;
+
+                    // IDS are identical
+                    if (localDocID == oldLocalDocID
+                        || oldLocalDocID == -1) {
+                                
+                        if (localDocID == -1)
                             break;
+                        
+                        // Count hits per resource
+                        if (itemsPerResource > 0) {
 
-                        // IDS are identical
-                        if (localDocID == oldLocalDocID
-                                || oldLocalDocID == -1) {
-                            if (localDocID == -1)
-                                break;
-
-                            if (itemsPerResourceCounter++ >= itemsPerResource) {
+                            // End of resourcecounter is reached
+                            if (itemsPerResourceCounter > itemsPerResource) {
                                 if (spans.skipTo(localDocID + 1) != true) {
                                     break;
                                 };
@@ -1633,13 +1648,15 @@
                                 localDocID = spans.doc();
                             };
                         }
+                    }
+                    // Reset counter
+                    else
+                        itemsPerResourceCounter = 1;
 
-                        // Reset counter
-                        else
-                            itemsPerResourceCounter = 0;
-
-                        oldLocalDocID = localDocID;
-                    };
+                    if (itemsPerResourceCounter == 1)
+                        j++;
+                    
+                    oldLocalDocID = localDocID;
                     i++;
                 };
                 atomicMatches.clear();
@@ -1649,6 +1666,7 @@
                 kr.setItemsPerResource(itemsPerResource);
 
             kr.setTotalResults(cutoff ? (long) -1 : (long) i);
+            kr.setTotalResources(cutoff ? (long) -1 : (long) j);
         }
 
         catch (IOException e) {
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
index 84d8636..f36cc28 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
@@ -440,8 +440,12 @@
         String json = getJsonString(getClass()
                 .getResource("/queries/bsp-itemsPerResource.jsonld").getFile());
 
-        Krill ks = new Krill(json);
-        Result kr = ks.apply(ki);
+        Krill ks;
+        Result kr;
+        KrillMeta meta;
+
+        ks = new Krill(json);
+        kr = ks.apply(ki);
         assertEquals(kr.getTotalResults(), 10);
         assertEquals(0, kr.getStartIndex());
         assertEquals(20, kr.getItemsPerPage());
@@ -452,6 +456,7 @@
         assertEquals("WPD_AAA.00002", kr.getMatch(7).getDocID());
         assertEquals("WPD_AAA.00002", kr.getMatch(8).getDocID());
         assertEquals("WPD_AAA.00004", kr.getMatch(9).getDocID());
+        assertEquals(kr.getTotalResources(), 3);
 
         ks = new Krill(json);
         ks.getMeta().setItemsPerResource(1);
@@ -463,6 +468,7 @@
         assertEquals("WPD_AAA.00004", kr.getMatch(2).getDocID());
 
         assertEquals(kr.getTotalResults(), 3);
+        assertEquals(kr.getTotalResources(), 3);
         assertEquals(0, kr.getStartIndex());
         assertEquals(20, kr.getItemsPerPage());
 
@@ -478,11 +484,12 @@
         assertEquals("WPD_AAA.00004", kr.getMatch(4).getDocID());
 
         assertEquals(kr.getTotalResults(), 5);
+        assertEquals(kr.getTotalResources(), 3);
         assertEquals(0, kr.getStartIndex());
         assertEquals(20, kr.getItemsPerPage());
 
         ks = new Krill(json);
-        KrillMeta meta = ks.getMeta();
+        meta = ks.getMeta();
         meta.setItemsPerResource(1);
         meta.setStartIndex(1);
         meta.setCount(1);
@@ -492,10 +499,28 @@
         assertEquals("WPD_AAA.00002", kr.getMatch(0).getDocID());
 
         assertEquals(kr.getTotalResults(), 3);
+        assertEquals(kr.getTotalResources(), 3);
         assertEquals(1, kr.getStartIndex());
         assertEquals(1, kr.getItemsPerPage());
 
         assertEquals((short) 1, kr.getItemsPerResource());
+
+        ks = new Krill(json);
+        meta = ks.getMeta();
+        meta.setItemsPerResource(2);
+        meta.setStartIndex(2);
+        meta.setCount(1);
+
+        kr = ks.apply(ki);
+
+        assertEquals("WPD_AAA.00002", kr.getMatch(0).getDocID());
+
+        assertEquals(kr.getTotalResults(), 5);
+        assertEquals(kr.getTotalResources(), 3);
+        assertEquals(2, kr.getStartIndex());
+        assertEquals(1, kr.getItemsPerPage());
+
+
     };