Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 많이 조회하는 키워드 테이블 제약 조건 추가 #903

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void updateWeight(double newWeight) {

public void resetWeight() {
this.weight = 1.0;
this.lastSearchedAt = LocalDateTime.now();
}

public void incrementTotalSearch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "article_search_keyword_ip_map", indexes = {
@Index(name = "idx_ip_address", columnList = "ipAddress")
@Index(name = "idx_ip_address", columnList = "ipAddress")}, uniqueConstraints = {
@UniqueConstraint(name = "ux_keyword_ip", columnNames = {"keyword_id", "ipAddress"})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

유니크를 걸어둬도 유니크 필드가 중복되면 결국 에러가 발생하는 것 아닌가요? 유니크만 걸어둬도 괜찮나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동시성 제어 어노테이션으로 나중에 어플리케이션 단에서 막을 것입니다!

})
@NoArgsConstructor(access = PROTECTED)
public class ArticleSearchKeywordIpMap extends BaseEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface ArticleSearchKeywordIpMapRepository extends Repository<ArticleS

Optional<ArticleSearchKeywordIpMap> findByArticleSearchKeywordAndIpAddress(ArticleSearchKeyword keyword, String ipAddress);

List<ArticleSearchKeywordIpMap> findByCreatedAtBetween(LocalDateTime fiveHoursThirtyMinutesAgo, LocalDateTime now);
List<ArticleSearchKeywordIpMap> findByUpdatedAtBetween(LocalDateTime before, LocalDateTime now);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public interface ArticleSearchKeywordRepository extends Repository<ArticleSearch
""")
List<String> findTopKeywordsByLatest(Pageable pageable);

List<ArticleSearchKeyword> findByCreatedAtBetween(LocalDateTime startTime, LocalDateTime endTime);
List<ArticleSearchKeyword> findByUpdatedAtBetween(LocalDateTime startTime, LocalDateTime endTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ private void updateKeywordWeightAndCount(ArticleSearchKeyword keyword, ArticleSe
if (map.getSearchCount() <= 10) {
keyword.updateWeight(keyword.getWeight() + additionalWeight);
}

keyword.updateWeight(keyword.getWeight() + additionalWeight);
articleSearchKeywordRepository.save(keyword);
}

Expand All @@ -221,9 +219,9 @@ public void resetWeightsAndCounts() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime before = now.minusHours(6).minusMinutes(30);

List<ArticleSearchKeyword> keywordsToUpdate = articleSearchKeywordRepository.findByCreatedAtBetween(
List<ArticleSearchKeyword> keywordsToUpdate = articleSearchKeywordRepository.findByUpdatedAtBetween(
before, now);
List<ArticleSearchKeywordIpMap> ipMapsToUpdate = articleSearchKeywordIpMapRepository.findByCreatedAtBetween(
List<ArticleSearchKeywordIpMap> ipMapsToUpdate = articleSearchKeywordIpMapRepository.findByUpdatedAtBetween(
before, now);

for (ArticleSearchKeyword keyword : keywordsToUpdate) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ALTER TABLE `article_search_keywords`
MODIFY COLUMN `keyword` VARCHAR(255) NOT NULL UNIQUE;

ALTER TABLE article_search_keyword_ip_map
ADD CONSTRAINT unique_keyword_ip UNIQUE (keyword_id, ip_address),
ADD CONSTRAINT fk_keyword_id
FOREIGN KEY (keyword_id)
REFERENCES article_search_keywords (id)
ON DELETE CASCADE;
Loading