Cppinecone
A C++ client for the Pinecone vector database
index_types.hpp
Go to the documentation of this file.
1 #pragma once
7 #include <cstdint>
8 #include <optional>
9 #include <string>
10 #include <vector>
11 
12 #include <nlohmann/json.hpp>
13 
14 #include "pinecone/util/result.hpp"
15 
16 using json = nlohmann::json;
17 
18 namespace pinecone::types
19 {
23 enum class database_state {
24  unknown,
25  initializing,
26  scaling_up,
27  scaling_down,
28  terminating,
29  ready
30 };
31 
32 // NOLINTNEXTLINE
33 NLOHMANN_JSON_SERIALIZE_ENUM(database_state, {{database_state::unknown, nullptr},
34  {database_state::initializing, "initializing"},
35  {database_state::scaling_up, "scaling_up"},
36  {database_state::scaling_down, "scaling_down"},
37  {database_state::terminating, "terminating"},
38  {database_state::ready, "ready"}});
39 
43 enum class pod_type {
44  unknown,
45  s1,
46  p1,
47  p2
48 };
49 
50 // NOLINTNEXTLINE
51 NLOHMANN_JSON_SERIALIZE_ENUM(pod_type, {{pod_type::unknown, nullptr},
52  {pod_type::s1, "s1"},
53  {pod_type::p1, "p1"},
54  {pod_type::p2, "p2"}});
55 
59 enum class pod_size {
60  unknown,
61  x1,
62  x2,
63  x4,
64  x8
65 };
66 
67 // NOLINTNEXTLINE
68 NLOHMANN_JSON_SERIALIZE_ENUM(pod_size, {{pod_size::unknown, nullptr},
69  {pod_size::x1, "x1"},
70  {pod_size::x2, "x2"},
71  {pod_size::x4, "x4"},
72  {pod_size::x8, "x8"}});
73 
77 enum class metric_type {
78  unknown,
79  euclidean,
80  cosine,
81  dotproduct
82 };
83 
84 // NOLINTNEXTLINE
85 NLOHMANN_JSON_SERIALIZE_ENUM(metric_type, {{metric_type::unknown, nullptr},
86  {metric_type::euclidean, "euclidean"},
87  {metric_type::cosine, "cosine"},
88  {metric_type::dotproduct, "dotproduct"}});
89 
94  pod_configuration() = default;
95  pod_configuration(pod_type type, pod_size size) noexcept : _type(type), _size(size) {}
96 
97  [[nodiscard]] auto type() const noexcept -> pod_type { return _type; }
98  [[nodiscard]] auto size() const noexcept -> pod_size { return _size; }
99 
100  friend void to_json(nlohmann ::json& nlohmann_json_j, const pod_configuration& nlohmann_json_t)
101  {
102  json typestr = nlohmann_json_t._type;
103  json sizestr = nlohmann_json_t._size;
104  nlohmann_json_j = typestr.get<std::string>() + "." + sizestr.get<std::string>();
105  }
106 
107  friend void from_json(const nlohmann ::json& nlohmann_json_j, pod_configuration& nlohmann_json_t)
108  {
109  std::string const& pod_type = nlohmann_json_j.get<std::string>();
110  json type_str = pod_type.substr(0, 2);
111  json size_str = pod_type.substr(3);
112  type_str.get_to(nlohmann_json_t._type);
113  size_str.get_to(nlohmann_json_t._size);
114  }
115 
116  private:
117  pod_type _type;
118  pod_size _size;
119 };
120 
125  NLOHMANN_DEFINE_TYPE_INTRUSIVE(database_status, ready, state);
126 
127  [[nodiscard]] auto db_ready() const noexcept -> bool { return ready; }
128  [[nodiscard]] auto db_state() const noexcept -> database_state { return state; }
129 
130  private:
131  bool ready;
132  database_state state;
133 };
134 
139  [[nodiscard]] constexpr auto db_name() const noexcept -> std::string const& { return name; }
140  [[nodiscard]] constexpr auto db_dimension() const noexcept -> uint32_t { return dimension; }
141  [[nodiscard]] constexpr auto db_metric() const noexcept -> metric_type { return metric; }
142  [[nodiscard]] constexpr auto db_pod_type() const noexcept -> pod_configuration const&
143  {
144  return pod_type;
145  }
146  [[nodiscard]] constexpr auto db_pods() const noexcept -> uint16_t { return pods; }
147  [[nodiscard]] constexpr auto db_replicas() const noexcept -> uint16_t { return replicas; }
148  [[nodiscard]] constexpr auto db_shards() const noexcept -> uint16_t { return shards; }
149 
150  NLOHMANN_DEFINE_TYPE_INTRUSIVE(database_detail, name, dimension, metric, pod_type, pods, replicas,
151  shards);
152 
153  private:
154  std::string name;
155  uint32_t dimension;
156  metric_type metric;
158  uint16_t pods;
159  uint16_t replicas;
160  uint16_t shards;
161 };
162 
166 struct database {
167  [[nodiscard]] auto db_status() const noexcept -> database_status { return status; }
168  [[nodiscard]] auto db_detail() const noexcept -> database_detail { return database; }
169 
170  NLOHMANN_DEFINE_TYPE_INTRUSIVE(database, status, database);
171 
172  private:
173  database_status status{};
175 };
176 
180 struct collection {
181  [[nodiscard]] auto col_name() const noexcept -> std::string { return name; }
182  [[nodiscard]] auto col_size() const noexcept -> uint64_t { return size; }
183  [[nodiscard]] auto col_status() const noexcept -> std::string { return status; }
184 
185  NLOHMANN_DEFINE_TYPE_INTRUSIVE(collection, name, status, size);
186 
187  private:
188  std::string name;
189  std::string status;
190  uint64_t size;
191 };
192 
197  index_configuration() = default;
199  : replicas(replicas), pod_type(pod_type)
200  {
201  }
202 
203  [[nodiscard]] auto index_replicas() const noexcept -> uint16_t { return replicas; }
204  [[nodiscard]] auto index_pod_config() const noexcept -> pod_configuration { return pod_type; }
205 
206  NLOHMANN_DEFINE_TYPE_INTRUSIVE(index_configuration, replicas, pod_type);
207 
208  private:
209  uint16_t replicas;
211 };
212 
217  new_collection() = default;
218  new_collection(std::string name, std::string source) noexcept
219  : name(std::move(name)), source(std::move(source))
220  {
221  }
222 
223  [[nodiscard]] auto col_name() const noexcept -> std::string const& { return name; }
224  [[nodiscard]] auto col_source() const noexcept -> std::string const& { return source; }
225 
226  NLOHMANN_DEFINE_TYPE_INTRUSIVE(new_collection, name, source);
227 
228  private:
229  std::string name;
230  std::string source;
231 };
232 
236 struct new_index {
237  struct builder {
238  builder(std::string name, uint32_t dimension) noexcept
239  : _name(std::move(name)), _dimension(dimension)
240  {
241  }
242 
243  auto with_metric(metric_type metric) noexcept -> builder&
244  {
245  _metric = metric;
246  return *this;
247  }
248 
249  auto with_pods(uint16_t pods) noexcept -> builder&
250  {
251  _pods = pods;
252  return *this;
253  }
254 
255  auto with_pod_type(pod_configuration pod_config) noexcept -> builder&
256  {
257  _pod_config = pod_config;
258  return *this;
259  }
260 
261  auto with_shards(uint16_t shards) noexcept -> builder&
262  {
263  _shards = shards;
264  return *this;
265  }
266 
267  auto with_replicas(uint16_t replicas) noexcept -> builder&
268  {
269  _replicas = replicas;
270  return *this;
271  }
272 
273  auto with_metadata_config(std::vector<std::string> indexed) noexcept -> builder&
274  {
275  _metadata_config = std::move(indexed);
276  return *this;
277  }
278 
279  auto with_source_collection(std::string source_collection) noexcept -> builder&
280  {
281  _source_collection = std::move(source_collection);
282  return *this;
283  }
284 
285  [[nodiscard]] auto build() noexcept -> new_index
286  {
287  return {std::move(_name),
288  _dimension,
289  _metric,
290  _pods,
291  _pod_config,
292  _shards,
293  _replicas,
294  std::move(_metadata_config),
295  std::move(_source_collection)};
296  }
297 
298  private:
299  std::string _name;
300  uint32_t _dimension;
301  std::optional<metric_type> _metric;
302  std::optional<uint16_t> _pods;
303  std::optional<pod_configuration> _pod_config;
304  std::optional<uint16_t> _shards;
305  std::optional<uint16_t> _replicas;
306  std::optional<std::vector<std::string>> _metadata_config;
307  std::optional<std::string> _source_collection;
308  };
309 
310  friend void to_json(nlohmann ::json& nlohmann_json_j, const new_index& nlohmann_json_t)
311  {
312  nlohmann_json_j["name"] = nlohmann_json_t.name;
313  nlohmann_json_j["dimension"] = nlohmann_json_t.dimension;
314  if (nlohmann_json_t.metric) {
315  nlohmann_json_j["metric"] = *nlohmann_json_t.metric;
316  }
317  if (nlohmann_json_t.pods) {
318  nlohmann_json_j["pods"] = *nlohmann_json_t.pods;
319  }
320  if (nlohmann_json_t.pod_type) {
321  nlohmann_json_j["pod_type"] = *nlohmann_json_t.pod_type;
322  }
323  if (nlohmann_json_t.shards) {
324  nlohmann_json_j["shards"] = *nlohmann_json_t.shards;
325  }
326  if (nlohmann_json_t.replicas) {
327  nlohmann_json_j["replicas"] = *nlohmann_json_t.replicas;
328  }
329  if (nlohmann_json_t.metadata_config) {
330  nlohmann_json_j["metadata_config"] = *nlohmann_json_t.metadata_config;
331  }
332  if (nlohmann_json_t.source_collection) {
333  nlohmann_json_j["source_collection"] = *nlohmann_json_t.source_collection;
334  }
335  }
336 
337  [[nodiscard]] auto index_name() const noexcept -> std::string { return name; }
338  [[nodiscard]] auto index_dimension() const noexcept -> uint16_t { return dimension; }
339  [[nodiscard]] auto index_metric() const noexcept -> std::optional<metric_type> { return metric; }
340  [[nodiscard]] auto index_pods() const noexcept -> std::optional<uint16_t> { return pods; }
341  [[nodiscard]] auto index_pod_config() const noexcept -> std::optional<pod_configuration>
342  {
343  return pod_type;
344  }
345  [[nodiscard]] auto index_shards() const noexcept -> std::optional<uint16_t> { return shards; }
346  [[nodiscard]] auto index_replicas() const noexcept -> std::optional<uint16_t> { return replicas; }
347  [[nodiscard]] auto index_metadata_config() const noexcept
348  -> std::optional<std::vector<std::string>>
349  {
350  return metadata_config;
351  }
352  [[nodiscard]] auto index_source_collection() const noexcept -> std::optional<std::string>
353  {
354  return source_collection;
355  }
356 
357  private:
358  std::string name;
359  uint16_t dimension{};
360  std::optional<metric_type> metric;
361  std::optional<uint16_t> pods;
362  std::optional<pod_configuration> pod_type;
363  std::optional<uint16_t> shards;
364  std::optional<uint16_t> replicas;
365  std::optional<std::vector<std::string>> metadata_config;
366  std::optional<std::string> source_collection;
367 
368  new_index() = default;
369  new_index(std::string name, uint32_t dimension, std::optional<metric_type> metric,
370  std::optional<uint16_t> pods, std::optional<pod_configuration> pod_config,
371  std::optional<uint16_t> shards, std::optional<uint16_t> replicas,
372  std::optional<std::vector<std::string>> metadata_config,
373  std::optional<std::string> source_collection) noexcept
374  : name(std::move(name)),
375  dimension(dimension),
376  metric(metric),
377  pods(pods),
378  pod_type(pod_config),
379  shards(shards),
380  replicas(replicas),
381  metadata_config(std::move(metadata_config)),
382  source_collection(std::move(source_collection))
383  {
384  }
385 };
386 } // namespace pinecone::types
pod_size
Pod sizes for individual databases.
Definition: index_types.hpp:59
pod_type
Pod types for individual databases.
Definition: index_types.hpp:43
metric_type
Metric types for individual databases.
Definition: index_types.hpp:77
database_state
Pod states for individual databases.
Definition: index_types.hpp:23
T move(T... args)
STL namespace.
operation_type
All operation types exposed by the Pinecone REST API.
Models the possibility of failure for Pinecone operations.
A snapshot of index contents.
The details of an individual database.
The status of an individual database.
The complete description of a Pinecone vector index.
The technical configuration of a single Pinecone index.
Arguments required to create a new collection.
Arguments required to create a new index.
Pod configuration for an individual index.
Definition: index_types.hpp:93
T substr(T... args)