summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_native_process.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/couchdb/couch_native_process.erl')
-rw-r--r--src/couchdb/couch_native_process.erl53
1 files changed, 29 insertions, 24 deletions
diff --git a/src/couchdb/couch_native_process.erl b/src/couchdb/couch_native_process.erl
index 65e4e131..b512f712 100644
--- a/src/couchdb/couch_native_process.erl
+++ b/src/couchdb/couch_native_process.erl
@@ -1,16 +1,16 @@
-% Licensed under the Apache License, Version 2.0 (the "License");
-% you may not use this file except in compliance with the License.
+% 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.
+% 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 language governing permissions
-% and limitations under the License.
+% and limitations under the License.
%
% This file drew much inspiration from erlview, which was written by and
% copyright Michael McDaniel [http://autosys.us], and is also under APL 2.0
@@ -25,14 +25,14 @@
%
% fun({Doc}) ->
% % Below, we emit a single record - the _id as key, null as value
-% DocId = proplists:get_value(Doc, <<"_id">>, null),
+% DocId = couch_util:get_value(Doc, <<"_id">>, null),
% Emit(DocId, null)
% end.
%
% which should be roughly the same as the javascript:
% emit(doc._id, null);
%
-% This module exposes enough functions such that a native erlang server can
+% This module exposes enough functions such that a native erlang server can
% act as a fully-fleged view server, but no 'helper' functions specifically
% for simplifying your erlang view code. It is expected other third-party
% extensions will evolve which offer useful layers on top of this view server
@@ -40,7 +40,8 @@
-module(couch_native_process).
-behaviour(gen_server).
--export([start_link/0,init/1,terminate/2,handle_call/3,handle_cast/2]).
+-export([start_link/0,init/1,terminate/2,handle_call/3,handle_cast/2,code_change/3,
+ handle_info/2]).
-export([set_timeout/2, prompt/2]).
-define(STATE, native_proc_state).
@@ -76,23 +77,27 @@ handle_call({prompt, Data}, _From, State) ->
throw:{error, Why} ->
{State, [<<"error">>, Why, Why]}
end,
-
+
case Resp of
{error, Reason} ->
Msg = io_lib:format("couch native server error: ~p", [Reason]),
{reply, [<<"error">>, <<"native_query_server">>, list_to_binary(Msg)], NewState};
[<<"error">> | Rest] ->
- Msg = io_lib:format("couch native server error: ~p", [Rest]),
+ % Msg = io_lib:format("couch native server error: ~p", [Rest]),
+ % TODO: markh? (jan)
{reply, [<<"error">> | Rest], NewState};
[<<"fatal">> | Rest] ->
- Msg = io_lib:format("couch native server error: ~p", [Rest]),
+ % Msg = io_lib:format("couch native server error: ~p", [Rest]),
+ % TODO: markh? (jan)
{stop, fatal, [<<"error">> | Rest], NewState};
Resp ->
{reply, Resp, NewState}
end.
-handle_cast(_Msg, State) -> {noreply, State}.
-handle_info(_Msg, State) -> {noreply, State}.
+handle_cast(foo, State) -> {noreply, State}.
+handle_info({'EXIT',_,normal}, State) -> {noreply, State};
+handle_info({'EXIT',_,Reason}, State) ->
+ {stop, Reason, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVersion, State, _Extra) -> {ok, State}.
@@ -168,12 +173,12 @@ ddoc(State, {DDoc}, [FunPath, Args]) ->
% load fun from the FunPath
BFun = lists:foldl(fun
(Key, {Props}) when is_list(Props) ->
- proplists:get_value(Key, Props, nil);
- (Key, Fun) when is_binary(Fun) ->
+ couch_util:get_value(Key, Props, nil);
+ (_Key, Fun) when is_binary(Fun) ->
Fun;
- (Key, nil) ->
+ (_Key, nil) ->
throw({error, not_found});
- (Key, Fun) ->
+ (_Key, _Fun) ->
throw({error, malformed_ddoc})
end, {DDoc}, FunPath),
ddoc(State, makefun(State, BFun, {DDoc}), FunPath, Args).
@@ -238,7 +243,7 @@ load_ddoc(DDocs, DDocId) ->
try dict:fetch(DDocId, DDocs) of
{DDoc} -> {DDoc}
catch
- _:Else -> throw({error, ?l2b(io_lib:format("Native Query Server missing DDoc with Id: ~s",[DDocId]))})
+ _:_Else -> throw({error, ?l2b(io_lib:format("Native Query Server missing DDoc with Id: ~s",[DDocId]))})
end.
bindings(State, Sig) ->
@@ -300,7 +305,7 @@ bindings(State, Sig, DDoc) ->
{'FoldRows', FoldRows}
],
case DDoc of
- {Props} ->
+ {_Props} ->
Bindings ++ [{'DDoc', DDoc}];
_Else -> Bindings
end.
@@ -308,11 +313,11 @@ bindings(State, Sig, DDoc) ->
% thanks to erlview, via:
% http://erlang.org/pipermail/erlang-questions/2003-November/010544.html
makefun(State, Source) ->
- Sig = erlang:md5(Source),
+ Sig = couch_util:md5(Source),
BindFuns = bindings(State, Sig),
{Sig, makefun(State, Source, BindFuns)}.
makefun(State, Source, {DDoc}) ->
- Sig = erlang:md5(lists:flatten([Source, term_to_binary(DDoc)])),
+ Sig = couch_util:md5(lists:flatten([Source, term_to_binary(DDoc)])),
BindFuns = bindings(State, Sig, {DDoc}),
{Sig, makefun(State, Source, BindFuns)};
makefun(_State, Source, BindFuns) when is_list(BindFuns) ->
@@ -365,7 +370,7 @@ start_list_resp(Self, Sig) ->
undefined -> {[{<<"headers">>, {[]}}]};
CurrHdrs -> CurrHdrs
end,
- Chunks =
+ Chunks =
case erlang:get(Sig) of
undefined -> [];
CurrChunks -> CurrChunks