6#include <userver/cache/update_type.hpp>
7#include <userver/utils/meta.hpp>
8#include <userver/utils/void_t.hpp>
10USERVER_NAMESPACE_BEGIN
12namespace formats::
bson {
20namespace mongo_cache::impl {
23using CollectionsField =
decltype(T::kMongoCollectionsField);
25inline constexpr bool kHasCollectionsField =
26 meta::kIsDetected<CollectionsField, T>;
29using UpdateFieldName =
decltype(T::kMongoUpdateFieldName);
31inline constexpr bool kHasUpdateFieldName =
32 meta::kIsDetected<UpdateFieldName, T>;
35using KeyField =
decltype(T::kKeyField);
37inline constexpr bool kHasKeyField = meta::kIsDetected<KeyField, T>;
40using DataType =
typename T::DataType;
42inline constexpr bool kHasValidDataType =
43 meta::kIsMap<meta::DetectedType<DataType, T>>;
46using HasSecondaryPreferred =
decltype(T::kIsSecondaryPreferred);
48inline constexpr bool kHasSecondaryPreferred =
49 meta::kIsDetected<HasSecondaryPreferred, T>;
52using HasDeserializeObject =
decltype(T::DeserializeObject);
54inline constexpr bool kHasDeserializeObject =
55 meta::kIsDetected<HasDeserializeObject, T>;
58using HasCorrectDeserializeObject =
59 meta::ExpectSame<
typename T::ObjectType,
60 decltype(std::declval<
const T&>().DeserializeObject(
61 std::declval<
const formats::bson::Document&>()))>;
63inline constexpr bool kHasCorrectDeserializeObject =
64 meta::kIsDetected<HasCorrectDeserializeObject, T>;
67using HasDefaultDeserializeObject =
decltype(T::kUseDefaultDeserializeObject);
69inline constexpr bool kHasDefaultDeserializeObject =
70 meta::kIsDetected<HasDefaultDeserializeObject, T>;
73using HasFindOperation =
decltype(T::GetFindOperation);
75inline constexpr bool kHasFindOperation =
76 meta::kIsDetected<HasFindOperation, T>;
79using HasCorrectFindOperation = meta::ExpectSame<
80 storages::mongo::operations::Find,
81 decltype(std::declval<
const T&>().GetFindOperation(
82 std::declval<cache::UpdateType>(),
83 std::declval<
const std::chrono::system_clock::time_point&>(),
84 std::declval<
const std::chrono::system_clock::time_point&>(),
85 std::declval<
const std::chrono::system_clock::duration&>()))>;
88inline constexpr bool kHasCorrectFindOperation =
89 meta::kIsDetected<HasCorrectFindOperation, T>;
92using HasDefaultFindOperation =
decltype(T::kUseDefaultFindOperation);
94inline constexpr bool kHasDefaultFindOperation =
95 meta::kIsDetected<HasDefaultFindOperation, T>;
98using HasInvalidDocumentsSkipped =
decltype(T::kAreInvalidDocumentsSkipped);
100inline constexpr bool kHasInvalidDocumentsSkipped =
101 meta::kIsDetected<HasInvalidDocumentsSkipped, T>;
104struct ClassByMemberPointer {};
105template <
typename T,
typename C>
106struct ClassByMemberPointer<T C::*> {
109template <
typename CollectionPtr>
110using CollectionsType =
111 typename ClassByMemberPointer<std::remove_cv_t<CollectionPtr>>::type;
113template <
typename MongoCacheTraits>
116 if constexpr (kHasDefaultDeserializeObject<MongoCacheTraits>) {
120 decltype(MongoCacheTraits::kUseDefaultDeserializeObject)>,
122 "Mongo cache traits must specify kUseDefaultDeserializeObject as "
125 if constexpr (kHasDefaultFindOperation<MongoCacheTraits>) {
129 decltype(MongoCacheTraits::kUseDefaultFindOperation)>,
131 "Mongo cache traits must specify kUseDefaultFindOperation as bool");
135 static_assert(kHasCollectionsField<MongoCacheTraits>,
136 "Mongo cache traits must specify collections field");
137 static_assert(kHasKeyField<MongoCacheTraits>,
138 "Mongo cache traits must specify key field");
139 static_assert(kHasValidDataType<MongoCacheTraits>,
140 "Mongo cache traits must specify mapping data type");
142 static_assert(kHasSecondaryPreferred<MongoCacheTraits>,
143 "Mongo cache traits must specify read preference");
146 std::decay_t<
decltype(MongoCacheTraits::kIsSecondaryPreferred)>,
148 "Mongo cache traits must specify read preference of a bool type");
150 static_assert(kHasInvalidDocumentsSkipped<MongoCacheTraits>,
151 "Mongo cache traits must specify validation policy");
154 std::decay_t<
decltype(MongoCacheTraits::kAreInvalidDocumentsSkipped)>,
156 "Mongo cache traits must specify validation policy of a bool type");
158 static_assert(kHasFindOperation<MongoCacheTraits> ||
159 kHasDefaultFindOperation<MongoCacheTraits>,
160 "Mongo cache traits must specify find operation");
161 static_assert(!kHasFindOperation<MongoCacheTraits> ||
162 kHasCorrectFindOperation<MongoCacheTraits>,
163 "Mongo cache traits must specify find operation with correct "
164 "signature and return value type: "
165 "static storages::mongo::operations::Find GetFindOperation("
166 "cache::UpdateType type, "
167 "const std::chrono::system_clock::time_point& last_update, "
168 "const std::chrono::system_clock::time_point& now, "
169 "const std::chrono::system_clock::duration& correction)");
171 static_assert(kHasDeserializeObject<MongoCacheTraits> ||
172 kHasDefaultDeserializeObject<MongoCacheTraits>,
173 "Mongo cache traits must specify deserialize object");
175 !kHasDeserializeObject<MongoCacheTraits> ||
176 kHasCorrectDeserializeObject<MongoCacheTraits>,
177 "Mongo cache traits must specify deserialize object with correct "
178 "signature and return value type: "
179 "static ObjectType DeserializeObject(const formats::bson::Document& "