Commits

Kevin Smith  committed a49445f

Wrapping analyzer usage in try..after blocks to insure proper clean up occurs

  • Participants
  • Parent commits f902857
  • Tags riak_search-beta2.1

Comments (0)

Files changed (5)

File apps/riak_search/src/riak_search_client.erl

 %% Error}.
 parse_query(Query) ->
     {ok, AnalyzerPid} = qilr_analyzer_sup:new_analyzer(),
-    Result = qilr_parse:string(AnalyzerPid, Query),
-    qilr_analyzer:close(AnalyzerPid),
-    Result.
+    try
+        qilr_parse:string(AnalyzerPid, Query)
+    after
+        qilr_analyzer:close(AnalyzerPid)
+    end.
 
 %% Run the Query, return the list of keys.
 %% Timeout is in milliseconds.

File apps/riak_search/src/riak_search_shell.erl

                 handler}).
 
 start(Index) ->
+    {ok, Client} = riak_search:local_client(),
     {ok, P} = qilr_analyzer_sup:new_analyzer(),
-    {ok, Client} = riak_search:local_client(),
-    read_input(#state{client=Client,
-                      index=Index,
-                      analyzer=P,
-                      handler=fun riak_search_shell:search/2}, []),
-    qilr_analyzer:close(P).
+    try
+        read_input(#state{client=Client,
+                          index=Index,
+                          analyzer=P,
+                          handler=fun riak_search_shell:search/2}, [])
+    after
+        qilr_analyzer:close(P)
+    end.
 
 start() ->
     start("search").

File apps/riak_search/src/search.erl

 %% Full text index the specified directory of plain text files.
 index_dir(IndexOrSchema, Directory) ->
     {ok, Client} = riak_search:local_client(),
-    {ok, AnalyzerPid} = qilr_analyzer_sup:new_analyzer(),
     {ok, Schema} = riak_search_config:get_schema(IndexOrSchema),
     Index = Schema:name(),
     Field = Schema:default_field(),
+    {ok, AnalyzerPid} = qilr_analyzer_sup:new_analyzer(),
     F = fun(BaseName, Body) ->
         Fields = [{Field, binary_to_list(Body)}],
         IdxDoc = riak_indexed_doc:new(BaseName, Index),
         end || X <- Terms],
         Client:store_idx_doc(IdxDoc2)
     end,
-    riak_search_utils:index_recursive(F, Directory),
-    qilr_analyzer:close(AnalyzerPid),
+    try
+        riak_search_utils:index_recursive(F, Directory)
+    after
+        qilr_analyzer:close(AnalyzerPid)
+    end,
     ok.

File apps/riak_solr/src/riak_solr_search_client.erl

 %% Parse a solr XML formatted file.
 parse_solr_xml(IndexOrSchema, Body) when is_binary(Body) ->
     {ok, AnalyzerPid} = qilr_analyzer_sup:new_analyzer(),
-    Result = parse_solr_xml(AnalyzerPid, IndexOrSchema, Body),
-    qilr_analyzer:close(AnalyzerPid),
-    Result.
+    try
+        parse_solr_xml(AnalyzerPid, IndexOrSchema, Body)
+    after
+        qilr_analyzer:close(AnalyzerPid)
+    end.
 
 %% Index a solr XML formatted file using the provided analyzer pid.
 parse_solr_xml(AnalyzerPid, IndexOrSchema, Body) ->
     {'id', ID};
 parse_solr_entry(_AnalyzerPid, _Index, delete, {"query", Query}) ->
     case Client:parse_query(Query) of
-        {ok, QueryOps} -> 
+        {ok, QueryOps} ->
             {'query', QueryOps};
         {error, Error} ->
             M = "Error parsing query '~s': ~p~n",
 %% Some unknown command...
 parse_solr_entry(_, _, Command, Entry) ->
     throw({?MODULE, unknown_command, Command, Entry}).
-        
+
 
 %% @private
 to_riak_idx_doc(Index, Doc) ->
     case lists:keyfind("id", 1, Doc) of
-        {"id", Id} -> 
+        {"id", Id} ->
             Id;
         false ->
             Id = undefined, % Prevent compiler warnings.
             throw({?MODULE, required_field_not_found, "id", Doc})
     end,
-    Fields = lists:keydelete("id", 1, Doc), 
+    Fields = lists:keydelete("id", 1, Doc),
     #riak_idx_doc{id=Id, index=Index, fields=Fields, props=[]}.
 
 
         Client:index_term(Index, Field, Term, DocID, Props)
     end,
     plists:map(F, Terms, {processes, 4}),
-    
+
     %% Store the document.
     Client:store_idx_doc(IdxDoc),
     run_solr_command(Schema, add, Docs);

File apps/riak_solr/src/solr_search.erl

 -module(solr_search).
 -export([
-    index_dir/1, 
+    index_dir/1,
     index_dir/2
 ]).
 
 %% to contain a solr formatted file.
 index_dir(IndexOrSchema, Directory) ->
     {ok, SolrClient} = riak_solr_app:local_client(),
+    {ok, Schema} = riak_search_config:get_schema(IndexOrSchema),
     {ok, AnalyzerPid} = qilr_analyzer_sup:new_analyzer(),
-    {ok, Schema} = riak_search_config:get_schema(IndexOrSchema),
     F = fun(_BaseName, Body) ->
         try
             {ok, Command, Docs} = SolrClient:parse_solr_xml(Schema, Body),
             error_logger:error_msg(M, [Schema:name(), Error, erlang:get_stacktrace()])
         end
     end,
-    riak_search_utils:index_recursive(F, Directory),
-    qilr_analyzer:close(AnalyzerPid),
-    ok.
-
-
+    try
+        riak_search_utils:index_recursive(F, Directory),
+        ok
+    after
+        qilr_analyzer:close(AnalyzerPid)
+    end.