Add Lucene Indexer Control
[couchdbimport.git] / CouchProjects / CouchDb / couch_lucene.erl
blob521adb6f692d916fd3e5861290b24cec54ba10f3
1 %% CouchDb
2 %% Copyright (C) 2006 Damien Katz
3 %%
4 %% This program is free software; you can redistribute it and/or
5 %% modify it under the terms of the GNU General Public License
6 %% as published by the Free Software Foundation; either version 2
7 %% of the License, or (at your option) any later version.
8 %%
9 %% This program is distributed in the hope that it will be useful,
10 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 %% GNU General Public License for more details.
13 %%
14 %% You should have received a copy of the GNU General Public License
15 %% along with this program; if not, write to the Free Software
16 %% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 -module(couch_lucene).
19 -behaviour(gen_server).
21 -include("couch_db.hrl").
23 -export([start_link/1, timeout/0]).
24 -export([init/1, terminate/2, handle_call/3, handle_cast/2, handle_info/2,code_change/3,stop/0]).
26 -define(ERR_HANDLE, {Port, {exit_status, Status}} -> {stop, {unknown_error, Status}, {unknown_error, Status}, Port}).
28 start_link(LuceneExec) ->
29 gen_server:start_link({local, couch_lucene}, couch_lucene, LuceneExec, []).
31 stop() ->
32 exit(whereis(couch_lucene), close).
34 timeout() ->
35 % in Msecs. maybe need to make this a configurable value
36 30000.
38 init(LuceneExec) ->
39 Cmd =
40 case erlang:system_info(os_type) of
41 {unix, _} ->
42 lists:flatten(io_lib:format("~s launched_from_erlang", [LuceneExec]));
43 {win32, _} ->
44 lists:flatten(io_lib:format("cmd /c\"~s\" launched_from_erlang", [LuceneExec]))
45 end,
46 Port = open_port({spawn, Cmd}, [nouse_stdio]),
47 {ok, {Cmd, Port}}.
49 terminate(_Reason, _Server) ->
50 ok.
52 handle_call(get_exe_cmd, _From, {Cmd, Port}) ->
53 ok.
56 handle_cast(_Whatever, {Exe, Port}) ->
57 {noreply, {Exe, Port}}.
59 handle_info({Port, {exit_status, Status}}, {Exe, Port}) ->
60 {stop, {lucene_server_exited, Status}, {Exe, Port}};
62 handle_info(_Whatever, {Exe, Port}) ->
63 {noreply, {Exe, Port}}.
65 code_change(_OldVsn, State, _Extra) ->
66 {ok, State}.