6#include <userver/cache/update_type.hpp>
7#include <userver/utils/meta.hpp>
8#include <userver/utils/void_t.hpp>
10USERVER_NAMESPACE_BEGIN
20namespace mongo_cache::impl {
23using CollectionsField =
decltype(T::kMongoCollectionsField);
25inline constexpr bool kHasCollectionsField = meta::kIsDetected<CollectionsField, T>;
28using UpdateFieldName =
decltype(T::kMongoUpdateFieldName);
30inline constexpr bool kHasUpdateFieldName = meta::kIsDetected<UpdateFieldName, T>;
33using KeyField =
decltype(T::kKeyField);
35inline constexpr bool kHasKeyField = meta::kIsDetected<KeyField, T>;
38using DataType =
typename T::DataType;
40inline constexpr bool kHasValidDataType = meta::kIsMap<meta::DetectedType<DataType, T>>;
43using HasSecondaryPreferred =
decltype(T::kIsSecondaryPreferred);
45inline constexpr bool kHasSecondaryPreferred = meta::kIsDetected<HasSecondaryPreferred, T>;
48using HasDeserializeObject =
decltype(T::DeserializeObject);
50inline constexpr bool kHasDeserializeObject = meta::kIsDetected<HasDeserializeObject, T>;
53using HasCorrectDeserializeObject = meta::ExpectSame<
54 typename T::ObjectType,
55 decltype(std::declval<
const T&>().DeserializeObject(std::declval<
const formats::bson::Document&>()))>;
57inline constexpr bool kHasCorrectDeserializeObject = meta::kIsDetected<HasCorrectDeserializeObject, T>;
60using HasDefaultDeserializeObject =
decltype(T::kUseDefaultDeserializeObject);
62inline constexpr bool kHasDefaultDeserializeObject = meta::kIsDetected<HasDefaultDeserializeObject, T>;
65using HasFindOperation =
decltype(T::GetFindOperation);
67inline constexpr bool kHasFindOperation = meta::kIsDetected<HasFindOperation, T>;
70using HasCorrectFindOperation = meta::ExpectSame<
71 storages::mongo::operations::Find,
72 decltype(std::declval<
const T&>().GetFindOperation(
73 std::declval<cache::UpdateType>(),
74 std::declval<
const std::chrono::system_clock::time_point&>(),
75 std::declval<
const std::chrono::system_clock::time_point&>(),
76 std::declval<
const std::chrono::system_clock::duration&>()
80inline constexpr bool kHasCorrectFindOperation = meta::kIsDetected<HasCorrectFindOperation, T>;
83using HasDefaultFindOperation =
decltype(T::kUseDefaultFindOperation);
85inline constexpr bool kHasDefaultFindOperation = meta::kIsDetected<HasDefaultFindOperation, T>;
88using HasInvalidDocumentsSkipped =
decltype(T::kAreInvalidDocumentsSkipped);
90inline constexpr bool kHasInvalidDocumentsSkipped = meta::kIsDetected<HasInvalidDocumentsSkipped, T>;
93struct ClassByMemberPointer {};
94template <
typename T,
typename C>
95struct ClassByMemberPointer<T C::*> {
98template <
typename CollectionPtr>
99using CollectionsType =
typename ClassByMemberPointer<std::remove_cv_t<CollectionPtr>>::type;
101template <
typename MongoCacheTraits>
104 if constexpr (kHasDefaultDeserializeObject<MongoCacheTraits>) {
106 std::is_same_v<std::decay_t<
decltype(MongoCacheTraits::kUseDefaultDeserializeObject)>,
bool>,
107 "Mongo cache traits must specify kUseDefaultDeserializeObject as "
111 if constexpr (kHasDefaultFindOperation<MongoCacheTraits>) {
113 std::is_same_v<std::decay_t<
decltype(MongoCacheTraits::kUseDefaultFindOperation)>,
bool>,
114 "Mongo cache traits must specify kUseDefaultFindOperation as bool"
119 static_assert(kHasCollectionsField<MongoCacheTraits>,
"Mongo cache traits must specify collections field");
120 static_assert(kHasKeyField<MongoCacheTraits>,
"Mongo cache traits must specify key field");
121 static_assert(kHasValidDataType<MongoCacheTraits>,
"Mongo cache traits must specify mapping data type");
123 static_assert(kHasSecondaryPreferred<MongoCacheTraits>,
"Mongo cache traits must specify read preference");
125 std::is_same_v<std::decay_t<
decltype(MongoCacheTraits::kIsSecondaryPreferred)>,
bool>,
126 "Mongo cache traits must specify read preference of a bool type"
129 static_assert(kHasInvalidDocumentsSkipped<MongoCacheTraits>,
"Mongo cache traits must specify validation policy");
131 std::is_same_v<std::decay_t<
decltype(MongoCacheTraits::kAreInvalidDocumentsSkipped)>,
bool>,
132 "Mongo cache traits must specify validation policy of a bool type"
136 kHasFindOperation<MongoCacheTraits> || kHasDefaultFindOperation<MongoCacheTraits>,
137 "Mongo cache traits must specify find operation"
140 !kHasFindOperation<MongoCacheTraits> || kHasCorrectFindOperation<MongoCacheTraits>,
141 "Mongo cache traits must specify find operation with correct "
142 "signature and return value type: "
143 "static storages::mongo::operations::Find GetFindOperation("
144 "cache::UpdateType type, "
145 "const std::chrono::system_clock::time_point& last_update, "
146 "const std::chrono::system_clock::time_point& now, "
147 "const std::chrono::system_clock::duration& correction)"
151 kHasDeserializeObject<MongoCacheTraits> || kHasDefaultDeserializeObject<MongoCacheTraits>,
152 "Mongo cache traits must specify deserialize object"
155 !kHasDeserializeObject<MongoCacheTraits> || kHasCorrectDeserializeObject<MongoCacheTraits>,
156 "Mongo cache traits must specify deserialize object with correct "
157 "signature and return value type: "
158 "static ObjectType DeserializeObject(const formats::bson::Document& "