summaryrefslogtreecommitdiff
path: root/src/couch/src/couch_partition.erl
blob: 101b5b324327aaf4cffa8c17d53e1beedb0d6b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_partition).

-export([
    extract/1,
    from_docid/1,
    is_member/2,

    start_key/1,
    end_key/1,
    shard_key/1,

    validate_dbname/2,
    validate_docid/1,
    validate_partition/1,

    hash/1
]).

-include_lib("couch/include/couch_db.hrl").

extract(Value) when is_binary(Value) ->
    case binary:split(Value, <<":">>) of
        [Partition, Rest] ->
            {Partition, Rest};
        _ ->
            undefined
    end;
extract(_) ->
    undefined.

from_docid(DocId) ->
    case extract(DocId) of
        undefined ->
            throw({illegal_docid, <<"Doc id must be of form partition:id">>});
        {Partition, _} ->
            Partition
    end.

is_member(DocId, Partition) ->
    case extract(DocId) of
        {Partition, _} ->
            true;
        _ ->
            false
    end.

start_key(Partition) ->
    <<Partition/binary, ":">>.

end_key(Partition) ->
    <<Partition/binary, ";">>.

shard_key(Partition) ->
    <<Partition/binary, ":foo">>.

validate_dbname(DbName, Options) when is_list(DbName) ->
    validate_dbname(?l2b(DbName), Options);
validate_dbname(DbName, Options) when is_binary(DbName) ->
    Props = couch_util:get_value(props, Options, []),
    IsPartitioned = couch_util:get_value(partitioned, Props, false),

    if
        not IsPartitioned ->
            ok;
        true ->
            DbsDbName = config:get("mem3", "shards_db", "_dbs"),
            NodesDbName = config:get("mem3", "nodes_db", "_nodes"),
            UsersDbSuffix = config:get("couchdb", "users_db_suffix", "_users"),
            Suffix = couch_db:dbname_suffix(DbName),

            SysDbNames = [
                iolist_to_binary(DbsDbName),
                iolist_to_binary(NodesDbName)
                | ?SYSTEM_DATABASES
            ],

            Suffices = [
                <<"_replicator">>,
                <<"_users">>,
                iolist_to_binary(UsersDbSuffix)
            ],

            IsSysDb =
                lists:member(DbName, SysDbNames) orelse
                    lists:member(Suffix, Suffices),

            if
                not IsSysDb -> ok;
                true -> throw({bad_request, <<"Cannot partition a system database">>})
            end
    end.

validate_docid(<<"_design/", _/binary>>) ->
    ok;
validate_docid(<<"_local/", _/binary>>) ->
    ok;
validate_docid(DocId) when is_binary(DocId) ->
    % When this function is called we already know that
    % DocId is already valid thus we only need to
    % ensure that the partition exists and is not empty.
    case extract(DocId) of
        undefined ->
            throw({illegal_docid, <<"Doc id must be of form partition:id">>});
        {Partition, PartitionedDocId} ->
            validate_partition(Partition),
            couch_doc:validate_docid(PartitionedDocId)
    end.

validate_partition(<<>>) ->
    throw({illegal_partition, <<"Partition must not be empty">>});
validate_partition(Partition) when is_binary(Partition) ->
    case Partition of
        <<"_", _/binary>> ->
            Msg1 = <<"Partition must not start with an underscore">>,
            throw({illegal_partition, Msg1});
        _ ->
            ok
    end,
    case couch_util:validate_utf8(Partition) of
        true ->
            ok;
        false ->
            Msg2 = <<"Partition must be valid UTF-8">>,
            throw({illegal_partition, Msg2})
    end,
    case extract(Partition) of
        {_, _} ->
            Msg3 = <<"Partition must not contain a colon">>,
            throw({illegal_partition, Msg3});
        undefined ->
            ok
    end;
validate_partition(_) ->
    throw({illegal_partition, <<"Partition must be a string">>}).

% Document ids that start with an underscore
% (i.e., _local and _design) do not contain a
% partition and thus do not use the partition
% hashing.
hash(<<"_", _/binary>> = DocId) ->
    erlang:crc32(DocId);
hash(DocId) when is_binary(DocId) ->
    erlang:crc32(from_docid(DocId)).