com.mathworks.xzheng.tools.remote.SearchServer.java Source code

Java tutorial

Introduction

Here is the source code for com.mathworks.xzheng.tools.remote.SearchServer.java

Source

package com.mathworks.xzheng.tools.remote;

/**
 * Copyright Manning Publications Co.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific lan      
*/

import java.io.File;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.mathworks.xzheng.meetlucene.Searcher;

// From chapter 9
public class SearchServer {
    private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: SearchServer <basedir>");
            System.exit(-1);
        }

        String basedir = args[0]; //1
        Directory[] dirs = new Directory[ALPHABET.length()];
        IndexReader[] searchables = new IndexReader[ALPHABET.length()];
        for (int i = 0; i < ALPHABET.length(); i++) {
            dirs[i] = FSDirectory.open(new File(basedir, "" + ALPHABET.charAt(i)));
            searchables[i] = DirectoryReader.open(dirs[i]); //2
        }

        LocateRegistry.createRegistry(1099); //3

        IndexSearcher multiSearcher = new IndexSearcher(new MultiReader(searchables)); //4

        ExecutorService pool = Executors.newFixedThreadPool(nThreads);

        IndexSearcher multiImpl = //4
                new IndexSearcher(multiSearcher); //4
        Naming.rebind("//localhost/LIA_Multi", multiImpl); //4

        Searcher parallelSearcher = //5
                new ParallelMultiSearcher(searchables); //5
        RemoteSearchable parallelImpl = //5
                new RemoteSearchable(parallelSearcher); //5
        Naming.rebind("//localhost/LIA_Parallel", parallelImpl);//5

        System.out.println("Server started");

        for (int i = 0; i < ALPHABET.length(); i++) {
            dirs[i].close();
        }
    }
}

/*
  #1 Indexes under basedir
  #2 Open IndexSearcher for each index
  #3 Create RMI registry
  #4 MultiSearcher over all indexes
  #5 ParallelMultiSearcher over all indexes
*/