[infinispan-commits] Infinispan SVN: r1408 - in trunk/demos/lucene-directory: src/main/java/org/infinispan/lucenedemo and 1 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Fri Jan 22 11:50:06 EST 2010
Author: sannegrinovero
Date: 2010-01-22 11:50:06 -0500 (Fri, 22 Jan 2010)
New Revision: 1408
Modified:
trunk/demos/lucene-directory/pom.xml
trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoActions.java
trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoDriver.java
trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DirectoryFactory.java
trunk/demos/lucene-directory/src/test/java/org/infinispan/lucenedemo/CacheConfigurationTest.java
Log:
[ISPN-339] (Lucene demo not working in standalone transactions), and some minor code cleanup
Modified: trunk/demos/lucene-directory/pom.xml
===================================================================
--- trunk/demos/lucene-directory/pom.xml 2010-01-21 16:28:47 UTC (rev 1407)
+++ trunk/demos/lucene-directory/pom.xml 2010-01-22 16:50:06 UTC (rev 1408)
@@ -24,7 +24,7 @@
<dependency>
<groupId>jboss.jbossts</groupId>
<artifactId>jbossjts</artifactId>
- <version>4.9.0.GA</version>
+ <version>4.6.0.GA</version>
</dependency>
<dependency>
<groupId>jboss.jbossts</groupId>
Modified: trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoActions.java
===================================================================
--- trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoActions.java 2010-01-21 16:28:47 UTC (rev 1407)
+++ trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoActions.java 2010-01-22 16:50:06 UTC (rev 1408)
@@ -23,6 +23,7 @@
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
@@ -63,28 +64,38 @@
public DemoActions() {
index = DirectoryFactory.getIndex("index");
}
+
+ public DemoActions(InfinispanDirectory index) {
+ this.index = index;
+ }
/**
* Runs a Query and returns the stored field for each matching document
* @throws IOException
*/
- public List<String> listStoredValuesMatchingQuery(Query query) throws IOException {
- IndexSearcher searcher = new IndexSearcher(index);
- TopDocs topDocs = searcher.search(query, null, 100);// demo limited to 100 documents!
- ScoreDoc[] scoreDocs = topDocs.scoreDocs;
- List<String> list = new ArrayList<String>();
- for (ScoreDoc sd : scoreDocs) {
- Document doc = searcher.doc(sd.doc);
- list.add(doc.get(MAIN_FIELD));
+ public List<String> listStoredValuesMatchingQuery(Query query) {
+ try {
+ IndexSearcher searcher = new IndexSearcher(index);
+ TopDocs topDocs = searcher.search(query, null, 100);// demo limited to 100 documents!
+ ScoreDoc[] scoreDocs = topDocs.scoreDocs;
+ List<String> list = new ArrayList<String>();
+ for (ScoreDoc sd : scoreDocs) {
+ Document doc = searcher.doc(sd.doc);
+ list.add(doc.get(MAIN_FIELD));
+ }
+ return list;
+ } catch (IOException ioe) {
+ // not recommended: in the simple demo this likely means that the index was not yet
+ // initialized, so returning empty list.
+ return Collections.EMPTY_LIST;
}
- return list;
}
/**
* Returns a list of the values of all stored fields
* @throws IOException
*/
- public List<String> listAllDocuments() throws IOException {
+ public List<String> listAllDocuments() {
MatchAllDocsQuery q = new MatchAllDocsQuery();
return listStoredValuesMatchingQuery(q);
}
Modified: trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoDriver.java
===================================================================
--- trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoDriver.java 2010-01-21 16:28:47 UTC (rev 1407)
+++ trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DemoDriver.java 2010-01-22 16:50:06 UTC (rev 1408)
@@ -44,6 +44,7 @@
public static void main(String[] args) throws IOException {
DemoDriver driver = new DemoDriver();
driver.run();
+ DirectoryFactory.close();
}
private void doQuery(Scanner scanner) throws IOException {
@@ -70,7 +71,7 @@
actions.addNewDocument(line);
}
- private void listAllDocuments() throws IOException {
+ private void listAllDocuments() {
List<String> listMatches = actions.listAllDocuments();
printResult(listMatches);
}
@@ -90,7 +91,7 @@
"\t[5] quit");
}
- private void printResult(List<String> storedValues) throws IOException {
+ private void printResult(List<String> storedValues) {
System.out.println("Matching documents:\n");
if (storedValues.isEmpty()) {
System.out.println("\tNo documents found.");
@@ -133,8 +134,7 @@
break;
case 5:
System.out.println("Quit.");
- System.exit(0);
- break;
+ return;
default:
System.out.println("Invalid option.");
}
Modified: trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DirectoryFactory.java
===================================================================
--- trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DirectoryFactory.java 2010-01-21 16:28:47 UTC (rev 1407)
+++ trunk/demos/lucene-directory/src/main/java/org/infinispan/lucenedemo/DirectoryFactory.java 2010-01-22 16:50:06 UTC (rev 1408)
@@ -81,5 +81,11 @@
}
return dir;
}
+
+ public static synchronized void close() {
+ if (manager!=null) {
+ manager.stop();
+ }
+ }
}
Modified: trunk/demos/lucene-directory/src/test/java/org/infinispan/lucenedemo/CacheConfigurationTest.java
===================================================================
--- trunk/demos/lucene-directory/src/test/java/org/infinispan/lucenedemo/CacheConfigurationTest.java 2010-01-21 16:28:47 UTC (rev 1407)
+++ trunk/demos/lucene-directory/src/test/java/org/infinispan/lucenedemo/CacheConfigurationTest.java 2010-01-22 16:50:06 UTC (rev 1408)
@@ -21,8 +21,15 @@
*/
package org.infinispan.lucenedemo;
-import org.apache.lucene.store.Directory;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.search.Query;
+import org.infinispan.lucene.InfinispanDirectory;
import org.infinispan.lucenedemo.DirectoryFactory;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
@@ -33,15 +40,42 @@
*/
@Test
public class CacheConfigurationTest {
+
+ private InfinispanDirectory cacheForIndex1;
+ private InfinispanDirectory cacheForIndex2;
+ private InfinispanDirectory cacheForIndex3;
+ @BeforeClass
+ public void init() {
+ cacheForIndex1 = DirectoryFactory.getIndex("firstIndex");
+ cacheForIndex2 = DirectoryFactory.getIndex("firstIndex");
+ cacheForIndex3 = DirectoryFactory.getIndex("secondIndex");
+ }
+
+ @AfterClass
+ public void cleanup() {
+ DirectoryFactory.close();
+ }
+
@Test
- public void testAbleToCreateCaches() {
- Directory cacheForIndex1 = DirectoryFactory.getIndex("firstIndex");
- Directory cacheForIndex2 = DirectoryFactory.getIndex("firstIndex");
- Directory cacheForIndex3 = DirectoryFactory.getIndex("secondIndex");
+ public void testCorrectCacheInstances() {
assert cacheForIndex1 != null;
assert cacheForIndex1 == cacheForIndex2;
assert cacheForIndex1 != cacheForIndex3;
}
+
+ @Test
+ public void inserting() throws IOException, ParseException {
+ DemoActions node1 = new DemoActions(cacheForIndex1);
+ DemoActions node2 = new DemoActions(cacheForIndex2);
+ node1.addNewDocument("hello?");
+ assert node1.listAllDocuments().size()==1;
+ node1.addNewDocument("anybody there?");
+ assert node2.listAllDocuments().size()==2;
+ Query query = node1.parseQuery("hello world");
+ List<String> valuesMatchingQuery = node2.listStoredValuesMatchingQuery(query);
+ assert valuesMatchingQuery.size()==1;
+ assert valuesMatchingQuery.get(0).equals("hello?");
+ }
}
More information about the infinispan-commits
mailing list