CatapultServer
v0.5.0.1 (Elephant)
|
Go to the documentation of this file.
27 #include <unordered_set>
29 namespace catapult {
namespace mongo {
namespace storages {
33 template<
typename TCacheTraits,
typename TElementContainerType>
37 static void RemoveCommonElements(TElementContainerType& addedElements, TElementContainerType& removedElements) {
38 auto commonIds =
GetCommonIds(addedElements, removedElements);
44 static auto GetCommonIds(
const TElementContainerType& addedElements,
const TElementContainerType& removedElements) {
45 std::set<typename TCacheTraits::KeyType> addedElementIds;
46 for (
const auto* pAddedElement : addedElements)
47 addedElementIds.insert(TCacheTraits::GetId(*pAddedElement));
49 std::set<typename TCacheTraits::KeyType> removedElementIds;
50 for (
const auto* pRemovedElement : removedElements)
51 removedElementIds.insert(TCacheTraits::GetId(*pRemovedElement));
53 std::set<typename TCacheTraits::KeyType> commonIds;
54 std::set_intersection(
55 addedElementIds.cbegin(),
56 addedElementIds.cend(),
57 removedElementIds.cbegin(),
58 removedElementIds.cend(),
59 std::inserter(commonIds, commonIds.cbegin()));
63 static void RemoveElements(TElementContainerType& elements, std::set<typename TCacheTraits::KeyType>& ids) {
64 for (
auto iter = elements.cbegin(); elements.cend() != iter;) {
65 if (ids.cend() != ids.find(TCacheTraits::GetId(**iter)))
66 iter = elements.erase(iter);
75 template<
typename TDescriptor>
84 using KeyType =
typename TDescriptor::KeyType;
90 static constexpr
auto GetId = TDescriptor::GetKeyFromValue;
94 template<
typename TCacheTraits>
98 typename TCacheTraits::CacheDeltaType,
99 typename TCacheTraits::CacheType::CacheValueType>;
106 :
m_database(storageContext.createDatabaseConnection())
122 auto modifiedIds =
GetIds(modifiedElements);
123 auto removedIds =
GetIds(removedElements);
124 modifiedIds.insert(removedIds.cbegin(), removedIds.cend());
128 modifiedElements.insert(addedElements.cbegin(), addedElements.cend());
140 auto deleteResult = collection.delete_many(filter.view());
145 if (elements.empty())
148 std::vector<typename TCacheTraits::ModelType> allModels;
149 for (
const auto* pElement : elements) {
151 std::move(models.begin(), models.end(), std::back_inserter(allModels));
155 return TCacheTraits::MapToMongoDocument(model);
165 for (
const auto* pElement : elements)
166 ids.insert(TCacheTraits::GetId(*pElement));
172 using namespace bsoncxx::builder::stream;
175 return document() << finalize;
185 array << TCacheTraits::MapToMongoId(
id);
187 array << close_array;
188 doc << close_document;
189 return doc << finalize;
200 template<
typename TCacheTraits>
204 using KeyType =
typename TCacheTraits::KeyType;
211 :
m_database(storageContext.createDatabaseConnection())
230 modifiedElements.insert(addedElements.cbegin(), addedElements.cend());
231 upsertAll(modifiedElements);
236 if (elements.empty())
245 if (elements.empty())
248 auto createDocument = [networkIdentifier =
m_networkIdentifier](
const auto* pModel,
auto) {
249 return TCacheTraits::MapToMongoDocument(*pModel, networkIdentifier);
258 return CreateFilterByKey(TCacheTraits::GetId(*pModel));
262 using namespace bsoncxx::builder::stream;
BulkWriteResultFuture bulkDelete(const std::string &collectionName, const TContainer &entities, const CreateFilter< typename TContainer::value_type > &createFilter)
Deletes entities from the collection named collectionName matching the specified entity filter (creat...
Definition: MongoBulkWriter.h:151
Defines a mongo element filter.
Definition: MongoCacheStorage.h:34
Typed interface for saving cache data to external storage.
Definition: ExternalCacheStorage.h:61
MongoErrorPolicy m_errorPolicy
Definition: MongoCacheStorage.h:194
typename TDescriptor::KeyType KeyType
Key type.
Definition: MongoCacheStorage.h:84
static IdContainerType GetIds(const ElementContainerType &elements)
Definition: MongoCacheStorage.h:163
static void RemoveElements(TElementContainerType &elements, std::set< typename TCacheTraits::KeyType > &ids)
Definition: MongoCacheStorage.h:63
typename TDescriptor::CacheDeltaType CacheDeltaType
Cache delta type.
Definition: MongoCacheStorage.h:81
MongoErrorPolicy m_errorPolicy
Definition: MongoCacheStorage.h:269
BulkWriteResultFuture bulkInsert(const std::string &collectionName, const TContainer &entities, const CreateDocument< typename TContainer::value_type > &createDocument)
Definition: MongoBulkWriter.h:103
void saveDelta(const CacheChangesType &changes) override
Saves cache changes to external storage.
Definition: MongoCacheStorage.h:218
T get()
Returns the result of this future and blocks until the result is available.
Definition: Future.h:50
typename TCacheTraits::KeyType KeyType
Definition: MongoCacheStorage.h:204
A mongo cache storage that persists historical cache data using delete and insert.
Definition: MongoCacheStorage.h:95
static constexpr auto Id_Property_Name
Definition: MongoHashLockInfoCacheStorage.cpp:31
void saveDelta(const CacheChangesType &changes) override
Saves cache changes to external storage.
Definition: MongoCacheStorage.h:113
static constexpr auto Collection_Name
Definition: MongoHashLockInfoCacheStorage.cpp:30
void insertAll(const ElementContainerType &elements)
Definition: MongoCacheStorage.h:144
typename TCacheTraits::IdContainerType IdContainerType
Definition: MongoCacheStorage.h:101
MongoBulkWriter & m_bulkWriter
Definition: MongoCacheStorage.h:270
std::unordered_set< const ModelType * > ElementContainerType
Definition: MongoCacheStorage.h:206
Provides common view of single sub cache changes.
Definition: CacheChanges.h:59
MongoDatabase m_database
Definition: MongoCacheStorage.h:268
MongoHistoricalCacheStorage(MongoStorageContext &storageContext, model::NetworkIdentifier networkIdentifier)
Creates a cache storage around storageContext and networkIdentifier.
Definition: MongoCacheStorage.h:105
void removeAll(const IdContainerType &ids)
Definition: MongoCacheStorage.h:133
typename TCacheTraits::ElementContainerType ElementContainerType
Definition: MongoCacheStorage.h:100
Result of a bulk write operation to the database.
Definition: BulkWriteResult.h:27
static BulkWriteResult Aggregate(const std::vector< BulkWriteResult > &results)
Aggregates all bulk write results in results into a single result.
Definition: BulkWriteResult.h:49
Defines types for mongo cache storage given a cache descriptor.
Definition: MongoCacheStorage.h:76
Error policy for checking mongo operation results.
Definition: MongoErrorPolicy.h:30
MongoDatabase m_database
Definition: MongoCacheStorage.h:193
MongoFlatCacheStorage(MongoStorageContext &storageContext, model::NetworkIdentifier networkIdentifier)
Creates a cache storage around storageContext and networkIdentifier.
Definition: MongoCacheStorage.h:210
model::NetworkIdentifier m_networkIdentifier
Definition: MongoCacheStorage.h:271
static bsoncxx::document::value CreateDeleteFilter(const IdContainerType &ids)
Definition: MongoCacheStorage.h:171
BulkWriteResultFuture bulkUpsert(const std::string &collectionName, const TContainer &entities, const CreateDocument< typename TContainer::value_type > &createDocument, const CreateFilter< typename TContainer::value_type > &createFilter)
Definition: MongoBulkWriter.h:133
static auto GetCommonIds(const TElementContainerType &addedElements, const TElementContainerType &removedElements)
Definition: MongoCacheStorage.h:44
model::NetworkIdentifier m_networkIdentifier
Definition: MongoCacheStorage.h:196
void checkUpserted(uint64_t numExpected, const BulkWriteResult &result, const std::string &itemsDescription) const
Checks that result indicates exactly numExpected upsertions occurred given itemsDescription.
Definition: MongoErrorPolicy.cpp:73
static bsoncxx::document::value CreateFilterByKey(const KeyType &key)
Definition: MongoCacheStorage.h:261
static constexpr auto GetId
Gets the key corresponding to a value.
Definition: MongoCacheStorage.h:90
typename TCacheTraits::ModelType ModelType
Definition: MongoCacheStorage.h:205
void upsertAll(const ElementContainerType &elements)
Definition: MongoCacheStorage.h:244
Definition: MongoBulkWriter.h:43
NetworkIdentifier
Possible network identifiers.
Definition: NetworkInfo.h:45
void checkDeletedAtLeast(uint64_t numExpected, const BulkWriteResult &result, const std::string &itemsDescription) const
Checks that result indicates at least numExpected deletions occurred given itemsDescription.
Definition: MongoErrorPolicy.cpp:51
void removeAll(const ElementContainerType &elements)
Definition: MongoCacheStorage.h:235
MongoBulkWriter & m_bulkWriter
Definition: MongoCacheStorage.h:195
static bsoncxx::document::value CreateFilter(const ModelType *pModel)
Definition: MongoCacheStorage.h:257
PointerContainer addedElements() const
Gets pointers to all added elements.
Definition: CacheChanges.h:78
static void RemoveCommonElements(TElementContainerType &addedElements, TElementContainerType &removedElements)
Filters all elements common to added elements (addedElements) and removed elements (removedElements).
Definition: MongoCacheStorage.h:37
void checkDeleted(uint64_t numExpected, const BulkWriteResult &result, const std::string &itemsDescription) const
Checks that result indicates exactly numExpected deletions occurred given itemsDescription.
Definition: MongoErrorPolicy.cpp:43
Definition: AddressExtractionExtension.cpp:28
typename TDescriptor::CacheType CacheType
Cache type.
Definition: MongoCacheStorage.h:78
Context for creating a mongo storage.
Definition: MongoStorageContext.h:30
PointerContainer modifiedElements() const
Gets pointers to all modified elements.
Definition: CacheChanges.h:83
typename TDescriptor::ValueType ModelType
Model type.
Definition: MongoCacheStorage.h:87
A mongo cache storage that persists flat cache data using delete and upsert.
Definition: MongoCacheStorage.h:201
std::vector< T > get_all(std::vector< future< T >> &&futures)
Definition: FutureUtils.h:141
void checkInserted(uint64_t numExpected, const BulkWriteResult &result, const std::string &itemsDescription) const
Checks that result indicates exactly numExpected insertions occurred given itemsDescription.
Definition: MongoErrorPolicy.cpp:65
Represents a mongo database.
Definition: MongoDatabase.h:31
PointerContainer removedElements() const
Gets pointers to all removed elements.
Definition: CacheChanges.h:88