summaryrefslogtreecommitdiff
path: root/src/mango/src/mango_idx.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mango/src/mango_idx.erl')
-rw-r--r--src/mango/src/mango_idx.erl105
1 files changed, 99 insertions, 6 deletions
diff --git a/src/mango/src/mango_idx.erl b/src/mango/src/mango_idx.erl
index 8af92b946..1a98047a7 100644
--- a/src/mango/src/mango_idx.erl
+++ b/src/mango/src/mango_idx.erl
@@ -33,6 +33,7 @@
name/1,
type/1,
def/1,
+ partitioned/1,
opts/1,
columns/1,
is_usable/3,
@@ -59,8 +60,10 @@ list(Db) ->
get_usable_indexes(Db, Selector, Opts) ->
ExistingIndexes = mango_idx:list(Db),
-
- GlobalIndexes = mango_cursor:remove_indexes_with_partial_filter_selector(ExistingIndexes),
+ MatchingPartitionIndexes = filter_partition_indexes(ExistingIndexes, Opts),
+ GlobalIndexes = mango_cursor:remove_indexes_with_partial_filter_selector(
+ MatchingPartitionIndexes
+ ),
UserSpecifiedIndex = mango_cursor:maybe_filter_indexes_by_ddoc(ExistingIndexes, Opts),
UsableIndexes0 = lists:usort(GlobalIndexes ++ UserSpecifiedIndex),
@@ -110,6 +113,7 @@ new(Db, Opts) ->
name = IdxName,
type = Type,
def = Def,
+ partitioned = get_idx_partitioned(Opts),
opts = filter_opts(Opts)
}}.
@@ -121,10 +125,11 @@ validate_new(Idx, Db) ->
add(DDoc, Idx) ->
Mod = idx_mod(Idx),
- {ok, NewDDoc} = Mod:add(DDoc, Idx),
+ {ok, NewDDoc1} = Mod:add(DDoc, Idx),
+ NewDDoc2 = set_ddoc_partitioned(NewDDoc1, Idx),
% Round trip through JSON for normalization
- Body = ?JSON_DECODE(?JSON_ENCODE(NewDDoc#doc.body)),
- {ok, NewDDoc#doc{body = Body}}.
+ Body = ?JSON_DECODE(?JSON_ENCODE(NewDDoc2#doc.body)),
+ {ok, NewDDoc2#doc{body = Body}}.
remove(DDoc, Idx) ->
@@ -176,7 +181,8 @@ from_ddoc(Db, {Props}) ->
lists:map(fun(Idx) ->
Idx#idx{
dbname = DbName,
- ddoc = DDoc
+ ddoc = DDoc,
+ partitioned = set_idx_partitioned(Db, Props)
}
end, Idxs).
@@ -213,6 +219,10 @@ def(#idx{def=Def}) ->
Def.
+partitioned(#idx{partitioned=Partitioned}) ->
+ Partitioned.
+
+
opts(#idx{opts=Opts}) ->
Opts.
@@ -329,6 +339,87 @@ gen_name(Idx, Opts0) ->
mango_util:enc_hex(Sha).
+get_idx_partitioned(Opts) ->
+ case proplists:get_value(partitioned, Opts) of
+ B when is_boolean(B) ->
+ B;
+ default ->
+ undefined
+ end.
+
+
+set_ddoc_partitioned(DDoc, Idx) ->
+ % We have to verify that the new index being added
+ % to this design document either matches the current
+ % ddoc's design options *or* this is a new design doc
+ #doc{
+ id = DDocId,
+ revs = Revs,
+ body = {BodyProps}
+ } = DDoc,
+ OldDOpts = couch_util:get_value(<<"options">>, BodyProps),
+ OldOpt = case OldDOpts of
+ {OldDOptProps} when is_list(OldDOptProps) ->
+ couch_util:get_value(<<"partitioned">>, OldDOptProps);
+ _ ->
+ undefined
+ end,
+ % If new matches old we're done
+ if Idx#idx.partitioned == OldOpt -> DDoc; true ->
+ % If we're creating a ddoc then we can set the options
+ case Revs == {0, []} of
+ true when Idx#idx.partitioned /= undefined ->
+ set_ddoc_partitioned_option(DDoc, Idx#idx.partitioned);
+ true when Idx#idx.partitioned == undefined ->
+ DDoc;
+ false ->
+ ?MANGO_ERROR({partitioned_option_mismatch, DDocId})
+ end
+ end.
+
+
+set_ddoc_partitioned_option(DDoc, Partitioned) ->
+ #doc{
+ body = {BodyProps}
+ } = DDoc,
+ NewProps = case couch_util:get_value(<<"options">>, BodyProps) of
+ {Existing} when is_list(Existing) ->
+ Opt = {<<"partitioned">>, Partitioned},
+ New = lists:keystore(<<"partitioned">>, 1, Existing, Opt),
+ lists:keystore(<<"options">>, 1, BodyProps, {<<"options">>, New});
+ undefined ->
+ New = {<<"options">>, {[{<<"partitioned">>, Partitioned}]}},
+ lists:keystore(<<"options">>, 1, BodyProps, New)
+ end,
+ DDoc#doc{body = {NewProps}}.
+
+
+set_idx_partitioned(Db, DDocProps) ->
+ Default = fabric_util:is_partitioned(Db),
+ case couch_util:get_value(<<"options">>, DDocProps) of
+ {DesignOpts} ->
+ case couch_util:get_value(<<"partitioned">>, DesignOpts) of
+ P when is_boolean(P) ->
+ P;
+ undefined ->
+ Default
+ end;
+ undefined ->
+ Default
+ end.
+
+
+filter_partition_indexes(Indexes, Opts) ->
+ PFilt = case couch_util:get_value(partition, Opts) of
+ <<>> ->
+ fun(#idx{partitioned = P}) -> not P end;
+ Partition when is_binary(Partition) ->
+ fun(#idx{partitioned = P}) -> P end
+ end,
+ Filt = fun(Idx) -> type(Idx) == <<"special">> orelse PFilt(Idx) end,
+ lists:filter(Filt, Indexes).
+
+
filter_opts([]) ->
[];
filter_opts([{user_ctx, _} | Rest]) ->
@@ -341,6 +432,8 @@ filter_opts([{type, _} | Rest]) ->
filter_opts(Rest);
filter_opts([{w, _} | Rest]) ->
filter_opts(Rest);
+filter_opts([{partitioned, _} | Rest]) ->
+ filter_opts(Rest);
filter_opts([Opt | Rest]) ->
[Opt | filter_opts(Rest)].