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

View File

@@ -1,59 +1,69 @@
<?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>
<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-deployment</artifactId>
<name>Semrack Core - Deployment</name>
<parent>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>semrack-core-deployment</artifactId>
<name>Semrack Core - Deployment</name>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-security-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-security-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>fr.codeanddata.semrack</groupId>
<artifactId>semrack-core</artifactId>
</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>
<version>${project.version}</version>
<scope>test</scope>
</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>
<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

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

View File

@@ -16,6 +16,10 @@
<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-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;
import fr.codeanddata.semrack.core.models.SearchRequest;
import fr.codeanddata.semrack.core.models.SearchResult;
import fr.codeanddata.semrack.core.models.SemrackDocument;
import io.smallrye.mutiny.Uni;
import java.util.List;
public interface SemdocStorage {
Uni<SemrackDocument> readDocument(String uid);
Uni<SearchResult> searchDocument(SearchRequest query);
Uni<Long> countDocuments(SearchRequest request);
Uni<Boolean> documentsExist(SearchRequest query);
Uni<SemrackDocument> get(String uid);
Uni<List<SemrackDocument>> get(List<String> uids);
Uni<SemrackDocument> storeDocument(SemrackDocument document);
}

View File

@@ -24,7 +24,7 @@ public class AnnotateWriteInterceptor implements SemdocWriteInterceptor {
final Map<String, Object> directives = context.getDirectives();
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)));
}

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;
import fr.codeanddata.semrack.core.SemdocStorage;
import fr.codeanddata.semrack.core.SemdocWriteInterceptor;
import fr.codeanddata.semrack.core.exceptions.SemrackRuntimeException;
import fr.codeanddata.semrack.core.models.*;
import fr.codeanddata.semrack.core.storages.SemdocStorageProxy;
import fr.codeanddata.semrack.core.utils.UIDGenerator;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
@@ -19,7 +19,7 @@ import java.util.concurrent.atomic.AtomicReference;
public class SemdocRepository {
@Inject
SemdocStorage semdocStorage;
SemdocStorageProxy semdocStorage;
@Inject
@Any
@@ -28,10 +28,14 @@ public class SemdocRepository {
@Inject
UIDGenerator uidGenerator;
Uni<SearchResult> searchDocument(SearchRequest query) {
public Uni<SearchResult> searchDocument(SearchRequest query) {
return semdocStorage.searchDocument(query);
}
public Uni<SemrackDocument> get(String documentId) {
return semdocStorage.readDocument(documentId);
}
public Uni<SemrackDocument> pushDocument(PushDocumentRequest pushDocument) {
return Optional.ofNullable(pushDocument.getUid()).map(semdocStorage::readDocument).orElse(Uni.createFrom().nullItem())
.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.models.SearchRequest;
import fr.codeanddata.semrack.core.models.SearchResult;
import fr.codeanddata.semrack.core.models.SemrackDocument;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@ApplicationScoped
public class MockedStorageImpl implements SemdocStorage {
@Override
public class SemdocStorageProxy {
@Inject
SemdocStorage storage;
@Inject
SemdocIndex index;
public Uni<SemrackDocument> readDocument(String uid) {
return null;
return storage.get(uid);
}
@Override
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) {
return null;
return index.count(request);
}
@Override
public Uni<Boolean> documentsExist(SearchRequest query) {
return null;
return index.exist(query);
}
@Override
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());
}
}
}