Couchbase Transactions C++ Client  1.0.0
Transactions client for couchbase
collection.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2021 Couchbase, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <chrono>
25 #include <couchbase/support.hxx>
26 #include <string>
27 #include <thread>
28 #include <vector>
29 
35 namespace couchbase
36 {
37 
38 enum class store_operation { upsert, insert, replace };
39 
40 result
41 store_impl(collection* coll, store_operation op, const std::string& id, const std::string& payload, uint64_t cas, durability_level level);
42 
47 {
48  friend class bucket;
49 
50  private:
51  std::string scope_;
52  std::string name_;
53  std::weak_ptr<bucket> bucket_;
54  std::chrono::microseconds kv_timeout_;
55  static const std::string DEFAULT;
56 
57  friend result store_impl(collection* coll,
58  store_operation op,
59  const std::string& id,
60  const std::string& payload,
61  uint64_t cas,
62  durability_level level,
63  std::chrono::microseconds timeout);
64 
65  template<typename Content>
66  result store(store_operation operation,
67  const std::string& id,
68  const Content& value,
69  uint64_t cas,
70  durability_level level,
71  std::chrono::microseconds timeout)
72  {
73  nlohmann::json j = value;
74  std::string payload = j.dump();
75  return store_impl(this, operation, id, payload, cas, level, timeout);
76  }
77 
78  std::unique_ptr<pool<lcb_st*>>& instance_pool()
79  {
80  return bucket_.lock()->instance_pool_;
81  }
82  result wrap_call_for_retry(std::chrono::microseconds timeout, std::function<result(std::chrono::microseconds)> fn);
83 
84  collection(std::shared_ptr<bucket> bucket, std::string scope, std::string name, std::chrono::microseconds kv_timeout);
85 
86  static void install_callbacks(lcb_st* lcb);
87 
88  bool set_collection()
89  {
90  return (name_ != bucket::default_name || scope_ != bucket::default_name);
91  }
92 
93  public:
104  result get(const std::string& id, const get_options& opts = get_options());
105 
113  result exists(const std::string& id, const exists_options& opts = exists_options());
114 
126  template<typename Content>
127  result upsert(const std::string& id, const Content& value, const upsert_options& opts = upsert_options())
128  {
129  return wrap_call_for_retry(boost::get_optional_value_or(opts.timeout(), default_kv_timeout()),
130  [&](std::chrono::microseconds timeout) -> result {
131  return store(store_operation::upsert,
132  id,
133  value,
134  boost::get_optional_value_or(opts.cas(), 0),
135  boost::get_optional_value_or(opts.durability(), durability_level::none),
136  timeout);
137  });
138  }
139 
151  template<typename Content>
152  result insert(const std::string& id, const Content& value, const insert_options& opts = insert_options())
153  {
154  return wrap_call_for_retry(
155  boost::get_optional_value_or(opts.timeout(), default_kv_timeout()), [&](std::chrono::microseconds timeout) -> result {
156  return store(
157  store_operation::insert, id, value, 0, boost::get_optional_value_or(opts.durability(), durability_level::none), timeout);
158  });
159  }
160 
173  template<typename Content>
174  result replace(const std::string& id, const Content& value, const replace_options& opts = replace_options())
175  {
176  return wrap_call_for_retry(boost::get_optional_value_or(opts.timeout(), default_kv_timeout()),
177  [&](std::chrono::microseconds timeout) -> result {
178  return store(store_operation::replace,
179  id,
180  value,
181  boost::get_optional_value_or(opts.cas(), 0),
182  boost::get_optional_value_or(opts.durability(), durability_level::none),
183  timeout);
184  });
185  }
186 
197  result remove(const std::string& id, const remove_options& opts = remove_options());
198 
210  result mutate_in(const std::string& id, std::vector<mutate_in_spec> specs, const mutate_in_options& opts = mutate_in_options());
211 
223  result lookup_in(const std::string& id, std::vector<lookup_in_spec> specs, const lookup_in_options& opts = lookup_in_options());
224 
230  CB_NODISCARD const std::string& name() const
231  {
232  return name_;
233  }
234 
240  CB_NODISCARD const std::string& scope() const
241  {
242  return scope_;
243  }
244 
250  CB_NODISCARD const std::string& bucket_name() const
251  {
252  return bucket_.lock()->name();
253  }
254 
260  CB_NODISCARD std::shared_ptr<couchbase::bucket> get_bucket()
261  {
262  return bucket_.lock();
263  }
264 
271  CB_NODISCARD std::chrono::microseconds default_kv_timeout() const
272  {
273  return kv_timeout_;
274  }
275 };
276 } // namespace couchbase
result mutate_in(const std::string &id, std::vector< mutate_in_spec > specs, const mutate_in_options &opts=mutate_in_options())
Mutate some elements of a document.
CB_NODISCARD std::shared_ptr< couchbase::bucket > get_bucket()
Get bucket for this collection.
Definition: collection.hxx:260
Options for collection::insert()
Definition: options.hxx:222
Exposes bucket-level operations and collections accessors.
Definition: bucket.hxx:45
Definition: bucket.hxx:33
Options for collection::remove()
Definition: options.hxx:215
result insert(const std::string &id, const Content &value, const insert_options &opts=insert_options())
Insert document.
Definition: collection.hxx:152
result exists(const std::string &id, const exists_options &opts=exists_options())
Test existence of a document.
The result of an operation on a cluster.
Definition: result.hxx:113
result lookup_in(const std::string &id, std::vector< lookup_in_spec > specs, const lookup_in_options &opts=lookup_in_options())
Lookup some elements of a document.
Options for collection::exists()
Definition: options.hxx:194
result replace(const std::string &id, const Content &value, const replace_options &opts=replace_options())
Replace document.
Definition: collection.hxx:174
Options for collection::upsert()
Definition: options.hxx:201
CB_NODISCARD const std::string & name() const
Get name of collection.
Definition: collection.hxx:230
Options for collection::get()
Definition: options.hxx:153
Options for collection::lookup_in()
Definition: options.hxx:229
CB_NODISCARD const std::string & bucket_name() const
Get name of bucket for this collection.
Definition: collection.hxx:250
durability_level
KV Write Durability.
Definition: options.hxx:38
result upsert(const std::string &id, const Content &value, const upsert_options &opts=upsert_options())
Upsert document.
Definition: collection.hxx:127
CB_NODISCARD const std::string & scope() const
Name of scope for this collection.
Definition: collection.hxx:240
Options for collection::replace()
Definition: options.hxx:208
Exposes collection-level kv operations.
Definition: collection.hxx:46
Options for collection::mutate_in()
Definition: options.hxx:265
CB_NODISCARD std::chrono::microseconds default_kv_timeout() const
Return default timeout for the kv operations.
Definition: collection.hxx:271