Skip to content

Commit 13af7c1

Browse files
authored
Use the same formatting as the userver (#12)
1 parent bb365b8 commit 13af7c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1470
-1516
lines changed

.clang-format

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
BasedOnStyle: google
1+
BasedOnStyle: Google
22
DerivePointerAlignment: false
33
IncludeBlocks: Preserve
4+
AttributeMacros: ["noexcept"]
5+
IndentRequires: false
6+
ColumnLimit: 120
7+
IndentWidth: 4
8+
TabWidth: 4
9+
AccessModifierOffset: -4
10+
BinPackParameters: false
11+
BinPackArguments: false
12+
AllowAllParametersOfDeclarationOnNextLine: false
13+
AlignAfterOpenBracket: BlockIndent
14+
AlwaysBreakAfterDefinitionReturnType: None
15+
PenaltyReturnTypeOnItsOwnLine: 200

src/cache/articles_cache.cpp

Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,78 @@
55
namespace real_medium::cache::articles_cache {
66

77
userver::storages::postgres::Query ArticlesCachePolicy::kQuery =
8-
userver::storages::postgres::Query(
9-
real_medium::sql::kSelectFullArticleInfo.data());
8+
userver::storages::postgres::Query(real_medium::sql::kSelectFullArticleInfo.data());
109

1110
void ArticlesCacheContainer::insert_or_assign(Key&& key, Article&& article) {
12-
auto articlePtr = std::make_shared<const Article>(std::move(article));
13-
auto oldValue = articleByKey_.find(key);
14-
if (oldValue != articleByKey_.end()) {
15-
articleBySlug_.erase(oldValue->second->slug);
16-
for (const auto& oldFollower : oldValue->second->authorFollowedByUsersIds)
17-
if (articlePtr->authorFollowedByUsersIds.find(oldFollower) ==
18-
articlePtr->authorFollowedByUsersIds.end())
19-
articlesByFollower_[oldFollower].erase(articlePtr->articleId);
20-
}
11+
auto articlePtr = std::make_shared<const Article>(std::move(article));
12+
auto oldValue = articleByKey_.find(key);
13+
if (oldValue != articleByKey_.end()) {
14+
articleBySlug_.erase(oldValue->second->slug);
15+
for (const auto& oldFollower : oldValue->second->authorFollowedByUsersIds)
16+
if (articlePtr->authorFollowedByUsersIds.find(oldFollower) == articlePtr->authorFollowedByUsersIds.end())
17+
articlesByFollower_[oldFollower].erase(articlePtr->articleId);
18+
}
2119

22-
articleByKey_.insert_or_assign(key, articlePtr);
23-
articleBySlug_.insert_or_assign(articlePtr->slug, articlePtr);
24-
for (const auto& follower : articlePtr->authorFollowedByUsersIds)
25-
articlesByFollower_[follower].insert_or_assign(articlePtr->articleId,
26-
articlePtr);
27-
recentArticles_.insert_or_assign(
28-
{articlePtr->createdAt, articlePtr->articleId}, articlePtr);
20+
articleByKey_.insert_or_assign(key, articlePtr);
21+
articleBySlug_.insert_or_assign(articlePtr->slug, articlePtr);
22+
for (const auto& follower : articlePtr->authorFollowedByUsersIds)
23+
articlesByFollower_[follower].insert_or_assign(articlePtr->articleId, articlePtr);
24+
recentArticles_.insert_or_assign({articlePtr->createdAt, articlePtr->articleId}, articlePtr);
2925
}
3026

3127
size_t ArticlesCacheContainer::size() const { return articleByKey_.size(); }
3228

33-
ArticlesCacheContainer::ArticlePtr ArticlesCacheContainer::findArticleBySlug(
34-
const Slug& slug) const {
35-
auto it = articleBySlug_.find(slug);
36-
if (it == articleBySlug_.end()) return nullptr;
37-
return it->second;
29+
ArticlesCacheContainer::ArticlePtr ArticlesCacheContainer::findArticleBySlug(const Slug& slug) const {
30+
auto it = articleBySlug_.find(slug);
31+
if (it == articleBySlug_.end()) return nullptr;
32+
return it->second;
3833
}
3934

40-
std::vector<ArticlesCacheContainer::ArticlePtr>
41-
ArticlesCacheContainer::getRecent(
42-
real_medium::handlers::ArticleFilterDTO& filter) const {
43-
std::vector<ArticlePtr> articles;
44-
int offset = 0;
45-
for (const auto& it : recentArticles_) {
46-
if (filter.limit &&
47-
articles.size() >=
48-
userver::utils::numeric_cast<std::size_t>(filter.limit))
49-
break;
35+
std::vector<ArticlesCacheContainer::ArticlePtr> ArticlesCacheContainer::getRecent(
36+
real_medium::handlers::ArticleFilterDTO& filter
37+
) const {
38+
std::vector<ArticlePtr> articles;
39+
int offset = 0;
40+
for (const auto& it : recentArticles_) {
41+
if (filter.limit && articles.size() >= userver::utils::numeric_cast<std::size_t>(filter.limit)) break;
5042

51-
const auto& tags = it.second->tags;
52-
if (filter.tag && it.second->tags.find(filter.tag.value()) == tags.end())
53-
continue;
43+
const auto& tags = it.second->tags;
44+
if (filter.tag && it.second->tags.find(filter.tag.value()) == tags.end()) continue;
5445

55-
if (filter.author && it.second->authorInfo.username != filter.author)
56-
continue;
46+
if (filter.author && it.second->authorInfo.username != filter.author) continue;
5747

58-
const auto& favorited = it.second->articleFavoritedByUsernames;
59-
if (filter.favorited &&
60-
favorited.find(filter.favorited.value()) == favorited.end())
61-
continue;
48+
const auto& favorited = it.second->articleFavoritedByUsernames;
49+
if (filter.favorited && favorited.find(filter.favorited.value()) == favorited.end()) continue;
6250

63-
if (filter.offset && offset < filter.offset) {
64-
++offset;
65-
continue;
51+
if (filter.offset && offset < filter.offset) {
52+
++offset;
53+
continue;
54+
}
55+
articles.push_back(it.second);
6656
}
67-
articles.push_back(it.second);
68-
}
69-
return articles;
57+
return articles;
7058
}
71-
std::vector<ArticlesCacheContainer::ArticlePtr> ArticlesCacheContainer::getFeed(
72-
real_medium::handlers::FeedArticleFilterDTO& filter, UserId authId) const {
73-
auto followedArticlesUMap = articlesByFollower_.find(authId);
74-
if (followedArticlesUMap == articlesByFollower_.end()) return {};
59+
std::vector<ArticlesCacheContainer::ArticlePtr>
60+
ArticlesCacheContainer::getFeed(real_medium::handlers::FeedArticleFilterDTO& filter, UserId authId) const {
61+
auto followedArticlesUMap = articlesByFollower_.find(authId);
62+
if (followedArticlesUMap == articlesByFollower_.end()) return {};
7563

76-
RecentArticlesMap followedArticlesOrdered;
77-
for (const auto& it : followedArticlesUMap->second)
78-
followedArticlesOrdered.insert_or_assign(
79-
{it.second->createdAt, it.second->articleId}, it.second);
64+
RecentArticlesMap followedArticlesOrdered;
65+
for (const auto& it : followedArticlesUMap->second)
66+
followedArticlesOrdered.insert_or_assign({it.second->createdAt, it.second->articleId}, it.second);
8067

81-
std::vector<ArticlePtr> articles;
82-
;
83-
int offset = 0;
84-
for (const auto& it : followedArticlesOrdered) {
85-
if (filter.limit &&
86-
articles.size() >=
87-
userver::utils::numeric_cast<std::size_t>(filter.limit))
88-
break;
89-
if (filter.offset && offset < filter.offset) {
90-
++offset;
91-
continue;
68+
std::vector<ArticlePtr> articles;
69+
;
70+
int offset = 0;
71+
for (const auto& it : followedArticlesOrdered) {
72+
if (filter.limit && articles.size() >= userver::utils::numeric_cast<std::size_t>(filter.limit)) break;
73+
if (filter.offset && offset < filter.offset) {
74+
++offset;
75+
continue;
76+
}
77+
articles.push_back(it.second);
9278
}
93-
articles.push_back(it.second);
94-
}
95-
return articles;
79+
return articles;
9680
}
9781

9882
} // namespace real_medium::cache::articles_cache

src/cache/articles_cache.hpp

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,56 @@
44
#include <unordered_map>
55
#include <userver/cache/base_postgres_cache.hpp>
66
#include <userver/storages/postgres/io/chrono.hpp>
7-
#include "../db/sql.hpp"
8-
#include "../dto/filter.hpp"
9-
#include "../models/article.hpp"
7+
#include "db/sql.hpp"
8+
#include "dto/filter.hpp"
9+
#include "models/article.hpp"
1010

1111
namespace real_medium::cache::articles_cache {
1212
class ArticlesCacheContainer {
13-
using Timepoint = userver::storages::postgres::TimePointTz;
14-
using Slug = std::string;
15-
using UserId = std::string;
13+
using Timepoint = userver::storages::postgres::TimePointTz;
14+
using Slug = std::string;
15+
using UserId = std::string;
1616

17-
public:
18-
using Key = real_medium::models::ArticleId;
19-
using Article = real_medium::models::FullArticleInfo;
20-
using ArticlePtr = std::shared_ptr<const Article>;
21-
using AuthorName = std::string;
22-
void insert_or_assign(Key&& key, Article&& config);
23-
size_t size() const;
17+
public:
18+
using Key = real_medium::models::ArticleId;
19+
using Article = real_medium::models::FullArticleInfo;
20+
using ArticlePtr = std::shared_ptr<const Article>;
21+
using AuthorName = std::string;
22+
void insert_or_assign(Key&& key, Article&& config);
23+
size_t size() const;
2424

25-
ArticlePtr findArticleBySlug(const Slug& slug) const;
26-
std::vector<ArticlePtr> getRecent(
27-
real_medium::handlers::ArticleFilterDTO& filter_) const;
28-
std::vector<ArticlePtr> getFeed(
29-
real_medium::handlers::FeedArticleFilterDTO& filter_,
30-
UserId authId_) const;
25+
ArticlePtr findArticleBySlug(const Slug& slug) const;
26+
std::vector<ArticlePtr> getRecent(real_medium::handlers::ArticleFilterDTO& filter_) const;
27+
std::vector<ArticlePtr> getFeed(real_medium::handlers::FeedArticleFilterDTO& filter_, UserId authId_) const;
3128

32-
private:
33-
struct TimepointedArticle {
34-
Timepoint created;
35-
Key articleId;
36-
bool operator<(const TimepointedArticle& other) const {
37-
return created != other.created ? created < other.created
38-
: articleId < other.articleId;
39-
}
40-
bool operator==(const TimepointedArticle& other) const {
41-
return created == other.created && articleId == other.articleId;
42-
}
43-
bool operator>(const TimepointedArticle& other) const {
44-
return !(*this == other) && !(*this < other);
45-
}
46-
};
29+
private:
30+
struct TimepointedArticle {
31+
Timepoint created;
32+
Key articleId;
33+
bool operator<(const TimepointedArticle& other) const {
34+
return created != other.created ? created < other.created : articleId < other.articleId;
35+
}
36+
bool operator==(const TimepointedArticle& other) const {
37+
return created == other.created && articleId == other.articleId;
38+
}
39+
bool operator>(const TimepointedArticle& other) const { return !(*this == other) && !(*this < other); }
40+
};
4741

48-
using RecentArticlesMap =
49-
std::map<TimepointedArticle /*created_at*/, ArticlePtr,
50-
std::greater<TimepointedArticle>>;
51-
std::unordered_map<Key, ArticlePtr> articleByKey_;
52-
std::unordered_map<Slug, ArticlePtr> articleBySlug_;
53-
std::unordered_map<UserId /*follower*/, std::unordered_map<Key, ArticlePtr>>
54-
articlesByFollower_;
55-
RecentArticlesMap recentArticles_;
42+
using RecentArticlesMap = std::map<TimepointedArticle /*created_at*/, ArticlePtr, std::greater<TimepointedArticle>>;
43+
std::unordered_map<Key, ArticlePtr> articleByKey_;
44+
std::unordered_map<Slug, ArticlePtr> articleBySlug_;
45+
std::unordered_map<UserId /*follower*/, std::unordered_map<Key, ArticlePtr>> articlesByFollower_;
46+
RecentArticlesMap recentArticles_;
5647
};
5748

5849
struct ArticlesCachePolicy {
59-
static constexpr auto kName = "articles-cache";
60-
using ValueType = ArticlesCacheContainer::Article;
61-
using CacheContainer = ArticlesCacheContainer;
62-
static constexpr auto kKeyMember = &ValueType::articleId;
63-
static userver::storages::postgres::Query kQuery;
64-
static constexpr auto kUpdatedField = "updated_at";
65-
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
50+
static constexpr auto kName = "articles-cache";
51+
using ValueType = ArticlesCacheContainer::Article;
52+
using CacheContainer = ArticlesCacheContainer;
53+
static constexpr auto kKeyMember = &ValueType::articleId;
54+
static userver::storages::postgres::Query kQuery;
55+
static constexpr auto kUpdatedField = "updated_at";
56+
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
6657
};
6758
using ArticlesCache = ::userver::components::PostgreCache<ArticlesCachePolicy>;
6859
} // namespace real_medium::cache::articles_cache

src/cache/comments_cache.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
#include <userver/cache/base_postgres_cache.hpp>
22
#include <userver/storages/postgres/io/chrono.hpp>
3-
#include "userver/storages/postgres/query.hpp"
4-
#include "userver/utils/algo.hpp"
3+
#include <userver/storages/postgres/query.hpp>
4+
#include <userver/utils/algo.hpp>
55

66
#include "comments_cache.hpp"
77
#include "db/sql.hpp"
88

99
namespace real_medium::cache::comments_cache {
1010

1111
userver::storages::postgres::Query CommentCachePolicy::kQuery =
12-
userver::storages::postgres::Query(
13-
real_medium::sql::kSelectCachedComments.data());
12+
userver::storages::postgres::Query(real_medium::sql::kSelectCachedComments.data());
1413

1514
void CommentsCacheContainer::insert_or_assign(
1615
real_medium::cache::comments_cache::CommentsCacheContainer::Key&& commentId,
17-
real_medium::cache::comments_cache::CommentsCacheContainer::Comment&&
18-
comment) {
19-
auto commentPtr = std::make_shared<const Comment>(std::move(comment));
20-
if (comment_to_key_.count(commentId)) {
21-
auto& oldSlug = comment_to_key_[commentId]->slug;
22-
if (oldSlug != commentPtr->slug) {
23-
auto& comments = comments_to_slug_[oldSlug];
24-
comments_to_slug_[commentPtr->slug] = comments;
25-
comments_to_slug_.erase(oldSlug);
16+
real_medium::cache::comments_cache::CommentsCacheContainer::Comment&& comment
17+
) {
18+
auto commentPtr = std::make_shared<const Comment>(std::move(comment));
19+
if (comment_to_key_.count(commentId)) {
20+
auto& oldSlug = comment_to_key_[commentId]->slug;
21+
if (oldSlug != commentPtr->slug) {
22+
auto& comments = comments_to_slug_[oldSlug];
23+
comments_to_slug_[commentPtr->slug] = comments;
24+
comments_to_slug_.erase(oldSlug);
25+
}
2626
}
27-
}
28-
comment_to_key_.insert_or_assign(commentId, commentPtr);
29-
comments_to_slug_[commentPtr->slug].insert_or_assign(commentId, commentPtr);
27+
comment_to_key_.insert_or_assign(commentId, commentPtr);
28+
comments_to_slug_[commentPtr->slug].insert_or_assign(commentId, commentPtr);
3029
};
3130

3231
size_t CommentsCacheContainer::size() const { return comment_to_key_.size(); }
3332

34-
std::map<CommentsCacheContainer::Key, CommentsCacheContainer::CommentPtr>
35-
CommentsCacheContainer::findComments(const Slug& slug) const {
36-
if (!comments_to_slug_.count(slug)) return {};
37-
return comments_to_slug_.at(slug);
33+
std::map<CommentsCacheContainer::Key, CommentsCacheContainer::CommentPtr> CommentsCacheContainer::findComments(
34+
const Slug& slug
35+
) const {
36+
if (!comments_to_slug_.count(slug)) return {};
37+
return comments_to_slug_.at(slug);
3838
}
3939

4040
} // namespace real_medium::cache::comments_cache

src/cache/comments_cache.hpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@
1212
namespace real_medium::cache::comments_cache {
1313

1414
class CommentsCacheContainer {
15-
public:
16-
using Key = real_medium::models::CommentId;
17-
using Comment = real_medium::models::CachedComment;
18-
using CommentPtr = std::shared_ptr<const Comment>;
19-
using Slug = std::string;
20-
void insert_or_assign(Key&& key, Comment&& config);
21-
size_t size() const;
15+
public:
16+
using Key = real_medium::models::CommentId;
17+
using Comment = real_medium::models::CachedComment;
18+
using CommentPtr = std::shared_ptr<const Comment>;
19+
using Slug = std::string;
20+
void insert_or_assign(Key&& key, Comment&& config);
21+
size_t size() const;
2222

23-
std::map<Key, CommentPtr> findComments(const Slug& key) const;
23+
std::map<Key, CommentPtr> findComments(const Slug& key) const;
2424

25-
private:
26-
std::unordered_map<Slug, std::map<Key, CommentPtr>> comments_to_slug_;
27-
std::unordered_map<Key, CommentPtr> comment_to_key_;
25+
private:
26+
std::unordered_map<Slug, std::map<Key, CommentPtr>> comments_to_slug_;
27+
std::unordered_map<Key, CommentPtr> comment_to_key_;
2828
};
2929

3030
struct CommentCachePolicy {
31-
static constexpr auto kName = "comments-cache";
32-
using ValueType = real_medium::models::CachedComment;
33-
using CacheContainer = CommentsCacheContainer;
34-
static constexpr auto kKeyMember = &real_medium::models::CachedComment::id;
35-
static userver::storages::postgres::Query kQuery;
36-
static constexpr auto kUpdatedField = "c.updated_at";
37-
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
31+
static constexpr auto kName = "comments-cache";
32+
using ValueType = real_medium::models::CachedComment;
33+
using CacheContainer = CommentsCacheContainer;
34+
static constexpr auto kKeyMember = &real_medium::models::CachedComment::id;
35+
static userver::storages::postgres::Query kQuery;
36+
static constexpr auto kUpdatedField = "c.updated_at";
37+
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
3838
};
3939

4040
using CommentsCache = ::userver::components::PostgreCache<CommentCachePolicy>;

0 commit comments

Comments
 (0)