Skip to content

Commit

Permalink
Remove generics from Backend interface
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed Jun 8, 2019
1 parent 8e42de3 commit cdf3e1a
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 41 deletions.
Expand Up @@ -20,25 +20,25 @@


/** /**
* Abstraction of a asynchronous service used by adapters and modeled as a single abstract method (SAM). * Abstraction of a asynchronous service used by adapters and modeled as a single abstract method (SAM).
*
* @param <T> type of the result (computation)
*/ */
public interface Backend<T> { public interface Backend {


/** /**
* Apply an operation on the back-end. {@code operation} in this context can mean query / update * Apply an operation on the back-end. {@code operation} in this context can mean query / update
* / insert / index etc. * / insert / index etc.
* *
* <p>Produced publisher can represent empty, single, multiple or unbounded event flow.
*
* @param operation operation to be performed on the back-end. * @param operation operation to be performed on the back-end.
* @return empty or unbounded flow of events (can also be a single response) * @return empty, single, multiple or unbounded event flow
*/ */
Publisher<T> execute(Operation operation); <T> Publisher<T> execute(Operation<T> operation);


/** /**
* Generic operation to be executed on the back-end. Typical operations include * Generic operation to be executed on the back-end. Typical operations include
* query, update, delete etc. * query, update, delete etc.
*/ */
interface Operation { interface Operation<T> {


} }


Expand Down
Expand Up @@ -28,13 +28,13 @@
public class InternalReader<T> implements Repository.Reader<T> { public class InternalReader<T> implements Repository.Reader<T> {


private final ImmutableQuery query; private final ImmutableQuery query;
private final Backend<T> backend; private final Backend backend;


public InternalReader(DocumentCriteria<T> criteria, Backend<T> backend) { public InternalReader(DocumentCriteria<T> criteria, Backend backend) {
this(ImmutableQuery.of(criteria), backend); this(ImmutableQuery.of(criteria), backend);
} }


private InternalReader(ImmutableQuery query, Backend<T> backend) { private InternalReader(ImmutableQuery query, Backend backend) {
this.query = Objects.requireNonNull(query, "query"); this.query = Objects.requireNonNull(query, "query");
this.backend = Objects.requireNonNull(backend, "backend"); this.backend = Objects.requireNonNull(backend, "backend");
} }
Expand Down
Expand Up @@ -25,10 +25,10 @@
* Query sent to a backend * Query sent to a backend
*/ */
@Value.Immutable @Value.Immutable
public interface Query extends Backend.Operation { public interface Query<T> extends Backend.Operation<T> {


@Value.Parameter @Value.Parameter
DocumentCriteria<?> criteria(); DocumentCriteria<T> criteria();


OptionalLong limit(); OptionalLong limit();


Expand Down
Expand Up @@ -43,43 +43,42 @@


/** /**
* Queries <a href="https://www.elastic.co/">ElasticSearch</a> data-store. * Queries <a href="https://www.elastic.co/">ElasticSearch</a> data-store.
* @param <T>
*/ */
public class ElasticBackend<T> implements Backend<T> { public class ElasticBackend implements Backend {


private final RestClient restClient; private final RestClient restClient;
private final ObjectMapper mapper; private final ObjectMapper mapper;
private final Class<T> type; private final Class<?> type;
private final String index; private final String index;


public ElasticBackend(RestClient restClient, public ElasticBackend(RestClient restClient,
Class<T> type,
ObjectMapper mapper, ObjectMapper mapper,
Class<?> type,
String index) { String index) {
this.restClient = Objects.requireNonNull(restClient, "restClient"); this.restClient = Objects.requireNonNull(restClient, "restClient");
this.type = Objects.requireNonNull(type, "type");
this.mapper = Objects.requireNonNull(mapper, "mapper"); this.mapper = Objects.requireNonNull(mapper, "mapper");
this.type = type;
this.index = Objects.requireNonNull(index, "index"); this.index = Objects.requireNonNull(index, "index");
} }


@Override @Override
public Publisher<T> execute(Operation query) { public <T> Publisher<T> execute(Operation<T> query) {
Objects.requireNonNull(query, "query"); Objects.requireNonNull(query, "query");


return queryInternal((Query) query); return queryInternal((Query<T>) query);
} }


private Publisher<T> queryInternal(Query query) { private <T> Publisher<T> queryInternal(Query<T> query) {
final Request request = new Request("POST", String.format("/%s/_search", index)); final Request request = new Request("POST", String.format("/%s/_search", index));
final Expression expression = Criterias.toExpression(query.criteria()); final Expression expression = Criterias.toExpression(query.criteria());
final ObjectNode json = Elasticsearch.converter(mapper).convert(expression); final ObjectNode json = Elasticsearch.converter(mapper).convert(expression);
query.limit().ifPresent(limit -> json.put("size", limit)); query.limit().ifPresent(limit -> json.put("size", limit));
query.offset().ifPresent(offset -> json.put("start", offset)); query.offset().ifPresent(offset -> json.put("start", offset));
request.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON)); request.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
return Reactive.flatMapIterable(Reactive.map(new AsyncRestPublisher(restClient, request), converter()), x -> x); return Reactive.flatMapIterable(Reactive.map(new AsyncRestPublisher(restClient, request), converter((Class<T>) type)), x -> x);
} }


private Function<Response, List<T>> converter() { private <T> Function<Response, List<T>> converter(Class<T> type) {
return response -> { return response -> {
try (InputStream is = response.getEntity().getContent()) { try (InputStream is = response.getEntity().getContent()) {
final ObjectNode root = mapper.readValue(is, ObjectNode.class); final ObjectNode root = mapper.readValue(is, ObjectNode.class);
Expand Down
Expand Up @@ -45,7 +45,7 @@ public class ElasticsearchIntegrationTest {


private static final String INDEX_NAME = "mymodel"; private static final String INDEX_NAME = "mymodel";


private ElasticBackend<ElasticModel> backend; private ElasticBackend backend;
private ElasticModelRepository repository; private ElasticModelRepository repository;


@BeforeClass @BeforeClass
Expand Down Expand Up @@ -78,7 +78,7 @@ public static void setupElastic() throws Exception {


@Before @Before
public void setupRepository() throws Exception { public void setupRepository() throws Exception {
this.backend = new ElasticBackend<>(RESOURCE.restClient(), ElasticModel.class, MAPPER, INDEX_NAME); this.backend = new ElasticBackend(RESOURCE.restClient(), MAPPER, ElasticModel.class, INDEX_NAME);
this.repository = new ElasticModelRepository(backend); this.repository = new ElasticModelRepository(backend);
} }


Expand Down
Expand Up @@ -23,7 +23,7 @@


import java.util.Objects; import java.util.Objects;


public class GeodeBackend<T> implements Backend<T> { public class GeodeBackend<T> implements Backend {


private final Region<?, ?> region; private final Region<?, ?> region;


Expand All @@ -32,7 +32,7 @@ public GeodeBackend(Region<?, ?> region) {
} }


@Override @Override
public Publisher<T> execute(Operation query) { public <T> Publisher<T> execute(Operation<T> query) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


Expand Down
16 changes: 6 additions & 10 deletions criteria/mongo/src/org/immutables/criteria/mongo/MongoBackend.java
Expand Up @@ -17,7 +17,6 @@
package org.immutables.criteria.mongo; package org.immutables.criteria.mongo;


import com.mongodb.reactivestreams.client.MongoCollection; import com.mongodb.reactivestreams.client.MongoCollection;
import com.mongodb.reactivestreams.client.Success;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import org.immutables.criteria.Criterias; import org.immutables.criteria.Criterias;
import org.immutables.criteria.internal.Backend; import org.immutables.criteria.internal.Backend;
Expand All @@ -31,21 +30,18 @@
* *
* <p>Based on <a href="https://mongodb.github.io/mongo-java-driver-reactivestreams/">Mongo reactive streams driver</a> * <p>Based on <a href="https://mongodb.github.io/mongo-java-driver-reactivestreams/">Mongo reactive streams driver</a>
*/ */
class MongoBackend<T> implements Backend<T> { class MongoBackend implements Backend {


private final MongoCollection<T> collection; private final MongoCollection<?> collection;


MongoBackend(MongoCollection<T> collection) { MongoBackend(MongoCollection<?> collection) {
this.collection = Objects.requireNonNull(collection, "collection"); this.collection = Objects.requireNonNull(collection, "collection");
} }


// which one is _id ?
public Publisher<Success> insert(T entity) {
return collection.insertOne(entity);
}

@Override @Override
public Publisher<T> execute(Operation operation) { public <T> Publisher<T> execute(Operation<T> operation) {
@SuppressWarnings("unchecked")
final MongoCollection<T> collection = (MongoCollection<T>) this.collection;
final Bson filter = Mongos.converter(collection.getCodecRegistry()).convert(Criterias.toExpression(((Query) operation).criteria())); final Bson filter = Mongos.converter(collection.getCodecRegistry()).convert(Criterias.toExpression(((Query) operation).criteria()));
return collection.find(filter); return collection.find(filter);
} }
Expand Down
Expand Up @@ -49,7 +49,7 @@ public class MongoRepositoryTest {


private MongoCollection<Person> collection; private MongoCollection<Person> collection;


private MongoBackend<Person> backend; private MongoBackend backend;
private PersonRepository repository; private PersonRepository repository;


@Before @Before
Expand All @@ -69,11 +69,11 @@ public void setUp() throws Exception {
.withDocumentClass(Person.class) .withDocumentClass(Person.class)
.withCodecRegistry(JacksonCodecs.registryFromMapper(mapper)); .withCodecRegistry(JacksonCodecs.registryFromMapper(mapper));


this.backend = new MongoBackend<>(this.collection); this.backend = new MongoBackend(this.collection);
this.repository = new PersonRepository(backend); this.repository = new PersonRepository(backend);
PersonGenerator generator = new PersonGenerator(); final PersonGenerator generator = new PersonGenerator();


Flowable.fromPublisher(backend.insert(generator.next().withFullName("test"))) Flowable.fromPublisher(collection.insertOne(generator.next().withFullName("test")))
.test() .test()
.awaitDone(1, TimeUnit.SECONDS) .awaitDone(1, TimeUnit.SECONDS)
.assertComplete(); .assertComplete();
Expand Down
Expand Up @@ -57,9 +57,9 @@ import java.util.Objects;
[/if] [/if]
[type.typeDocument.access] class [type.name]Repository implements Repository.Readable<[type.name]> { [type.typeDocument.access] class [type.name]Repository implements Repository.Readable<[type.name]> {


private final Backend<[type.name]> backend; private final Backend backend;


public [type.name]Repository(Backend<[type.name]> backend) { public [type.name]Repository(Backend backend) {
this.backend = Objects.requireNonNull(backend, "backend"); this.backend = Objects.requireNonNull(backend, "backend");
} }


Expand Down

0 comments on commit cdf3e1a

Please sign in to comment.