Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support _find query #186

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Copyright (c) 2009-2016 Benoît Chesneau.

__Version:__ 1.4.2
__Version:__ 1.6.0

# couchbeam

Expand Down
2 changes: 1 addition & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Copyright (c) 2009-2016 Benoît Chesneau.

__Version:__ 1.4.2
__Version:__ 1.5.0

# couchbeam

Expand Down
2 changes: 1 addition & 1 deletion doc/overview.edoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@copyright 2009-2016 Benoît Chesneau.
@version 1.4.2
@version 1.5.0
@title Couchbeam - simple Barrel and Apache CouchDB client library for Erlang applications

@doc
Expand Down
23 changes: 12 additions & 11 deletions include/couchbeam.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
-type db_name() :: binary() | string().
-type docid() :: binary() | string().

-type ejson() :: ejson_object() | ejson_array().
-type ejson() :: ejson_term().

-type ejson_array() :: [ejson_term()].
-type ejson_array() :: [ejson_term()] | [{ejson_key(), ejson_term()}].
-type ejson_object() :: {[{ejson_key(), ejson_term()}]}.

-type ejson_key() :: binary() | atom().
Expand All @@ -39,6 +39,7 @@
| ejson_object()
| ejson_string()
| ejson_number()
| ejson_key()
| true | false | null.

-type ejson_string() :: binary().
Expand All @@ -48,10 +49,11 @@
-type doc() :: ejson_object().

-type changes_option() :: continuous | longpoll | normal
| include_docs | {since, integer()}
| include_docs | {since, integer()} | {since, now}
| {timeout, integer()}
| heartbeat | {heartbeat, integer()}
| {filter, string()} | {filter, string(), list({string(), string() | integer()}
| {binary(),binary()}
| {filter, string() | binary()} | {filter, string() | binary(), list({string() | binary(), string() | binary() | integer()}
)}
| conflicts | {style, string()} | descending.
-type changes_options() :: list(changes_option()).
Expand All @@ -60,8 +62,7 @@
| include_docs | {since, integer()}
| {timeout, integer()}
| heartbeat | {heartbeat, integer()}
| {filter, string()} | {filter, string(), list({string(), string() | integer()}
)}
| {filter, string() | binary()} | {filter, string() | binary(), list({string() | binary(), string() | binary() | integer()})}
| conflicts | {style, string()} | descending.
-type changes_options1() :: list(changes_option1()).

Expand All @@ -71,7 +72,7 @@
| {timeout, integer()}
| heartbeat | {heartbeat, integer()}
| {filter, string() | binary()}
| {filter, string() | binary(), list({string(), string() | integer()})}
| {filter, string() | binary(), list({string() | binary(), string() | binary() | integer()})}
| conflicts
| {style, string() | binary()}
| descending
Expand All @@ -81,9 +82,9 @@

-type stale() :: ok | update_after.

-type view_option() :: {key, binary()} | {start_docid, binary()}
| {end_docid, binary()} | {start_key, binary()}
| {end_key, binary()} | {limit, integer()}
-type view_option() :: {key, ejson()} | {start_docid, binary()}
| {end_docid, binary()} | {start_key, ejson()}
| {end_key, ejson()} | {limit, integer()}
| {stale, stale()}
| descending
| {skip, integer()}
Expand Down Expand Up @@ -112,7 +113,7 @@
% record to keep database information
-record(db, {
server :: server(),
name :: string(),
name :: binary(),
options = [] :: list()
}).

Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{deps, [
{jsx, "2.8.0"},
{hackney, "1.6.4"}
{hackney, "v1.10.1"}
]}.


Expand Down
4 changes: 2 additions & 2 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Rebar2Deps = [
{jsx, ".*", {git, "https://github.com/talentdeficit/jsx.git",
{tag,"2.8.0"}}},

{hackney, ".*", {git, "https://github.com/benoitc/hackney.git",
{tag, "1.6.4"}}}
{hackney, ".*", {git, "https://github.com/obi458/hackney.git",
{tag, "v1.9.1"}}}
],

case IsRebar3 of
Expand Down
2 changes: 1 addition & 1 deletion src/couchbeam.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{application, couchbeam,
[{description, "Erlang CouchDB client"},
{vsn, "1.4.2"},
{vsn, "1.7.0"},
{modules, []},
{registered, [
couchbeam_sup
Expand Down
69 changes: 58 additions & 11 deletions src/couchbeam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
put_attachment/4, put_attachment/5, send_attachment/2,
ensure_full_commit/1, ensure_full_commit/2,
compact/1, compact/2,
get_missing_revs/2]).
get_missing_revs/2,
find_docs/3, create_index/2]).

-opaque doc_stream() :: {atom(), any()}.
-export_type([doc_stream/0]).
Expand Down Expand Up @@ -79,7 +80,7 @@ server_connection(URL) when is_list(URL) orelse is_binary(URL) ->
server_connection(URL, Options) when is_list(Options) ->
#server{url=hackney_url:fix_path(URL), options=Options};
server_connection(Host, Port) when is_integer(Port) ->
server_connection(Host, Port, "", []).
server_connection(Host, Port, <<>>, []).


%% @doc Create a server for connectiong to a CouchDB node
Expand All @@ -91,8 +92,8 @@ server_connection(Host, Port) when is_integer(Port) ->
%%
%% For a description of SSL Options, look in the <a href="http://www.erlang.org/doc/apps/ssl/index.html">ssl</a> manpage.
%%
-spec server_connection(Host::string(), Port::non_neg_integer(), Prefix::string(), OptionsList::list()) ->
Server::server().
%-spec server_connection(Host::string(), Port::non_neg_integer(), Prefix::string(), OptionsList::list()) ->
% Server::server().
%% OptionsList() = [option()]
%% option() =
%% {is_ssl, boolean()} |
Expand Down Expand Up @@ -506,7 +507,7 @@ stream_doc({_Ref, Cont}) ->

%% @doc stop to receive the multipart response of the doc api and close
%% the connection.
-spec end_doc_stream(doc_stream()) -> ok.
%-spec end_doc_stream(doc_stream()) -> ok.
end_doc_stream({Ref, _Cont}) ->
hackney:close(Ref).

Expand Down Expand Up @@ -566,8 +567,8 @@ save_doc(Db, Doc, Options) ->
%% `<<"identity">>' if normal or `<<"gzip">>' if the attachments is
%% gzipped.

-spec save_doc(Db::db(), doc(), mp_attachments(), Options::list()) ->
{ok, doc()} | {error, term()}.
%-spec save_doc(Db::db(), doc(), mp_attachments(), Options::list()) ->
% {ok, doc()} | {error, term()}.
save_doc(#db{server=Server, options=Opts}=Db, {Props}=Doc, Atts, Options) ->
DocId = case couchbeam_util:get_value(<<"_id">>, Props) of
undefined ->
Expand Down Expand Up @@ -782,9 +783,9 @@ fetch_attachment(Db, DocId, Name) ->
%% <li>Other options that can be sent using the REST API</li>
%% </ul>
%%
-spec fetch_attachment(db(), string(), string(),
list())
-> {ok, binary()}| {ok, atom()} |{error, term()}.
%-spec fetch_attachment(db(), string(), string(),
% list())
% -> {ok, binary()}| {ok, atom()} |{error, term()}.
fetch_attachment(#db{server=Server, options=Opts}=Db, DocId, Name, Options0) ->
{Stream, Options} = case couchbeam_util:get_value(stream, Options0) of
undefined ->
Expand Down Expand Up @@ -886,7 +887,7 @@ put_attachment(#db{server=Server, options=Opts}=Db, DocId, Name, Body,
end, Headers, Options),

DocId1 = couchbeam_util:encode_docid(DocId),
AttName = couchbeam_util:encode_att_name(Name),
AttName = couchbeam_util:to_binary(Name),%encode_att_name(Name),
Url = hackney_url:make_url(couchbeam_httpc:server_url(Server), [couchbeam_httpc:db_url(Db), DocId1,
AttName],
QueryArgs),
Expand Down Expand Up @@ -1048,6 +1049,52 @@ get_missing_revs(#db{server=Server, options=Opts}=Db, IdRevs) ->
Error
end.

find_docs(#db{server=Server, options=Opts}=Db, Selector, Params) ->
Url = hackney_url:make_url(couchbeam_httpc:server_url(Server),
[couchbeam_httpc:db_url(Db), <<"_find">>], []),
Headers = [ {<<"content-type">>, <<"application/json">>},
{<<"accept">>, <<"application/json">>} ],
BodyJson = {[{selector, Selector} | Params]},
case couchbeam_httpc:db_request(post, Url, Headers, couchbeam_ejson:encode(BodyJson), Opts,
[200, 201]) of
{ok, _, RespHeaders, Ref} ->
case hackney_headers:parse(<<"content-type">>, RespHeaders) of
{<<"multipart">>, _, _} ->
%% we get a multipart request, start to parse it.
InitialState = {Ref, fun() ->
couchbeam_httpc:wait_mp_doc(Ref, <<>>)
end},
{ok, {multipart, InitialState}};
_ ->
{ok, couchbeam_httpc:json_body(Ref)}
end;
Error ->
Error
end.

create_index(#db{server=Server, options=Opts}=Db, Params) ->
Url = hackney_url:make_url(couchbeam_httpc:server_url(Server),
[couchbeam_httpc:db_url(Db), <<"_index">>], []),
Headers = [ {<<"content-type">>, <<"application/json">>},
{<<"accept">>, <<"application/json">>} ],
BodyJson = {Params},
case couchbeam_httpc:db_request(post, Url, Headers, couchbeam_ejson:encode(BodyJson), Opts,
[200, 201]) of
{ok, _, RespHeaders, Ref} ->
case hackney_headers:parse(<<"content-type">>, RespHeaders) of
{<<"multipart">>, _, _} ->
%% we get a multipart request, start to parse it.
InitialState = {Ref, fun() ->
couchbeam_httpc:wait_mp_doc(Ref, <<>>)
end},
{ok, {multipart, InitialState}};
_ ->
{ok, couchbeam_httpc:json_body(Ref)}
end;
Error ->
Error
end.

%% --------------------------------------------------------------------
%% private functions.
%% --------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/couchbeam_changes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ follow_once(Db, Options) ->

cancel_stream(Ref) ->
with_changes_stream(Ref, fun(Pid) ->
case supervisor:terminate_child(couch_view_sup, Pid) of
case supervisor:terminate_child(couchbeam_view_sup, Pid) of
ok ->
case supervisor:delete_child(couch_view_sup, Pid) of
case supervisor:delete_child(couchbeam_view_sup, Pid) of
ok ->ok;
{error, not_found} -> ok;
Error -> Error
Expand Down
17 changes: 15 additions & 2 deletions src/couchbeam_changes_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ loop(#state{owner=Owner,
maybe_reconnect(State);
{hackney_response, ClientRef, <<"\n">>} ->
maybe_continue(State);
{hackney_response, ClientRef, <<>>} -> %cause hackney send buffer on close, which could be empty
loop(State);
{hackney_response, ClientRef, Data} when is_binary(Data) ->
decode_data(Data, State);
{hackney_response, ClientRef, Error} ->
Expand Down Expand Up @@ -240,8 +242,13 @@ wait_reconnect(#state{parent=Parent,

seq(Props,#state{owner=Owner,ref=Ref}) ->
Seq = couchbeam_util:get_value(<<"seq">>, Props),
put(last_seq, Seq),
Owner ! {Ref, {change, {Props}}}.
case Seq of
undefined ->
ok;
Seq ->
put(last_seq, Seq),
Owner ! {Ref, {change, {Props}}}
end.

decode(Data) ->
jsx:decode(Data,[return_tail,stream]).
Expand Down Expand Up @@ -296,6 +303,7 @@ maybe_continue(#state{parent=Parent,
owner=Owner,
ref=Ref,
mref=MRef,
client_ref=ClientRef,
async=once}=State) ->

receive
Expand All @@ -313,6 +321,8 @@ maybe_continue(#state{parent=Parent,
{system, From, Request} ->
sys:handle_system_msg(Request, From, Parent, ?MODULE, [],
{loop, State});
{hackney_response, ClientRef, done} ->
maybe_reconnect(State);
Else ->
error_logger:error_msg("Unexpected message: ~w~n", [Else]),
%% unregister the stream
Expand All @@ -326,6 +336,7 @@ maybe_continue(#state{parent=Parent,
maybe_continue(#state{parent=Parent,
owner=Owner,
ref=Ref,
client_ref=ClientRef,
mref=MRef}=State) ->
receive
{'DOWN', MRef, _, _, _} ->
Expand All @@ -344,6 +355,8 @@ maybe_continue(#state{parent=Parent,
{system, From, Request} ->
sys:handle_system_msg(Request, From, Parent, ?MODULE, [],
{loop, State});
{hackney_response, ClientRef, done} ->
maybe_reconnect(State);
Else ->
error_logger:error_msg("Unexpected message: ~w~n", [Else]),
%% unregister the stream
Expand Down
17 changes: 4 additions & 13 deletions src/couchbeam_ejson.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,21 @@

-include("couchbeam.hrl").


-ifndef('WITH_JIFFY').
-define(JSON_ENCODE(D), jsx:encode(pre_encode(D))).
-define(JSON_DECODE(D), post_decode(jsx:decode(D))).

-else.
-define(JSON_ENCODE(D), jiffy:encode(D, [uescape])).
-define(JSON_DECODE(D), jiffy:decode(D)).
-endif.


-spec encode(ejson()) -> binary().

%% @doc encode an erlang term to JSON. Throw an exception if there is
%% any error.
encode(D) ->
?JSON_ENCODE(D).
{M, F, A} = application:get_env(couchbeam, json_encode, {jsx, encode, []}),
apply(M, F, [D | A]).

-spec decode(binary()) -> ejson().
%% @doc decode a binary to an EJSON term. Throw an exception if there is
%% any error.
decode(D) ->
try
?JSON_DECODE(D)
{M, F, A} = application:get_env(couchbeam, json_decode, {jsx, decode, []}),
apply(M, F, [D | A])
catch
throw:Error ->
throw({invalid_json, Error});
Expand Down
10 changes: 2 additions & 8 deletions src/couchbeam_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,5 @@ proxy_token(Secret,UserName) ->
hackney_bstr:to_hex(hmac(sha, Secret, UserName)).

hmac(Alg, Key, Data) ->
case {Alg, erlang:function_exported(crypto, hmac, 3)} of
{_, true} ->
crypto:hmac(Alg, Key, Data);
{sha, false} ->
crypto:sha_mac(Key, Data);
{Alg, false} ->
throw({unsupported, Alg})
end.
code:ensure_loaded(crypto),
crypto:mac(hmac, Alg, Key, Data).
Loading