summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_view.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/couchdb/couch_view.erl')
-rw-r--r--src/couchdb/couch_view.erl104
1 files changed, 45 insertions, 59 deletions
diff --git a/src/couchdb/couch_view.erl b/src/couchdb/couch_view.erl
index f9218e1d..80850b65 100644
--- a/src/couchdb/couch_view.erl
+++ b/src/couchdb/couch_view.erl
@@ -369,81 +369,67 @@ nuke_dir(Dir) ->
% keys come back in the language of btree - tuples.
less_json_ids({JsonA, IdA}, {JsonB, IdB}) ->
- case JsonA == JsonB of
- false ->
- less_json(JsonA, JsonB);
- true ->
- IdA < IdB
- end.
-
-
-less_json(A, B) ->
- TypeA = type_sort(A),
- TypeB = type_sort(B),
- if TypeA == TypeB ->
- less_same_type(A, B);
- true ->
- TypeA < TypeB
+ case less_json0(JsonA, JsonB) of
+ 0 ->
+ IdA < IdB;
+ Result ->
+ Result < 0
end.
-type_sort(V) when is_atom(V) -> 0;
-type_sort(V) when is_integer(V) -> 1;
-type_sort(V) when is_float(V) -> 1;
-type_sort(V) when is_binary(V) -> 2;
-type_sort(V) when is_list(V) -> 3;
-type_sort({V}) when is_list(V) -> 4;
-type_sort(V) when is_tuple(V) -> 5.
+less_json(A,B) ->
+ less_json0(A,B) < 0.
+
+less_json0(A,A) -> 0;
+
+less_json0(A,B) when is_atom(A), is_atom(B) -> atom_sort(A) - atom_sort(B);
+less_json0(A,_) when is_atom(A) -> -1;
+less_json0(_,B) when is_atom(B) -> 1;
+
+less_json0(A,B) when is_number(A), is_number(B) -> A - B;
+less_json0(A,_) when is_number(A) -> -1;
+less_json0(_,B) when is_number(B) -> 1;
+
+less_json0(A,B) when is_binary(A), is_binary(B) -> couch_util:collate(A,B);
+less_json0(A,_) when is_binary(A) -> -1;
+less_json0(_,B) when is_binary(B) -> 1;
+
+less_json0(A,B) when is_list(A), is_list(B) -> less_list(A,B);
+less_json0(A,_) when is_list(A) -> -1;
+less_json0(_,B) when is_list(B) -> 1;
+less_json0({A},{B}) when is_list(A), is_list(B) -> less_props(A,B);
+less_json0({A},_) when is_list(A) -> -1;
+less_json0(_,{B}) when is_list(B) -> 1.
atom_sort(null) -> 1;
atom_sort(false) -> 2;
atom_sort(true) -> 3.
-
-less_same_type(A,B) when is_atom(A) ->
- atom_sort(A) < atom_sort(B);
-less_same_type(A,B) when is_binary(A) ->
- couch_util:collate(A, B) < 0;
-less_same_type({AProps}, {BProps}) ->
- less_props(AProps, BProps);
-less_same_type(A, B) when is_list(A) ->
- less_list(A, B);
-less_same_type(A, B) ->
- A < B.
-
less_props([], [_|_]) ->
- true;
+ -1;
less_props(_, []) ->
- false;
+ 1;
less_props([{AKey, AValue}|RestA], [{BKey, BValue}|RestB]) ->
case couch_util:collate(AKey, BKey) of
- -1 -> true;
- 1 -> false;
0 ->
- case less_json(AValue, BValue) of
- true -> true;
- false ->
- case less_json(BValue, AValue) of
- true -> false;
- false ->
- less_props(RestA, RestB)
- end
- end
+ case less_json0(AValue, BValue) of
+ 0 ->
+ less_props(RestA, RestB);
+ Result ->
+ Result
+ end;
+ Result ->
+ Result
end.
less_list([], [_|_]) ->
- true;
+ -1;
less_list(_, []) ->
- false;
+ 1;
less_list([A|RestA], [B|RestB]) ->
- case less_json(A,B) of
- true -> true;
- false ->
- case less_json(B,A) of
- true -> false;
- false ->
- less_list(RestA, RestB)
- end
+ case less_json0(A,B) of
+ 0 ->
+ less_list(RestA, RestB);
+ Result ->
+ Result
end.
-
-