summaryrefslogtreecommitdiff
path: root/src/mongo/s/batched_command_request.h
blob: 829ba0ed3e2daa87c4330b361b2421391f89d1b8 (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
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "mongo/base/disallow_copying.h"
#include "mongo/s/bson_serializable.h"
#include "mongo/s/batched_insert_request.h"
#include "mongo/s/batched_update_request.h"
#include "mongo/s/batched_delete_request.h"

namespace mongo {

    /**
     * This class wraps the different kinds of command requests into a generically usable write
     * command request.
     *
     * Designed to be a very thin wrapper that mimics the underlying requests exactly.  Owns the
     * wrapped request object once constructed.
     */
    class BatchedCommandRequest : public BSONSerializable {
    MONGO_DISALLOW_COPYING(BatchedCommandRequest);
    public:

        enum BatchType {
            BatchType_Insert, BatchType_Update, BatchType_Delete, BatchType_Unknown
        };

        //
        // construction / destruction
        //

        BatchedCommandRequest( BatchType batchType );

        BatchedCommandRequest( BatchedInsertRequest* insertReq ) :
                _batchType( BatchType_Insert ), _insertReq( insertReq ) {
        }
        BatchedCommandRequest( BatchedUpdateRequest* updateReq ) :
                _batchType( BatchType_Update ), _updateReq( updateReq ) {
        }
        BatchedCommandRequest( BatchedDeleteRequest* deleteReq ) :
                _batchType( BatchType_Delete ), _deleteReq( deleteReq ) {
        }

        virtual ~BatchedCommandRequest() {};

        /** Copies all the fields present in 'this' to 'other'. */
        void cloneTo( BatchedCommandRequest* other ) const;

        //
        // bson serializable interface implementation
        //

        virtual bool isValid( std::string* errMsg ) const;
        virtual BSONObj toBSON() const;
        virtual bool parseBSON( const BSONObj& source, std::string* errMsg );
        virtual void clear();
        virtual std::string toString() const;

        //
        // individual field accessors
        //

        BatchType getBatchType() const;
        BatchedInsertRequest* getInsertRequest() const;
        BatchedUpdateRequest* getUpdateRequest() const;
        BatchedDeleteRequest* getDeleteRequest() const;

        void setNS( const StringData& collName );
        void unsetNS();
        bool isNSSet() const;
        const std::string& getNS() const;

        /**
         * Write ops are BSONObjs, whose format depends on the type of request
         * TODO: Should be possible to further parse these ops generically if we come up with a
         * good scheme.
         */
        void setWriteOps( const std::vector<BSONObj>& writeOps );
        void unsetWriteOps();
        bool isWriteOpsSet() const;
        std::size_t sizeWriteOps() const;
        std::vector<BSONObj> getWriteOps() const;

        void setWriteConcern( const BSONObj& writeConcern );
        void unsetWriteConcern();
        bool isWriteConcernSet() const;
        const BSONObj& getWriteConcern() const;

        void setOrdered( bool ordered );
        void unsetOrdered();
        bool isOrderedSet() const;
        bool getOrdered() const;

        void setShardVersion( const ChunkVersion& shardVersion );
        void unsetShardVersion();
        bool isShardVersionSet() const;
        const ChunkVersion& getShardVersion() const;

        void setSession( long long session );
        void unsetSession();
        bool isSessionSet() const;
        long long getSession() const;

    private:

        BatchType _batchType;
        scoped_ptr<BatchedInsertRequest> _insertReq;
        scoped_ptr<BatchedUpdateRequest> _updateReq;
        scoped_ptr<BatchedDeleteRequest> _deleteReq;

    };

    /**
     * Similar to above, this class wraps the write items of a command request into a generically
     * usable type.  Very thin wrapper, does not own the write item itself.
     *
     * TODO: Use in BatchedCommandRequest above
     */
    class BatchItemRef {
    public:

        BatchItemRef( const BatchedCommandRequest* request, int itemIndex ) :
            _request( request ), _itemIndex( itemIndex ) {
            dassert( itemIndex < static_cast<int>( request->sizeWriteOps() ) );
        }

        const BatchedCommandRequest* getRequest() const {
            return _request;
        }

        int getItemIndex() const {
            return _itemIndex;
        }

        BatchedCommandRequest::BatchType getOpType() const {
            return _request->getBatchType();
        }

        BSONObj getDocument() const {
            return _request->getInsertRequest()->getDocumentsAt( _itemIndex );
        }

        const BatchedUpdateDocument* getUpdate() const {
            return _request->getUpdateRequest()->getUpdatesAt( _itemIndex );
        }

        const BatchedDeleteDocument* getDelete() const {
            return _request->getDeleteRequest()->getDeletesAt( _itemIndex );
        }

    private:

        const BatchedCommandRequest* _request;
        const int _itemIndex;
    };

} // namespace mongo