Permalink
Browse files

Implemented db.[collection].count()

  • Loading branch information...
1 parent 768a79d commit e0bf02aa84eeaa5e156111f12191d53dc87b1137 @wardbekker wardbekker committed Jun 13, 2012
Showing with 15 additions and 2 deletions.
  1. +1 −0 README.md
  2. +5 −0 src/riak_mongo_message.erl
  3. +0 −1 src/riak_mongo_protocol.erl
  4. +9 −1 src/riak_mongo_riak.erl
View
@@ -26,6 +26,7 @@ Then you can do some basic commands:
> db.things.findOne()
> db.things.find({a:1})
> db.things.find({a:1}, {b:1})
+ > db.things.count()
> db.things.remove({a:1})
> db.things.remove()
> db.things.insert({a:1})
@@ -19,6 +19,7 @@
%% @author Kresten Krab Thorup <krab@trifork.com>
%% @author Pavlo Baron <pb at pbit dot org>
+%% @author Ward Bekker <ward@equanimity.nl>
%% @doc Here we process all kind of messages
%% @copyright 2012 Pavlo Baron
@@ -35,6 +36,7 @@
-define(ADM, <<"admin">>).
-define(DROP, <<"drop">>).
-define(COLLSTATS, <<"collstats">>).
+-define(COUNT, <<"count">>).
%%
%% loop over messages
@@ -171,6 +173,9 @@ db_command(DataBase, ?DROP, Collection, _Options, State) ->
db_command(DataBase, ?COLLSTATS, Collection, _Options, State) ->
riak_mongo_riak:stats(riak_mongo_protocol:join_dbcoll({DataBase, Collection}), State);
+db_command(DataBase, ?COUNT, Collection, _Options, State) ->
+ riak_mongo_riak:count(riak_mongo_protocol:join_dbcoll({DataBase, Collection}), State);
+
db_command(DataBase, Command, Collection, _Options, State) ->
error_logger:info_msg("unhandled command: ~p, ~p:~p~n", [Command, DataBase, Collection]),
{ok, [{err, <<"unknown command: db=", DataBase, ", cmd=", Command/binary>>}, {ok, false}], State}.
@@ -141,7 +141,6 @@ decode_packet(<< ?HDR(_,OP), _/binary >> = All) ->
error_logger:info_msg("bad ~w~n", [All]),
exit({error, {bad_message, RequestId, OP}}).
-
split_dbcoll(Bin) ->
{Pos, _Len} = binary:match (Bin, <<$.>>),
<<DB :Pos /binary, $.:8, Coll /binary>> = Bin,
View
@@ -31,7 +31,7 @@
-compile([{parse_transform, lager_transform}]).
--export([insert/2, find/2, getmore/2, delete/2, update/2, stats/2]).
+-export([insert/2, find/2, getmore/2, delete/2, update/2, stats/2, count/2]).
-define(DEFAULT_TIMEOUT, 60000).
-define(DEFAULT_FIND_SIZE, 101).
@@ -214,6 +214,14 @@ delete(#mongo_delete{dbcoll=Bucket, selector=Selector, singleremove=SingleRemove
end
end.
+count(Bucket, State) ->
+ {ok, C} = riak:local_client(),
+ {ok, [Count]} = C:mapred(
+ Bucket,
+ [riak_kv_mapreduce:reduce_count_inputs(true)]
+ ),
+ Doc = [{n, Count}],
+ {ok, Doc, State}.
%% internals

0 comments on commit e0bf02a

Please sign in to comment.