it.unimi.di.mg4j.index.remote
Class IndexServer

java.lang.Object
  extended by it.unimi.di.mg4j.index.remote.IndexServer

public class IndexServer
extends Object

A daemon exposing an index remotely.

MG4J provides remote access to indices using a client/server pair. A thread running the start(Index, InetAddress, int, boolean) method of this class acts as a server.

Once a server is up and running, clients can connect by calling the getIndex(String, int, boolean, boolean) method, which sends to the server a GET_INDEX command and returns a (serialised) Index that will handle all communication with the server transparently. The static factory methods in Index (such as Index.getInstance(CharSequence)) will turn URIs such as mg4j://localhost into calls to getIndex(String, int, boolean, boolean).

Presently there are two main kinds of remote indices: those exposing the local bitstream remotely, and those exposing the results of document iterators remotely. In the first case, the bitstream is passed over the net, and it is decoded locally (it is very advisable not using a compression method requiring document sizes in that case, as the size list will be serialised into the returned Index instance). When started, an index server will by default try to expose the local bitstream, if possible, but it can be forced not to do so with a suitable parameter. Depending on the kind of access requested, the index server will return instances of RemoteBitStreamIndex or instances of RemoteIndex. The two approaches vary wildly in performance, and some profiling with specific applications is advisable before choosing one method over the other.

All other server commands available spawn a new thread that will handle a specific data structure over the newly created socket. Thus, after writing the command GET_SIZE_LIST the server will start a RemoteSizeList.ServerThread and answer to queries about the size list, whereas after writing the command GET_INDEX_READER the server will start a RemoteIndexReader.ServerThread and answer to queries about the underlying local IndexReader.

For simplicity and easiness in code editing, every client class starting a thread (e.g., RemoteTermMap, RemoteIndexReader, …) sports a nested static class called ServerThread containing the code of the corresponding server thread. In this way client and server code sit in the same source file, making editing and debugging simpler.

Author:
Alessandro Arrabito, Sebastiano Vigna

Field Summary
static int DEFAULT_PORT
           
static byte GET_CLIENT_INPUT_STREAM
           
static byte GET_CLIENT_POS_INPUT_STREAM
           
static byte GET_INDEX
           
static byte GET_INDEX_READER
           
static byte GET_OFFSET_LIST
           
static byte GET_PREFIX_MAP
           
static byte GET_SIZE_LIST
           
static byte GET_TERM_MAP
           
static byte QUIT
           
 
Constructor Summary
IndexServer()
           
 
Method Summary
static Index getIndex(String host, int port, boolean randomAccess, boolean documentSizes)
          Returns an index object corresponding a given index server specified by host and port.
static void main(String[] arg)
           
static void quit(String host, int port)
           
static void start(Index index, InetAddress address, int port, boolean forceRemoteIndex)
          Starts an index-server daemon thread.
static void start(Index index, ServerSocket serverSocket, boolean forceRemoteIndex)
          Starts an index server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT

public static final int DEFAULT_PORT
See Also:
Constant Field Values

GET_INDEX

public static final byte GET_INDEX
See Also:
Constant Field Values

GET_INDEX_READER

public static final byte GET_INDEX_READER
See Also:
Constant Field Values

GET_TERM_MAP

public static final byte GET_TERM_MAP
See Also:
Constant Field Values

GET_PREFIX_MAP

public static final byte GET_PREFIX_MAP
See Also:
Constant Field Values

GET_SIZE_LIST

public static final byte GET_SIZE_LIST
See Also:
Constant Field Values

GET_OFFSET_LIST

public static final byte GET_OFFSET_LIST
See Also:
Constant Field Values

GET_CLIENT_INPUT_STREAM

public static final byte GET_CLIENT_INPUT_STREAM
See Also:
Constant Field Values

GET_CLIENT_POS_INPUT_STREAM

public static final byte GET_CLIENT_POS_INPUT_STREAM
See Also:
Constant Field Values

QUIT

public static final byte QUIT
See Also:
Constant Field Values
Constructor Detail

IndexServer

public IndexServer()
Method Detail

getIndex

public static Index getIndex(String host,
                             int port,
                             boolean randomAccess,
                             boolean documentSizes)
                      throws IOException,
                             ClassNotFoundException
Returns an index object corresponding a given index server specified by host and port.

Parameters:
host - the server host.
port - the server port, or -1 for DEFAULT_PORT.
randomAccess - whether the index should be accessible randomly.
documentSizes - if true, document sizes will be loaded (note that sometimes document sizes might be loaded anyway because the compression method for positions requires it).
Throws:
IOException
ClassNotFoundException

quit

public static void quit(String host,
                        int port)
                 throws IOException
Throws:
IOException

start

public static void start(Index index,
                         ServerSocket serverSocket,
                         boolean forceRemoteIndex)
                  throws IOException
Starts an index server.

Parameters:
index - the underlying index.
serverSocket - the daemon socket.
forceRemoteIndex - force the thread to use a RemoteIndex instead of a RemoteBitStreamIndex, even if the latter would be usable.
Throws:
IOException

start

public static void start(Index index,
                         InetAddress address,
                         int port,
                         boolean forceRemoteIndex)
                  throws IOException
Starts an index-server daemon thread.

Parameters:
index - the underlying index.
address - the IP address for the daemon socket.
port - the IP port for the daemon socket.
forceRemoteIndex - force the thread to use a RemoteIndex instead of a RemoteBitStreamIndex, even if the latter would be usable.
Throws:
IOException

main

public static void main(String[] arg)
                 throws ConfigurationException,
                        IOException,
                        URISyntaxException,
                        ClassNotFoundException,
                        com.martiansoftware.jsap.JSAPException,
                        SecurityException,
                        InstantiationException,
                        IllegalAccessException,
                        InvocationTargetException,
                        NoSuchMethodException
Throws:
ConfigurationException
IOException
URISyntaxException
ClassNotFoundException
com.martiansoftware.jsap.JSAPException
SecurityException
InstantiationException
IllegalAccessException
InvocationTargetException
NoSuchMethodException