1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <vector>
34 
35 #include "mongo/base/status_with.h"
36 
37 #include "mongo/db/jsobj.h"
38 #include "mongo/db/namespace_string.h"
39 
40 namespace mongo {
41 
42 /**
43  * Provides support for parsing and serialization of arguments to the config server mergeChunk
44  * command.
45  */
46 class MergeChunkRequest {
47 public:
48     MergeChunkRequest(NamespaceString nss,
49                       std::string shardName,
50                       OID epoch,
51                       std::vector<BSONObj> chunkBoundaries);
52 
53     /**
54      * Parses the provided BSON content as the internal _configsvrCommitChunkMerge command, and if
55      * it contains the correct types, constructs a MergeChunkRequest object from it.
56      *
57      * {
58      *   _configsvrCommitChunkMerge: <NamespaceString nss>,
59      *   collEpoch: <OID epoch>,
60      *   chunkBoundaries: [
61      *       <BSONObj key1>,
62      *       <BSONObj key2>,
63      *       ...
64      *   ],
65      *   shard: <string shard>
66      * }
67      */
68     static StatusWith<MergeChunkRequest> parseFromConfigCommand(const BSONObj& cmdObj);
69 
70     /**
71      * Creates a BSONObjBuilder and uses it to create and return a BSONObj from this
72      * MergeChunkRequest instance. Calls appendAsConfigCommand and tacks on the passed-in
73      * writeConcern.
74      */
75     BSONObj toConfigCommandBSON(const BSONObj& writeConcern);
76 
77     /**
78      * Creates a serialized BSONObj of the internal _configsvCommitChunkMerge command from this
79      * MergeChunkRequest instance.
80      */
81     void appendAsConfigCommand(BSONObjBuilder* cmdBuilder);
82 
getNamespace()83     const NamespaceString& getNamespace() const {
84         return _nss;
85     }
86 
getEpoch()87     const OID& getEpoch() const {
88         return _epoch;
89     }
90 
getChunkBoundaries()91     const std::vector<BSONObj>& getChunkBoundaries() const {
92         return _chunkBoundaries;
93     }
94 
getShardName()95     const std::string& getShardName() const {
96         return _shardName;
97     }
98 
99 private:
100     NamespaceString _nss;
101     OID _epoch;
102 
103     // The boundaries of the chunks to be merged.
104     std::vector<BSONObj> _chunkBoundaries;
105 
106     std::string _shardName;
107 };
108 
109 }  // namespace mongo
110