* of a {@link RemoteBitStreamIndex}, even if the latter would be usable.
*/
public static void start( final Index index, final ServerSocket serverSocket, boolean forceRemoteIndex ) throws IOException {
LOGGER.info( "Index server started at " + serverSocket.getLocalSocketAddress() );
ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
Socket socket;
final SocketAddress localSocketAddress = serverSocket.getLocalSocketAddress();
int command;
for ( ;; ) {
socket = serverSocket.accept();
command = socket.getInputStream().read();
LOGGER.debug( "Remote command: " + command );
switch( command ) {
case GET_INDEX:
DataInputStream dis = new DataInputStream( socket.getInputStream() );
DataOutputStream dos = new DataOutputStream( socket.getOutputStream() );
boolean randomAccess = dis.readBoolean();
boolean documentSizes = dis.readBoolean();
if ( index instanceof BitStreamIndex && ! forceRemoteIndex ) {
BitStreamIndex localIndex = (BitStreamIndex)index;
if ( randomAccess && localIndex.offsets == null ) {
randomAccess = false;
LOGGER.warn( "Random access will not be available for index " + localIndex );
}
/** Note that in case of Golomb or interpolative position coding
* we are forced to serialise and transfer the entire size list,
* or decoding would be too slow. */
BinIO.storeObject( new RemoteBitStreamIndex( localSocketAddress,
index.numberOfDocuments, index.numberOfTerms,
index.numberOfPostings, index.numberOfOccurrences,
index.maxCount, localIndex.payload, localIndex.frequencyCoding, localIndex.pointerCoding,
localIndex.countCoding,
localIndex.positionCoding,
localIndex.quantum,
localIndex.height,
localIndex.bufferSize,
localIndex.termProcessor,
localIndex.field,
localIndex.properties,
localIndex.termMap != null ? new RemoteTermMap( localSocketAddress, index.numberOfTerms ) : null,
localIndex.prefixMap != null ? new RemotePrefixMap( localSocketAddress, index.numberOfTerms ) : null,
localIndex.positionCoding == Coding.GOLOMB || localIndex.positionCoding == Coding.INTERPOLATIVE ? localIndex.sizes :
( documentSizes ? new RemoteSizeList( localSocketAddress, localIndex.numberOfDocuments ) : null ),
randomAccess ? new RemoteOffsetList( localSocketAddress, localIndex.offsets.size() ) : null
), dos );
}
else
BinIO.storeObject( new RemoteIndex( localSocketAddress, index.numberOfDocuments, index.numberOfTerms, index.numberOfPostings, index.numberOfOccurrences, index.maxCount, index.payload, index.hasCounts,
index.hasPositions, index.termProcessor, index.field, ( documentSizes ? new RemoteSizeList( localSocketAddress, index.numberOfDocuments ) : null ), index.properties ), dos );
dos.flush();
break;
case GET_INDEX_READER:
threadPool.execute( new RemoteIndexReader.ServerThread( socket, index ) );
break;
case GET_TERM_MAP:
threadPool.execute( new RemoteTermMap.ServerThread( socket, ((BitStreamIndex)index).termMap ) );
break;
case GET_PREFIX_MAP:
threadPool.execute( new RemotePrefixMap.ServerThread( socket, ((BitStreamIndex)index).prefixMap ) );
break;
case GET_SIZE_LIST:
threadPool.execute( new RemoteSizeList.ServerThread( socket, index.sizes ) );
break;
case GET_OFFSET_LIST:
threadPool.execute( new RemoteOffsetList.ServerThread( socket, ((BitStreamIndex)index).offsets ) );
break;
case GET_CLIENT_INPUT_STREAM:
threadPool.execute( new RemoteInputStream.ServerThread( socket, ((BitStreamIndex)index).getInputStream() ) );
break;
}
}
}