This commit is contained in:
10
.github/workflows/build.yml
vendored
10
.github/workflows/build.yml
vendored
@@ -17,9 +17,13 @@ jobs:
|
|||||||
java-version: '21'
|
java-version: '21'
|
||||||
distribution: 'temurin'
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Set versions
|
||||||
|
run: |
|
||||||
|
export BUILD_VERSION=$(test "$GITHUB_REF_TYPE" = "tag" && echo ${GITHUB_REF_NAME} || (echo ${GITHUB_SHA} | head -c 8))
|
||||||
|
./mvnw versions:set -DnewVersion=${BUILD_VERSION}
|
||||||
|
|
||||||
- name: Build project
|
- name: Build project
|
||||||
run: ./mvnw -s .mvn/settings.xml clean install -DskipTests
|
run: ./mvnw -s .mvn/settings.xml clean install
|
||||||
|
|
||||||
- name: Deploy project
|
- name: Deploy project
|
||||||
run: ./mvnw -s .mvn/settings.xml deploy -DskipTests
|
run: ./mvnw -s .mvn/settings.xml deploy
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package fr.codeanddata;
|
|
||||||
|
|
||||||
import io.quarkus.test.junit.QuarkusIntegrationTest;
|
|
||||||
|
|
||||||
@QuarkusIntegrationTest
|
|
||||||
class SemrackApiIT extends SemrackApiTest {
|
|
||||||
// Execute the same tests but in packaged mode.
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package fr.codeanddata;
|
|
||||||
|
|
||||||
import io.quarkus.test.junit.QuarkusTest;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
|
|
||||||
@QuarkusTest
|
|
||||||
class SemrackApiTest {
|
|
||||||
@Test
|
|
||||||
void testHelloEndpoint() {
|
|
||||||
given()
|
|
||||||
.when().get("/hello")
|
|
||||||
.then()
|
|
||||||
.statusCode(200)
|
|
||||||
.body(is("Hello from Quarkus REST"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,50 +1,54 @@
|
|||||||
package fr.codeanddata.semrack.api.rest;
|
package fr.codeanddata.semrack.api.rest;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.PushDocumentRequest;
|
import fr.codeanddata.semrack.core.models.*;
|
||||||
import fr.codeanddata.semrack.core.models.SearchRequest;
|
import fr.codeanddata.semrack.core.repositories.DocumentRepository;
|
||||||
import fr.codeanddata.semrack.core.models.SearchResult;
|
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
|
||||||
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;
|
||||||
import jakarta.ws.rs.GET;
|
import jakarta.ws.rs.GET;
|
||||||
import jakarta.ws.rs.POST;
|
import jakarta.ws.rs.POST;
|
||||||
|
import jakarta.ws.rs.PUT;
|
||||||
import jakarta.ws.rs.Path;
|
import jakarta.ws.rs.Path;
|
||||||
import jakarta.ws.rs.Produces;
|
import jakarta.ws.rs.Produces;
|
||||||
import jakarta.ws.rs.core.MediaType;
|
import jakarta.ws.rs.core.MediaType;
|
||||||
import org.jboss.resteasy.reactive.RestPath;
|
import org.jboss.resteasy.reactive.RestPath;
|
||||||
|
|
||||||
@Path("/semrack/documents")
|
@Path("/semrack/documents")
|
||||||
//@Authenticated
|
|
||||||
public class SemrackApi {
|
public class SemrackApi {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SemdocRepository repository;
|
DocumentRepository repository;
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Uni<SemrackDocument> pushDocument(PushDocumentRequest semrackDocument) {
|
public Uni<Document> pushDocument(PushDocument semrackDocument) {
|
||||||
return repository.pushDocument(semrackDocument);
|
return repository.pushDocument(semrackDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/search")
|
@Path("/search")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Uni<SearchResult> searchDocument(SearchRequest query) {
|
public Uni<SearchResult> searchDocument(Search query) {
|
||||||
return repository.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<Document> getDocument(@RestPath String uid) {
|
||||||
return repository.get(uid);
|
return repository.get(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("{uid}")
|
@Path("{uid}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Uni<SemrackDocument> updateDocument(@RestPath String uid, PushDocumentRequest semrackDocument) {
|
public Uni<Document> getDocument(@RestPath String uid, StorageGet request) {
|
||||||
|
return repository.get(uid, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("{uid}/update")
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public Uni<Document> updateDocument(@RestPath String uid, PushDocument semrackDocument) {
|
||||||
return repository.pushDocument(semrackDocument);
|
return repository.pushDocument(semrackDocument);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package fr.codeanddata.semrack.core;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.models.IndexSearchResult;
|
||||||
|
import fr.codeanddata.semrack.core.models.Search;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
|
||||||
|
public interface Index {
|
||||||
|
Uni<Long> count(Search request);
|
||||||
|
Uni<Boolean> exist(Search query);
|
||||||
|
Uni<Void> index(String documentId);
|
||||||
|
Uni<IndexSearchResult> search(Search search);
|
||||||
|
Uni<Void> clear(String documentId);
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import jakarta.enterprise.inject.spi.CDI;
|
import jakarta.enterprise.inject.spi.CDI;
|
||||||
|
|
||||||
public interface SemrackLookupExpression<T> {
|
public interface LookupExpression<T> {
|
||||||
String apply(Object params);
|
String apply(Object params);
|
||||||
|
|
||||||
default T convert(Object params) {
|
default T convert(Object params) {
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package fr.codeanddata.semrack.core;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.models.ReadContext;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
|
||||||
|
public interface ReadInterceptor {
|
||||||
|
Uni<Void> interceptSemdocRead(ReadContext context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package fr.codeanddata.semrack.core;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.models.SearchContext;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
|
||||||
|
public interface SearchInterceptor {
|
||||||
|
Uni<Void> interceptSemdocSearch(SearchContext context);
|
||||||
|
}
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.core;
|
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.SemdocReadContext;
|
|
||||||
import io.smallrye.mutiny.Uni;
|
|
||||||
|
|
||||||
public interface SemdocReadInterceptor {
|
|
||||||
Uni<Void> interceptSemdocRead(SemdocReadContext context);
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.core;
|
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.SemdocSearchContext;
|
|
||||||
import io.smallrye.mutiny.Uni;
|
|
||||||
|
|
||||||
public interface SemdocSearchInterceptor {
|
|
||||||
Uni<Void> interceptSemdocSearch(SemdocSearchContext context);
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.core;
|
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
|
||||||
import io.smallrye.mutiny.Uni;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface SemdocStorage {
|
|
||||||
Uni<SemrackDocument> get(String uid);
|
|
||||||
Uni<List<SemrackDocument>> get(List<String> uids);
|
|
||||||
Uni<SemrackDocument> storeDocument(SemrackDocument document);
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.core;
|
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.SemdocWriteContext;
|
|
||||||
import io.smallrye.mutiny.Uni;
|
|
||||||
|
|
||||||
public interface SemdocWriteInterceptor {
|
|
||||||
Uni<Void> interceptSemdocWrite(SemdocWriteContext context);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package fr.codeanddata.semrack.core;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
|
import fr.codeanddata.semrack.core.models.StorageGet;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface Storage {
|
||||||
|
Uni<Document> get(String uid, StorageGet request);
|
||||||
|
Uni<List<Document>> get(List<String> uids, StorageGet request);
|
||||||
|
Uni<Document> storeDocument(Document document);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package fr.codeanddata.semrack.core;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.models.WriteContext;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
|
||||||
|
public interface WriteInterceptor {
|
||||||
|
Uni<Void> interceptSemdocWrite(WriteContext context);
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
package fr.codeanddata.semrack.core.enums;
|
package fr.codeanddata.semrack.core.enums;
|
||||||
|
|
||||||
public enum SemrackSortDirection {
|
public enum SortDirection {
|
||||||
asc, desc
|
asc, desc
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,8 @@ package fr.codeanddata.semrack.core.interceptors;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import fr.codeanddata.semrack.core.SemdocWriteInterceptor;
|
import fr.codeanddata.semrack.core.WriteInterceptor;
|
||||||
import fr.codeanddata.semrack.core.models.SemdocWriteContext;
|
import fr.codeanddata.semrack.core.models.WriteContext;
|
||||||
import io.smallrye.mutiny.Uni;
|
import io.smallrye.mutiny.Uni;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
@@ -11,7 +11,7 @@ import jakarta.inject.Inject;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class AnnotateWriteInterceptor implements SemdocWriteInterceptor {
|
public class AnnotateWriteInterceptor implements WriteInterceptor {
|
||||||
|
|
||||||
public static final String ANNOTATE_KEY = "annotate";
|
public static final String ANNOTATE_KEY = "annotate";
|
||||||
public static final String CUSTOM_PREFIX = "custom/";
|
public static final String CUSTOM_PREFIX = "custom/";
|
||||||
@@ -20,7 +20,7 @@ public class AnnotateWriteInterceptor implements SemdocWriteInterceptor {
|
|||||||
ObjectMapper objectMapper;
|
ObjectMapper objectMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uni<Void> interceptSemdocWrite(SemdocWriteContext context) {
|
public Uni<Void> interceptSemdocWrite(WriteContext context) {
|
||||||
final Map<String, Object> directives = context.getDirectives();
|
final Map<String, Object> directives = context.getDirectives();
|
||||||
|
|
||||||
if (directives.containsKey(ANNOTATE_KEY)) {
|
if (directives.containsKey(ANNOTATE_KEY)) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package fr.codeanddata.semrack.core.interceptors;
|
package fr.codeanddata.semrack.core.interceptors;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.SemdocWriteInterceptor;
|
import fr.codeanddata.semrack.core.WriteInterceptor;
|
||||||
import fr.codeanddata.semrack.core.models.SemdocWriteContext;
|
import fr.codeanddata.semrack.core.models.WriteContext;
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
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;
|
||||||
@@ -12,7 +12,7 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class PublishWriteInterceptor implements SemdocWriteInterceptor {
|
public class PublishWriteInterceptor implements WriteInterceptor {
|
||||||
|
|
||||||
final String PUBLICATION_PREFIX = "publication/";
|
final String PUBLICATION_PREFIX = "publication/";
|
||||||
|
|
||||||
@@ -20,18 +20,18 @@ public class PublishWriteInterceptor implements SemdocWriteInterceptor {
|
|||||||
UIDGenerator uidGenerator;
|
UIDGenerator uidGenerator;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uni<Void> interceptSemdocWrite(SemdocWriteContext context) {
|
public Uni<Void> interceptSemdocWrite(WriteContext context) {
|
||||||
final Map<String, Object> directives = context.getDirectives();
|
final Map<String, Object> directives = context.getDirectives();
|
||||||
final SemrackDocument current = context.getCurrentDocument();
|
final Document current = context.getCurrentDocument();
|
||||||
final SemrackDocument next = context.getNextDocument();
|
final Document next = context.getNextDocument();
|
||||||
|
|
||||||
int version = (int) Optional.ofNullable(current)
|
int version = (int) Optional.ofNullable(current)
|
||||||
.map(SemrackDocument::getAnnotations)
|
.map(Document::getAnnotations)
|
||||||
.map(annotations -> annotations.getOrDefault(PUBLICATION_PREFIX + "version", 0))
|
.map(annotations -> annotations.getOrDefault(PUBLICATION_PREFIX + "version", 0))
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
|
|
||||||
next.getAnnotations().put(PUBLICATION_PREFIX + "reference", Optional.ofNullable(current)
|
next.getAnnotations().put(PUBLICATION_PREFIX + "reference", Optional.ofNullable(current)
|
||||||
.map(SemrackDocument::getAnnotations)
|
.map(Document::getAnnotations)
|
||||||
.map(annotations -> annotations.getOrDefault(PUBLICATION_PREFIX + "reference", next.getUid()))
|
.map(annotations -> annotations.getOrDefault(PUBLICATION_PREFIX + "reference", next.getUid()))
|
||||||
.orElse(next.getUid()));
|
.orElse(next.getUid()));
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package fr.codeanddata.semrack.core.mappers;
|
package fr.codeanddata.semrack.core.mappers;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
import fr.codeanddata.semrack.core.models.PushDocumentRequest;
|
import fr.codeanddata.semrack.core.models.PushDocument;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
import org.mapstruct.MappingConstants;
|
import org.mapstruct.MappingConstants;
|
||||||
|
|
||||||
@Mapper(componentModel = MappingConstants.ComponentModel.CDI)
|
@Mapper(componentModel = MappingConstants.ComponentModel.CDI)
|
||||||
public abstract class SemrackDocumentMapper {
|
public abstract class DocumentMapper {
|
||||||
@Mapping(source = "uid", target = "uid")
|
@Mapping(source = "uid", target = "uid")
|
||||||
@Mapping(source = "metadata", target = "metadata")
|
@Mapping(source = "metadata", target = "metadata")
|
||||||
abstract SemrackDocument toDocument(PushDocumentRequest document);
|
abstract Document toDocument(PushDocument document);
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ import java.util.Map;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@RegisterForReflection(serialization = true)
|
@RegisterForReflection(serialization = true)
|
||||||
public class SemrackDocument {
|
public class Document {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* L'identifiant unique du document.
|
* L'identifiant unique du document.
|
||||||
@@ -29,4 +29,8 @@ public class SemrackDocument {
|
|||||||
*/
|
*/
|
||||||
Map<String, Object> metadata;
|
Map<String, Object> metadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
Map<String, Object> fields;
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ import lombok.*;
|
|||||||
@Builder
|
@Builder
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class SemrackPagination {
|
public class Pagination {
|
||||||
Integer page;
|
Integer page;
|
||||||
Integer size;
|
Integer size;
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ import java.util.Map;
|
|||||||
@Builder
|
@Builder
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class PushDocumentRequest {
|
public class PushDocument {
|
||||||
/**
|
/**
|
||||||
* L'identifiant unique du document.
|
* L'identifiant unique du document.
|
||||||
*/
|
*/
|
||||||
@@ -2,15 +2,11 @@ package fr.codeanddata.semrack.core.models;
|
|||||||
|
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class SemdocReadContext {
|
public class ReadContext {
|
||||||
|
Document currentDocument;
|
||||||
SemrackDocument currentDocument;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,9 +10,11 @@ import java.util.Map;
|
|||||||
@Builder
|
@Builder
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class SearchRequest {
|
public class Search {
|
||||||
Map<String, Object> filter;
|
Map<String, Object> filter;
|
||||||
List<SemrackSort> sort;
|
List<Sort> sort;
|
||||||
SemrackPagination paginate;
|
Pagination paginate;
|
||||||
List<String> fields;
|
Boolean annotations;
|
||||||
|
Boolean metadata;
|
||||||
|
Map<String, String> fields;
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ import java.util.Map;
|
|||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class SemdocSearchContext {
|
public class SearchContext {
|
||||||
|
|
||||||
Map<String, Object> search;
|
Map<String, Object> search;
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,6 @@ import java.util.List;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class SearchResult {
|
public class SearchResult {
|
||||||
List<SemrackDocument> documents;
|
List<Document> documents;
|
||||||
PaginationInfo pagination;
|
PaginationInfo pagination;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package fr.codeanddata.semrack.core.models;
|
package fr.codeanddata.semrack.core.models;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.enums.SemrackSortDirection;
|
import fr.codeanddata.semrack.core.enums.SortDirection;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@@ -8,7 +8,7 @@ import lombok.*;
|
|||||||
@Builder
|
@Builder
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class SemrackSort {
|
public class Sort {
|
||||||
String field;
|
String field;
|
||||||
SemrackSortDirection direction;
|
SortDirection direction;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package fr.codeanddata.semrack.core.models;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class StorageGet {
|
||||||
|
Boolean metadataSource;
|
||||||
|
Boolean annotationsSource;
|
||||||
|
Map<String, String> fields;
|
||||||
|
}
|
||||||
@@ -9,19 +9,19 @@ import java.util.Map;
|
|||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class SemdocWriteContext {
|
public class WriteContext {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Previous stored document
|
* Previous stored document
|
||||||
*/
|
*/
|
||||||
SemrackDocument currentDocument;
|
Document currentDocument;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Document to be store. It initialized with :
|
* Document to be store. It initialized with :
|
||||||
* - the currentDocument uid if exists, or a new one
|
* - the currentDocument uid if exists, or a new one
|
||||||
* - the metadata to be persisted
|
* - the metadata to be persisted
|
||||||
*/
|
*/
|
||||||
SemrackDocument nextDocument;
|
Document nextDocument;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The directives to be applied
|
* The directives to be applied
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package fr.codeanddata.semrack.core.repositories;
|
package fr.codeanddata.semrack.core.repositories;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.SemdocWriteInterceptor;
|
import fr.codeanddata.semrack.core.WriteInterceptor;
|
||||||
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.storages.StorageProxy;
|
||||||
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;
|
||||||
@@ -16,35 +16,43 @@ import java.util.Optional;
|
|||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class SemdocRepository {
|
public class DocumentRepository {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SemdocStorageProxy semdocStorage;
|
StorageProxy semdocStorage;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@Any
|
@Any
|
||||||
Instance<SemdocWriteInterceptor> writeInterceptors;
|
Instance<WriteInterceptor> writeInterceptors;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
UIDGenerator uidGenerator;
|
UIDGenerator uidGenerator;
|
||||||
|
|
||||||
public Uni<SearchResult> searchDocument(SearchRequest query) {
|
public Uni<SearchResult> searchDocument(Search query) {
|
||||||
return semdocStorage.searchDocument(query);
|
return semdocStorage.searchDocument(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Uni<SemrackDocument> get(String documentId) {
|
public Uni<Document> get(String documentId) {
|
||||||
return semdocStorage.readDocument(documentId);
|
return get(documentId, StorageGet.builder().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Uni<SemrackDocument> pushDocument(PushDocumentRequest pushDocument) {
|
public Uni<Document> get(String documentId, StorageGet request) {
|
||||||
return Optional.ofNullable(pushDocument.getUid()).map(semdocStorage::readDocument).orElse(Uni.createFrom().nullItem())
|
return semdocStorage.readDocument(documentId, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Document> pushDocument(PushDocument pushDocument) {
|
||||||
|
return Optional.ofNullable(pushDocument.getUid()).map(uid -> semdocStorage.readDocument(uid, StorageGet.builder()
|
||||||
|
.annotationsSource(true)
|
||||||
|
.metadataSource(true)
|
||||||
|
.build()))
|
||||||
|
.orElse(Uni.createFrom().nullItem())
|
||||||
.chain(currentDocument -> {
|
.chain(currentDocument -> {
|
||||||
|
|
||||||
final SemdocWriteContext context = SemdocWriteContext.builder()
|
final WriteContext context = WriteContext.builder()
|
||||||
.directives(pushDocument.getDirectives())
|
.directives(pushDocument.getDirectives())
|
||||||
.currentDocument(currentDocument)
|
.currentDocument(currentDocument)
|
||||||
.nextDocument(SemrackDocument.builder()
|
.nextDocument(Document.builder()
|
||||||
.uid(Optional.ofNullable(currentDocument).map(SemrackDocument::getUid).orElse(uidGenerator.apply(null)))
|
.uid(Optional.ofNullable(currentDocument).map(Document::getUid).orElse(uidGenerator.apply(null)))
|
||||||
.metadata(pushDocument.getMetadata())
|
.metadata(pushDocument.getMetadata())
|
||||||
.annotations(new HashMap<>())
|
.annotations(new HashMap<>())
|
||||||
.build())
|
.build())
|
||||||
@@ -57,7 +65,7 @@ public class SemdocRepository {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Uni<SemrackDocument> getOrCreate(SearchRequest query, PushDocumentRequest toCreate) {
|
public Uni<Document> getOrCreate(Search query, PushDocument toCreate) {
|
||||||
return searchDocument(query)
|
return searchDocument(query)
|
||||||
.chain(searchResult -> {
|
.chain(searchResult -> {
|
||||||
if (searchResult.getDocuments().isEmpty()) {
|
if (searchResult.getDocuments().isEmpty()) {
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package fr.codeanddata.semrack.core.services;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.Index;
|
||||||
|
import fr.codeanddata.semrack.core.models.IndexSearchResult;
|
||||||
|
import fr.codeanddata.semrack.core.models.Search;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class IndexService {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Index index;
|
||||||
|
|
||||||
|
public Uni<Void> index(String documentId) {
|
||||||
|
return index.index(documentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<IndexSearchResult> search(Search search) {
|
||||||
|
return index.search(search);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Long> count(Search request) {
|
||||||
|
return index.count(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Boolean> exist(Search query) {
|
||||||
|
return index.exist(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Void> clear(String documentId) {
|
||||||
|
return index.clear(documentId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package fr.codeanddata.semrack.core.services;
|
package fr.codeanddata.semrack.core.services;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.SemrackLookupExpression;
|
import fr.codeanddata.semrack.core.LookupExpression;
|
||||||
import io.quarkus.runtime.StartupEvent;
|
import io.quarkus.runtime.StartupEvent;
|
||||||
import io.smallrye.common.annotation.Identifier;
|
import io.smallrye.common.annotation.Identifier;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
@@ -14,13 +14,13 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class SemrackLookupService {
|
public class LookupService {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@Any
|
@Any
|
||||||
Instance<SemrackLookupExpression<?>> operators;
|
Instance<LookupExpression<?>> operators;
|
||||||
|
|
||||||
final Map<String, SemrackLookupExpression<?>> operatorsIndex = new HashMap<>();
|
final Map<String, LookupExpression<?>> operatorsIndex = new HashMap<>();
|
||||||
|
|
||||||
void startup(@Observes StartupEvent event) {
|
void startup(@Observes StartupEvent event) {
|
||||||
operators.stream()
|
operators.stream()
|
||||||
@@ -42,7 +42,7 @@ public class SemrackLookupService {
|
|||||||
if (! operatorsIndex.containsKey(lookupKey)) {
|
if (! operatorsIndex.containsKey(lookupKey)) {
|
||||||
throw new RuntimeException("Unknown lookup expression '" + lookupKey + "'");
|
throw new RuntimeException("Unknown lookup expression '" + lookupKey + "'");
|
||||||
} else {
|
} else {
|
||||||
final SemrackLookupExpression<?> lookup = operatorsIndex.get(lookupKey);
|
final LookupExpression<?> lookup = operatorsIndex.get(lookupKey);
|
||||||
final Object lookupParams = lookupExpression.get(lookupKey);
|
final Object lookupParams = lookupExpression.get(lookupKey);
|
||||||
return lookup.apply(lookupParams);
|
return lookup.apply(lookupParams);
|
||||||
}
|
}
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
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 SemdocStorageProxy {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
SemdocStorage storage;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
SemdocIndex index;
|
|
||||||
|
|
||||||
public Uni<SemrackDocument> readDocument(String uid) {
|
|
||||||
return storage.get(uid);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Uni<SearchResult> searchDocument(SearchRequest query) {
|
|
||||||
return index.search(query)
|
|
||||||
.chain(searchResult -> storage.get(searchResult.getUids())
|
|
||||||
.map(documents -> SearchResult.builder()
|
|
||||||
.documents(documents)
|
|
||||||
.pagination(searchResult.getPagination())
|
|
||||||
.build()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Uni<Long> countDocuments(SearchRequest request) {
|
|
||||||
return index.count(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Uni<Boolean> documentsExist(SearchRequest query) {
|
|
||||||
return index.exist(query);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Uni<SemrackDocument> storeDocument(SemrackDocument document) {
|
|
||||||
return storage.storeDocument(document)
|
|
||||||
.call(x -> index.index(x.getUid()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package fr.codeanddata.semrack.core.storages;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.Index;
|
||||||
|
import fr.codeanddata.semrack.core.Storage;
|
||||||
|
import fr.codeanddata.semrack.core.models.Search;
|
||||||
|
import fr.codeanddata.semrack.core.models.SearchResult;
|
||||||
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
|
import fr.codeanddata.semrack.core.models.StorageGet;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class StorageProxy {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Storage storage;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Index index;
|
||||||
|
|
||||||
|
public Uni<Document> readDocument(String uid) {
|
||||||
|
return readDocument(uid, StorageGet.builder().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Document> readDocument(String uid, StorageGet request) {
|
||||||
|
return storage.get(uid, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<SearchResult> searchDocument(Search query) {
|
||||||
|
return index.search(query)
|
||||||
|
.chain(searchResult -> storage.get(searchResult.getUids(), StorageGet.builder()
|
||||||
|
.annotationsSource(query.getAnnotations())
|
||||||
|
.metadataSource(query.getMetadata())
|
||||||
|
.fields(query.getFields())
|
||||||
|
.build())
|
||||||
|
.map(documents -> SearchResult.builder()
|
||||||
|
.documents(documents)
|
||||||
|
.pagination(searchResult.getPagination())
|
||||||
|
.build()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Long> countDocuments(Search request) {
|
||||||
|
return index.count(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Boolean> documentsExist(Search query) {
|
||||||
|
return index.exist(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Document> storeDocument(Document document) {
|
||||||
|
return storage.storeDocument(document)
|
||||||
|
.call(x -> index.index(x.getUid()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ import jakarta.inject.Inject;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class SemdocUtils {
|
public class DocumentUtils {
|
||||||
@Inject
|
@Inject
|
||||||
ObjectMapper objectMapper;
|
ObjectMapper objectMapper;
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package fr.codeanddata.semrack.core.utils;
|
package fr.codeanddata.semrack.core.utils;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
|
|
||||||
@@ -10,10 +10,10 @@ import java.util.UUID;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class UIDGenerator implements Function<SemrackDocument, String> {
|
public class UIDGenerator implements Function<Document, String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apply(SemrackDocument semrackDocument) {
|
public String apply(Document document) {
|
||||||
final String uuid = UUID.randomUUID().toString();
|
final String uuid = UUID.randomUUID().toString();
|
||||||
final LocalDateTime now = LocalDateTime.now();
|
final LocalDateTime now = LocalDateTime.now();
|
||||||
final long timestamp = now.toInstant(ZoneOffset.UTC).toEpochMilli();
|
final long timestamp = now.toInstant(ZoneOffset.UTC).toEpochMilli();
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package fr.codeanddata.semrack.core;
|
package fr.codeanddata.semrack.core;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.IndexSearchResult;
|
import fr.codeanddata.semrack.core.models.IndexSearchResult;
|
||||||
import fr.codeanddata.semrack.core.models.SearchRequest;
|
import fr.codeanddata.semrack.core.models.Search;
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
|
import fr.codeanddata.semrack.core.models.StorageGet;
|
||||||
import io.quarkus.arc.DefaultBean;
|
import io.quarkus.arc.DefaultBean;
|
||||||
import io.smallrye.mutiny.Uni;
|
import io.smallrye.mutiny.Uni;
|
||||||
import jakarta.annotation.Priority;
|
import jakarta.annotation.Priority;
|
||||||
@@ -16,20 +17,20 @@ public class MockProducer {
|
|||||||
@Produces
|
@Produces
|
||||||
@DefaultBean
|
@DefaultBean
|
||||||
@Priority(100)
|
@Priority(100)
|
||||||
SemdocStorage produceStorage() {
|
Storage produceStorage() {
|
||||||
return new SemdocStorage() {
|
return new Storage() {
|
||||||
@Override
|
@Override
|
||||||
public Uni<SemrackDocument> get(String uid) {
|
public Uni<Document> get(String uid, StorageGet request) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uni<List<SemrackDocument>> get(List<String> uids) {
|
public Uni<List<Document>> get(List<String> uids, StorageGet request) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uni<SemrackDocument> storeDocument(SemrackDocument document) {
|
public Uni<Document> storeDocument(Document document) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -38,15 +39,15 @@ public class MockProducer {
|
|||||||
@Produces
|
@Produces
|
||||||
@DefaultBean
|
@DefaultBean
|
||||||
@Priority(100)
|
@Priority(100)
|
||||||
SemdocIndex produceIndex() {
|
Index produceIndex() {
|
||||||
return new SemdocIndex() {
|
return new Index() {
|
||||||
@Override
|
@Override
|
||||||
public Uni<Long> count(SearchRequest request) {
|
public Uni<Long> count(Search request) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uni<Boolean> exist(SearchRequest query) {
|
public Uni<Boolean> exist(Search query) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ public class MockProducer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uni<IndexSearchResult> search(SearchRequest searchRequest) {
|
public Uni<IndexSearchResult> search(Search search) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.index.postgres.it;
|
|
||||||
|
|
||||||
import io.quarkus.test.junit.QuarkusIntegrationTest;
|
|
||||||
|
|
||||||
@QuarkusIntegrationTest
|
|
||||||
public class SemrackIndexPostgresResourceIT extends SemrackIndexPostgresResourceTest {
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
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"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package fr.codeanddata.semrack.index.postgres;
|
package fr.codeanddata.semrack.index.postgres;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.SemdocIndex;
|
import fr.codeanddata.semrack.core.Index;
|
||||||
import fr.codeanddata.semrack.core.SemdocStorage;
|
import fr.codeanddata.semrack.core.Storage;
|
||||||
import fr.codeanddata.semrack.core.models.*;
|
import fr.codeanddata.semrack.core.models.*;
|
||||||
import fr.codeanddata.semrack.core.services.SemrackLookupService;
|
import fr.codeanddata.semrack.core.services.LookupService;
|
||||||
import fr.codeanddata.semrack.core.utils.Traverser;
|
import fr.codeanddata.semrack.core.utils.Traverser;
|
||||||
import fr.codeanddata.semrack.index.postgres.dtos.IndexEntry;
|
import fr.codeanddata.semrack.index.postgres.dtos.IndexEntry;
|
||||||
import fr.codeanddata.semrack.index.postgres.entities.SemrackIndexEntity;
|
import fr.codeanddata.semrack.index.postgres.entities.SemrackIndexEntity;
|
||||||
@@ -19,23 +19,23 @@ import org.hibernate.query.Page;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class SemdocJpaIndex implements SemdocIndex, PanacheRepository<SemrackIndexEntity> {
|
public class JpaIndex implements Index, PanacheRepository<SemrackIndexEntity> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
Traverser traverser;
|
Traverser traverser;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SemrackLookupService lookupService;
|
LookupService lookupService;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SemdocStorage storage;
|
Storage storage;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SemrackIndexRepository indexRepository;
|
SemrackIndexRepository indexRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@WithSession
|
@WithSession
|
||||||
public Uni<Long> count(SearchRequest request) {
|
public Uni<Long> count(Search request) {
|
||||||
final StringBuilder query = new StringBuilder("SELECT DISTINCT count(uid) FROM semrack_index");
|
final StringBuilder query = new StringBuilder("SELECT DISTINCT count(uid) FROM semrack_index");
|
||||||
|
|
||||||
final String lookup = lookupService.lookup(request.getFilter());
|
final String lookup = lookupService.lookup(request.getFilter());
|
||||||
@@ -47,14 +47,16 @@ public class SemdocJpaIndex implements SemdocIndex, PanacheRepository<SemrackInd
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uni<Boolean> exist(SearchRequest query) {
|
public Uni<Boolean> exist(Search query) {
|
||||||
return count(query).map(count -> count > 0);
|
return count(query).map(count -> count > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@WithTransaction
|
@WithTransaction
|
||||||
public Uni<Void> index(String documentId) {
|
public Uni<Void> index(String documentId) {
|
||||||
return storage.get(documentId)
|
return storage.get(documentId, StorageGet.builder()
|
||||||
|
.metadataSource(true)
|
||||||
|
.build())
|
||||||
.call(document -> clear(documentId))
|
.call(document -> clear(documentId))
|
||||||
.call(document -> {
|
.call(document -> {
|
||||||
final Map<String, IndexEntry> indexes = new HashMap<>();
|
final Map<String, IndexEntry> indexes = new HashMap<>();
|
||||||
@@ -78,7 +80,7 @@ public class SemdocJpaIndex implements SemdocIndex, PanacheRepository<SemrackInd
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@WithSession
|
@WithSession
|
||||||
public Uni<IndexSearchResult> search(SearchRequest request) {
|
public Uni<IndexSearchResult> search(Search request) {
|
||||||
final IndexSearchResult result = new IndexSearchResult();
|
final IndexSearchResult result = new IndexSearchResult();
|
||||||
final StringBuilder query = new StringBuilder("SELECT DISTINCT uid FROM semrack_index");
|
final StringBuilder query = new StringBuilder("SELECT DISTINCT uid FROM semrack_index");
|
||||||
|
|
||||||
@@ -105,11 +107,11 @@ public class SemdocJpaIndex implements SemdocIndex, PanacheRepository<SemrackInd
|
|||||||
.replaceWithVoid();
|
.replaceWithVoid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Uni<PaginationInfo> queryPaginationInfo(SearchRequest query) {
|
Uni<PaginationInfo> queryPaginationInfo(Search query) {
|
||||||
return count(query)
|
return count(query)
|
||||||
.map(count -> PaginationInfo.builder()
|
.map(count -> PaginationInfo.builder()
|
||||||
.page(Optional.ofNullable(query.getPaginate()).map(SemrackPagination::getPage).orElse(0))
|
.page(Optional.ofNullable(query.getPaginate()).map(Pagination::getPage).orElse(0))
|
||||||
.size(Optional.ofNullable(query.getPaginate()).map(SemrackPagination::getSize).orElse(10))
|
.size(Optional.ofNullable(query.getPaginate()).map(Pagination::getSize).orElse(10))
|
||||||
.total(count)
|
.total(count)
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package fr.codeanddata.semrack.index.postgres.operators;
|
package fr.codeanddata.semrack.index.postgres.operators;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.services.SemrackLookupService;
|
import fr.codeanddata.semrack.core.services.LookupService;
|
||||||
import io.smallrye.common.annotation.Identifier;
|
import io.smallrye.common.annotation.Identifier;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
@@ -10,10 +10,10 @@ import java.util.Map;
|
|||||||
|
|
||||||
@Identifier("and")
|
@Identifier("and")
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class AndLookup implements SemrackJpaLookupExpression<List<Map<String, Object>>> {
|
public class AndLookup implements JpaLookupExpression<List<Map<String, Object>>> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SemrackLookupService lookupService;
|
LookupService lookupService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apply(Object expressions) {
|
public String apply(Object expressions) {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import jakarta.inject.Inject;
|
|||||||
|
|
||||||
@Identifier("equal")
|
@Identifier("equal")
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class EqualLookup implements SemrackJpaLookupExpression<EqualLookupParams> {
|
public class EqualLookup implements JpaLookupExpression<EqualLookupParams> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ObjectMapper objectMapper;
|
ObjectMapper objectMapper;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import java.util.List;
|
|||||||
|
|
||||||
@Identifier("in")
|
@Identifier("in")
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class InLookup implements SemrackJpaLookupExpression<InLookupParams> {
|
public class InLookup implements JpaLookupExpression<InLookupParams> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ObjectMapper objectMapper;
|
ObjectMapper objectMapper;
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package fr.codeanddata.semrack.index.postgres.operators;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.LookupExpression;
|
||||||
|
|
||||||
|
public interface JpaLookupExpression<T> extends LookupExpression<T> {
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package fr.codeanddata.semrack.index.postgres.operators;
|
package fr.codeanddata.semrack.index.postgres.operators;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.services.SemrackLookupService;
|
import fr.codeanddata.semrack.core.services.LookupService;
|
||||||
import io.smallrye.common.annotation.Identifier;
|
import io.smallrye.common.annotation.Identifier;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
@@ -10,10 +10,10 @@ import java.util.Map;
|
|||||||
|
|
||||||
@Identifier("or")
|
@Identifier("or")
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class OrLookup implements SemrackJpaLookupExpression<List<Map<String, Object>>> {
|
public class OrLookup implements JpaLookupExpression<List<Map<String, Object>>> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SemrackLookupService lookupService;
|
LookupService lookupService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apply(Object expressions) {
|
public String apply(Object expressions) {
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.index.postgres.operators;
|
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.SemrackLookupExpression;
|
|
||||||
|
|
||||||
public interface SemrackJpaLookupExpression<T> extends SemrackLookupExpression<T> {
|
|
||||||
}
|
|
||||||
@@ -45,6 +45,16 @@
|
|||||||
<artifactId>quarkus-junit5-internal</artifactId>
|
<artifactId>quarkus-junit5-internal</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.quarkus</groupId>
|
||||||
|
<artifactId>quarkus-junit5</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.quarkus</groupId>
|
||||||
|
<artifactId>quarkus-test-vertx</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.codeanddata.semrack</groupId>
|
<groupId>fr.codeanddata.semrack</groupId>
|
||||||
<artifactId>semrack-core-testing</artifactId>
|
<artifactId>semrack-core-testing</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package fr.codeanddata.semrack.storage.postgres.storage;
|
||||||
|
|
||||||
|
import fr.codeanddata.semrack.core.models.StorageGet;
|
||||||
|
import io.quarkus.hibernate.reactive.panache.Panache;
|
||||||
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
|
import io.quarkus.test.vertx.RunOnVertxContext;
|
||||||
|
import io.quarkus.test.vertx.UniAsserter;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@QuarkusTest
|
||||||
|
public class JpaStorageTest {
|
||||||
|
@Inject
|
||||||
|
JpaStorage jpaStorage;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RunOnVertxContext
|
||||||
|
void testJpaStorageGetDefault(UniAsserter asserter) {
|
||||||
|
asserter.assertThat(
|
||||||
|
() -> Panache.withSession(() -> jpaStorage.get("doc-001", StorageGet.builder().build())),
|
||||||
|
doc -> {
|
||||||
|
assert doc != null;
|
||||||
|
assert "doc-001".equals(doc.getUid());
|
||||||
|
assert doc.getAnnotations() == null;
|
||||||
|
assert doc.getMetadata() == null;
|
||||||
|
assert doc.getFields() == null;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RunOnVertxContext
|
||||||
|
void testJpaStorageGetWithAnnotationsAndMetadata(UniAsserter asserter) {
|
||||||
|
asserter.assertThat(
|
||||||
|
() -> Panache.withSession(() -> jpaStorage.get("doc-001", StorageGet.builder()
|
||||||
|
.annotationsSource(true)
|
||||||
|
.metadataSource(true)
|
||||||
|
.build())),
|
||||||
|
doc -> {
|
||||||
|
assert doc != null;
|
||||||
|
assert doc.getAnnotations() != null;
|
||||||
|
assert doc.getMetadata() != null;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RunOnVertxContext
|
||||||
|
void testJpaStorageGetWithFields(UniAsserter asserter) {
|
||||||
|
asserter.assertThat(
|
||||||
|
() -> Panache.withSession(() -> jpaStorage.get("doc-001", StorageGet.builder()
|
||||||
|
.fields(Map.of(
|
||||||
|
"title", "title",
|
||||||
|
"content", "content",
|
||||||
|
"sku", "product.sku"
|
||||||
|
))
|
||||||
|
.build())),
|
||||||
|
doc -> {
|
||||||
|
assert doc != null;
|
||||||
|
assert doc.getFields() != null;
|
||||||
|
assert doc.getFields().containsKey("title");
|
||||||
|
assert "Test Document".equals(doc.getFields().get("title"));
|
||||||
|
assert doc.getFields().containsKey("content");
|
||||||
|
assert "Hello World".equals(doc.getFields().get("content"));
|
||||||
|
assert doc.getFields().containsKey("sku");
|
||||||
|
assert "001".equals(doc.getFields().get("sku"));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.storage.postgres.test;
|
|
||||||
|
|
||||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
|
||||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
|
||||||
|
|
||||||
import io.quarkus.test.QuarkusDevModeTest;
|
|
||||||
|
|
||||||
public class SemrackStoragePostgresDevModeTest {
|
|
||||||
|
|
||||||
// Start hot reload (DevMode) test with your extension loaded
|
|
||||||
@RegisterExtension
|
|
||||||
static final QuarkusDevModeTest devModeTest = new QuarkusDevModeTest()
|
|
||||||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void writeYourOwnDevModeTest() {
|
|
||||||
// Write your dev mode tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-hot-reload for more information
|
|
||||||
Assertions.assertTrue(true, "Add dev mode assertions to " + getClass().getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.storage.postgres.test;
|
|
||||||
|
|
||||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
|
||||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
|
||||||
|
|
||||||
import io.quarkus.test.QuarkusUnitTest;
|
|
||||||
|
|
||||||
public class SemrackStoragePostgresTest {
|
|
||||||
|
|
||||||
// Start unit test with your extension loaded
|
|
||||||
@RegisterExtension
|
|
||||||
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
|
|
||||||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void writeYourOwnUnitTest() {
|
|
||||||
// Write your unit tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-extensions for more information
|
|
||||||
Assertions.assertTrue(true, "Add some assertions to " + getClass().getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
quarkus.flyway.enabled=false
|
||||||
|
quarkus.hibernate-orm.schema-management.strategy=drop-and-create
|
||||||
|
quarkus.hibernate-orm.sql-load-script=import.sql
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
INSERT INTO semrack_document (id, uid, document)
|
||||||
|
VALUES (nextval('semrack_document_SEQ'), 'doc-001',
|
||||||
|
'{"uid":"doc-001","annotations":{"category":"test"},"metadata":{"product":{"sku":"001"},"title":"Test Document","content":"Hello World"}}'),
|
||||||
|
(nextval('semrack_document_SEQ'), 'doc-002',
|
||||||
|
'{"uid":"doc-002","annotations":{"category":"test"},"metadata":{"product":{"sku":"002"},"title":"Test Document 2","content":"Hello World 2"}}');
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package fr.codeanddata.semrack.storage.postgres.entities;
|
package fr.codeanddata.semrack.storage.postgres.entities;
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
import io.quarkus.hibernate.reactive.panache.PanacheEntity;
|
import io.quarkus.hibernate.reactive.panache.PanacheEntity;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
@@ -23,5 +23,5 @@ public class SemrackDocumentEntity extends PanacheEntity implements Serializable
|
|||||||
String uid;
|
String uid;
|
||||||
|
|
||||||
@JdbcTypeCode(SqlTypes.JSON)
|
@JdbcTypeCode(SqlTypes.JSON)
|
||||||
SemrackDocument document;
|
Document document;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,148 @@
|
|||||||
|
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.Storage;
|
||||||
|
import fr.codeanddata.semrack.core.models.Document;
|
||||||
|
import fr.codeanddata.semrack.core.models.StorageGet;
|
||||||
|
import fr.codeanddata.semrack.core.utils.UIDGenerator;
|
||||||
|
import fr.codeanddata.semrack.storage.postgres.entities.SemrackDocumentEntity;
|
||||||
|
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.smallrye.mutiny.Uni;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class JpaStorage implements Storage, PanacheRepository<SemrackDocumentEntity> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
UIDGenerator generator;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@WithSession
|
||||||
|
public Uni<Document> get(String uid, StorageGet request) {
|
||||||
|
final List<String> fields = selectFields(request);
|
||||||
|
return getSession()
|
||||||
|
.chain(session -> session.createNativeQuery("select " + String.join(",", fields) + " from semrack_document where uid = ?1")
|
||||||
|
.setParameter(1, uid)
|
||||||
|
.getSingleResult())
|
||||||
|
.map(result -> mapDocument(result, request));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@WithSession
|
||||||
|
public Uni<List<Document>> get(List<String> uids, StorageGet request) {
|
||||||
|
final List<String> fields = selectFields(request);
|
||||||
|
return getSession()
|
||||||
|
.chain(session -> session.createNativeQuery("select " + String.join(",", fields) + " from semrack_document where uid in ?1")
|
||||||
|
.setParameter(1, uids)
|
||||||
|
.getResultList())
|
||||||
|
.map(results -> mapDocuments(results, request));
|
||||||
|
}
|
||||||
|
|
||||||
|
@WithTransaction
|
||||||
|
@Override
|
||||||
|
public Uni<Document> storeDocument(Document document) {
|
||||||
|
if (document.getUid() == null) {
|
||||||
|
return createDocument(document)
|
||||||
|
.call(this::flush);
|
||||||
|
} else {
|
||||||
|
return find("uid = ?1", document.getUid())
|
||||||
|
.count()
|
||||||
|
.chain(n -> n == 0 ? createDocument(document) : updateDocument(document))
|
||||||
|
.call(this::flush);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO err : handle existing document
|
||||||
|
@WithTransaction
|
||||||
|
Uni<Document> createDocument(Document document) {
|
||||||
|
final String uid = document.getUid() == null ? generator.apply(document) : document.getUid();
|
||||||
|
document.setUid(uid);
|
||||||
|
final SemrackDocumentEntity entity = SemrackDocumentEntity.builder()
|
||||||
|
.uid(uid)
|
||||||
|
.document(document)
|
||||||
|
.build();
|
||||||
|
return persist(entity).map(SemrackDocumentEntity::getDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO err : document not exists
|
||||||
|
@WithTransaction
|
||||||
|
Uni<Document> updateDocument(Document document) {
|
||||||
|
return update("uid = ?1, document = ?2 WHERE uid = ?1", document.getUid(), document)
|
||||||
|
.map(x -> document);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> selectFields(StorageGet request) {
|
||||||
|
final List<String> fields = new ArrayList<>(List.of(
|
||||||
|
"uid as _uid",
|
||||||
|
(Optional.ofNullable(request.getAnnotationsSource()).orElse(false) ? "document->'annotations'" : "null") + " as _annotations",
|
||||||
|
(Optional.ofNullable(request.getMetadataSource()).orElse(false) ? "document->'metadata'" : "null") + " as _metadata"
|
||||||
|
));
|
||||||
|
|
||||||
|
if (request.getFields() != null) {
|
||||||
|
int i = 0;
|
||||||
|
for (String field : request.getFields().values()) {
|
||||||
|
fields.add("jsonb_path_query(document, '$.metadata." + field + "') as field_" + i++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
Document mapDocument(Object result, StorageGet request) {
|
||||||
|
final Map<String, Integer> fieldMap = buildFieldMap(request);
|
||||||
|
final List<Object> resultList = objectMapper.convertValue(result, new TypeReference<>() {
|
||||||
|
});
|
||||||
|
final Object oAnnotations = resultList.get(fieldMap.get("_annotations"));
|
||||||
|
final Object oMetadata = resultList.get(fieldMap.get("_metadata"));
|
||||||
|
final Map<String, Object> annotations = oAnnotations == null ? null : objectMapper.convertValue(oAnnotations, new TypeReference<>() {
|
||||||
|
});
|
||||||
|
final Map<String, Object> metadata = oMetadata == null ? null : objectMapper.convertValue(oMetadata, new TypeReference<>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
final Map<String, Object> fields = new HashMap<>();
|
||||||
|
if (request.getFields() != null) {
|
||||||
|
for (final String field : request.getFields().keySet()) {
|
||||||
|
fields.put(field, resultList.get(fieldMap.get(field)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Document.builder()
|
||||||
|
.uid((String) resultList.get(fieldMap.get("_uid")))
|
||||||
|
.annotations(annotations)
|
||||||
|
.metadata(metadata)
|
||||||
|
.fields(fields.isEmpty() ? null : fields)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Document> mapDocuments(List<Object> results, StorageGet request) {
|
||||||
|
return results.stream().map(doc -> mapDocument(doc, request)).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Integer> buildFieldMap(StorageGet request) {
|
||||||
|
final Map<String, Integer> fieldsMap = new HashMap<>(Map.of(
|
||||||
|
"_uid", 0,
|
||||||
|
"_annotations", 1,
|
||||||
|
"_metadata", 2
|
||||||
|
));
|
||||||
|
|
||||||
|
if (request.getFields() != null) {
|
||||||
|
Integer i = 3;
|
||||||
|
for (String field : request.getFields().keySet()) {
|
||||||
|
fieldsMap.put(field, i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fieldsMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,144 +0,0 @@
|
|||||||
package fr.codeanddata.semrack.storage.postgres.storage;
|
|
||||||
|
|
||||||
import fr.codeanddata.semrack.core.SemdocStorage;
|
|
||||||
import fr.codeanddata.semrack.core.models.SemrackDocument;
|
|
||||||
import fr.codeanddata.semrack.core.services.SemrackLookupService;
|
|
||||||
import fr.codeanddata.semrack.core.utils.UIDGenerator;
|
|
||||||
import fr.codeanddata.semrack.storage.postgres.entities.SemrackDocumentEntity;
|
|
||||||
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.smallrye.mutiny.Uni;
|
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
|
||||||
import jakarta.inject.Inject;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@ApplicationScoped
|
|
||||||
public class SemdocJpaStorage implements SemdocStorage, PanacheRepository<SemrackDocumentEntity> {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
UIDGenerator generator;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
SemrackLookupService lookupService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@WithSession
|
|
||||||
public Uni<SemrackDocument> get(String uid) {
|
|
||||||
return find("uid = ?1", uid)
|
|
||||||
.firstResult().map(d -> Optional.ofNullable(d).map(SemrackDocumentEntity::getDocument).orElse(null));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@WithSession
|
|
||||||
public Uni<List<SemrackDocument>> get(List<String> uids) {
|
|
||||||
return find("uid in ?1", uids).list()
|
|
||||||
.map(d -> d.stream().map(SemrackDocumentEntity::getDocument).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
@WithTransaction
|
|
||||||
@Override
|
|
||||||
public Uni<SemrackDocument> storeDocument(SemrackDocument document) {
|
|
||||||
if (document.getUid() == null) {
|
|
||||||
return createDocument(document);
|
|
||||||
} else {
|
|
||||||
return find("uid = ?1", document.getUid())
|
|
||||||
.count()
|
|
||||||
.chain(n -> n == 0 ? createDocument(document) : updateDocument(document));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO err : handle existing document
|
|
||||||
@WithTransaction
|
|
||||||
Uni<SemrackDocument> createDocument(SemrackDocument document) {
|
|
||||||
final String uid = document.getUid() == null ? generator.apply(document) : document.getUid();
|
|
||||||
document.setUid(uid);
|
|
||||||
final SemrackDocumentEntity entity = SemrackDocumentEntity.builder()
|
|
||||||
.uid(uid)
|
|
||||||
.document(document)
|
|
||||||
.build();
|
|
||||||
return persist(entity).map(SemrackDocumentEntity::getDocument);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO err : document not exists
|
|
||||||
@WithTransaction
|
|
||||||
Uni<SemrackDocument> updateDocument(SemrackDocument document) {
|
|
||||||
return update("uid = ?1, document = ?2 WHERE uid = ?1", document.getUid(), document)
|
|
||||||
.map(x -> document);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uni<SearchResult> search(SearchRequest request) {
|
|
||||||
// final String baseQuery = buildBaseQuery(request);
|
|
||||||
//
|
|
||||||
// final StringBuilder sorting = new StringBuilder();
|
|
||||||
// if (request.getSort() != null && !request.getSort().isEmpty()) {
|
|
||||||
// sorting.append(" ORDER BY ");
|
|
||||||
// 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();
|
|
||||||
// if (request.getPaginate() != null) {
|
|
||||||
// final SemrackPagination pagination = request.getPaginate();
|
|
||||||
// final Integer size = pagination.getSize() == null ? 200 : pagination.getSize();
|
|
||||||
// final Integer page = pagination.getPage() == null ? 0 : pagination.getPage();
|
|
||||||
//
|
|
||||||
// paginate.append(" LIMIT ").append(size).append(" OFFSET ").append(size * page);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return getSession()
|
|
||||||
// .chain(s -> {
|
|
||||||
// final SearchResult searchResult = SearchResult.builder().build();
|
|
||||||
// return countDocuments(request)
|
|
||||||
// .invoke(count -> searchResult.setPagination(PaginationInfo.builder()
|
|
||||||
// .page(Optional.ofNullable(request.getPaginate()).map(SemrackPagination::getPage).orElse(0))
|
|
||||||
// .size(Optional.ofNullable(request.getPaginate()).map(SemrackPagination::getSize).orElse(0))
|
|
||||||
// .total(count)
|
|
||||||
// .build()))
|
|
||||||
// .call(() -> s
|
|
||||||
// .createNativeQuery(baseQuery + sorting + paginate, Object.class).getResultList()
|
|
||||||
// .invoke(results -> searchResult.setDocuments(project(request.getFields(), results))))
|
|
||||||
// .map(count -> searchResult);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// String buildBaseQuery(SearchRequest request) {
|
|
||||||
// final String lookup = lookupService.lookup(request.getFilter());
|
|
||||||
// final String whereClause = lookup.isEmpty() ? "" : " WHERE " + lookup;
|
|
||||||
//
|
|
||||||
// if (request.getFields() == null || request.getFields().isEmpty()) {
|
|
||||||
// return "SELECT document FROM semrack_document" + whereClause;
|
|
||||||
// } else {
|
|
||||||
// return "SELECT " + toJsonbPathExtract(request.getFields()) + " FROM semrack_document" + whereClause;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// String toJsonbPathExtract(List<String> fields) {
|
|
||||||
// return String.join(",", fields.stream().map(field -> {
|
|
||||||
// final String serializedField = "'" + String.join("','", field.split("\\.")) + "'";
|
|
||||||
// return "jsonb_extract_path(document, " + serializedField + ")";
|
|
||||||
// }).toList());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// List<SemrackDocument> project(List<String> fields, Object results) {
|
|
||||||
// final List<?> projectedResults = objectMapper.convertValue(results, List.class);
|
|
||||||
// if (fields == null || fields.isEmpty()) {
|
|
||||||
// return projectedResults.stream().map(result -> objectMapper.convertValue(result, SemrackDocument.class)).toList();
|
|
||||||
// } else {
|
|
||||||
// return projectedResults.stream().map(result -> {
|
|
||||||
// final List<Object> row = fields.size() == 1 ? List.of(result) : objectMapper.convertValue(result, new TypeReference<>() {
|
|
||||||
// });
|
|
||||||
// final List<Object> rowMut = new ArrayList<>(row);
|
|
||||||
// final List<String> fieldCp = new ArrayList<>(fields);
|
|
||||||
// final Map<String, Object> document = new HashMap<>();
|
|
||||||
// while (!fieldCp.isEmpty() && !row.isEmpty()) {
|
|
||||||
// final String field = fieldCp.removeFirst();
|
|
||||||
// final Object value = rowMut.removeFirst();
|
|
||||||
// pathMapper.map(field, document, value);
|
|
||||||
// }
|
|
||||||
// return objectMapper.convertValue(document, SemrackDocument.class);
|
|
||||||
// }).toList();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
6
pom.xml
6
pom.xml
@@ -29,11 +29,11 @@
|
|||||||
|
|
||||||
<!-- Other properties -->
|
<!-- Other properties -->
|
||||||
<commons-codec.version>1.16.0</commons-codec.version>
|
<commons-codec.version>1.16.0</commons-codec.version>
|
||||||
|
<jsonpath.version>2.9.0</jsonpath.version>
|
||||||
<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>
|
<quarkus.version>3.31.3</quarkus.version>
|
||||||
<quarkus.version>3.29.4</quarkus.version>
|
<semrack.version>${project.version}</semrack.version>
|
||||||
<jsonpath.version>2.9.0</jsonpath.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|||||||
Reference in New Issue
Block a user