Ditto 4.13.1
 
Loading...
Searching...
No Matches
DocumentId.hpp
1#ifndef DITTO_DOCUMENT_ID_H
2#define DITTO_DOCUMENT_ID_H
3
4#include "DocumentIdPath.hpp"
5
6#include <functional>
7#include <string>
8#include <vector>
9
10#include "json.hpp"
11
12#include "dittoffi.hpp"
13
14namespace ditto {
15
21// TODO(v5): Remove DocumentId, but need to change
22// QueryResult::mutated_document_ids to another type.
23struct DocumentId {
24 friend class Collection;
25 friend class Document;
26 friend class DocumentOperator;
27 friend class PendingIDSpecificOperation;
28 friend class QueryOperator;
29 friend class ScopedWriteTransaction;
30 friend struct DocumentIdHasher;
31 friend class QueryResult;
32
33 DocumentId() = default;
34
59 explicit DocumentId(nlohmann::json id_value);
60
61 bool operator==(const DocumentId &rhs) const;
62 bool operator!=(const DocumentId &rhs) const;
63
64 DITTO_DISABLE_DEPRECATED_WARNINGS("DocumentIdPath is deprecated")
65
66
73 DocumentIdPath operator[](std::string key) const;
74
83 DocumentIdPath operator[](std::size_t index) const;
84
85 DITTO_REENABLE_WARNINGS
86
94 bool empty() const;
95
102 nlohmann::json value() const;
103
116 std::string to_string() const;
117
118private:
119 std::vector<uint8_t> id_bytes;
120
121 std::string formatted_for_query_string(
122 StringPrimitiveFormat_t string_primitive_format) const;
123
124 explicit DocumentId(CDocument_t *doc_ptr);
125 explicit DocumentId(slice_boxed_uint8_t);
126 explicit DocumentId(std::vector<uint8_t> id_bytes);
127};
128
129void to_json(nlohmann::json &j, const DocumentId &d_id);
130
131std::vector<uint8_t> validate_id_cbor(nlohmann::json &input_val);
132
134
135// TODO(v5): Remove DocumentIdHasher, which is only used by the legacy query
136// builder API.
137struct DITTO_DEPRECATED_BECAUSE(
138 "Use DQL (Ditto Query Language) instead. For more information see: "
139 "https://ditto.com/link/legacy-to-dql-guide") DocumentIdHasher {
140 std::size_t operator()(const DocumentId &k) const {
141 // Taken from Boost's `boost::hash_combine`. See discussion here:
142 // https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
143 std::size_t seed = k.id_bytes.size();
144 for (const auto &i : k.id_bytes) {
145 // NOLINTNEXTLINE(hicpp-signed-bitwise)
146 seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
147 }
148 return seed;
149 }
150};
151
153
154} // namespace ditto
155
156#endif
Provides an interface to specify a path to a key in a document ID that you can then call a function o...
Definition DocumentIdPath.hpp:28
Namespace for the Ditto C++ SDK types and functions.
Definition AbstractDocumentPath.hpp:19
bool empty() const
Returns whether or not the document ID is empty (and therefore invalid).
nlohmann::json value() const
Get the underlying value of the document identifier as a native type.
std::string to_string() const
Returns a stringified representation of a document identifier.
DocumentId(nlohmann::json id_value)
Creates a new DocumentId.