Skip to content

Commit

Permalink
Migrate back to non-reactive
Browse files Browse the repository at this point in the history
- Frontend was blocking, doesn't make sense to have the reactive backend.
- Filtering and sorting in the MongoDB instead of Java
  • Loading branch information
mithandir committed Sep 9, 2023
1 parent 1a3529e commit fd0a9fa
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 77 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
Expand Down Expand Up @@ -112,7 +112,7 @@
<configuration>
<jvmArguments>
-Dspring.profiles.active=local
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 --enable-preview
--enable-preview
</jvmArguments>
</configuration>
</plugin>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/ch/climbd/newsfeed/config/MongoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ch.climbd.newsfeed.config;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@EnableMongoRepositories
public class MongoApplication
extends AbstractMongoClientConfiguration {

@Bean
public MongoClient mongoClient() {
return MongoClients.create();
}

@Override
protected String getDatabaseName() {
return "reactive";
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ch.climbd.newsfeed.config;

import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.client.MongoClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;

import java.time.ZoneId;
Expand All @@ -19,7 +19,7 @@
import java.util.List;

@Configuration
public class ReactiveMongoConfig {
public class MongoConfig {

@Autowired
private MongoClient mongoClient;
Expand All @@ -28,8 +28,8 @@ public class ReactiveMongoConfig {
String db;

@Bean
public ReactiveMongoTemplate reactiveMongoTemplate() {
return new ReactiveMongoTemplate(mongoClient, db);
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoClient, db);
}

@Bean
Expand Down

This file was deleted.

63 changes: 38 additions & 25 deletions src/main/java/ch/climbd/newsfeed/controller/MongoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

@Service
Expand All @@ -22,24 +22,24 @@ public class MongoController {
private static final Logger LOG = LoggerFactory.getLogger(MongoController.class);

@Autowired
private ReactiveMongoTemplate template;
private MongoTemplate template;

public Mono<NewsEntry> findByLink(String link) {
public NewsEntry findByLink(String link) {
return template.findById(link, NewsEntry.class);
}

public Flux<NewsEntry> findAllOrderedByDate(Set<String> language) {
public List<NewsEntry> findAllOrderedByDate(Set<String> language) {
Query query = new Query();
query.addCriteria(Criteria.where("publishedAt").gte(ZonedDateTime.now().minusDays(2).toInstant()));
query.addCriteria(Criteria.where("language").in(language));
query.with(Sort.by(Sort.Direction.DESC, "publishedAt"));
query.limit(100);

return template.find(query, NewsEntry.class)
.sort(Comparator.comparing(NewsEntry::getPublishedDateTime).reversed())
.take(100);
return template.find(query, NewsEntry.class);
}

public Flux<NewsEntry> findAllOrderedByVotes(Set<String> language) {
Comparator<NewsEntry> compareByVote = (NewsEntry o1, NewsEntry o2) -> {
public List<NewsEntry> findAllOrderedByVotes(Set<String> language) {
Comparator<NewsEntry> compareByVotePerDay = (NewsEntry o1, NewsEntry o2) -> {
// Cut down to just plain day without hours, minutes and seconds.
// Then add the amount of votes as seconds to have sorting inside a day.
var obj1 = o1.getPublishedDateTime()
Expand All @@ -55,38 +55,51 @@ public Flux<NewsEntry> findAllOrderedByVotes(Set<String> language) {
Query query = new Query();
query.addCriteria(Criteria.where("language").in(language));
query.addCriteria(Criteria.where("votes").gte(1));
query.with(Sort.by(Sort.Direction.DESC, "votes"));
query.limit(100);

return template.find(query, NewsEntry.class).sort(compareByVote).take(100);
var result = template.find(query, NewsEntry.class);
result.sort(compareByVotePerDay);
return result;
}

public boolean exists(NewsEntry newsEntry) {
return findByLink(newsEntry.getLink()).hasElement().block();
var result = findByLink(newsEntry.getLink());
return (result != null && result.getTitle() != null);
}

public void save(NewsEntry newsEntry) {
template.save(Mono.just(newsEntry)).subscribe(success -> LOG.info("SavedEntry: " + success.toString()), error -> LOG.error("Could not save entry: " + error));
template.save(newsEntry);
}

public void update(NewsEntry newsEntry) {
template.save(Mono.just(newsEntry)).subscribe(success -> LOG.info("Updated entry: " + success.toString()), error -> LOG.error("Could not update entry: " + error));
template.save(newsEntry);
}

public void increaseVote(NewsEntry newsEntry) {
template.findById(newsEntry.getLink(), NewsEntry.class).subscribe(success -> {
success.setVotes(success.getVotes() + 1);
update(success);
}, error -> LOG.error("Error: " + error));
var result = template.findById(newsEntry.getLink(), NewsEntry.class);
if (result != null) {
result.setVotes(result.getVotes() + 1);
update(result);
}
}

public void decreaseVote(NewsEntry newsEntry) {
template.findById(newsEntry.getLink(), NewsEntry.class).subscribe(success -> {
success.setVotes(success.getVotes() - 1);
update(success);
}, error -> LOG.error("Error: " + error));
var result = template.findById(newsEntry.getLink(), NewsEntry.class);
if (result != null) {
result.setVotes(result.getVotes() - 1);
update(result);
}
}

public Flux<NewsEntry> searchEntries(String searchString) {
public List<NewsEntry> searchEntries(String searchString) {
Criteria regex = Criteria.where("title").regex(".*" + searchString + ".*", "i");
return template.find(new Query().addCriteria(regex), NewsEntry.class).sort(Comparator.comparing(NewsEntry::getPublishedDateTime).reversed()).take(100);

var query = new Query()
.addCriteria(regex)
.with(Sort.by(Sort.Direction.DESC, "publishedAt"))
.limit(100);

return template.find(query, NewsEntry.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ public void processRss(String url, String language) {
private NewsEntry map(SyndEntry item) {
NewsEntry result = new NewsEntry();
String title = item.getTitle().strip();
if (title == null) {
result.setTitle(null);
} else {
result.setTitle(filter.replaceHtml(title));
}
result.setTitle(filter.replaceHtml(title));

StringBuilder content = new StringBuilder();
if (item.getContents() != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/climbd/newsfeed/views/LatestView.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void init() {

add(commonSessionComponents.createMenu());

sourceData = new LinkedList<>(mongo.findAllOrderedByDate(commonSessionComponents.getSelectedLanguages()).collectList().block());
sourceData = new LinkedList<>(mongo.findAllOrderedByDate(commonSessionComponents.getSelectedLanguages()));
newsItems = newsItemComponent.createNewsItem(sourceData);
newsItems.setWidthFull();
newsItems.getStyle().set("margin-left", commonComponents.isMobile() ? "2%" : "10%");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/climbd/newsfeed/views/PopularView.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void init() {

add(commonSessionComponents.createMenu());

VerticalLayout newsItems = newsItemComponent.createNewsItem(mongo.findAllOrderedByVotes(commonSessionComponents.getSelectedLanguages()).collectList().block());
VerticalLayout newsItems = newsItemComponent.createNewsItem(mongo.findAllOrderedByVotes(commonSessionComponents.getSelectedLanguages()));
newsItems.setWidthFull();
newsItems.getStyle().set("margin-left", commonComponents.isMobile() ? "2%" : "10%");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ public String findIcon(String pageUrl) {

Element e2 = doc.head().select("meta[itemprop=image]").first();
if (e2 != null && !e2.attr("itemprop").isBlank()) {
String url = e1.attr("itemprop");
if (!url.startsWith("http")) {
String url = null;
if (e1 != null) {
url = e1.attr("itemprop");
}
if (url != null && !url.startsWith("http")) {
url = pageUrl + url;
}
iconCache.put(pageUrl, url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import com.vaadin.flow.component.textfield.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.util.List;

@Component
public class SearchComponent {
Expand All @@ -31,10 +32,10 @@ public VerticalLayout createSearchBar(VerticalLayout newsItems) {
searchButton.addClickListener(event -> {
var searchValue = textField.getValue().strip();
if (searchValue.length() >= 4 && searchValue.matches("\\p{Alnum}*")) {
Flux<NewsEntry> newsEntries = mongoController.searchEntries(searchValue);
List<NewsEntry> newsEntries = mongoController.searchEntries(searchValue);
newsItems.getUI().get().access(() -> {
newsItems.removeAll();
newsItems.add(newsItemComponent.createNewsItem(newsEntries.collectList().block()));
newsItems.add(newsItemComponent.createNewsItem(newsEntries));
});
}
});
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server:
http2.enabled: true
compression.enabled: true

management:
endpoints:
web.exposure.include: health

spring:
main:
lazy-initialization: true
jmx.enabled: false

logging:
level:
org.springframework:
data.mongodb.core.MongoTemplate: DEBUG
8 changes: 0 additions & 8 deletions src/main/resources/example-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ pushover:
api-key: <your pushover api key>
user-key: <your user key>

server:
http2.enabled: true
compression.enabled: true

management:
endpoints:
web.exposure.include: health

spring:
data.mongodb:
host: <host>
Expand Down

0 comments on commit fd0a9fa

Please sign in to comment.