Add semrack index postgresql
All checks were successful
Maven build / build (push) Successful in 3m17s

This commit is contained in:
Guillaume Dugas
2025-11-19 23:31:18 +01:00
parent de339f9554
commit 03b1e0851b
62 changed files with 1301 additions and 318 deletions

11
docker-compose.yml Normal file
View File

@@ -0,0 +1,11 @@
services:
postgres:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
ports:
- "18432:5432"
volumes:
- pgdata:/var/lib/postgres
volumes:
pgdata:

View File

@@ -19,12 +19,15 @@
<groupId>fr.codeanddata.semrack</groupId> <groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-storage-postgres</artifactId> <artifactId>semrack-storage-postgres</artifactId>
</dependency> </dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres</artifactId>
</dependency>
<dependency> <dependency>
<groupId>fr.codeanddata.semrack</groupId> <groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-api-rest</artifactId> <artifactId>semrack-api-rest</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId> <artifactId>quarkus-junit5</artifactId>

View File

@@ -1,6 +1,6 @@
%dev.quarkus.log.min-level=TRACE %dev.quarkus.log.min-level=TRACE
quarkus.hibernate-orm.mapping.format.global=ignore #quarkus.hibernate-orm.mapping.format.global=ignore
# Datasource # Datasource
quarkus.datasource.devservices.port=35432 quarkus.datasource.devservices.port=35432
@@ -9,6 +9,7 @@ quarkus.hibernate-orm.scripts.generation=none
%prod.quarkus.datasource.reactive.url=postgresql://${semrack.db.host}:${semrack.db.port}/${semrack.db.name} %prod.quarkus.datasource.reactive.url=postgresql://${semrack.db.host}:${semrack.db.port}/${semrack.db.name}
%prod.quarkus.datasource.username=${semrack.db.user} %prod.quarkus.datasource.username=${semrack.db.user}
%prod.quarkus.datasource.password=${semrack.db.password} %prod.quarkus.datasource.password=${semrack.db.password}
quarkus.hibernate-orm.log.sql=true
## Flyway ## Flyway
quarkus.flyway.active=false quarkus.flyway.active=false

View File

@@ -46,6 +46,11 @@
<artifactId>quarkus-junit5-internal</artifactId> <artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -1,10 +1,9 @@
package fr.codeanddata.semrack.api.rest; package fr.codeanddata.semrack.api.rest;
import fr.codeanddata.semrack.core.SemdocStorage;
import fr.codeanddata.semrack.core.models.SemrackDocument;
import fr.codeanddata.semrack.core.models.PushDocumentRequest; import fr.codeanddata.semrack.core.models.PushDocumentRequest;
import fr.codeanddata.semrack.core.models.SearchRequest; import fr.codeanddata.semrack.core.models.SearchRequest;
import fr.codeanddata.semrack.core.models.SearchResult; import fr.codeanddata.semrack.core.models.SearchResult;
import fr.codeanddata.semrack.core.models.SemrackDocument;
import fr.codeanddata.semrack.core.repositories.SemdocRepository; import fr.codeanddata.semrack.core.repositories.SemdocRepository;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject; import jakarta.inject.Inject;
@@ -19,9 +18,6 @@ import org.jboss.resteasy.reactive.RestPath;
//@Authenticated //@Authenticated
public class SemrackApi { public class SemrackApi {
@Inject
SemdocStorage storage;
@Inject @Inject
SemdocRepository repository; SemdocRepository repository;
@@ -35,14 +31,14 @@ public class SemrackApi {
@Path("/search") @Path("/search")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Uni<SearchResult> searchDocument(SearchRequest query) { public Uni<SearchResult> searchDocument(SearchRequest query) {
return storage.searchDocument(query); return repository.searchDocument(query);
} }
@GET @GET
@Path("{uid}") @Path("{uid}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Uni<SemrackDocument> getDocument(@RestPath String uid) { public Uni<SemrackDocument> getDocument(@RestPath String uid) {
return storage.readDocument(uid); return repository.get(uid);
} }
@POST @POST

View File

@@ -1,59 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>fr.codeanddata.semrack</groupId> <groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-parent</artifactId> <artifactId>semrack-core-parent</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>semrack-core-deployment</artifactId> <artifactId>semrack-core-deployment</artifactId>
<name>Semrack Core - Deployment</name> <name>Semrack Core - Deployment</name>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId> <artifactId>quarkus-arc-deployment</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-security-deployment</artifactId> <artifactId>quarkus-vertx-deployment</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId> <artifactId>quarkus-security-deployment</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>fr.codeanddata.semrack</groupId> <groupId>io.quarkus</groupId>
<artifactId>semrack-core</artifactId> <artifactId>quarkus-jackson-deployment</artifactId>
<version>${project.version}</version> </dependency>
</dependency> <dependency>
<dependency> <groupId>fr.codeanddata.semrack</groupId>
<groupId>io.quarkus</groupId> <artifactId>semrack-core</artifactId>
<artifactId>quarkus-junit5-internal</artifactId> </dependency>
<scope>test</scope> <dependency>
</dependency> <groupId>io.quarkus</groupId>
</dependencies> <artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-testing</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<executions> <executions>
<execution> <execution>
<id>default-compile</id> <id>default-compile</id>
<configuration> <configuration>
<annotationProcessorPaths> <annotationProcessorPaths>
<path> <path>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId> <artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version> <version>${quarkus.version}</version>
</path> </path>
</annotationProcessorPaths> </annotationProcessorPaths>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@@ -15,6 +15,7 @@
<modules> <modules>
<module>deployment</module> <module>deployment</module>
<module>runtime</module> <module>runtime</module>
<module>testing</module>
</modules> </modules>
</project> </project>

View File

@@ -16,6 +16,10 @@
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId> <artifactId>quarkus-arc</artifactId>
</dependency> </dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency> <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-security</artifactId> <artifactId>quarkus-security</artifactId>

View File

@@ -0,0 +1,13 @@
package fr.codeanddata.semrack.core;
import fr.codeanddata.semrack.core.models.IndexSearchResult;
import fr.codeanddata.semrack.core.models.SearchRequest;
import io.smallrye.mutiny.Uni;
public interface SemdocIndex {
Uni<Long> count(SearchRequest request);
Uni<Boolean> exist(SearchRequest query);
Uni<Void> index(String documentId);
Uni<IndexSearchResult> search(SearchRequest searchRequest);
Uni<Void> clear(String documentId);
}

View File

@@ -1,14 +1,12 @@
package fr.codeanddata.semrack.core; package fr.codeanddata.semrack.core;
import fr.codeanddata.semrack.core.models.SearchRequest;
import fr.codeanddata.semrack.core.models.SearchResult;
import fr.codeanddata.semrack.core.models.SemrackDocument; import fr.codeanddata.semrack.core.models.SemrackDocument;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import java.util.List;
public interface SemdocStorage { public interface SemdocStorage {
Uni<SemrackDocument> readDocument(String uid); Uni<SemrackDocument> get(String uid);
Uni<SearchResult> searchDocument(SearchRequest query); Uni<List<SemrackDocument>> get(List<String> uids);
Uni<Long> countDocuments(SearchRequest request);
Uni<Boolean> documentsExist(SearchRequest query);
Uni<SemrackDocument> storeDocument(SemrackDocument document); Uni<SemrackDocument> storeDocument(SemrackDocument document);
} }

View File

@@ -24,7 +24,7 @@ public class AnnotateWriteInterceptor implements SemdocWriteInterceptor {
final Map<String, Object> directives = context.getDirectives(); final Map<String, Object> directives = context.getDirectives();
if (directives.containsKey(ANNOTATE_KEY)) { if (directives.containsKey(ANNOTATE_KEY)) {
final Map<String, Object> annotate = objectMapper.convertValue(directives.get(ANNOTATE_KEY), new TypeReference<Map<String, Object>>() {}); final Map<String, Object> annotate = objectMapper.convertValue(directives.get(ANNOTATE_KEY), new TypeReference<>() {});
annotate.keySet().forEach(key -> context.getNextDocument().getAnnotations().put(CUSTOM_PREFIX + key, annotate.get(key))); annotate.keySet().forEach(key -> context.getNextDocument().getAnnotations().put(CUSTOM_PREFIX + key, annotate.get(key)));
} }

View File

@@ -0,0 +1,15 @@
package fr.codeanddata.semrack.core.models;
import lombok.*;
import java.util.List;
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class IndexSearchResult {
List<String> uids;
PaginationInfo pagination;
}

View File

@@ -0,0 +1,15 @@
package fr.codeanddata.semrack.core.models;
import fr.codeanddata.semrack.core.utils.Traverser;
import lombok.*;
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TraverserPath {
Traverser.PathTypes type;
String fullPath;
Object value;
}

View File

@@ -1,9 +1,9 @@
package fr.codeanddata.semrack.core.repositories; package fr.codeanddata.semrack.core.repositories;
import fr.codeanddata.semrack.core.SemdocStorage;
import fr.codeanddata.semrack.core.SemdocWriteInterceptor; import fr.codeanddata.semrack.core.SemdocWriteInterceptor;
import fr.codeanddata.semrack.core.exceptions.SemrackRuntimeException; import fr.codeanddata.semrack.core.exceptions.SemrackRuntimeException;
import fr.codeanddata.semrack.core.models.*; import fr.codeanddata.semrack.core.models.*;
import fr.codeanddata.semrack.core.storages.SemdocStorageProxy;
import fr.codeanddata.semrack.core.utils.UIDGenerator; import fr.codeanddata.semrack.core.utils.UIDGenerator;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
@@ -19,7 +19,7 @@ import java.util.concurrent.atomic.AtomicReference;
public class SemdocRepository { public class SemdocRepository {
@Inject @Inject
SemdocStorage semdocStorage; SemdocStorageProxy semdocStorage;
@Inject @Inject
@Any @Any
@@ -28,10 +28,14 @@ public class SemdocRepository {
@Inject @Inject
UIDGenerator uidGenerator; UIDGenerator uidGenerator;
Uni<SearchResult> searchDocument(SearchRequest query) { public Uni<SearchResult> searchDocument(SearchRequest query) {
return semdocStorage.searchDocument(query); return semdocStorage.searchDocument(query);
} }
public Uni<SemrackDocument> get(String documentId) {
return semdocStorage.readDocument(documentId);
}
public Uni<SemrackDocument> pushDocument(PushDocumentRequest pushDocument) { public Uni<SemrackDocument> pushDocument(PushDocumentRequest pushDocument) {
return Optional.ofNullable(pushDocument.getUid()).map(semdocStorage::readDocument).orElse(Uni.createFrom().nullItem()) return Optional.ofNullable(pushDocument.getUid()).map(semdocStorage::readDocument).orElse(Uni.createFrom().nullItem())
.chain(currentDocument -> { .chain(currentDocument -> {

View File

@@ -0,0 +1,35 @@
package fr.codeanddata.semrack.core.services;
import fr.codeanddata.semrack.core.SemdocIndex;
import fr.codeanddata.semrack.core.models.IndexSearchResult;
import fr.codeanddata.semrack.core.models.SearchRequest;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@ApplicationScoped
public class SemrackIndexService {
@Inject
SemdocIndex semdocIndex;
public Uni<Void> index(String documentId) {
return semdocIndex.index(documentId);
}
public Uni<IndexSearchResult> search(SearchRequest searchRequest) {
return semdocIndex.search(searchRequest);
}
public Uni<Long> count(SearchRequest request) {
return semdocIndex.count(request);
}
public Uni<Boolean> exist(SearchRequest query) {
return semdocIndex.exist(query);
}
public Uni<Void> clear(String documentId) {
return semdocIndex.clear(documentId);
}
}

View File

@@ -1,36 +1,46 @@
package fr.codeanddata.semrack.core.test; package fr.codeanddata.semrack.core.storages;
import fr.codeanddata.semrack.core.SemdocIndex;
import fr.codeanddata.semrack.core.SemdocStorage; import fr.codeanddata.semrack.core.SemdocStorage;
import fr.codeanddata.semrack.core.models.SearchRequest; import fr.codeanddata.semrack.core.models.SearchRequest;
import fr.codeanddata.semrack.core.models.SearchResult; import fr.codeanddata.semrack.core.models.SearchResult;
import fr.codeanddata.semrack.core.models.SemrackDocument; import fr.codeanddata.semrack.core.models.SemrackDocument;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@ApplicationScoped @ApplicationScoped
public class MockedStorageImpl implements SemdocStorage { public class SemdocStorageProxy {
@Override
@Inject
SemdocStorage storage;
@Inject
SemdocIndex index;
public Uni<SemrackDocument> readDocument(String uid) { public Uni<SemrackDocument> readDocument(String uid) {
return null; return storage.get(uid);
} }
@Override
public Uni<SearchResult> searchDocument(SearchRequest query) { public Uni<SearchResult> searchDocument(SearchRequest query) {
return null; return index.search(query)
.chain(searchResult -> storage.get(searchResult.getUids())
.map(documents -> SearchResult.builder()
.documents(documents)
.pagination(searchResult.getPagination())
.build()));
} }
@Override
public Uni<Long> countDocuments(SearchRequest request) { public Uni<Long> countDocuments(SearchRequest request) {
return null; return index.count(request);
} }
@Override
public Uni<Boolean> documentsExist(SearchRequest query) { public Uni<Boolean> documentsExist(SearchRequest query) {
return null; return index.exist(query);
} }
@Override
public Uni<SemrackDocument> storeDocument(SemrackDocument document) { public Uni<SemrackDocument> storeDocument(SemrackDocument document) {
return null; return storage.storeDocument(document)
.call(x -> index.index(x.getUid()));
} }
} }

View File

@@ -0,0 +1,91 @@
package fr.codeanddata.semrack.core.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.codeanddata.semrack.core.models.TraverserPath;
import io.vertx.core.json.Json;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.*;
import java.util.function.Function;
@ApplicationScoped
public class Traverser implements Function<Object, List<TraverserPath>> {
@Inject
ObjectMapper mapper;
@Override
public List<TraverserPath> apply(Object o) {
return parse("", o);
}
public enum PathTypes {
UNKNOWN,
STRING,
NUMBER,
BOOLEAN,
LIST,
OBJECT
}
List<TraverserPath> parse(String basePath, Object data) {
final List<TraverserPath> paths = new ArrayList<>();
PathTypes type = PathTypes.UNKNOWN;
if (data instanceof String) {
type = PathTypes.STRING;
} else if (data instanceof Boolean) {
type = PathTypes.BOOLEAN;
} else if (data instanceof Number) {
type = PathTypes.NUMBER;
} else if (data instanceof Collection) {
type = PathTypes.LIST;
} else if (data instanceof Map) {
type = PathTypes.OBJECT;
}
switch (type) {
case PathTypes.LIST:
final Collection<?> values = mapper.convertValue(data, Collection.class);
paths.add(TraverserPath.builder()
.fullPath(basePath)
.value(null)
.type(PathTypes.LIST)
.build());
int i = 0;
for (Iterator<?> it = values.iterator(); it.hasNext(); i++) {
final String fullPath = basePath + "[" + i + "]";
final Object value = it.next();
paths.addAll(parse(fullPath, value));
}
break;
case PathTypes.OBJECT:
paths.add(TraverserPath.builder()
.fullPath(basePath)
.value(null)
.type(PathTypes.OBJECT)
.build());
final Map<String, ?> map = mapper.convertValue(data, new TypeReference<>() {
});
for (Map.Entry<String, ?> entry : map.entrySet()) {
final String fullPath = basePath + "." + entry.getKey();
final Object value = entry.getValue();
paths.addAll(parse(fullPath, value));
}
break;
default:
paths.add(TraverserPath.builder()
.type(type)
.fullPath(basePath)
.value(data)
.build());
break;
}
return paths;
}
}

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>semrack-core-testing</artifactId>
<name>Semrack Core - Testing</name>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,69 @@
package fr.codeanddata.semrack.core;
import fr.codeanddata.semrack.core.models.IndexSearchResult;
import fr.codeanddata.semrack.core.models.SearchRequest;
import fr.codeanddata.semrack.core.models.SemrackDocument;
import io.quarkus.arc.DefaultBean;
import io.smallrye.mutiny.Uni;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
import java.util.List;
@Dependent
public class MockProducer {
@Produces
@DefaultBean
@Priority(100)
SemdocStorage produceStorage() {
return new SemdocStorage() {
@Override
public Uni<SemrackDocument> get(String uid) {
return null;
}
@Override
public Uni<List<SemrackDocument>> get(List<String> uids) {
return null;
}
@Override
public Uni<SemrackDocument> storeDocument(SemrackDocument document) {
return null;
}
};
}
@Produces
@DefaultBean
@Priority(100)
SemdocIndex produceIndex() {
return new SemdocIndex() {
@Override
public Uni<Long> count(SearchRequest request) {
return null;
}
@Override
public Uni<Boolean> exist(SearchRequest query) {
return null;
}
@Override
public Uni<Void> index(String documentId) {
return null;
}
@Override
public Uni<IndexSearchResult> search(SearchRequest searchRequest) {
return null;
}
@Override
public Uni<Void> clear(String documentId) {
return null;
}
};
}
}

View File

@@ -0,0 +1,9 @@
name: Semrack Core Testing
#description: Do something useful.
metadata:
# keywords:
# - semrack-core
# guide: ... # To create and publish this guide, see https://github.com/quarkiverse/quarkiverse/wiki#documenting-your-extension
# categories:
# - "miscellaneous"
# status: "preview"

View File

@@ -0,0 +1,54 @@
package fr.codeanddata.semrack.core.utils;
import fr.codeanddata.semrack.core.models.TraverserPath;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@QuarkusTest
public class TraverserTest {
@Inject
Traverser traverser;
@Test
void testUtil() {
Assertions.assertNotNull(traverser);
final Map<String, Object> user = Map.of(
"user", Map.of(
"name", "bob",
"age", 14,
"isAdult", false,
"family", List.of(
Map.of("name", "mom", "age", 38, "isAdult", true),
Map.of("name", "bro", "age", 6, "isAdult", false)
)));
List<TraverserPath> paths = traverser.apply(user);
Assertions.assertNotNull(paths);
Assertions.assertEquals(14, paths.size());
List<List<?>> expectations = List.of(
Arrays.asList(".user", null, Traverser.PathTypes.OBJECT),
Arrays.asList(".user.age", 14, Traverser.PathTypes.NUMBER),
Arrays.asList(".user.family[0].age", 38, Traverser.PathTypes.NUMBER),
Arrays.asList(".user.family[0].isAdult", true, Traverser.PathTypes.BOOLEAN),
Arrays.asList(".user.family[0].name", "mom", Traverser.PathTypes.STRING),
Arrays.asList(".user.family[1].age", 6, Traverser.PathTypes.NUMBER),
Arrays.asList(".user.family[1].isAdult", false, Traverser.PathTypes.BOOLEAN),
Arrays.asList(".user.family[1].name", "bro", Traverser.PathTypes.STRING),
Arrays.asList(".user.isAdult", false, Traverser.PathTypes.BOOLEAN),
Arrays.asList(".user.name", "bob", Traverser.PathTypes.STRING)
);
for (List<?> expectation : expectations) {
final TraverserPath path = paths.stream().filter(x -> expectation.getFirst().equals(x.getFullPath())).findFirst().orElse(null);
Assertions.assertNotNull(path);
Assertions.assertEquals(expectation.get(1), path.getValue());
}
}
}

View File

@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>semrack-index-postgres-deployment</artifactId>
<name>Semrack Index Postgres - Deployment</name>
<dependencies>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive-panache-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-pg-client-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql-deployment</artifactId>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-testing</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,14 @@
package fr.codeanddata.semrack.index.postgres.deployment;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
class SemrackIndexPostgresProcessor {
private static final String FEATURE = "semrack-index-postgres";
@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}
}

View File

@@ -0,0 +1,5 @@
package fr.codeanddata.semrack.index.postgres.test;
public class SemrackIndexPostgresDevModeTest {
}

View File

@@ -0,0 +1,5 @@
package fr.codeanddata.semrack.index.postgres.test;
public class SemrackIndexPostgresTest {
}

View File

@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>semrack-index-postgres-integration-tests</artifactId>
<name>Semrack Index Postgres - Integration Tests</name>
<properties>
<skipITs>true</skipITs>
</properties>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<skipITs>false</skipITs>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.codeanddata.semrack.index.postgres.it;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
@Path("/semrack-index-postgres")
@ApplicationScoped
public class SemrackIndexPostgresResource {
// add some rest methods here
@GET
public String hello() {
return "Hello semrack-index-postgres";
}
}

View File

@@ -0,0 +1,7 @@
package fr.codeanddata.semrack.index.postgres.it;
import io.quarkus.test.junit.QuarkusIntegrationTest;
@QuarkusIntegrationTest
public class SemrackIndexPostgresResourceIT extends SemrackIndexPostgresResourceTest {
}

View File

@@ -0,0 +1,21 @@
package fr.codeanddata.semrack.index.postgres.it;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
@QuarkusTest
public class SemrackIndexPostgresResourceTest {
@Test
public void testHelloEndpoint() {
given()
.when().get("/semrack-index-postgres")
.then()
.statusCode(200)
.body(is("Hello semrack-index-postgres"));
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>semrack-index-postgres-parent</artifactId>
<name>Semrack Index Postgres - Parent</name>
<packaging>pom</packaging>
<modules>
<module>deployment</module>
<module>runtime</module>
</modules>
</project>

View File

@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>semrack-index-postgres</artifactId>
<name>Semrack Index Postgres - Runtime</name>
<dependencies>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-pg-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-maven-plugin</artifactId>
<version>${quarkus.version}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>extension-descriptor</goal>
</goals>
<configuration>
<deployment>${project.groupId}:${project.artifactId}-deployment:${project.version}</deployment>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
<annotationProcessorPathsUseDepMgmt>true</annotationProcessorPathsUseDepMgmt>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package fr.codeanddata.semrack.index.postgres;
import lombok.*;
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EqualLookupParams {
String field;
Object value;
}

View File

@@ -1,4 +1,4 @@
package fr.codeanddata.semrack.storage.postgres.dtos; package fr.codeanddata.semrack.index.postgres;
import lombok.*; import lombok.*;
@@ -11,5 +11,5 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
public class InLookupParams { public class InLookupParams {
String field; String field;
List<String> values; List<Object> values;
} }

View File

@@ -0,0 +1,136 @@
package fr.codeanddata.semrack.index.postgres;
import fr.codeanddata.semrack.core.SemdocIndex;
import fr.codeanddata.semrack.core.SemdocStorage;
import fr.codeanddata.semrack.core.models.*;
import fr.codeanddata.semrack.core.services.SemrackLookupService;
import fr.codeanddata.semrack.core.utils.Traverser;
import fr.codeanddata.semrack.index.postgres.entities.IndexDocumentEntity;
import fr.codeanddata.semrack.index.postgres.entities.IndexKeyEntity;
import fr.codeanddata.semrack.index.postgres.repositories.IndexDocumentRepository;
import fr.codeanddata.semrack.index.postgres.repositories.IndexKeyRepository;
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.quarkus.vertx.ConsumeEvent;
import io.smallrye.mutiny.Uni;
import io.vertx.core.eventbus.EventBus;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.hibernate.query.Page;
import java.util.*;
@ApplicationScoped
public class SemdocJpaIndex implements SemdocIndex, PanacheRepository<IndexKeyEntity> {
public static final String INDEX_CONSUMER = "semdoc-jpa-index";
@Inject
Traverser traverser;
@Inject
SemrackLookupService lookupService;
@Inject
SemdocStorage storage;
@Inject
IndexDocumentRepository indexDocumentRepository;
@Inject
IndexKeyRepository indexKeyRepository;
@Inject
EventBus bus;
@Override
@WithSession
public Uni<Long> count(SearchRequest request) {
final StringBuilder query = new StringBuilder("SELECT DISTINCT count(d.id) FROM index_document d JOIN index_key idx on d.id = idx.index_document_id");
final String lookup = lookupService.lookup(request.getFilter());
final String whereClause = lookup.isEmpty() ? "" : " WHERE " + lookup;
query.append(whereClause);
return getSession()
.chain(s -> s.createNativeQuery(query.toString(), Long.class).getSingleResultOrNull())
.map(count -> count == null ? 0 : count);
}
@Override
public Uni<Boolean> exist(SearchRequest query) {
return count(query).map(count -> count > 0);
}
@Override
@WithTransaction
public Uni<Void> index(String documentId) {
return storage.get(documentId)
.call(document -> clear(documentId))
.call(document -> indexDocumentRepository.persist(IndexDocumentEntity.builder()
.uid(documentId)
.build())
.call(index -> {
final List<TraverserPath> annotationPaths = traverser.apply(document.getAnnotations()).stream().peek(x -> x.setFullPath("annotations" + x.getFullPath())).toList();
final List<TraverserPath> metadataPaths = traverser.apply(document.getMetadata()).stream().peek(x -> x.setFullPath("metadata" + x.getFullPath())).toList();
final List<TraverserPath> allPaths = new ArrayList<>();
allPaths.addAll(annotationPaths);
allPaths.addAll(metadataPaths);
return indexKeyRepository.persist(allPaths
.stream()
.map(path -> {
final Map<String, Object> values = new HashMap<>();
values.put("v", path.getValue());
return IndexKeyEntity.builder()
.index(index)
.type(path.getType())
.fullPath(path.getFullPath())
.value(values)
.build();
}));
}))
.replaceWithVoid();
}
@Override
@WithSession
public Uni<IndexSearchResult> search(SearchRequest request) {
final IndexSearchResult result = new IndexSearchResult();
final StringBuilder query = new StringBuilder("SELECT DISTINCT d.uid FROM index_document d JOIN index_key idx on d.id = idx.index_document_id");
final String lookup = lookupService.lookup(request.getFilter());
final String whereClause = lookup.isEmpty() ? "" : " WHERE " + lookup;
query.append(whereClause);
query.append(" GROUP BY d.uid");
return getSession()
.chain(s -> queryPaginationInfo(request)
.invoke(result::setPagination)
.call(() -> s
.createNativeQuery(query.toString(), String.class)
.setPage(Page.page(Integer.parseInt(result.getPagination().getSize() + ""), Integer.parseInt(result.getPagination().getPage() + "")))
.getResultList()
.invoke(result::setUids))
.map(count -> result));
}
@Override
@WithTransaction
public Uni<Void> clear(String documentId) {
return Uni.createFrom().nullItem()
.call(() -> IndexKeyEntity.delete("index.uid = ?1", documentId))
.call(() -> IndexDocumentEntity.delete("uid = ?1", documentId))
.replaceWithVoid();
}
Uni<PaginationInfo> queryPaginationInfo(SearchRequest query) {
return count(query)
.map(count -> PaginationInfo.builder()
.page(Optional.ofNullable(query.getPaginate()).map(SemrackPagination::getPage).orElse(0))
.size(Optional.ofNullable(query.getPaginate()).map(SemrackPagination::getSize).orElse(10))
.total(count)
.build());
}
}

View File

@@ -0,0 +1,20 @@
package fr.codeanddata.semrack.index.postgres.entities;
import io.quarkus.hibernate.reactive.panache.PanacheEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.*;
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "IndexDocument")
@Table(name = "index_document")
public class IndexDocumentEntity extends PanacheEntity {
@Column(nullable = false, unique = true, columnDefinition = "varchar(256)")
String uid;
}

View File

@@ -0,0 +1,33 @@
package fr.codeanddata.semrack.index.postgres.entities;
import fr.codeanddata.semrack.core.utils.Traverser;
import io.quarkus.hibernate.reactive.panache.PanacheEntity;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
import java.util.Map;
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "IndexAnnotation")
@Table(name = "index_key")
public class IndexKeyEntity extends PanacheEntity {
@ManyToOne
@JoinColumn(name = "index_document_id", nullable = false)
IndexDocumentEntity index;
@Column(name = "fullpath")
String fullPath;
@Enumerated(EnumType.STRING)
Traverser.PathTypes type;
@JdbcTypeCode(SqlTypes.JSON)
Map<String, Object> value;
}

View File

@@ -1,4 +1,4 @@
package fr.codeanddata.semrack.storage.postgres.operators; package fr.codeanddata.semrack.index.postgres.operators;
import fr.codeanddata.semrack.core.services.SemrackLookupService; import fr.codeanddata.semrack.core.services.SemrackLookupService;
import io.smallrye.common.annotation.Identifier; import io.smallrye.common.annotation.Identifier;

View File

@@ -0,0 +1,32 @@
package fr.codeanddata.semrack.index.postgres.operators;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.codeanddata.semrack.core.exceptions.SemrackRuntimeException;
import fr.codeanddata.semrack.index.postgres.EqualLookupParams;
import io.smallrye.common.annotation.Identifier;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@Identifier("equal")
@ApplicationScoped
public class EqualLookup implements SemrackJpaLookupExpression<EqualLookupParams> {
@Inject
ObjectMapper objectMapper;
@Override
public String apply(Object params) {
final EqualLookupParams equalLookupParams = convert(params, EqualLookupParams.class);
if (equalLookupParams.getField() == null || equalLookupParams.getField().isEmpty()) {
throw new SemrackRuntimeException("Field expected");
}
try {
return "(idx.fullpath = '"+ equalLookupParams.getField() + "' AND idx.value->'v' = '"+ objectMapper.writeValueAsString(equalLookupParams.getValue())+"'::jsonb)";
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,39 @@
package fr.codeanddata.semrack.index.postgres.operators;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.codeanddata.semrack.core.exceptions.SemrackRuntimeException;
import fr.codeanddata.semrack.index.postgres.InLookupParams;
import io.smallrye.common.annotation.Identifier;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@Identifier("in")
@ApplicationScoped
public class InLookup implements SemrackJpaLookupExpression<InLookupParams> {
@Inject
ObjectMapper objectMapper;
@Override
public String apply(Object params) {
final InLookupParams inLookupParams = convert(params, InLookupParams.class);
if (inLookupParams.getField() == null || inLookupParams.getField().isEmpty()) {
throw new SemrackRuntimeException("Field expected");
}
try {
final List<String> inValues = new ArrayList<>();
for (Object value : inLookupParams.getValues()) {
inValues.add("'" + objectMapper.writeValueAsString(value) + "'::jsonb");
}
return "(idx.fullpath = '" + inLookupParams.getField() + "' AND idx.value->'v' in (" + String.join(",", inValues) + "))";
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,4 +1,4 @@
package fr.codeanddata.semrack.storage.postgres.operators; package fr.codeanddata.semrack.index.postgres.operators;
import fr.codeanddata.semrack.core.services.SemrackLookupService; import fr.codeanddata.semrack.core.services.SemrackLookupService;
import io.smallrye.common.annotation.Identifier; import io.smallrye.common.annotation.Identifier;

View File

@@ -1,4 +1,4 @@
package fr.codeanddata.semrack.storage.postgres.operators; package fr.codeanddata.semrack.index.postgres.operators;
import fr.codeanddata.semrack.core.SemrackLookupExpression; import fr.codeanddata.semrack.core.SemrackLookupExpression;

View File

@@ -0,0 +1,9 @@
package fr.codeanddata.semrack.index.postgres.repositories;
import fr.codeanddata.semrack.index.postgres.entities.IndexDocumentEntity;
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class IndexDocumentRepository implements PanacheRepository<IndexDocumentEntity> {
}

View File

@@ -0,0 +1,9 @@
package fr.codeanddata.semrack.index.postgres.repositories;
import fr.codeanddata.semrack.index.postgres.entities.IndexKeyEntity;
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class IndexKeyRepository implements PanacheRepository<IndexKeyEntity> {
}

View File

@@ -0,0 +1,9 @@
name: Semrack Index Postgres
#description: Do something useful.
metadata:
# keywords:
# - semrack-index-postgres
# guide: ... # To create and publish this guide, see https://github.com/quarkiverse/quarkiverse/wiki#documenting-your-extension
# categories:
# - "miscellaneous"
# status: "preview"

View File

@@ -0,0 +1,9 @@
quarkus.datasource.db-kind=postgresql
quarkus.flyway.enabled=true
quarkus.flyway.active=false
quarkus.flyway.migrate-at-start=true
quarkus.flyway.out-of-order=true
quarkus.flyway.ignore-missing-migrations=true
quarkus.native.resources.includes=src/main/resources/db/migration/**
quarkus.native.additional-build-args=-H:SerializationConfigurationResources=serialization-config.json

View File

@@ -0,0 +1,20 @@
create table index_document
(
id bigint not null,
uid varchar(256) not null,
primary key (id)
);
create table index_key
(
id bigint not null,
fullpath varchar(255),
type varchar(255) check (type in ('UNKNOWN', 'STRING', 'NUMBER', 'BOOLEAN', 'LIST', 'OBJECT')),
value jsonb,
index_document_id bigint not null,
primary key (id)
);
alter table if exists index_document drop constraint if exists UKc9q9jn623dprjanh1v3wvh86;
alter table if exists index_document add constraint UKc9q9jn623dprjanh1v3wvh86 unique (uid);
create sequence index_document_SEQ start with 1 increment by 50;
create sequence index_key_SEQ start with 1 increment by 50;
alter table if exists index_key add constraint FKgy9d5xg0nf2gsvxu58hnw8fa2 foreign key (index_document_id) references index_document;

View File

@@ -0,0 +1,3 @@
[
{"name": "java.lang.String"}
]

View File

@@ -39,13 +39,17 @@
<dependency> <dependency>
<groupId>fr.codeanddata.semrack</groupId> <groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-storage-postgres</artifactId> <artifactId>semrack-storage-postgres</artifactId>
<version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId> <artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -0,0 +1 @@
quarkus.hibernate-orm.mapping.format.global=ignore

View File

@@ -44,6 +44,11 @@
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
</dependency> </dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>io.quarkus</groupId> <groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId> <artifactId>quarkus-junit5</artifactId>

View File

@@ -1,24 +0,0 @@
package fr.codeanddata.semrack.storage.postgres.operators;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.smallrye.common.annotation.Identifier;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@Identifier("contains")
@ApplicationScoped
public class ContainsLookup implements SemrackJpaLookupExpression<Object> {
@Inject
ObjectMapper objectMapper;
@Override
public String apply(Object params) {
try {
return "document @> '" + objectMapper.writeValueAsString(params) + "'";
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,31 +0,0 @@
package fr.codeanddata.semrack.storage.postgres.operators;
import fr.codeanddata.semrack.core.exceptions.SemrackRuntimeException;
import fr.codeanddata.semrack.storage.postgres.dtos.InLookupParams;
import io.smallrye.common.annotation.Identifier;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Identifier("in")
@ApplicationScoped
public class InLookup implements SemrackJpaLookupExpression<InLookupParams> {
@Override
public String apply(Object params) {
final InLookupParams inLookupParams = convert(params, InLookupParams.class);
final List<String> fields = new ArrayList<>(Arrays.asList(inLookupParams.getField().split("\\.")));
if (fields.isEmpty()) {
throw new SemrackRuntimeException("Field expected");
}
String query = "->> '" + fields.removeLast() + "'";
if (!fields.isEmpty()) {
query = "-> '" + String.join("' -> '", fields) + "' " + query;
}
return "document " + query + " in " + "('" + String.join("', '", inLookupParams.getValues()) + "')";
}
}

View File

@@ -1,14 +1,10 @@
package fr.codeanddata.semrack.storage.postgres.storage; package fr.codeanddata.semrack.storage.postgres.storage;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.codeanddata.semrack.core.SemdocStorage; import fr.codeanddata.semrack.core.SemdocStorage;
import fr.codeanddata.semrack.core.enums.SemrackSortDirection; import fr.codeanddata.semrack.core.models.SemrackDocument;
import fr.codeanddata.semrack.core.models.*;
import fr.codeanddata.semrack.core.services.SemrackLookupService; import fr.codeanddata.semrack.core.services.SemrackLookupService;
import fr.codeanddata.semrack.core.utils.UIDGenerator; import fr.codeanddata.semrack.core.utils.UIDGenerator;
import fr.codeanddata.semrack.storage.postgres.entities.SemrackDocumentEntity; import fr.codeanddata.semrack.storage.postgres.entities.SemrackDocumentEntity;
import fr.codeanddata.semrack.storage.postgres.utils.PathMapper;
import io.quarkus.hibernate.reactive.panache.PanacheRepository; import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import io.quarkus.hibernate.reactive.panache.common.WithSession; import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.quarkus.hibernate.reactive.panache.common.WithTransaction; import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
@@ -16,7 +12,8 @@ import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import java.util.*; import java.util.List;
import java.util.Optional;
@ApplicationScoped @ApplicationScoped
public class SemdocJpaStorage implements SemdocStorage, PanacheRepository<SemrackDocumentEntity> { public class SemdocJpaStorage implements SemdocStorage, PanacheRepository<SemrackDocumentEntity> {
@@ -24,42 +21,21 @@ public class SemdocJpaStorage implements SemdocStorage, PanacheRepository<Semrac
@Inject @Inject
UIDGenerator generator; UIDGenerator generator;
@Inject
PathMapper pathMapper;
@Inject @Inject
SemrackLookupService lookupService; SemrackLookupService lookupService;
@Inject
ObjectMapper objectMapper;
@Override @Override
@WithSession @WithSession
public Uni<SemrackDocument> readDocument(String uid) { public Uni<SemrackDocument> get(String uid) {
return find("uid = ?1", uid) return find("uid = ?1", uid)
.firstResult().map(d -> Optional.ofNullable(d).map(SemrackDocumentEntity::getDocument).orElse(null)); .firstResult().map(d -> Optional.ofNullable(d).map(SemrackDocumentEntity::getDocument).orElse(null));
} }
@Override @Override
@WithSession @WithSession
public Uni<SearchResult> searchDocument(SearchRequest query) { public Uni<List<SemrackDocument>> get(List<String> uids) {
return search(query); return find("uid in ?1", uids).list()
} .map(d -> d.stream().map(SemrackDocumentEntity::getDocument).toList());
@Override
public Uni<Long> countDocuments(SearchRequest request) {
final String lookup = lookupService.lookup(request.getFilter());
final String whereClause = lookup.isEmpty() ? "" : " WHERE " + lookup;
return getSession()
.chain(s -> s.createNativeQuery("SELECT count(id) FROM semrack_document" + whereClause, Long.class).getSingleResultOrNull())
.map(count -> count == null ? 0 : count);
}
@Override
public Uni<Boolean> documentsExist(SearchRequest query) {
return countDocuments(query)
.map(c -> c > 0);
} }
@WithTransaction @WithTransaction
@@ -93,76 +69,76 @@ public class SemdocJpaStorage implements SemdocStorage, PanacheRepository<Semrac
.map(x -> document); .map(x -> document);
} }
Uni<SearchResult> search(SearchRequest request) { // Uni<SearchResult> search(SearchRequest request) {
final String baseQuery = buildBaseQuery(request); // final String baseQuery = buildBaseQuery(request);
//
final StringBuilder sorting = new StringBuilder(); // final StringBuilder sorting = new StringBuilder();
if (request.getSort() != null && !request.getSort().isEmpty()) { // if (request.getSort() != null && !request.getSort().isEmpty()) {
sorting.append(" ORDER BY "); // sorting.append(" ORDER BY ");
sorting.append(String.join(", ", request.getSort().stream().map(sort -> sort.getField() + " " + Optional.ofNullable(sort.getDirection()).orElse(SemrackSortDirection.asc).name()).toList())); // sorting.append(String.join(", ", request.getSort().stream().map(sort -> sort.getField() + " " + Optional.ofNullable(sort.getDirection()).orElse(SemrackSortDirection.asc).name()).toList()));
} // }
//
final StringBuilder paginate = new StringBuilder(); // final StringBuilder paginate = new StringBuilder();
if (request.getPaginate() != null) { // if (request.getPaginate() != null) {
final SemrackPagination pagination = request.getPaginate(); // final SemrackPagination pagination = request.getPaginate();
final Integer size = pagination.getSize() == null ? 200 : pagination.getSize(); // final Integer size = pagination.getSize() == null ? 200 : pagination.getSize();
final Integer page = pagination.getPage() == null ? 0 : pagination.getPage(); // final Integer page = pagination.getPage() == null ? 0 : pagination.getPage();
//
paginate.append(" LIMIT ").append(size).append(" OFFSET ").append(size * page); // paginate.append(" LIMIT ").append(size).append(" OFFSET ").append(size * page);
} // }
//
return getSession() // return getSession()
.chain(s -> { // .chain(s -> {
final SearchResult searchResult = SearchResult.builder().build(); // final SearchResult searchResult = SearchResult.builder().build();
return countDocuments(request) // return countDocuments(request)
.invoke(count -> searchResult.setPagination(PaginationInfo.builder() // .invoke(count -> searchResult.setPagination(PaginationInfo.builder()
.page(Optional.ofNullable(request.getPaginate()).map(SemrackPagination::getPage).orElse(0)) // .page(Optional.ofNullable(request.getPaginate()).map(SemrackPagination::getPage).orElse(0))
.size(Optional.ofNullable(request.getPaginate()).map(SemrackPagination::getSize).orElse(0)) // .size(Optional.ofNullable(request.getPaginate()).map(SemrackPagination::getSize).orElse(0))
.total(count) // .total(count)
.build())) // .build()))
.call(() -> s // .call(() -> s
.createNativeQuery(baseQuery + sorting + paginate, Object.class).getResultList() // .createNativeQuery(baseQuery + sorting + paginate, Object.class).getResultList()
.invoke(results -> searchResult.setDocuments(project(request.getFields(), results)))) // .invoke(results -> searchResult.setDocuments(project(request.getFields(), results))))
.map(count -> searchResult); // .map(count -> searchResult);
}); // });
} // }
//
String buildBaseQuery(SearchRequest request) { // String buildBaseQuery(SearchRequest request) {
final String lookup = lookupService.lookup(request.getFilter()); // final String lookup = lookupService.lookup(request.getFilter());
final String whereClause = lookup.isEmpty() ? "" : " WHERE " + lookup; // final String whereClause = lookup.isEmpty() ? "" : " WHERE " + lookup;
//
if (request.getFields() == null || request.getFields().isEmpty()) { // if (request.getFields() == null || request.getFields().isEmpty()) {
return "SELECT document FROM semrack_document" + whereClause; // return "SELECT document FROM semrack_document" + whereClause;
} else { // } else {
return "SELECT " + toJsonbPathExtract(request.getFields()) + " FROM semrack_document" + whereClause; // return "SELECT " + toJsonbPathExtract(request.getFields()) + " FROM semrack_document" + whereClause;
} // }
} // }
//
String toJsonbPathExtract(List<String> fields) { // String toJsonbPathExtract(List<String> fields) {
return String.join(",", fields.stream().map(field -> { // return String.join(",", fields.stream().map(field -> {
final String serializedField = "'" + String.join("','", field.split("\\.")) + "'"; // final String serializedField = "'" + String.join("','", field.split("\\.")) + "'";
return "jsonb_extract_path(document, " + serializedField + ")"; // return "jsonb_extract_path(document, " + serializedField + ")";
}).toList()); // }).toList());
} // }
//
List<SemrackDocument> project(List<String> fields, Object results) { // List<SemrackDocument> project(List<String> fields, Object results) {
final List<?> projectedResults = objectMapper.convertValue(results, List.class); // final List<?> projectedResults = objectMapper.convertValue(results, List.class);
if (fields == null || fields.isEmpty()) { // if (fields == null || fields.isEmpty()) {
return projectedResults.stream().map(result -> objectMapper.convertValue(result, SemrackDocument.class)).toList(); // return projectedResults.stream().map(result -> objectMapper.convertValue(result, SemrackDocument.class)).toList();
} else { // } else {
return projectedResults.stream().map(result -> { // return projectedResults.stream().map(result -> {
final List<Object> row = fields.size() == 1 ? List.of(result) : objectMapper.convertValue(result, new TypeReference<>() { // final List<Object> row = fields.size() == 1 ? List.of(result) : objectMapper.convertValue(result, new TypeReference<>() {
}); // });
final List<Object> rowMut = new ArrayList<>(row); // final List<Object> rowMut = new ArrayList<>(row);
final List<String> fieldCp = new ArrayList<>(fields); // final List<String> fieldCp = new ArrayList<>(fields);
final Map<String, Object> document = new HashMap<>(); // final Map<String, Object> document = new HashMap<>();
while (!fieldCp.isEmpty() && !row.isEmpty()) { // while (!fieldCp.isEmpty() && !row.isEmpty()) {
final String field = fieldCp.removeFirst(); // final String field = fieldCp.removeFirst();
final Object value = rowMut.removeFirst(); // final Object value = rowMut.removeFirst();
pathMapper.map(field, document, value); // pathMapper.map(field, document, value);
} // }
return objectMapper.convertValue(document, SemrackDocument.class); // return objectMapper.convertValue(document, SemrackDocument.class);
}).toList(); // }).toList();
} // }
} // }
} }

View File

@@ -2,6 +2,8 @@ quarkus.datasource.db-kind=postgresql
quarkus.flyway.enabled=true quarkus.flyway.enabled=true
quarkus.flyway.active=false quarkus.flyway.active=false
quarkus.flyway.migrate-at-start=true quarkus.flyway.migrate-at-start=true
quarkus.flyway.out-of-order=true
quarkus.flyway.ignore-missing-migrations=true
quarkus.native.resources.includes=src/main/resources/db/migration/** quarkus.native.resources.includes=src/main/resources/db/migration/**
quarkus.native.additional-build-args=-H:SerializationConfigurationResources=serialization-config.json quarkus.native.additional-build-args=-H:SerializationConfigurationResources=serialization-config.json

View File

@@ -1,25 +0,0 @@
package fr.codeanddata.semrack.storage.postgres.operators;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.common.annotation.Identifier;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Map;
@QuarkusTest
class ContainsLookupTest {
@Inject
@Identifier("contains")
ContainsLookup subject;
@Test
void testLookup() {
final String lookup = subject.apply(Map.of("product", Map.of("sku", "xxx")));
final String expectedLookup = "document @> '{\"product\":{\"sku\":\"xxx\"}}'";
Assertions.assertEquals(expectedLookup, lookup);
}
}

View File

@@ -1,43 +0,0 @@
package fr.codeanddata.semrack.storage.postgres.operators;
import fr.codeanddata.semrack.storage.postgres.dtos.InLookupParams;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.common.annotation.Identifier;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
@QuarkusTest
class InLookupTest {
@Inject
@Identifier("in")
InLookup subject;
@Test
void testConversion() {
final String field = "root.parent.child.field";
final List<String> values = List.of("un", "deux", "trois");
final Map<String, Object> inParams = Map.of(
"field", field,
"values", values
);
final InLookupParams convertedParams = subject.convert(inParams, InLookupParams.class);
Assertions.assertEquals(field, convertedParams.getField());
Assertions.assertEquals(values, convertedParams.getValues());
}
@Test
void testLookup() {
final String lookup = subject.apply(Map.of(
"field", "lookup.field",
"values", List.of("list", "of", "values")));
final String expectedLookup = "document -> 'lookup' ->> 'field' in ('list', 'of', 'values')";
Assertions.assertEquals(expectedLookup, lookup);
}
}

View File

@@ -0,0 +1 @@
quarkus.hibernate-orm.mapping.format.global=ignore

18
pom.xml
View File

@@ -9,6 +9,7 @@
<modules> <modules>
<module>modules/semrack-core</module> <module>modules/semrack-core</module>
<module>modules/semrack-index-postgres</module>
<module>modules/semrack-storage-postgres</module> <module>modules/semrack-storage-postgres</module>
<module>modules/semrack-api-rest</module> <module>modules/semrack-api-rest</module>
<!-- <module>integration/semrack-integration</module>--> <!-- <module>integration/semrack-integration</module>-->
@@ -31,7 +32,7 @@
<lombok.version>1.18.38</lombok.version> <lombok.version>1.18.38</lombok.version>
<org.mapstruct.version>1.6.3</org.mapstruct.version> <org.mapstruct.version>1.6.3</org.mapstruct.version>
<semrack.version>1.0-SNAPSHOT</semrack.version> <semrack.version>1.0-SNAPSHOT</semrack.version>
<quarkus.version>3.25.3</quarkus.version> <quarkus.version>3.27.0</quarkus.version>
<jsonpath.version>2.9.0</jsonpath.version> <jsonpath.version>2.9.0</jsonpath.version>
</properties> </properties>
@@ -67,11 +68,26 @@
<artifactId>semrack-core</artifactId> <artifactId>semrack-core</artifactId>
<version>${semrack.version}</version> <version>${semrack.version}</version>
</dependency> </dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-testing</artifactId>
<version>${semrack.version}</version>
</dependency>
<dependency> <dependency>
<groupId>fr.codeanddata.semrack</groupId> <groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-deployment</artifactId> <artifactId>semrack-core-deployment</artifactId>
<version>${semrack.version}</version> <version>${semrack.version}</version>
</dependency> </dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres</artifactId>
<version>${semrack.version}</version>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-index-postgres-deployment</artifactId>
<version>${semrack.version}</version>
</dependency>
<dependency> <dependency>
<groupId>fr.codeanddata.semrack</groupId> <groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-storage-postgres</artifactId> <artifactId>semrack-storage-postgres</artifactId>