Skip to content

Commit

Permalink
Implemented db.[collection].count()
Browse files Browse the repository at this point in the history
  • Loading branch information
wardbekker committed Jun 13, 2012
1 parent 768a79d commit e0bf02a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Then you can do some basic commands:
> db.things.findOne() > db.things.findOne()
> db.things.find({a:1}) > db.things.find({a:1})
> db.things.find({a:1}, {b:1}) > db.things.find({a:1}, {b:1})
> db.things.count()
> db.things.remove({a:1}) > db.things.remove({a:1})
> db.things.remove() > db.things.remove()
> db.things.insert({a:1}) > db.things.insert({a:1})
Expand Down
5 changes: 5 additions & 0 deletions src/riak_mongo_message.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


%% @author Kresten Krab Thorup <krab@trifork.com> %% @author Kresten Krab Thorup <krab@trifork.com>
%% @author Pavlo Baron <pb at pbit dot org> %% @author Pavlo Baron <pb at pbit dot org>
%% @author Ward Bekker <ward@equanimity.nl>
%% @doc Here we process all kind of messages %% @doc Here we process all kind of messages
%% @copyright 2012 Pavlo Baron %% @copyright 2012 Pavlo Baron


Expand All @@ -35,6 +36,7 @@
-define(ADM, <<"admin">>). -define(ADM, <<"admin">>).
-define(DROP, <<"drop">>). -define(DROP, <<"drop">>).
-define(COLLSTATS, <<"collstats">>). -define(COLLSTATS, <<"collstats">>).
-define(COUNT, <<"count">>).


%% %%
%% loop over messages %% loop over messages
Expand Down Expand Up @@ -171,6 +173,9 @@ db_command(DataBase, ?DROP, Collection, _Options, State) ->
db_command(DataBase, ?COLLSTATS, Collection, _Options, State) -> db_command(DataBase, ?COLLSTATS, Collection, _Options, State) ->
riak_mongo_riak:stats(riak_mongo_protocol:join_dbcoll({DataBase, Collection}), 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) -> db_command(DataBase, Command, Collection, _Options, State) ->
error_logger:info_msg("unhandled command: ~p, ~p:~p~n", [Command, DataBase, Collection]), 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}. {ok, [{err, <<"unknown command: db=", DataBase, ", cmd=", Command/binary>>}, {ok, false}], State}.
1 change: 0 additions & 1 deletion src/riak_mongo_protocol.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ decode_packet(<< ?HDR(_,OP), _/binary >> = All) ->
error_logger:info_msg("bad ~w~n", [All]), error_logger:info_msg("bad ~w~n", [All]),
exit({error, {bad_message, RequestId, OP}}). exit({error, {bad_message, RequestId, OP}}).



split_dbcoll(Bin) -> split_dbcoll(Bin) ->
{Pos, _Len} = binary:match (Bin, <<$.>>), {Pos, _Len} = binary:match (Bin, <<$.>>),
<<DB :Pos /binary, $.:8, Coll /binary>> = Bin, <<DB :Pos /binary, $.:8, Coll /binary>> = Bin,
Expand Down
10 changes: 9 additions & 1 deletion src/riak_mongo_riak.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


-compile([{parse_transform, lager_transform}]). -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_TIMEOUT, 60000).
-define(DEFAULT_FIND_SIZE, 101). -define(DEFAULT_FIND_SIZE, 101).
Expand Down Expand Up @@ -214,6 +214,14 @@ delete(#mongo_delete{dbcoll=Bucket, selector=Selector, singleremove=SingleRemove
end end
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 %% internals


Expand Down

7 comments on commit e0bf02a

@krestenkrab
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superb! Looks good.

@pavlobaron
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah! I'll open the repo to you both so you can just push to master. I'm among serious hackers there, no need for bottle necks looking at things between the travels at the airport...

@wardbekker
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx!

@pavlobaron
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, can it be that count also returnes deleted ones? I've been showing riak_mongo to a colleague and, without a further check, ran into the issue that after deleting the objects they still have been counted...

@krestenkrab
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can test if an object is deleted using riak_kv_util:is_x_deleted/1, folding/mr can return deleted objects (tombstones)

@pavlobaron
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reworked count() with low level piped mapred, no tombstones anymore

@krestenkrab
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Please sign in to comment.