Skip to content

Commit 34ffd86

Browse files
authored
Use userver::utils::zstring_view for better safety and fix naming of some variables (#14)
1 parent 3e1ca55 commit 34ffd86

23 files changed

+142
-137
lines changed

src/cache/articles_cache.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@
55
namespace real_medium::cache::articles_cache {
66

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

1010
void ArticlesCacheContainer::insert_or_assign(Key&& key, Article&& article) {
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);
11+
auto article_ptr = std::make_shared<const Article>(std::move(article));
12+
auto old_value = article_by_key_.find(key);
13+
if (old_value != article_by_key_.end()) {
14+
article_by_slug_.erase(old_value->second->slug);
15+
for (const auto& oldFollower : old_value->second->authorFollowedByUsersIds)
16+
if (article_ptr->authorFollowedByUsersIds.find(oldFollower) == article_ptr->authorFollowedByUsersIds.end())
17+
articles_by_follower_[oldFollower].erase(article_ptr->articleId);
1818
}
1919

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);
20+
article_by_key_.insert_or_assign(key, article_ptr);
21+
article_by_slug_.insert_or_assign(article_ptr->slug, article_ptr);
22+
for (const auto& follower : article_ptr->authorFollowedByUsersIds)
23+
articles_by_follower_[follower].insert_or_assign(article_ptr->articleId, article_ptr);
24+
recent_articles_.insert_or_assign({article_ptr->createdAt, article_ptr->articleId}, article_ptr);
2525
}
2626

27-
size_t ArticlesCacheContainer::size() const { return articleByKey_.size(); }
27+
size_t ArticlesCacheContainer::size() const { return article_by_key_.size(); }
2828

2929
ArticlesCacheContainer::ArticlePtr ArticlesCacheContainer::findArticleBySlug(const Slug& slug) const {
30-
auto it = articleBySlug_.find(slug);
31-
if (it == articleBySlug_.end()) return nullptr;
30+
auto it = article_by_slug_.find(slug);
31+
if (it == article_by_slug_.end()) return nullptr;
3232
return it->second;
3333
}
3434

@@ -37,7 +37,7 @@ std::vector<ArticlesCacheContainer::ArticlePtr> ArticlesCacheContainer::getRecen
3737
) const {
3838
std::vector<ArticlePtr> articles;
3939
int offset = 0;
40-
for (const auto& it : recentArticles_) {
40+
for (const auto& it : recent_articles_) {
4141
if (filter.limit && articles.size() >= userver::utils::numeric_cast<std::size_t>(filter.limit)) break;
4242

4343
const auto& tags = it.second->tags;
@@ -57,16 +57,16 @@ std::vector<ArticlesCacheContainer::ArticlePtr> ArticlesCacheContainer::getRecen
5757
return articles;
5858
}
5959
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 {};
60+
ArticlesCacheContainer::getFeed(real_medium::handlers::FeedArticleFilterDTO& filter, UserId auth_id) const {
61+
auto followed_articles_umap = articles_by_follower_.find(auth_id);
62+
if (followed_articles_umap == articles_by_follower_.end()) return {};
6363

6464
RecentArticlesMap followedArticlesOrdered;
65-
for (const auto& it : followedArticlesUMap->second)
65+
for (const auto& it : followed_articles_umap->second)
6666
followedArticlesOrdered.insert_or_assign({it.second->createdAt, it.second->articleId}, it.second);
6767

6868
std::vector<ArticlePtr> articles;
69-
;
69+
7070
int offset = 0;
7171
for (const auto& it : followedArticlesOrdered) {
7272
if (filter.limit && articles.size() >= userver::utils::numeric_cast<std::size_t>(filter.limit)) break;

src/cache/articles_cache.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#pragma once
2+
23
#include <docs/api/api.hpp>
34
#include <memory>
45
#include <unordered_map>
6+
57
#include <userver/cache/base_postgres_cache.hpp>
68
#include <userver/storages/postgres/io/chrono.hpp>
9+
710
#include "db/sql.hpp"
811
#include "dto/filter.hpp"
912
#include "models/article.hpp"
1013

1114
namespace real_medium::cache::articles_cache {
15+
1216
class ArticlesCacheContainer {
1317
using Timepoint = userver::storages::postgres::TimePointTz;
1418
using Slug = std::string;
@@ -40,10 +44,10 @@ class ArticlesCacheContainer {
4044
};
4145

4246
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_;
47+
std::unordered_map<Key, ArticlePtr> article_by_key_;
48+
std::unordered_map<Slug, ArticlePtr> article_by_slug_;
49+
std::unordered_map<UserId /*follower*/, std::unordered_map<Key, ArticlePtr>> articles_by_follower_;
50+
RecentArticlesMap recent_articles_;
4751
};
4852

4953
struct ArticlesCachePolicy {
@@ -55,5 +59,7 @@ struct ArticlesCachePolicy {
5559
static constexpr auto kUpdatedField = "updated_at";
5660
using UpdatedFieldType = userver::storages::postgres::TimePointTz;
5761
};
62+
5863
using ArticlesCache = ::userver::components::PostgreCache<ArticlesCachePolicy>;
64+
5965
} // namespace real_medium::cache::articles_cache

src/cache/comments_cache.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99
namespace real_medium::cache::comments_cache {
1010

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

1414
void CommentsCacheContainer::insert_or_assign(
15-
real_medium::cache::comments_cache::CommentsCacheContainer::Key&& commentId,
15+
real_medium::cache::comments_cache::CommentsCacheContainer::Key&& comment_id,
1616
real_medium::cache::comments_cache::CommentsCacheContainer::Comment&& comment
1717
) {
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);
18+
auto comment_ptr = std::make_shared<const Comment>(std::move(comment));
19+
if (comment_to_key_.count(comment_id)) {
20+
auto& old_slug = comment_to_key_[comment_id]->slug;
21+
if (old_slug != comment_ptr->slug) {
22+
auto& comments = comments_to_slug_[old_slug];
23+
comments_to_slug_[comment_ptr->slug] = comments;
24+
comments_to_slug_.erase(old_slug);
2525
}
2626
}
27-
comment_to_key_.insert_or_assign(commentId, commentPtr);
28-
comments_to_slug_[commentPtr->slug].insert_or_assign(commentId, commentPtr);
27+
comment_to_key_.insert_or_assign(comment_id, comment_ptr);
28+
comments_to_slug_[comment_ptr->slug].insert_or_assign(comment_id, comment_ptr);
2929
};
3030

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

src/db/sql.hpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#pragma once
22

3-
#include <string_view>
3+
#include <userver/utils/zstring_view.hpp>
44

55
namespace real_medium::sql {
66

7-
inline constexpr std::string_view kInsertUser = R"~(
7+
inline constexpr userver::utils::zstring_view kInsertUser = R"~(
88
INSERT INTO real_medium.users(username, email, bio, image, password_hash, salt)
99
VALUES($1, $2, $3, $4, $5, $6)
1010
RETURNING *
1111
)~";
1212

13-
inline constexpr std::string_view kSelectUserByEmailAndPassword = R"~(
13+
inline constexpr userver::utils::zstring_view kSelectUserByEmailAndPassword = R"~(
1414
SELECT * FROM real_medium.users
1515
WHERE email = $1 AND password_hash = $2
1616
)~";
1717

18-
inline constexpr std::string_view kUpdateUser = R"~(
18+
inline constexpr userver::utils::zstring_view kUpdateUser = R"~(
1919
UPDATE real_medium.users SET
2020
username = COALESCE($2, username),
2121
email = COALESCE($3, email),
@@ -27,16 +27,16 @@ WHERE user_id = $1
2727
RETURNING *
2828
)~";
2929

30-
inline constexpr std::string_view kFindUserById = R"~(
30+
inline constexpr userver::utils::zstring_view kFindUserById = R"~(
3131
SELECT * FROM real_medium.users WHERE user_id = $1
3232
)~";
3333

34-
inline constexpr std::string_view kFindUserIDByUsername = R"~(
34+
inline constexpr userver::utils::zstring_view kFindUserIDByUsername = R"~(
3535
SELECT user_id FROM real_medium.users WHERE username = $1
3636
)~";
3737

3838
// Comments
39-
inline constexpr std::string_view kFindCommentByIdAndSlug = R"~(
39+
inline constexpr userver::utils::zstring_view kFindCommentByIdAndSlug = R"~(
4040
WITH article AS (
4141
SELECT article_id FROM real_medium.articles WHERE slug = $2
4242
)
@@ -45,7 +45,7 @@ JOIN article ON article.article_id = real_medium.comments.article_id
4545
WHERE comment_id = $1
4646
)~";
4747

48-
inline constexpr std::string_view kFindCommentsByArticleId = R"~(
48+
inline constexpr userver::utils::zstring_view kFindCommentsByArticleId = R"~(
4949
WITH comments AS (
5050
SELECT * FROM real_medium.comments WHERE article_id = $1
5151
)
@@ -66,12 +66,12 @@ SELECT
6666
FROM comments
6767
)~";
6868

69-
inline constexpr std::string_view kDeleteCommentById = R"~(
69+
inline constexpr userver::utils::zstring_view kDeleteCommentById = R"~(
7070
DELETE FROM real_medium.comments WHERE comment_id = $1 AND user_id = $2
7171
RETURNING *
7272
)~";
7373

74-
inline constexpr std::string_view kAddComment = R"~(
74+
inline constexpr userver::utils::zstring_view kAddComment = R"~(
7575
WITH comment AS (
7676
INSERT INTO real_medium.comments(body, user_id, article_id)
7777
VALUES ($1, $2, $3)
@@ -91,16 +91,16 @@ SELECT
9191
FROM comment
9292
)~";
9393

94-
inline constexpr std::string_view kFindIdArticleBySlug = R"~(
94+
inline constexpr userver::utils::zstring_view kFindIdArticleBySlug = R"~(
9595
SELECT article_id FROM real_medium.articles WHERE slug = $1
9696
)~";
9797

98-
inline constexpr std::string_view kIsProfileFollowing = R"~(
98+
inline constexpr userver::utils::zstring_view kIsProfileFollowing = R"~(
9999
RETURN EXISTS (SELECT 1 FROM real_medium.followers WHERE follower_user_id = $1 AND followed_user_id = $2);
100100
)~";
101101

102102
// TODO: reuse common kIsProfileFollowing
103-
inline constexpr std::string_view kGetProfileByUsername = R"~(
103+
inline constexpr userver::utils::zstring_view kGetProfileByUsername = R"~(
104104
WITH profile AS (
105105
SELECT * FROM real_medium.users WHERE username = $1
106106
)
@@ -112,11 +112,11 @@ SELECT profile.username, profile.bio, profile.image,
112112
FROM profile
113113
)~";
114114

115-
inline constexpr std::string_view kGetSaltByEmail = R"~(
115+
inline constexpr userver::utils::zstring_view kGetSaltByEmail = R"~(
116116
SELECT salt FROM real_medium.users WHERE email = $1
117117
)~";
118118

119-
inline constexpr std::string_view KFollowingUser = R"~(
119+
inline constexpr userver::utils::zstring_view KFollowingUser = R"~(
120120
WITH profile AS (
121121
SELECT * FROM real_medium.users WHERE user_id = $1
122122
), following AS (
@@ -132,7 +132,7 @@ SELECT
132132
FROM profile
133133
)~";
134134

135-
inline constexpr std::string_view KUnFollowingUser = R"~(
135+
inline constexpr userver::utils::zstring_view KUnFollowingUser = R"~(
136136
WITH profile AS (
137137
SELECT * FROM real_medium.users WHERE user_id = $1
138138
), following AS (
@@ -147,19 +147,19 @@ SELECT
147147
FROM profile
148148
)~";
149149

150-
inline constexpr std::string_view kCreateArticle{R"~(
150+
inline constexpr userver::utils::zstring_view kCreateArticle{R"~(
151151
SELECT real_medium.create_article($1, $2, $3, $4, $5, $6)
152152
)~"};
153153

154-
inline constexpr std::string_view kGetArticleWithAuthorProfile{R"~(
154+
inline constexpr userver::utils::zstring_view kGetArticleWithAuthorProfile{R"~(
155155
SELECT real_medium.get_article_with_author_profile($1, $2)
156156
)~"};
157157

158-
inline constexpr std::string_view kGetArticleWithAuthorProfileBySlug{R"~(
158+
inline constexpr userver::utils::zstring_view kGetArticleWithAuthorProfileBySlug{R"~(
159159
SELECT real_medium.get_article_with_author_profile_by_slug($1, $2)
160160
)~"};
161161

162-
inline constexpr std::string_view kInsertFavoritePair = R"~(
162+
inline constexpr userver::utils::zstring_view kInsertFavoritePair = R"~(
163163
WITH tmp(article_id, user_id) AS (
164164
SELECT article_id, $1 FROM real_medium.articles WHERE slug=$2
165165
)
@@ -168,13 +168,13 @@ ON CONFLICT DO NOTHING
168168
RETURNING article_id
169169
)~";
170170

171-
inline constexpr std::string_view kIncrementFavoritesCount = R"~(
171+
inline constexpr userver::utils::zstring_view kIncrementFavoritesCount = R"~(
172172
UPDATE real_medium.articles
173173
SET favorites_count=favorites_count + 1
174174
WHERE article_id=$1
175175
)~";
176176

177-
inline constexpr std::string_view kDeleteFavoritePair = R"~(
177+
inline constexpr userver::utils::zstring_view kDeleteFavoritePair = R"~(
178178
WITH tmp(article_id, user_id) AS (
179179
SELECT article_id, $1 FROM real_medium.articles WHERE slug=$2
180180
)
@@ -183,33 +183,33 @@ WHERE (article_id, user_id) IN (SELECT article_id, user_id FROM tmp)
183183
RETURNING article_id
184184
)~";
185185

186-
inline constexpr std::string_view kDecrementFavoritesCount = R"~(
186+
inline constexpr userver::utils::zstring_view kDecrementFavoritesCount = R"~(
187187
UPDATE real_medium.articles
188188
SET favorites_count=favorites_count - 1
189189
WHERE article_id=$1
190190
)~";
191191

192-
inline constexpr std::string_view kFindArticlesByFollowedUsers = R"~(
192+
inline constexpr userver::utils::zstring_view kFindArticlesByFollowedUsers = R"~(
193193
SELECT real_medium.get_feed_articles($1, $2, $3)
194194
)~";
195195

196-
inline constexpr std::string_view kGetArticleIdBySlug{R"~(
196+
inline constexpr userver::utils::zstring_view kGetArticleIdBySlug{R"~(
197197
SELECT real_medium.get_article_id_by_slug($1)
198198
)~"};
199199

200-
inline constexpr std::string_view kUpdateArticleBySlug{R"~(
200+
inline constexpr userver::utils::zstring_view kUpdateArticleBySlug{R"~(
201201
SELECT real_medium.update_article_by_slug($1, $2, $3, $4, $5, $6)
202202
)~"};
203203

204-
inline constexpr std::string_view kDeleteArticleBySlug{R"~(
204+
inline constexpr userver::utils::zstring_view kDeleteArticleBySlug{R"~(
205205
SELECT real_medium.delete_article_by_slug($1, $2)
206206
)~"};
207207

208-
inline constexpr std::string_view kFindArticlesByFilters{R"~(
208+
inline constexpr userver::utils::zstring_view kFindArticlesByFilters{R"~(
209209
SELECT real_medium.get_articles_by_filters($1, $2, $3, $4, $5, $6)
210210
)~"};
211211

212-
inline constexpr std::string_view kSelectFullArticleInfo = {R"~(
212+
inline constexpr userver::utils::zstring_view kSelectFullArticleInfo = {R"~(
213213
SELECT a.article_id AS articleId,
214214
a.title,
215215
a.slug,
@@ -245,7 +245,7 @@ FROM real_medium.articles a
245245
JOIN real_medium.users u ON a.user_id = u.user_id
246246
)~"};
247247

248-
inline constexpr std::string_view kSelectCachedComments = {R"~(
248+
inline constexpr userver::utils::zstring_view kSelectCachedComments = {R"~(
249249
SELECT c.comment_id,
250250
c.created_at AS createdAt,
251251
c.updated_at AS updatedAt,

src/handlers/articles/articles_favorite.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
2121
userver::storages::postgres::Transaction::RW
2222
);
2323

24-
auto res = transaction.Execute(sql::kInsertFavoritePair.data(), user_id, slug);
24+
auto res = transaction.Execute(sql::kInsertFavoritePair.c_str(), user_id, slug);
2525

2626
if (!res.IsEmpty()) {
2727
auto article_id = res.AsSingleRow<std::string>();
2828

29-
transaction.Execute(sql::kIncrementFavoritesCount.data(), article_id);
29+
transaction.Execute(sql::kIncrementFavoritesCount.c_str(), article_id);
3030
transaction.Commit();
3131
}
3232

3333
const auto get_article_res = GetPg().Execute(
3434
userver::storages::postgres::ClusterHostType::kSlave,
35-
real_medium::sql::kGetArticleWithAuthorProfileBySlug.data(),
35+
real_medium::sql::kGetArticleWithAuthorProfileBySlug.c_str(),
3636
slug,
3737
user_id
3838
);

src/handlers/articles/articles_get.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
2626

2727
auto user_id = context.GetData<std::optional<std::string>>("id");
2828
auto data = GetArticlesCache().Get();
29-
auto recentArticles = data->getRecent(filter);
29+
auto recent_articles = data->getRecent(filter);
3030
userver::formats::json::ValueBuilder builder;
3131
builder["articles"] = userver::formats::common::Type::kArray;
32-
for (auto& article : recentArticles) builder["articles"].PushBack(dto::Article::Parse(*article, user_id));
33-
builder["articlesCount"] = recentArticles.size();
32+
for (auto& article : recent_articles) builder["articles"].PushBack(dto::Article::Parse(*article, user_id));
33+
builder["articlesCount"] = recent_articles.size();
3434
return builder.ExtractValue();
3535
}
3636

0 commit comments

Comments
 (0)