No more IOExceptions on numberOf()
diff --git a/src/main/java/de/ids_mannheim/korap/KorapIndex.java b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
index 340e527..c2e6720 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
@@ -476,18 +476,29 @@
*/
public long numberOf (KorapCollection collection,
String field,
- String type) throws IOException {
+ String type) {
// Short cut for documents
if (type.equals("documents")) {
if (collection.getCount() <= 0) {
- return (long) this.reader().numDocs();
+ try {
+ return (long) this.reader().numDocs();
+ }
+ catch (Exception e) {
+ log.warn(e.getLocalizedMessage());
+ };
+ return (long) 0;
};
long docCount = 0;
int i = 1;
- for (AtomicReaderContext atomic : this.reader().leaves()) {
- docCount += collection.bits(atomic).cardinality();
- i++;
+ try {
+ for (AtomicReaderContext atomic : this.reader().leaves()) {
+ docCount += collection.bits(atomic).cardinality();
+ i++;
+ };
+ }
+ catch (IOException e) {
+ log.warn(e.getLocalizedMessage());
};
return docCount;
};
@@ -508,14 +519,14 @@
}
// Something went wrong
- catch (IOException e) {
+ catch (Exception e) {
log.warn( e.getLocalizedMessage() );
};
return occurrences;
};
- public long numberOf (String field, String type) throws IOException {
+ public long numberOf (String field, String type) {
return this.numberOf(new KorapCollection(this), field, type);
};
@@ -528,7 +539,7 @@
*
* @see #numberOf(String, String)
*/
- public long numberOf (String type) throws IOException {
+ public long numberOf (String type) {
return this.numberOf("tokens", type);
};
diff --git a/src/main/java/de/ids_mannheim/korap/server/Resource.java b/src/main/java/de/ids_mannheim/korap/server/Resource.java
index e777ce2..aa718ce 100644
--- a/src/main/java/de/ids_mannheim/korap/server/Resource.java
+++ b/src/main/java/de/ids_mannheim/korap/server/Resource.java
@@ -90,15 +90,8 @@
KorapResponse kresp = new KorapResponse(KorapNode.getName(), index.getVersion());
kresp.setListener(KorapNode.getListener());
long texts = -1;
- try {
- // Try to receive the number of documents
- texts = index.numberOf("documents");
- }
- catch (IOException e) {
- log.error(e.getLocalizedMessage());
- };
- return kresp.setTotalTexts(texts)
+ return kresp.setTotalTexts(index.numberOf("documents"))
.setMsg("Up and running!")
.toJSON();
};
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestKorapIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestKorapIndex.java
index 9ce5175..b2a0507 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestKorapIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestKorapIndex.java
@@ -25,6 +25,11 @@
public void indexExample () throws IOException {
KorapIndex ki = new KorapIndex();
+ assertEquals(0, ki.numberOf("base", "documents"));
+ assertEquals(0, ki.numberOf("base", "tokens"));
+ assertEquals(0, ki.numberOf("base", "sentences"));
+ assertEquals(0, ki.numberOf("base", "paragraphs"));
+
FieldDocument fd = new FieldDocument();
fd.addString("name", "Peter");