net.ion.craken.node.problem.distribute.DemoActions.java Source code

Java tutorial

Introduction

Here is the source code for net.ion.craken.node.problem.distribute.DemoActions.java

Source

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010 Red Hat Inc. and/or its affiliates and other
 * contributors as indicated by the @author tags. All rights reserved.
 * See the copyright.txt in the distribution for a full listing of
 * individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package net.ion.craken.node.problem.distribute;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.ion.framework.util.ListUtil;
import net.ion.nsearcher.common.SearchConstant;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
import org.infinispan.Cache;
import org.infinispan.lucene.InfinispanDirectory;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.remoting.transport.Address;

/**
 * DemoActions does some basic operations on the Lucene index,
 * to be used by DemoDriver to show base operations on Lucene.
 * 
 * @author Sanne Grinovero
 * @since 4.0
 */
public class DemoActions {

    /** The MAIN_FIELD */
    private static final String MAIN_FIELD = "myField";

    /** The Analyzer used in all methods **/
    private static final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33);

    private InfinispanDirectory idir;

    private final Cache<?, ?> cache;

    public DemoActions(InfinispanDirectory index, Cache<?, ?> cache) {
        this.idir = index;
        this.cache = cache;
    }

    /**
     * Runs a Query and returns the stored field for each matching document
     * @throws IOException
     */
    public List<String> listStoredValuesMatchingQuery(Query query) {
        try {
            IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(idir));
            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 ListUtil.EMPTY;
        }
    }

    /**
     * Returns a list of the values of all stored fields
     * @throws IOException 
     */
    public List<String> listAllDocuments() {
        MatchAllDocsQuery q = new MatchAllDocsQuery();
        return listStoredValuesMatchingQuery(q);
    }

    /**
     * Creates a new document having just one field containing a string
     * 
     * @param line The text snippet to add
     * @throws IOException
     */
    public void addNewDocument(String line) throws IOException {
        IndexWriterConfig iwconfig = new IndexWriterConfig(SearchConstant.LuceneVersion, analyzer);
        IndexWriter iw = new IndexWriter(idir, iwconfig);
        try {
            Document doc = new Document();
            Field field = new Field(MAIN_FIELD, line, Store.YES, Index.ANALYZED);
            doc.add(field);
            iw.addDocument(doc);
            iw.commit();
        } finally {
            iw.close();
        }
    }

    /**
     * Parses a query using the single field as default
     * 
     * @throws ParseException 
     */
    public Query parseQuery(String queryLine) throws ParseException {
        QueryParser parser = new QueryParser(Version.LUCENE_33, MAIN_FIELD, analyzer);
        return parser.parse(queryLine);
    }

    /**
     * Returns a list of Addresses of all members in the cluster 
     */
    public List<Address> listAllMembers() {
        EmbeddedCacheManager cacheManager = cache.getCacheManager();
        return cacheManager.getMembers();
    }

}