1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2017 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <memcached/engine.h>
21 #include <memcached/engine_error.h>
22 #include <platform/sized_buffer.h>
23 
24 #include <memory>
25 #include <mutex>
26 
27 class KVBucket;
28 class VBucket;
29 
30 namespace Collections {
31 
32 class Manifest;
33 class Filter;
34 
35 /**
36  * Collections::Manager provides some bucket level management functions
37  * such as the code which enables the MCBP set_collections command.
38  */
39 class Manager {
40 public:
41     Manager();
42 
43     /**
44      * Update the bucket with the latest JSON collections manifest.
45      *
46      * Locks the Manager and prevents concurrent updates, concurrent updates
47      * are failed with TMPFAIL as in reality there should be 1 admin connection.
48      *
49      * @param bucket the bucket receiving a set-collections command.
50      * @param json the json manifest form a set-collections command.
51      * @returns engine_error indicating why the update failed.
52      */
53     cb::engine_error update(KVBucket& bucket, const std::string& json);
54 
55     /**
56      * Retrieve the current manifest
57      * @return JSON version of the current manifest
58      */
59     cb::EngineErrorStringPair getManifest() const;
60 
61     /**
62      * Update the vbucket's manifest with the current Manifest
63      * The Manager is locked to prevent current changing whilst this update
64      * occurs.
65      */
66     void update(VBucket& vb) const;
67 
68     /**
69      * make a Collections::Filter
70      * @param dcpOpenFlags The dcp flags, we will test for DCP_OPEN_COLLECTIONS
71      * @param jsonExtra a buffer which may contain JSON data (to be verified by
72      *        filter construction).
73      * @returns a Collections::Filter
74      * @throws exceptions generated by Collections::Filter::Filter
75      */
76     Collections::Filter makeFilter(uint32_t dcpOpenFlags,
77                                    cb::const_byte_buffer jsonExtra) const;
78 
79     /**
80      * For development, log as much collections stuff as we can
81      */
82     void logAll(KVBucket& bucket) const;
83 
84     /**
85      * Write to std::cerr this
86      */
87     void dump() const;
88 
89 private:
90     friend std::ostream& operator<<(std::ostream& os, const Manager& manager);
91 
92     mutable std::mutex lock;
93 
94     /// Store the most recent (current) manifest received
95     std::unique_ptr<Manifest> current;
96 };
97 
98 std::ostream& operator<<(std::ostream& os, const Manager& manager);
99 }