Skip to content
This repository has been archived by the owner on Jun 2, 2019. It is now read-only.

Commit

Permalink
Refactor in more modules and reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
dethi committed Dec 9, 2017
1 parent 787261c commit 4b59150
Show file tree
Hide file tree
Showing 29 changed files with 102 additions and 118 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
4 changes: 3 additions & 1 deletion domain/src/main/java/com/epita/domain/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.util.HashMap;

@SuppressWarnings("unused")
public class Document {
public String url;
public HashMap<String, Term> terms;

public Document() {}
public Document() {
}

public Document(final String url, final HashMap<String, Term> terms) {
this.url = url;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
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;
import org.slf4j.LoggerFactory;

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 ]";
Expand Down Expand Up @@ -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<Document, Double> search(final List<Document> docs, final String query) {
final HashMap<Document, Double> hits = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.guereza.service.indexer;
package com.epita.domain;

import java.util.Arrays;
import java.util.HashSet;
Expand Down
4 changes: 3 additions & 1 deletion domain/src/main/java/com/epita/domain/Term.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import java.util.List;

@SuppressWarnings("unused")
public class Term {
public String token;
public List<Integer> position;
public double frequency;

public Term() {}
public Term() {
}

public Term(final String token, final List<Integer> position, final double frequency) {
this.token = token;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.epita.eventbus;

import com.epita.eventbus.client.EventBusClient;

import java.time.LocalDateTime;
import java.util.function.Consumer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.eventbus;
package com.epita.eventbus.client;

import java.time.LocalDateTime;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,7 +22,7 @@ public NettyClientInitializer(Consumer<EventMessage> 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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,10 +18,10 @@
public class NettyEventBusClient implements EventBusClient {
private static final Logger LOGGER = LoggerFactory.getLogger(NettyEventBusClient.class);
private final Map<String, List<Subscription>> 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<>();
Expand Down Expand Up @@ -52,13 +53,6 @@ public boolean start() {
return true;
}

/**
* Close the EventBus
*/
public void shutdown() {
group.shutdownGracefully();
}

/**
* Subscribe to a new netty channel
*
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.eventbus;
package com.epita.eventbus.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.eventbus;
package com.epita.eventbus.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.eventbus;
package com.epita.eventbus.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
Expand All @@ -14,7 +14,7 @@ public class ServerInitializer extends ChannelInitializer<SocketChannel> {
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());

Expand Down
13 changes: 13 additions & 0 deletions eventsourcing/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
group 'com.epita'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.guereza.eventsourcing;
package com.epita.eventsourcing;

@SuppressWarnings("WeakerAccess")
public class Event<BEAN_TYPE> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.guereza.eventsourcing;
package com.epita.eventsourcing;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.epita.guereza.eventsourcing;
package com.epita.eventsourcing;

public interface Reducer {
void reduce(Event<?> event);
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ rootProject.name = 'guereza'

include 'domain', ':main'
include 'winter', ':main'
include 'eventbus', ':main'
include 'eventbus', ':main'
include 'eventsourcing', ':main'

31 changes: 10 additions & 21 deletions src/main/java/com/epita/guereza/Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)));
}

Expand Down Expand Up @@ -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<Document, Double> res = indexer.search(index.docs, query);
for (final Map.Entry<Document, Double> doc : res.entrySet()) {
System.out.printf("%100s %f\n", doc.getKey().url, doc.getValue());
}
}
}
14 changes: 0 additions & 14 deletions src/main/java/com/epita/guereza/WrapperStringArray.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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);
Expand Down
Loading

0 comments on commit 4b59150

Please sign in to comment.