summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/collection_options.h
blob: f5dc1dd0c55d02b73700396d071e899a24de0d6a (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include <boost/optional.hpp>
#include <string>

#include "mongo/base/status.h"
#include "mongo/crypto/encryption_fields_gen.h"
#include "mongo/db/catalog/clustered_collection_options_gen.h"
#include "mongo/db/catalog/collection_options_gen.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/pipeline/change_stream_pre_and_post_images_options_gen.h"
#include "mongo/db/timeseries/timeseries_gen.h"
#include "mongo/util/uuid.h"

namespace mongo {

class CollatorFactoryInterface;
class CreateCommand;

struct CollectionOptions {
    /**
     * Returns true if the options indicate the namespace is a view.
     */
    bool isView() const;

    /**
     * The 'uuid' member is a collection property stored in the catalog with user-settable options,
     * but is not valid for the user to specify as collection option. So, parsing commands must
     * reject the 'uuid' property, but parsing stored options must accept it.
     */
    enum ParseKind { parseForCommand, parseForStorage };

    /**
     * Confirms that collection options can be converted to BSON and back without errors.
     */
    Status validateForStorage() const;

    /**
     * Parses the collection 'options' into the appropriate struct fields.
     *
     * When 'kind' is set to ParseKind::parseForStorage, the 'uuid' field is parsed,
     * otherwise the 'uuid' field is not parsed.
     *
     * When 'kind' is set to ParseKind::parseForCommand, the 'idIndex' field is parsed,
     * otherwise the 'idIndex' field is not parsed.
     */
    static StatusWith<CollectionOptions> parse(const BSONObj& options,
                                               ParseKind kind = parseForCommand);

    /**
     * Converts a client "create" command invocation.
     */
    static CollectionOptions fromCreateCommand(const CreateCommand& cmd);

    static StatusWith<long long> checkAndAdjustCappedSize(long long cappedSize);
    static StatusWith<long long> checkAndAdjustCappedMaxDocs(long long cappedMaxDocs);

    /**
     * Serialize to BSON. The 'includeUUID' parameter is used for the listCollections command to do
     * special formatting for the uuid. Aside from the UUID, if 'includeFields' is non-empty, only
     * the specified fields will be included.
     */
    void appendBSON(BSONObjBuilder* builder,
                    bool includeUUID,
                    const StringDataSet& includeFields) const;
    BSONObj toBSON(bool includeUUID = true, const StringDataSet& includeFields = {}) const;

    /**
     * Returns true if given options matches to this.
     *
     * Uses the collatorFactory to normalize the collation property being compared.
     *
     * Note: ignores idIndex property.
     */
    bool matchesStorageOptions(const CollectionOptions& other,
                               CollatorFactoryInterface* collatorFactory) const;

    // Collection UUID. If not set, specifies that the storage engine should generate the UUID (for
    // a new collection). For an existing collection parsed for storage, it will always be present.
    boost::optional<UUID> uuid;

    bool capped = false;
    long long cappedSize = 0;
    long long cappedMaxDocs = 0;

    // The behavior of _id index creation when collection created
    void setNoIdIndex() {
        autoIndexId = NO;
    }
    enum {
        DEFAULT,  // currently yes for most collections, NO for some system ones
        YES,      // create _id index
        NO        // do not create _id index
    } autoIndexId = DEFAULT;

    bool temp = false;

    // Indicates whether "recordPreImages" collection option was used in the collection definition.
    // This needs to be remembered to make it possible to efficiently remove the option upon FCV
    // upgrade. Otherwise, this option is not supported.
    // TODO SERVER-74036: Remove once FCV 7.0 becomes last-LTS.
    bool recordPreImagesOptionUsed{false};

    // Change stream options define whether or not to store the pre-images of the documents affected
    // by update and delete operations in a dedicated collection, that will be used for reading data
    // via changeStreams.
    ChangeStreamPreAndPostImagesOptions changeStreamPreAndPostImagesOptions{false};

    // Storage engine collection options. Always owned or empty.
    BSONObj storageEngine;

    // Default options for indexes created on the collection.
    IndexOptionDefaults indexOptionDefaults;

    // Index specs for the _id index.
    BSONObj idIndex;

    // Always owned or empty.
    BSONObj validator;
    boost::optional<ValidationActionEnum> validationAction;
    boost::optional<ValidationLevelEnum> validationLevel;

    // The namespace's default collation.
    BSONObj collation;

    // Exists if the collection is clustered.
    boost::optional<ClusteredCollectionInfo> clusteredIndex;

    // If present, the number of seconds after which old data should be deleted. Only for
    // collections which are clustered on _id.
    boost::optional<int64_t> expireAfterSeconds;

    // View-related options.
    // The namespace of the view or collection that "backs" this view, or the empty string if this
    // collection is not a view.
    std::string viewOn;
    // The aggregation pipeline that defines this view.
    BSONObj pipeline;

    // The options that define the time-series collection, or boost::none if not a time-series
    // collection.
    boost::optional<TimeseriesOptions> timeseries;

    // The options for collections with encrypted fields
    boost::optional<EncryptedFieldConfig> encryptedFieldConfig;
};
}  // namespace mongo