Skip to content

Commit

Permalink
include mrview options in _explain result
Browse files Browse the repository at this point in the history
_explain previously returned the options passed in by the user but
not those modified at execution time by Mango. Now we include
index-specific options (mrargs for map/reduce indexes) in the output,
allowing us to see e.g. when include_docs was used.
  • Loading branch information
willholley committed Sep 4, 2017
1 parent 1bbb244 commit ad29220
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
45 changes: 26 additions & 19 deletions src/mango/src/mango_cursor_view.erl
Expand Up @@ -56,18 +56,31 @@ create(Db, Indexes, Selector, Opts) ->

explain(Cursor) ->
#cursor{
index = Idx,
ranges = Ranges
opts = Opts
} = Cursor,
case Ranges of
[empty] ->
[{range, empty}];
_ ->
[{range, {[
{start_key, mango_idx:start_key(Idx, Ranges)},
{end_key, mango_idx:end_key(Idx, Ranges)}
]}}]
end.

BaseArgs = base_args(Cursor),
Args = apply_opts(Opts, BaseArgs),
[{mrargs, {[
{include_docs, Args#mrargs.include_docs},
{view_type, Args#mrargs.view_type},
{reduce, Args#mrargs.reduce},
{start_key, Args#mrargs.start_key},
{end_key, Args#mrargs.end_key},
{direction, Args#mrargs.direction},
{stable, Args#mrargs.stable},
{update, Args#mrargs.update}
]}}].


base_args(#cursor{index = Idx} = Cursor) ->
#mrargs{
view_type = map,
reduce = false,
start_key = mango_idx:start_key(Idx, Cursor#cursor.ranges),
end_key = mango_idx:end_key(Idx, Cursor#cursor.ranges),
include_docs = true
}.


execute(#cursor{db = Db, index = Idx, execution_stats = Stats} = Cursor0, UserFun, UserAcc) ->
Expand All @@ -81,16 +94,10 @@ execute(#cursor{db = Db, index = Idx, execution_stats = Stats} = Cursor0, UserFu
% empty indicates unsatisfiable ranges, so don't perform search
{ok, UserAcc};
_ ->
BaseArgs = #mrargs{
view_type = map,
reduce = false,
start_key = mango_idx:start_key(Idx, Cursor#cursor.ranges),
end_key = mango_idx:end_key(Idx, Cursor#cursor.ranges),
include_docs = true
},
BaseArgs = base_args(Cursor),
#cursor{opts = Opts, bookmark = Bookmark} = Cursor,
Args0 = apply_opts(Opts, BaseArgs),
Args = mango_json_bookmark:update_args(Bookmark, Args0),
Args = mango_json_bookmark:update_args(Bookmark, Args0),
UserCtx = couch_util:get_value(user_ctx, Opts, #user_ctx{}),
DbOpts = [{user_ctx, UserCtx}],
Result = case mango_idx:def(Idx) of
Expand Down
13 changes: 12 additions & 1 deletion src/mango/test/02-basic-find-test.py
Expand Up @@ -14,7 +14,6 @@

import mango


class BasicFindTests(mango.UserDocsTests):

def test_bad_selector(self):
Expand Down Expand Up @@ -264,3 +263,15 @@ def test_unsatisfiable_range(self):
]
})
assert len(docs) == 0

def test_explain_view_args(self):
explain = self.db.find({
"age":{"$gt": 0}
}, fields=["manager"],
explain=True)
assert explain["mrargs"]["stable"] == False
assert explain["mrargs"]["update"] == True
assert explain["mrargs"]["reduce"] == False
assert explain["mrargs"]["start_key"] == [0]
assert explain["mrargs"]["end_key"] == [{}]
assert explain["mrargs"]["include_docs"] == True

0 comments on commit ad29220

Please sign in to comment.