Ditto 1.1.7
DocumentId.hpp
1#ifndef _DITTO_DOCUMENT_ID_
2#define _DITTO_DOCUMENT_ID_
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
21struct DocumentId {
22 friend class Collection;
23 friend class Document;
24 friend class DocumentOperator;
25 friend class PendingIDSpecificOperation;
26 friend class QueryOperator;
27 friend class ScopedWriteTransaction;
28 friend struct DocumentIdHasher;
29
30 DocumentId() = default;
31
56 explicit DocumentId(nlohmann::json id_value);
57
58 bool operator==(const DocumentId &rhs) const;
59 bool operator!=(const DocumentId &rhs) const;
60
68 DocumentIdPath operator[](std::string key) const;
69
78 DocumentIdPath operator[](std::size_t index) const;
79
87 bool empty() const;
88
95 nlohmann::json value() const;
96
109 std::string to_string() const;
110
116
117private:
118 std::vector<uint8_t> id_bytes;
119
120 std::string formatted_for_query_string(
121 StringPrimitiveFormat_t string_primitive_format) const;
122
123 explicit DocumentId(CDocument_t *doc_ptr);
124 explicit DocumentId(std::vector<uint8_t> id_bytes);
125};
126
127void to_json(nlohmann::json &j, const DocumentId &d_id);
128
129std::vector<uint8_t> validate_id_cbor(nlohmann::json &input_val);
130
132
133struct DocumentIdHasher {
134 std::size_t operator()(const DocumentId &k) const {
135 // Taken from Boost's `boost::hash_combine`. See discussion here:
136 // https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
137 std::size_t seed = k.id_bytes.size();
138 for (auto &i : k.id_bytes) {
139 seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
140 }
141 return seed;
142 }
143};
144
146
147} // namespace ditto
148
149#endif
A reference to a collection in a Store.
Definition: Collection.hpp:26
A document belonging to a Collection with an inner value and an identifier (DocumentId).
Definition: Document.hpp:17
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:22
These objects are returned when using Collection::find_by_id functionality.
Definition: PendingIDSpecificOperation.hpp:44
ScopedWriteTransaction exposes functionality that allows you to perform multiple operations on the st...
Definition: ScopedWriteTransaction.hpp:23
basic_json<> json
default JSON class
Definition: json.hpp:2933
An identifier for a Document.
Definition: DocumentId.hpp:21
DocumentIdPath operator[](std::string key) const
Used to specify a path to a key in the document ID that you can subscript further to access a nested ...
Definition: DocumentId.cpp:35
bool empty() const
Returns whether or not the document ID is empty (and therefore invalid).
Definition: DocumentId.cpp:43
std::string to_string() const
Returns a stringified representation of a document identifier.
Definition: DocumentId.cpp:49
nlohmann::json value() const
Get the underlying value of the document identifier as a native type.
Definition: DocumentId.cpp:45
nlohmann::json to_native() const
Definition: DocumentId.cpp:47
DocumentId(nlohmann::json id_value)
Creates a new DocumentId.