Skip to content

Commit

Permalink
Make fold function more like the View counterpart (put key/bbox and I…
Browse files Browse the repository at this point in the history
…D in a tuple). Fixed a bug with multi bbox lookups (this is an internal function).
  • Loading branch information
vmx committed Mar 1, 2011
1 parent bf79a0f commit deb767d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/geocouch/couch_httpd_spatial.erl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ make_spatial_fold_funs(Req, _QueryArgs, Etag, _Db, UpdateSeq, HelperFuns) ->
% The Acc is there to output characters that belong to the previous line,
% but only if one line follows (think of a comma separated list which
% doesn't have a comma at the last item)
fun({Bbox, DocId, Value}, {Resp, Acc}) ->
fun({{Bbox, DocId}, Value}, {Resp, Acc}) ->
case Resp of
undefined ->
{ok, NewResp, BeginBody} = StartRespFun(Req, Etag, UpdateSeq),
Expand Down
4 changes: 2 additions & 2 deletions src/geocouch/couch_httpd_spatial_list.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ make_spatial_start_resp_fun(QueryServer, Db, LName) ->

% Counterpart to make_map_send_row_fun/1 in couch_http_show.
make_spatial_get_row_fun(QueryServer, Resp) ->
fun({_Bbox, _DocId, _Value}=Row, _Acc) ->
fun({{_Bbox, _DocId}, _Value}=Row, _Acc) ->
{State, Result} = send_list_row(Resp, QueryServer, Row),
{State, {Resp, Result}}
end.
Expand All @@ -141,6 +141,6 @@ send_list_row(Resp, QueryServer, Row) ->
throw({already_sent, Resp, Error})
end.

prompt_list_row({Proc, _DDocId}, {Bbox, DocId, Value}) ->
prompt_list_row({Proc, _DDocId}, {{Bbox, DocId}, Value}) ->
JsonRow = {[{id, DocId}, {key, tuple_to_list(Bbox)}, {value, Value}]},
couch_query_servers:proc_prompt(Proc, [<<"list_row">>, JsonRow]).
9 changes: 9 additions & 0 deletions src/geocouch/couch_spatial.erl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ code_change(_OldVsn, State, _Extra) ->

% counterpart in couch_view is fold/4
fold(Group, Index, FoldFun, InitAcc, Bbox, Bounds) ->
% WrapperFun = fun(Node, Acc) ->
%?LOG_DEBUG("fold: FoldFun: ~p", [Node]),
%?LOG_DEBUG("fold: FoldFun: expanded: ~p", [couch_view:expand_dups([Node], [])]),
% FoldFun(couch_view:expand_dups([Node], []), Acc)
% end,
% {_State, Acc} = vtree:lookup(
% Group#spatial_group.fd, Index#spatial.treepos, Bbox,
% {WrapperFun, InitAcc}, Bounds),
% {ok, Acc}.
{_State, Acc} = vtree:lookup(
Group#spatial_group.fd, Index#spatial.treepos, Bbox,
{FoldFun, InitAcc}, Bounds),
Expand Down
12 changes: 8 additions & 4 deletions src/vtree/vtree.erl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ fold(Fd, Pos, Fun, Acc) ->
% This lookup function is mainly for testing
lookup(Fd, Pos, Bbox) ->
% default function returns a list of 3-tuple with MBR, id, value
lookup(Fd, Pos, Bbox, {fun({Bbox2, DocId, Value}, Acc) ->
lookup(Fd, Pos, Bbox, {fun({{Bbox2, DocId}, Value}, Acc) ->
% NOTE vmx (2011-02-01) This should perhaps also be changed from
% {Bbox2, DocId, Value} to {{Bbox2, DocId}, Value}
Acc2 = [{Bbox2, DocId, Value}|Acc],
{ok, Acc2}
end, []}, nil).
Expand Down Expand Up @@ -176,14 +178,14 @@ lookup(Fd, Pos, Bboxes, {FoldFun, InitAcc}) ->
% all children are within the bbox we search with
true ->
foldl_stop(fun({Mbr, _Meta, {Id, Value}}, Acc) ->
FoldFun({Mbr, Id, Value}, Acc)
FoldFun({{Mbr, Id}, Value}, Acc)
end, InitAcc, NodesPos);
false ->
% loop through all data nodes and find not disjoint ones
foldl_stop(fun({Mbr, _Meta, {Id, Value}}, Acc) ->
case bboxes_not_disjoint(Mbr, Bboxes) of
true ->
FoldFun({Mbr, Id, Value}, Acc);
FoldFun({{Mbr, Id}, Value}, Acc);
false ->
{ok, Acc}
end
Expand All @@ -192,8 +194,10 @@ lookup(Fd, Pos, Bboxes, {FoldFun, InitAcc}) ->
end.


lookup(Fd, Pos, Bbox, FoldFunAndAcc, nil) when is_list(Bbox) ->
lookup(Fd, Pos, Bbox, FoldFunAndAcc);
lookup(Fd, Pos, Bbox, FoldFunAndAcc, nil) ->
lookup(Fd, Pos, [Bbox], FoldFunAndAcc);
lookup(Fd, Pos, Bbox, FoldFunAndAcc);
% Only a single bounding box. It may be split if it covers the data line
lookup(Fd, Pos, Bbox, FoldFunAndAcc, Bounds) when not is_list(Bbox) ->
Bboxes = split_bbox_if_flipped(Bbox, Bounds),
Expand Down

0 comments on commit deb767d

Please sign in to comment.