From 4b591507e73ec4407c93d3bc08447351166883ed Mon Sep 17 00:00:00 2001 From: Thibault Deutsch Date: Sat, 9 Dec 2017 22:29:22 +0100 Subject: [PATCH] Refactor in more modules and reformat --- build.gradle | 3 +- .../main/java/com/epita/domain/Document.java | 4 ++- .../java/com/epita/domain/SimpleCrawler.java | 8 ++--- .../java/com/epita/domain/SimpleIndexer.java | 17 ++-------- .../java/com/epita/domain}/StopWords.java | 2 +- .../src/main/java/com/epita/domain/Term.java | 4 ++- .../java/com/epita/eventbus/EventMessage.java | 1 + .../com/epita/eventbus/EventSubscription.java | 2 ++ .../eventbus/{ => client}/EventBusClient.java | 2 +- .../{ => client}/NettyClientHandler.java | 3 +- .../{ => client}/NettyClientInitializer.java | 5 +-- .../{ => client}/NettyEventBusClient.java | 21 +++++++------ .../eventbus/{ => server}/NettyServer.java | 2 +- .../eventbus/{ => server}/ServerHandler.java | 2 +- .../{ => server}/ServerInitializer.java | 4 +-- eventsourcing/build.gradle | 13 ++++++++ .../java/com.epita}/eventsourcing/Event.java | 2 +- .../com.epita}/eventsourcing/EventStore.java | 2 +- .../com.epita}/eventsourcing/Reducer.java | 2 +- settings.gradle | 4 ++- src/main/java/com/epita/guereza/Main.java | 31 ++++++------------- .../com/epita/guereza/WrapperStringArray.java | 14 --------- .../epita/guereza/{ => application}/App.java | 4 +-- .../guereza/{ => application}/CrawlerApp.java | 11 ++++--- .../{ => application}/EventStoreApp.java | 14 +++++---- .../guereza/{ => application}/IndexerApp.java | 11 ++----- .../guereza/{ => application}/ServerApp.java | 6 ++-- .../guereza/{ => reducer}/RetroIndex.java | 6 ++-- .../epita/guereza/{ => reducer}/UrlStore.java | 20 ++++++------ 29 files changed, 102 insertions(+), 118 deletions(-) rename src/main/java/com/epita/guereza/service/CrawlerService.java => domain/src/main/java/com/epita/domain/SimpleCrawler.java (82%) rename src/main/java/com/epita/guereza/service/indexer/IndexerService.java => domain/src/main/java/com/epita/domain/SimpleIndexer.java (91%) rename {src/main/java/com/epita/guereza/service/indexer => domain/src/main/java/com/epita/domain}/StopWords.java (97%) rename eventbus/src/main/java/com/epita/eventbus/{ => client}/EventBusClient.java (98%) rename eventbus/src/main/java/com/epita/eventbus/{ => client}/NettyClientHandler.java (92%) rename eventbus/src/main/java/com/epita/eventbus/{ => client}/NettyClientInitializer.java (83%) rename eventbus/src/main/java/com/epita/eventbus/{ => client}/NettyEventBusClient.java (98%) rename eventbus/src/main/java/com/epita/eventbus/{ => server}/NettyServer.java (97%) rename eventbus/src/main/java/com/epita/eventbus/{ => server}/ServerHandler.java (97%) rename eventbus/src/main/java/com/epita/eventbus/{ => server}/ServerInitializer.java (84%) create mode 100644 eventsourcing/build.gradle rename {src/main/java/com/epita/guereza => eventsourcing/src/main/java/com.epita}/eventsourcing/Event.java (85%) rename {src/main/java/com/epita/guereza => eventsourcing/src/main/java/com.epita}/eventsourcing/EventStore.java (93%) rename {src/main/java/com/epita/guereza => eventsourcing/src/main/java/com.epita}/eventsourcing/Reducer.java (60%) delete mode 100644 src/main/java/com/epita/guereza/WrapperStringArray.java rename src/main/java/com/epita/guereza/{ => application}/App.java (95%) rename src/main/java/com/epita/guereza/{ => application}/CrawlerApp.java (85%) rename src/main/java/com/epita/guereza/{ => application}/EventStoreApp.java (82%) rename src/main/java/com/epita/guereza/{ => application}/IndexerApp.java (84%) rename src/main/java/com/epita/guereza/{ => application}/ServerApp.java (62%) rename src/main/java/com/epita/guereza/{ => reducer}/RetroIndex.java (85%) rename src/main/java/com/epita/guereza/{ => reducer}/UrlStore.java (83%) diff --git a/build.gradle b/build.gradle index dd2b0a2..de06229 100644 --- a/build.gradle +++ b/build.gradle @@ -13,12 +13,11 @@ dependencies { compile project(':domain') compile project(':winter') compile project(':eventbus') + compile project(':eventsourcing') - compile group: 'org.jsoup', name: 'jsoup', version: '1.11.2' compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3' compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' - compile group: 'io.netty', name: 'netty-all', version: '4.1.17.Final' compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.2' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.2' } diff --git a/domain/src/main/java/com/epita/domain/Document.java b/domain/src/main/java/com/epita/domain/Document.java index 2e3d335..0fc0478 100644 --- a/domain/src/main/java/com/epita/domain/Document.java +++ b/domain/src/main/java/com/epita/domain/Document.java @@ -2,11 +2,13 @@ import java.util.HashMap; +@SuppressWarnings("unused") public class Document { public String url; public HashMap terms; - public Document() {} + public Document() { + } public Document(final String url, final HashMap terms) { this.url = url; diff --git a/src/main/java/com/epita/guereza/service/CrawlerService.java b/domain/src/main/java/com/epita/domain/SimpleCrawler.java similarity index 82% rename from src/main/java/com/epita/guereza/service/CrawlerService.java rename to domain/src/main/java/com/epita/domain/SimpleCrawler.java index 4a51c0f..eb2e57a 100644 --- a/src/main/java/com/epita/guereza/service/CrawlerService.java +++ b/domain/src/main/java/com/epita/domain/SimpleCrawler.java @@ -1,7 +1,5 @@ -package com.epita.guereza.service; +package com.epita.domain; -import com.epita.domain.Crawler; -import com.epita.domain.RawDocument; import org.jsoup.Jsoup; import org.jsoup.select.Elements; import org.slf4j.Logger; @@ -9,8 +7,8 @@ import java.io.IOException; -public class CrawlerService implements Crawler { - private static final Logger LOGGER = LoggerFactory.getLogger(CrawlerService.class); +public class SimpleCrawler implements Crawler { + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleCrawler.class); @Override public RawDocument crawl(final String url) { diff --git a/src/main/java/com/epita/guereza/service/indexer/IndexerService.java b/domain/src/main/java/com/epita/domain/SimpleIndexer.java similarity index 91% rename from src/main/java/com/epita/guereza/service/indexer/IndexerService.java rename to domain/src/main/java/com/epita/domain/SimpleIndexer.java index 78a2303..31aebdb 100644 --- a/src/main/java/com/epita/guereza/service/indexer/IndexerService.java +++ b/domain/src/main/java/com/epita/domain/SimpleIndexer.java @@ -1,9 +1,5 @@ -package com.epita.guereza.service.indexer; +package com.epita.domain; -import com.epita.domain.Document; -import com.epita.domain.Indexer; -import com.epita.domain.Term; -import com.epita.guereza.service.CrawlerService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,8 +9,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -public class IndexerService implements Indexer { - private static final Logger LOGGER = LoggerFactory.getLogger(CrawlerService.class); +public class SimpleIndexer implements Indexer { + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleCrawler.class); private static final String REGEX_PUNCTUATION = "[.!?]"; private static final String REGEX_SPACE = "\\s+"; private static final String REGEX_ALPHANUM = "[^-\\dA-Za-z ]"; @@ -50,13 +46,6 @@ public Document index(final String text, final String url) { return new Document(url, terms); } - /* - @Override - public void publish(final Index i, final Document d) { - i.docs.add(d); - } - */ - @Override public Map search(final List docs, final String query) { final HashMap hits = new HashMap<>(); diff --git a/src/main/java/com/epita/guereza/service/indexer/StopWords.java b/domain/src/main/java/com/epita/domain/StopWords.java similarity index 97% rename from src/main/java/com/epita/guereza/service/indexer/StopWords.java rename to domain/src/main/java/com/epita/domain/StopWords.java index 361ec1f..50ea685 100644 --- a/src/main/java/com/epita/guereza/service/indexer/StopWords.java +++ b/domain/src/main/java/com/epita/domain/StopWords.java @@ -1,4 +1,4 @@ -package com.epita.guereza.service.indexer; +package com.epita.domain; import java.util.Arrays; import java.util.HashSet; diff --git a/domain/src/main/java/com/epita/domain/Term.java b/domain/src/main/java/com/epita/domain/Term.java index 3c1b14f..d6e9e75 100644 --- a/domain/src/main/java/com/epita/domain/Term.java +++ b/domain/src/main/java/com/epita/domain/Term.java @@ -2,12 +2,14 @@ import java.util.List; +@SuppressWarnings("unused") public class Term { public String token; public List position; public double frequency; - public Term() {} + public Term() { + } public Term(final String token, final List position, final double frequency) { this.token = token; diff --git a/eventbus/src/main/java/com/epita/eventbus/EventMessage.java b/eventbus/src/main/java/com/epita/eventbus/EventMessage.java index 6e5698b..914b9bc 100644 --- a/eventbus/src/main/java/com/epita/eventbus/EventMessage.java +++ b/eventbus/src/main/java/com/epita/eventbus/EventMessage.java @@ -1,5 +1,6 @@ package com.epita.eventbus; +import com.epita.eventbus.client.EventBusClient; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/eventbus/src/main/java/com/epita/eventbus/EventSubscription.java b/eventbus/src/main/java/com/epita/eventbus/EventSubscription.java index 5aada8a..5cbe390 100644 --- a/eventbus/src/main/java/com/epita/eventbus/EventSubscription.java +++ b/eventbus/src/main/java/com/epita/eventbus/EventSubscription.java @@ -1,5 +1,7 @@ package com.epita.eventbus; +import com.epita.eventbus.client.EventBusClient; + import java.time.LocalDateTime; import java.util.function.Consumer; diff --git a/eventbus/src/main/java/com/epita/eventbus/EventBusClient.java b/eventbus/src/main/java/com/epita/eventbus/client/EventBusClient.java similarity index 98% rename from eventbus/src/main/java/com/epita/eventbus/EventBusClient.java rename to eventbus/src/main/java/com/epita/eventbus/client/EventBusClient.java index a1bce88..64e3b56 100644 --- a/eventbus/src/main/java/com/epita/eventbus/EventBusClient.java +++ b/eventbus/src/main/java/com/epita/eventbus/client/EventBusClient.java @@ -1,4 +1,4 @@ -package com.epita.eventbus; +package com.epita.eventbus.client; import java.time.LocalDateTime; import java.util.function.Consumer; diff --git a/eventbus/src/main/java/com/epita/eventbus/NettyClientHandler.java b/eventbus/src/main/java/com/epita/eventbus/client/NettyClientHandler.java similarity index 92% rename from eventbus/src/main/java/com/epita/eventbus/NettyClientHandler.java rename to eventbus/src/main/java/com/epita/eventbus/client/NettyClientHandler.java index 3229f68..34045d5 100644 --- a/eventbus/src/main/java/com/epita/eventbus/NettyClientHandler.java +++ b/eventbus/src/main/java/com/epita/eventbus/client/NettyClientHandler.java @@ -1,5 +1,6 @@ -package com.epita.eventbus; +package com.epita.eventbus.client; +import com.epita.eventbus.EventMessage; import com.fasterxml.jackson.databind.ObjectMapper; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; diff --git a/eventbus/src/main/java/com/epita/eventbus/NettyClientInitializer.java b/eventbus/src/main/java/com/epita/eventbus/client/NettyClientInitializer.java similarity index 83% rename from eventbus/src/main/java/com/epita/eventbus/NettyClientInitializer.java rename to eventbus/src/main/java/com/epita/eventbus/client/NettyClientInitializer.java index 4fc7f1e..19e3a0a 100644 --- a/eventbus/src/main/java/com/epita/eventbus/NettyClientInitializer.java +++ b/eventbus/src/main/java/com/epita/eventbus/client/NettyClientInitializer.java @@ -1,5 +1,6 @@ -package com.epita.eventbus; +package com.epita.eventbus.client; +import com.epita.eventbus.EventMessage; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; @@ -21,7 +22,7 @@ public NettyClientInitializer(Consumer c) { public void initChannel(SocketChannel ch) { final ChannelPipeline pipeline = ch.pipeline(); - pipeline.addLast(new DelimiterBasedFrameDecoder(1048576*2, Delimiters.lineDelimiter())); + pipeline.addLast(new DelimiterBasedFrameDecoder(1048576 * 2, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); diff --git a/eventbus/src/main/java/com/epita/eventbus/NettyEventBusClient.java b/eventbus/src/main/java/com/epita/eventbus/client/NettyEventBusClient.java similarity index 98% rename from eventbus/src/main/java/com/epita/eventbus/NettyEventBusClient.java rename to eventbus/src/main/java/com/epita/eventbus/client/NettyEventBusClient.java index bce4ce8..7f5ddc9 100644 --- a/eventbus/src/main/java/com/epita/eventbus/NettyEventBusClient.java +++ b/eventbus/src/main/java/com/epita/eventbus/client/NettyEventBusClient.java @@ -1,5 +1,6 @@ -package com.epita.eventbus; +package com.epita.eventbus.client; +import com.epita.eventbus.EventSubscription; import com.fasterxml.jackson.databind.ObjectMapper; import io.netty.bootstrap.Bootstrap; import io.netty.channel.EventLoopGroup; @@ -17,10 +18,10 @@ public class NettyEventBusClient implements EventBusClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyEventBusClient.class); private final Map> subscriptionsMap; - private io.netty.channel.Channel nettyChannel; - private EventLoopGroup group; private final String host; private final int port; + private io.netty.channel.Channel nettyChannel; + private EventLoopGroup group; public NettyEventBusClient(final String host, final int port) { subscriptionsMap = new HashMap<>(); @@ -52,13 +53,6 @@ public boolean start() { return true; } - /** - * Close the EventBus - */ - public void shutdown() { - group.shutdownGracefully(); - } - /** * Subscribe to a new netty channel * @@ -111,6 +105,13 @@ public void publish(final NettyEventBusClient.Message message) { } } + /** + * Close the EventBus + */ + public void shutdown() { + group.shutdownGracefully(); + } + private void trigger(final NettyEventBusClient.Message message) { subscriptionsMap.getOrDefault(message.getChannel(), new ArrayList<>()) .forEach(c -> c.getCallback().accept(message)); diff --git a/eventbus/src/main/java/com/epita/eventbus/NettyServer.java b/eventbus/src/main/java/com/epita/eventbus/server/NettyServer.java similarity index 97% rename from eventbus/src/main/java/com/epita/eventbus/NettyServer.java rename to eventbus/src/main/java/com/epita/eventbus/server/NettyServer.java index e7384e2..6d53475 100644 --- a/eventbus/src/main/java/com/epita/eventbus/NettyServer.java +++ b/eventbus/src/main/java/com/epita/eventbus/server/NettyServer.java @@ -1,4 +1,4 @@ -package com.epita.eventbus; +package com.epita.eventbus.server; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; diff --git a/eventbus/src/main/java/com/epita/eventbus/ServerHandler.java b/eventbus/src/main/java/com/epita/eventbus/server/ServerHandler.java similarity index 97% rename from eventbus/src/main/java/com/epita/eventbus/ServerHandler.java rename to eventbus/src/main/java/com/epita/eventbus/server/ServerHandler.java index bdec6a0..9a5b3a6 100644 --- a/eventbus/src/main/java/com/epita/eventbus/ServerHandler.java +++ b/eventbus/src/main/java/com/epita/eventbus/server/ServerHandler.java @@ -1,4 +1,4 @@ -package com.epita.eventbus; +package com.epita.eventbus.server; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; diff --git a/eventbus/src/main/java/com/epita/eventbus/ServerInitializer.java b/eventbus/src/main/java/com/epita/eventbus/server/ServerInitializer.java similarity index 84% rename from eventbus/src/main/java/com/epita/eventbus/ServerInitializer.java rename to eventbus/src/main/java/com/epita/eventbus/server/ServerInitializer.java index 3d44cb5..5a057b5 100644 --- a/eventbus/src/main/java/com/epita/eventbus/ServerInitializer.java +++ b/eventbus/src/main/java/com/epita/eventbus/server/ServerInitializer.java @@ -1,4 +1,4 @@ -package com.epita.eventbus; +package com.epita.eventbus.server; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; @@ -14,7 +14,7 @@ public class ServerInitializer extends ChannelInitializer { protected void initChannel(SocketChannel socketChannel) { final ChannelPipeline pipeline = socketChannel.pipeline(); - pipeline.addLast(new DelimiterBasedFrameDecoder(1048576*2, Delimiters.lineDelimiter())); + pipeline.addLast(new DelimiterBasedFrameDecoder(1048576 * 2, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); diff --git a/eventsourcing/build.gradle b/eventsourcing/build.gradle new file mode 100644 index 0000000..4c3acd2 --- /dev/null +++ b/eventsourcing/build.gradle @@ -0,0 +1,13 @@ +group 'com.epita' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { +} diff --git a/src/main/java/com/epita/guereza/eventsourcing/Event.java b/eventsourcing/src/main/java/com.epita/eventsourcing/Event.java similarity index 85% rename from src/main/java/com/epita/guereza/eventsourcing/Event.java rename to eventsourcing/src/main/java/com.epita/eventsourcing/Event.java index 2e5d9c1..a6627f1 100644 --- a/src/main/java/com/epita/guereza/eventsourcing/Event.java +++ b/eventsourcing/src/main/java/com.epita/eventsourcing/Event.java @@ -1,4 +1,4 @@ -package com.epita.guereza.eventsourcing; +package com.epita.eventsourcing; @SuppressWarnings("WeakerAccess") public class Event { diff --git a/src/main/java/com/epita/guereza/eventsourcing/EventStore.java b/eventsourcing/src/main/java/com.epita/eventsourcing/EventStore.java similarity index 93% rename from src/main/java/com/epita/guereza/eventsourcing/EventStore.java rename to eventsourcing/src/main/java/com.epita/eventsourcing/EventStore.java index 44762a0..b2b0be6 100644 --- a/src/main/java/com/epita/guereza/eventsourcing/EventStore.java +++ b/eventsourcing/src/main/java/com.epita/eventsourcing/EventStore.java @@ -1,4 +1,4 @@ -package com.epita.guereza.eventsourcing; +package com.epita.eventsourcing; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/epita/guereza/eventsourcing/Reducer.java b/eventsourcing/src/main/java/com.epita/eventsourcing/Reducer.java similarity index 60% rename from src/main/java/com/epita/guereza/eventsourcing/Reducer.java rename to eventsourcing/src/main/java/com.epita/eventsourcing/Reducer.java index c4a834c..88dae15 100644 --- a/src/main/java/com/epita/guereza/eventsourcing/Reducer.java +++ b/eventsourcing/src/main/java/com.epita/eventsourcing/Reducer.java @@ -1,4 +1,4 @@ -package com.epita.guereza.eventsourcing; +package com.epita.eventsourcing; public interface Reducer { void reduce(Event event); diff --git a/settings.gradle b/settings.gradle index 124982b..d3c6668 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,4 +2,6 @@ rootProject.name = 'guereza' include 'domain', ':main' include 'winter', ':main' -include 'eventbus', ':main' \ No newline at end of file +include 'eventbus', ':main' +include 'eventsourcing', ':main' + diff --git a/src/main/java/com/epita/guereza/Main.java b/src/main/java/com/epita/guereza/Main.java index 6b34c99..70c2c3b 100644 --- a/src/main/java/com/epita/guereza/Main.java +++ b/src/main/java/com/epita/guereza/Main.java @@ -1,20 +1,20 @@ package com.epita.guereza; import com.epita.domain.Crawler; -import com.epita.domain.Document; -import com.epita.domain.Index; import com.epita.domain.Indexer; -import com.epita.eventbus.EventBusClient; -import com.epita.eventbus.NettyEventBusClient; -import com.epita.guereza.eventsourcing.EventStore; -import com.epita.guereza.service.CrawlerService; -import com.epita.guereza.service.indexer.IndexerService; +import com.epita.domain.SimpleCrawler; +import com.epita.domain.SimpleIndexer; +import com.epita.eventbus.client.EventBusClient; +import com.epita.eventbus.client.NettyEventBusClient; +import com.epita.eventsourcing.EventStore; +import com.epita.guereza.application.*; +import com.epita.guereza.reducer.RetroIndex; +import com.epita.guereza.reducer.UrlStore; import com.epita.winter.Scope; import com.epita.winter.provider.LazySingleton; import com.epita.winter.provider.Prototype; import com.epita.winter.provider.Singleton; -import java.util.Map; import java.util.function.Function; import static java.lang.System.exit; @@ -49,8 +49,8 @@ public static void main(String[] args) { private static Scope createScope(final String host, final int port) { return new Scope() - .register(new Singleton<>(Crawler.class, new CrawlerService())) - .register(new Singleton<>(Indexer.class, new IndexerService())) + .register(new Singleton<>(Crawler.class, new SimpleCrawler())) + .register(new Singleton<>(Indexer.class, new SimpleIndexer())) .register(new Singleton<>(EventBusClient.class, new NettyEventBusClient(host, port))); } @@ -99,15 +99,4 @@ private static void runApp(Scope scope) { private static void runServer() { new ServerApp(NETTY_PORT).run(); } - - private static void testSearch(final Index index, final String query) { - IndexerService indexer = new IndexerService(); - - System.out.printf("Results for '%s'\n", query); - - final Map res = indexer.search(index.docs, query); - for (final Map.Entry doc : res.entrySet()) { - System.out.printf("%100s %f\n", doc.getKey().url, doc.getValue()); - } - } } diff --git a/src/main/java/com/epita/guereza/WrapperStringArray.java b/src/main/java/com/epita/guereza/WrapperStringArray.java deleted file mode 100644 index edff3d1..0000000 --- a/src/main/java/com/epita/guereza/WrapperStringArray.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.epita.guereza; - -import java.util.Arrays; -import java.util.List; - -public class WrapperStringArray { - public List content; - - public WrapperStringArray() {} - - public WrapperStringArray(String[] content) { - this.content = Arrays.asList(content); - } -} diff --git a/src/main/java/com/epita/guereza/App.java b/src/main/java/com/epita/guereza/application/App.java similarity index 95% rename from src/main/java/com/epita/guereza/App.java rename to src/main/java/com/epita/guereza/application/App.java index 4812b33..413df66 100644 --- a/src/main/java/com/epita/guereza/App.java +++ b/src/main/java/com/epita/guereza/application/App.java @@ -1,7 +1,7 @@ -package com.epita.guereza; +package com.epita.guereza.application; -import com.epita.eventbus.EventBusClient; import com.epita.eventbus.EventMessage; +import com.epita.eventbus.client.EventBusClient; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; diff --git a/src/main/java/com/epita/guereza/CrawlerApp.java b/src/main/java/com/epita/guereza/application/CrawlerApp.java similarity index 85% rename from src/main/java/com/epita/guereza/CrawlerApp.java rename to src/main/java/com/epita/guereza/application/CrawlerApp.java index 5c0b2da..1272fc4 100644 --- a/src/main/java/com/epita/guereza/CrawlerApp.java +++ b/src/main/java/com/epita/guereza/application/CrawlerApp.java @@ -1,12 +1,13 @@ -package com.epita.guereza; +package com.epita.guereza.application; import com.epita.domain.Crawler; import com.epita.domain.RawDocument; -import com.epita.eventbus.EventBusClient; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.epita.eventbus.client.EventBusClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; + public class CrawlerApp extends App { private static final Logger LOGGER = LoggerFactory.getLogger(CrawlerApp.class); private final Crawler crawler; @@ -34,7 +35,7 @@ private void requestNextUrl() { private void storeUrls(final String[] urls) { if (urls != null) { LOGGER.info("Store {} urls", urls.length); - sendMessage("/store/crawler", new WrapperStringArray(urls)); + sendMessage("/store/crawler", Arrays.asList(urls)); } } @@ -43,7 +44,7 @@ private void storeUrls(final String[] urls) { public void run() { eventBus.subscribe(subscribeUrl, msg -> { if (msg != null) { - String url = (String)mappingObject(msg); + String url = (String) mappingObject(msg); if (url != null) { LOGGER.info("Receive url: {}", url); final String[] urls = crawlAndExtract(url); diff --git a/src/main/java/com/epita/guereza/EventStoreApp.java b/src/main/java/com/epita/guereza/application/EventStoreApp.java similarity index 82% rename from src/main/java/com/epita/guereza/EventStoreApp.java rename to src/main/java/com/epita/guereza/application/EventStoreApp.java index f9431af..17ba36f 100644 --- a/src/main/java/com/epita/guereza/EventStoreApp.java +++ b/src/main/java/com/epita/guereza/application/EventStoreApp.java @@ -1,20 +1,21 @@ -package com.epita.guereza; +package com.epita.guereza.application; import com.epita.domain.Document; -import com.epita.eventbus.EventBusClient; -import com.epita.guereza.eventsourcing.Event; -import com.epita.guereza.eventsourcing.EventStore; +import com.epita.eventbus.client.EventBusClient; +import com.epita.eventsourcing.Event; +import com.epita.eventsourcing.EventStore; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; import java.util.function.Consumer; public class EventStoreApp extends App { private static final Logger LOGGER = LoggerFactory.getLogger(EventStore.class); private final EventStore eventStore; - protected EventStoreApp(EventBusClient eventBus, EventStore eventStore) { + public EventStoreApp(EventBusClient eventBus, EventStore eventStore) { super(eventBus); this.eventStore = eventStore; @@ -33,6 +34,7 @@ private void extractThen(final EventBusClient.Message msg, final Consumer extractThen(msg, o -> { @@ -44,7 +46,7 @@ public void run() { eventStore.dispatch(ev); })); eventBus.subscribe("/store/crawler", msg -> extractThen(msg, o -> { - Event ev = new Event<>("ADD_URLS", (WrapperStringArray) o); + Event> ev = new Event<>("ADD_URLS", (List) o); eventStore.dispatch(ev); })); eventBus.subscribe("/store/indexer", msg -> extractThen(msg, o -> { diff --git a/src/main/java/com/epita/guereza/IndexerApp.java b/src/main/java/com/epita/guereza/application/IndexerApp.java similarity index 84% rename from src/main/java/com/epita/guereza/IndexerApp.java rename to src/main/java/com/epita/guereza/application/IndexerApp.java index 96f942a..2829a74 100644 --- a/src/main/java/com/epita/guereza/IndexerApp.java +++ b/src/main/java/com/epita/guereza/application/IndexerApp.java @@ -1,18 +1,13 @@ -package com.epita.guereza; +package com.epita.guereza.application; import com.epita.domain.Crawler; import com.epita.domain.Document; import com.epita.domain.Indexer; import com.epita.domain.RawDocument; -import com.epita.eventbus.EventBusClient; +import com.epita.eventbus.client.EventBusClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; - public class IndexerApp extends App { private static final Logger LOGGER = LoggerFactory.getLogger(IndexerApp.class); private final Indexer indexer; @@ -50,7 +45,7 @@ private void requestNextUrl() { public void run() { eventBus.subscribe(subscribeUrl, msg -> { if (msg != null) { - String url = (String)mappingObject(msg); + String url = (String) mappingObject(msg); if (url != null) { LOGGER.info("Receive url: {}", url); indexAndPublish(url); diff --git a/src/main/java/com/epita/guereza/ServerApp.java b/src/main/java/com/epita/guereza/application/ServerApp.java similarity index 62% rename from src/main/java/com/epita/guereza/ServerApp.java rename to src/main/java/com/epita/guereza/application/ServerApp.java index 3e97dab..b7359ca 100644 --- a/src/main/java/com/epita/guereza/ServerApp.java +++ b/src/main/java/com/epita/guereza/application/ServerApp.java @@ -1,12 +1,12 @@ -package com.epita.guereza; +package com.epita.guereza.application; -import com.epita.eventbus.NettyServer; +import com.epita.eventbus.server.NettyServer; public class ServerApp { private final NettyServer ns = new NettyServer(); private final int port; - protected ServerApp(int port) { + public ServerApp(int port) { this.port = port; } diff --git a/src/main/java/com/epita/guereza/RetroIndex.java b/src/main/java/com/epita/guereza/reducer/RetroIndex.java similarity index 85% rename from src/main/java/com/epita/guereza/RetroIndex.java rename to src/main/java/com/epita/guereza/reducer/RetroIndex.java index a45455d..74fffaf 100644 --- a/src/main/java/com/epita/guereza/RetroIndex.java +++ b/src/main/java/com/epita/guereza/reducer/RetroIndex.java @@ -1,9 +1,9 @@ -package com.epita.guereza; +package com.epita.guereza.reducer; import com.epita.domain.Document; import com.epita.domain.Index; -import com.epita.guereza.eventsourcing.Event; -import com.epita.guereza.eventsourcing.Reducer; +import com.epita.eventsourcing.Event; +import com.epita.eventsourcing.Reducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/epita/guereza/UrlStore.java b/src/main/java/com/epita/guereza/reducer/UrlStore.java similarity index 83% rename from src/main/java/com/epita/guereza/UrlStore.java rename to src/main/java/com/epita/guereza/reducer/UrlStore.java index ed8c22e..663d116 100644 --- a/src/main/java/com/epita/guereza/UrlStore.java +++ b/src/main/java/com/epita/guereza/reducer/UrlStore.java @@ -1,10 +1,10 @@ -package com.epita.guereza; +package com.epita.guereza.reducer; -import com.epita.eventbus.EventBusClient; +import com.epita.domain.SimpleCrawler; import com.epita.eventbus.EventMessage; -import com.epita.guereza.eventsourcing.Event; -import com.epita.guereza.eventsourcing.Reducer; -import com.epita.guereza.service.CrawlerService; +import com.epita.eventbus.client.EventBusClient; +import com.epita.eventsourcing.Event; +import com.epita.eventsourcing.Reducer; import com.fasterxml.jackson.core.JsonProcessingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,7 +12,7 @@ import java.util.*; public class UrlStore implements Reducer { - private static final Logger LOGGER = LoggerFactory.getLogger(CrawlerService.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleCrawler.class); private static final String STARTING_URL = "http://www.wikipedia.org"; private final EventBusClient eventBus; @@ -23,7 +23,7 @@ public class UrlStore implements Reducer { public UrlStore(final EventBusClient eventBus) { this.eventBus = eventBus; - final List initialList = new ArrayList(); + final List initialList = new ArrayList<>(); initialList.add(STARTING_URL); store(initialList); } @@ -33,7 +33,7 @@ public UrlStore(final EventBusClient eventBus) { public void reduce(final Event event) { switch (event.type) { case "ADD_URLS": - addUrls((Event) event); + addUrls((Event>) event); break; case "CRAWLER_REQUEST_URL": crawlerRequestUrl((Event) event); @@ -57,8 +57,8 @@ private void store(List urls) { } } - private void addUrls(Event event) { - store(event.obj.content); + private void addUrls(Event> event) { + store(event.obj); LOGGER.info("added URLs to the repo"); }