Skip to content

Commit 9a57001

Browse files
committed
joined and queries
1 parent daf72b1 commit 9a57001

19 files changed

+583
-156
lines changed

deploy.sh

Whitespace-only changes.

deploy/postgres.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: postgres
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: postgres
9+
template:
10+
metadata:
11+
labels:
12+
app: postgres
13+
spec:
14+
containers:
15+
- name: postgres
16+
image: postgres:12
17+
ports:
18+
- containerPort: 5432
19+
protocol: TCP
20+
env:
21+
- name: POSTGRES_DB
22+
value: kineteco
23+
- name: POSTGRES_USER
24+
valueFrom:
25+
secretKeyRef:
26+
name: kineteco-credentials
27+
key: username
28+
- name: POSTGRES_PASSWORD
29+
valueFrom:
30+
secretKeyRef:
31+
name: kineteco-credentials
32+
key: password
33+
---
34+
apiVersion: v1
35+
kind: Service
36+
metadata:
37+
labels:
38+
app: postgres
39+
name: postgres
40+
spec:
41+
ports:
42+
- name: http
43+
port: 5432
44+
protocol: TCP
45+
selector:
46+
app: postgres
47+
type: NodePort
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.infinispan.inmemory;
2+
3+
import io.quarkus.runtime.StartupEvent;
4+
import org.apache.commons.io.IOUtils;
5+
import org.infinispan.client.hotrod.RemoteCacheManager;
6+
import org.infinispan.commons.configuration.XMLStringConfiguration;
7+
import org.infinispan.inmemory.config.InmemoryCatalogueConfig;
8+
import org.jboss.logging.Logger;
9+
10+
import javax.enterprise.context.ApplicationScoped;
11+
import javax.enterprise.event.Observes;
12+
import javax.inject.Inject;
13+
import java.io.IOException;
14+
import java.net.URL;
15+
import java.nio.charset.StandardCharsets;
16+
17+
@ApplicationScoped
18+
public class ConfigureSQLStore {
19+
private static final Logger LOGGER = Logger.getLogger(ConfigureSQLStore.class);
20+
21+
@Inject
22+
RemoteCacheManager remoteCacheManager;
23+
24+
@Inject
25+
InmemoryCatalogueConfig inmemoryCatalogueConfig;
26+
27+
void onStart(@Observes StartupEvent ev) {
28+
LOGGER.info("Infinispan SQL Store is starting Powered by Quarkus");
29+
LOGGER.info(" _ _ _ _ _ _ _ _");
30+
LOGGER.info(" / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\");
31+
LOGGER.info("( S | q | l | S | t | o | r | e )");
32+
LOGGER.info(" \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/");
33+
try {
34+
LOGGER.info("Create caches");
35+
URL tableStoreCacheConfig = this.getClass().getClassLoader().getResource("tableStore.xml");
36+
URL queryStoreCacheConfig = this.getClass().getClassLoader().getResource("queryStore.xml");
37+
String configTable = replaceDBConnectionConfiguration(tableStoreCacheConfig);
38+
String configQuery = replaceDBConnectionConfiguration(queryStoreCacheConfig);
39+
remoteCacheManager.administration()
40+
.getOrCreateCache(inmemoryCatalogueConfig.catalogCacheName(), new XMLStringConfiguration(configTable));
41+
remoteCacheManager.administration()
42+
.getOrCreateCache(inmemoryCatalogueConfig.soldProductsName(), new XMLStringConfiguration(configQuery));
43+
} catch (Exception e) {
44+
LOGGER.error("Something went wrong creating caches");
45+
}
46+
}
47+
48+
private String replaceDBConnectionConfiguration(URL cacheConfig) throws IOException {
49+
String config = IOUtils.toString(cacheConfig, StandardCharsets.UTF_8)
50+
.replace("CONNECTION_URL", inmemoryCatalogueConfig.connectionUrl())
51+
.replace("USER_NAME", inmemoryCatalogueConfig.userName())
52+
.replace("PASSWORD", inmemoryCatalogueConfig.password());
53+
return config;
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,108 @@
11
package org.infinispan.inmemory;
22

3-
import io.quarkus.runtime.StartupEvent;
3+
import org.infinispan.client.hotrod.RemoteCache;
44
import org.infinispan.client.hotrod.RemoteCacheManager;
5-
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
6-
import org.jboss.logging.Logger;
5+
import org.infinispan.client.hotrod.Search;
6+
import org.infinispan.inmemory.config.InmemoryCatalogueConfig;
7+
import org.infinispan.inmemory.schema.PurchasedProductKey;
8+
import org.infinispan.inmemory.schema.PurchasedProductValue;
9+
import org.infinispan.inmemory.schema.RetailProductValue;
10+
import org.infinispan.query.dsl.Query;
11+
import org.infinispan.query.dsl.QueryFactory;
712

813
import javax.enterprise.context.ApplicationScoped;
9-
import javax.enterprise.event.Observes;
1014
import javax.inject.Inject;
1115
import javax.ws.rs.Consumes;
1216
import javax.ws.rs.GET;
1317
import javax.ws.rs.Path;
18+
import javax.ws.rs.PathParam;
1419
import javax.ws.rs.Produces;
20+
import javax.ws.rs.QueryParam;
1521
import javax.ws.rs.core.MediaType;
1622
import javax.ws.rs.core.Response;
23+
import javax.ws.rs.core.Response.Status;
24+
import java.util.List;
1725

1826
@ApplicationScoped
19-
@Path("/products")
2027
@Produces(MediaType.APPLICATION_JSON)
2128
@Consumes(MediaType.APPLICATION_JSON)
29+
@Path("/")
2230
public class InmemoryCatalogResource {
23-
private static final Logger LOGGER = Logger.getLogger(InmemoryCatalogResource.class);
24-
25-
void onStart(@Observes StartupEvent ev) {
26-
LOGGER.info("Infinispan SQL Store is starting Powered by Quarkus");
27-
LOGGER.info(" _ _ _ _ _ _ _ _");
28-
LOGGER.info(" / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\");
29-
LOGGER.info("( S | q | l | S | t | o | r | e )");
30-
LOGGER.info(" \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/");
31+
final RemoteCache<Long, RetailProductValue> catalogue;
32+
final RemoteCache<PurchasedProductKey, PurchasedProductValue> soldProducts;
33+
34+
@Inject public InmemoryCatalogResource(InmemoryCatalogueConfig inmemoryCatalogueConfig, RemoteCacheManager cacheManager) {
35+
this.catalogue = cacheManager.getCache(inmemoryCatalogueConfig.catalogCacheName());
36+
this.soldProducts = cacheManager.getCache(inmemoryCatalogueConfig.soldProductsName());
37+
}
38+
39+
@GET
40+
public String health() {
41+
return String.format("Service is up! catalogue[%d] sold_products[%d]", catalogue.size(), soldProducts.size());
42+
}
43+
44+
@GET
45+
@Path("catalogue/{code}")
46+
public Response getByCode(@PathParam("code") String code) {
47+
String queryByCode = "from retail.RetailProductValue where code= :code";
48+
QueryFactory queryFactory = Search.getQueryFactory(catalogue);
49+
Query<RetailProductValue> query = queryFactory.create(queryByCode);
50+
query.setParameter("code", code);
51+
query.maxResults(1);
52+
List<RetailProductValue> list = query.execute().list();
53+
if (list.isEmpty()) {
54+
return Response.status(Status.NOT_FOUND).build();
55+
}
56+
57+
return Response.ok(list.get(0)).build();
3158
}
3259

3360
@GET
34-
public Response catalog() {
35-
return Response.ok("in progress").build();
61+
@Path("catalogue")
62+
public Response queryCatalogue(@QueryParam("name") String name,
63+
@QueryParam("stock") Integer stock,
64+
@QueryParam("price-max") Integer priceMax,
65+
@QueryParam("price-min") Integer priceMin) {
66+
if (name == null) {
67+
name = "*";
68+
} else {
69+
name = "*" + name + "*";
70+
71+
}
72+
String fullTextQuery = String.format("from retail.RetailProductValue where name: '%s'", name);
73+
74+
if (stock != null) {
75+
fullTextQuery = fullTextQuery + String.format(" and stock >= %d", stock);
76+
}
77+
78+
if (priceMin != null && priceMax != null) {
79+
fullTextQuery = fullTextQuery + String.format(" and price : [%d to %d]", priceMin, priceMax);
80+
}
81+
82+
QueryFactory queryFactory = Search.getQueryFactory(catalogue);
83+
Query<RetailProductValue> query = queryFactory.create(fullTextQuery);
84+
query.maxResults(Integer.MAX_VALUE); // all. TODO: use pagination
85+
return Response.ok(query.execute().list()).build();
3686
}
3787

88+
@GET
89+
@Path("sales")
90+
public Response querySales(@QueryParam("name") String name, @QueryParam("country") String country) {
91+
if (name == null) {
92+
name = "*";
93+
} else {
94+
name = "*" + name + "*";
95+
}
96+
97+
String fullTextQuery = "from retail.PurchasedProductValue where name: '" + name + "'";
98+
99+
if (country != null) {
100+
fullTextQuery = fullTextQuery + "and country='" + country +"'";
101+
}
102+
QueryFactory queryFactory = Search.getQueryFactory(soldProducts);
103+
Query<RetailProductValue> query = queryFactory.create(fullTextQuery);
104+
query.maxResults(Integer.MAX_VALUE); // TODO: All. Use pagination
105+
return Response.ok(query.execute().list()).build();
106+
}
38107

39108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.infinispan.inmemory.config;
2+
3+
import io.smallrye.config.ConfigMapping;
4+
import io.smallrye.config.WithDefault;
5+
import io.smallrye.config.WithName;
6+
7+
@ConfigMapping(prefix = "inmemory")
8+
public interface InmemoryCatalogueConfig {
9+
10+
String connectionUrl();
11+
12+
@WithName("username")
13+
String userName();
14+
15+
String password();
16+
17+
@WithDefault("catalogue-table-store")
18+
String catalogCacheName();
19+
20+
@WithDefault("sold-products-query-store")
21+
String soldProductsName();
22+
}

inmemory-catalogue/src/main/java/org/infinispan/inmemory/schema/InmemoryCatalogueSchema.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import org.infinispan.protostream.GeneratedSchema;
44
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;
5-
import org.infinispan.protostream.types.java.math.BigDecimalAdapter;
65

7-
@AutoProtoSchemaBuilder(includeClasses= { RetailProductKey.class, RetailProductValue.class, BigDecimalAdapter.class },
6+
@AutoProtoSchemaBuilder(includeClasses= { RetailProductValue.class, PurchasedProductKey.class, PurchasedProductValue.class},
87
schemaPackageName = "retail")
98
public interface InmemoryCatalogueSchema extends GeneratedSchema {
109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.infinispan.inmemory.schema;
2+
3+
import org.infinispan.protostream.annotations.ProtoFactory;
4+
import org.infinispan.protostream.annotations.ProtoField;
5+
6+
public class PurchasedProductKey {
7+
private Long commandId;
8+
private Long productId;
9+
10+
@ProtoFactory
11+
public PurchasedProductKey(Long commandId, Long productId) {
12+
this.commandId = commandId;
13+
this.productId = productId;
14+
}
15+
16+
@ProtoField(number = 1, name = "id")
17+
public Long getCommandId() {
18+
return commandId;
19+
}
20+
21+
public void setCommandId(Long commandId) {
22+
this.commandId = commandId;
23+
}
24+
25+
@ProtoField(number = 2, name = "products_id")
26+
public Long getProductId() {
27+
return productId;
28+
}
29+
30+
public void setProductId(Long productId) {
31+
this.productId = productId;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.infinispan.inmemory.schema;
2+
3+
import org.infinispan.protostream.annotations.ProtoDoc;
4+
import org.infinispan.protostream.annotations.ProtoFactory;
5+
import org.infinispan.protostream.annotations.ProtoField;
6+
7+
@ProtoDoc("@Indexed")
8+
public class PurchasedProductValue {
9+
private String name;
10+
private String country;
11+
12+
@ProtoFactory
13+
public PurchasedProductValue(String name, String country) {
14+
this.name = name;
15+
this.country = country;
16+
}
17+
18+
19+
@ProtoField(1)
20+
@ProtoDoc("@Field(index=Index.YES, analyze = Analyze.YES, store = Store.YES, analyzer = @Analyzer(definition = \"keyword\"))")
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
@ProtoField(2)
30+
@ProtoDoc("@Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)")
31+
public String getCountry() {
32+
return country;
33+
}
34+
35+
public void setCountry(String country) {
36+
this.country = country;
37+
}
38+
39+
}

inmemory-catalogue/src/main/java/org/infinispan/inmemory/schema/RetailProductValue.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package org.infinispan.inmemory.schema;
22

3+
import org.infinispan.protostream.annotations.ProtoDoc;
34
import org.infinispan.protostream.annotations.ProtoFactory;
45
import org.infinispan.protostream.annotations.ProtoField;
56

6-
import java.math.BigDecimal;
77
import java.util.Objects;
88

9+
@ProtoDoc("@Indexed")
910
public class RetailProductValue {
1011
private String code;
1112
private String name;
12-
private BigDecimal price;
13+
private Double price;
1314
private Integer stock;
1415

1516
@ProtoFactory
16-
public RetailProductValue(String code, String name, BigDecimal price, Integer stock) {
17+
public RetailProductValue(String code, String name, Double price, Integer stock) {
1718
this.code = code;
1819
this.name = name;
1920
this.price = price;
@@ -30,6 +31,7 @@ public void setCode(String code) {
3031
}
3132

3233
@ProtoField(2)
34+
@ProtoDoc("@Field(index=Index.YES, analyze = Analyze.YES, store = Store.YES)")
3335
public String getName() {
3436
return name;
3537
}
@@ -39,15 +41,17 @@ public void setName(String name) {
3941
}
4042

4143
@ProtoField(3)
42-
public BigDecimal getPrice() {
44+
@ProtoDoc("@Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)")
45+
public Double getPrice() {
4346
return price;
4447
}
4548

46-
public void setPrice(BigDecimal price) {
49+
public void setPrice(Double price) {
4750
this.price = price;
4851
}
4952

5053
@ProtoField(4)
54+
@ProtoDoc("@Field(index=Index.YES, analyze = Analyze.NO, store = Store.YES)")
5155
public Integer getStock() {
5256
return stock;
5357
}

0 commit comments

Comments
 (0)