Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
removed useless AmazonEsSearch and gave a better name to the Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Sep 6, 2017
1 parent 464cc55 commit d20071e
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 400 deletions.
Expand Up @@ -46,7 +46,7 @@
import com.amihaiemil.charles.aws.requests.AwsPost;
import com.amihaiemil.charles.aws.requests.EsHttpRequest;
import com.amihaiemil.charles.aws.requests.SignedRequest;
import com.amihaiemil.charles.github.AwsEsRepository;
import com.amihaiemil.charles.rest.model.SearchResultsPage;

/**
* AWS ElasticSearch repository that sends the webpages to the
Expand All @@ -59,8 +59,8 @@
* @todo #250:30min Rename this class to Index (since that's what it is), and remove AmazonEsSearch class,
* moving the search method here
*/
public final class AmazonEsRepository implements AwsEsRepository {
private static final Logger LOG = LoggerFactory.getLogger(AmazonEsRepository.class);
public final class AmazonElasticSearch implements ElasticSearch {
private static final Logger LOG = LoggerFactory.getLogger(AmazonElasticSearch.class);

/**
* Name of the Es index where the pages will be exported.
Expand All @@ -87,6 +87,20 @@ public final class AmazonEsRepository implements AwsEsRepository {
*/
private EsEndPoint esEdp;

/**
* Ctor.
* @param indexName Name of the index.
*/
public AmazonElasticSearch(final String indexName) {
this(
indexName,
new StAccessKeyId(),
new StSecretKey(),
new StRegion(),
new StEsEndPoint()
);
}

/**
* ctor.
* @param indexName Name of the Es index where the pages will be exported.
Expand All @@ -95,7 +109,7 @@ public final class AmazonEsRepository implements AwsEsRepository {
* @param reg AWS ElasticSearch region.
* @param es ElasticSearch URL.
*/
public AmazonEsRepository(
public AmazonElasticSearch(
final String indexName,
final AccessKeyId accesskey,
final SecretKey secretKey,
Expand All @@ -109,6 +123,30 @@ public AmazonEsRepository(
this.esEdp = es;
}

@Override
public SearchResultsPage search(final SearchQuery query) {
final Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
AwsHttpRequest<SearchResultsPage> search =
new SignedRequest<>(
new AwsHttpHeaders<>(
new AwsPost<>(
new EsHttpRequest<>(
this.esEdp,
this.indexName + "/_search",
new SearchResponseHandler(),
new SimpleAwsErrorHandler(false)
),
new ByteArrayInputStream(query.toJson().toString().getBytes())
), headers
),
this.accesskey,
this.secretKey,
this.reg
);
return search.perform();
}

@Override
public void export(List<WebPage> pages) throws DataExportException {
try {
Expand Down
126 changes: 0 additions & 126 deletions src/main/java/com/amihaiemil/charles/aws/AmazonEsSearch.java

This file was deleted.

Expand Up @@ -23,26 +23,34 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.charles.github;
package com.amihaiemil.charles.aws;

import java.util.List;

import com.amihaiemil.charles.DataExportException;
import com.amihaiemil.charles.Repository;
import com.amihaiemil.charles.WebPage;
import com.amihaiemil.charles.rest.model.SearchResultsPage;

/**
* Our repository is an ElasticSearch Index in the AWS cloud. Extend the Repository interface
* to add our methods too.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
*/
public interface AwsEsRepository extends Repository {

/**
* Does the index exist?
* @return True or false.
*/
public interface ElasticSearch extends Repository {

/**
* Perform a search.
* @param query Search query.
* @return A page of search results.
*/
SearchResultsPage search(final SearchQuery query);

/**
* Does the index exist?
* @return True or false.
*/
boolean exists();

/**
Expand All @@ -62,7 +70,7 @@ public interface AwsEsRepository extends Repository {
* Fake for unit tests.
*
*/
final static class Fake implements AwsEsRepository {
final static class Fake implements ElasticSearch {

/**
* Does this index exist?
Expand Down Expand Up @@ -90,10 +98,19 @@ public boolean exists() {
public void delete() {
//Fake delete; nothing to do.
}
@Override
public void delete(String type, String id) {
//Fake delete; nothing to do.
}
@Override
public void delete(String type, String id) {
//Fake delete; nothing to do.
}

/**
* @todo 261:30min After SearchResultsPage is refactored and broken
* into an interface + smart implmentation, provide a Fake one and return it here.
*/
@Override
public SearchResultsPage search(SearchQuery query) {
return null;
}

}
}
14 changes: 2 additions & 12 deletions src/main/java/com/amihaiemil/charles/github/DeleteIndex.java
Expand Up @@ -29,11 +29,7 @@

import org.slf4j.Logger;

import com.amihaiemil.charles.aws.AmazonEsRepository;
import com.amihaiemil.charles.aws.StAccessKeyId;
import com.amihaiemil.charles.aws.StEsEndPoint;
import com.amihaiemil.charles.aws.StRegion;
import com.amihaiemil.charles.aws.StSecretKey;
import com.amihaiemil.charles.aws.AmazonElasticSearch;

/**
* Step that deletes the index from AWS es.
Expand All @@ -57,13 +53,7 @@ public DeleteIndex(Step next) {
public void perform(Command command, Logger logger) throws IOException {
logger.info("Starting index deletion...");
try {
new AmazonEsRepository(
command.indexName(),
new StAccessKeyId(),
new StSecretKey(),
new StRegion(),
new StEsEndPoint()
).delete();
new AmazonElasticSearch(command.indexName()).delete();
} catch (IOException e) {
logger.error("Exception while deleting the index!", e);
throw new IOException("Exception while deleting the index!" , e);
Expand Down
27 changes: 7 additions & 20 deletions src/main/java/com/amihaiemil/charles/github/IndexExistsCheck.java
Expand Up @@ -25,15 +25,12 @@
*/
package com.amihaiemil.charles.github;

import org.slf4j.Logger;
import java.io.IOException;

import com.amihaiemil.charles.aws.AmazonEsRepository;
import com.amihaiemil.charles.aws.StAccessKeyId;
import com.amihaiemil.charles.aws.StEsEndPoint;
import com.amihaiemil.charles.aws.StRegion;
import com.amihaiemil.charles.aws.StSecretKey;
import org.slf4j.Logger;

import java.io.IOException;
import com.amihaiemil.charles.aws.AmazonElasticSearch;
import com.amihaiemil.charles.aws.ElasticSearch;

/**
* Step that checks if an index exists in elasticsearch
Expand All @@ -47,25 +44,15 @@ public final class IndexExistsCheck extends PreconditionCheckStep {
/**
* AWS elasticsearch repository.
*/
private AwsEsRepository repo;
private ElasticSearch repo;

/**
* Constructor.
* @param onTrue The step to perform on successful check.
* @param onFalse the step to perform in unsuccessful check.
*/
public IndexExistsCheck(String index, Step onTrue, Step onFalse) {

this(
new AmazonEsRepository(
index,
new StAccessKeyId(),
new StSecretKey(),
new StRegion(),
new StEsEndPoint()
),
onTrue, onFalse
);
this(new AmazonElasticSearch(index), onTrue, onFalse);
}

/**
Expand All @@ -74,7 +61,7 @@ public IndexExistsCheck(String index, Step onTrue, Step onFalse) {
* @param onTrue The step to perform on successful check.
* @param onFalse The step to perform in unsuccessful check.
*/
public IndexExistsCheck(AwsEsRepository repo, Step onTrue, Step onFalse) {
public IndexExistsCheck(ElasticSearch repo, Step onTrue, Step onFalse) {
super(onTrue, onFalse);
this.repo = repo;
}
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/amihaiemil/charles/github/IndexPage.java
Expand Up @@ -35,11 +35,7 @@
import com.amihaiemil.charles.LiveWebPage;
import com.amihaiemil.charles.SnapshotWebPage;
import com.amihaiemil.charles.WebPage;
import com.amihaiemil.charles.aws.AmazonEsRepository;
import com.amihaiemil.charles.aws.StAccessKeyId;
import com.amihaiemil.charles.aws.StEsEndPoint;
import com.amihaiemil.charles.aws.StRegion;
import com.amihaiemil.charles.aws.StSecretKey;
import com.amihaiemil.charles.aws.AmazonElasticSearch;

/**
* Step to index a single page.
Expand Down Expand Up @@ -68,12 +64,8 @@ public void perform(Command command, Logger logger) throws IOException {
driver.get(link);
WebPage snapshot = new SnapshotWebPage(new LiveWebPage(driver));
logger.info("Page crawled. Sending to aws...");
new AmazonEsRepository(
command.indexName(),
new StAccessKeyId(),
new StSecretKey(),
new StRegion(),
new StEsEndPoint()
new AmazonElasticSearch(
command.indexName()
).export(Arrays.asList(snapshot));
logger.info("Page successfully sent to aws!");
} catch (
Expand Down

0 comments on commit d20071e

Please sign in to comment.