Search in sources :

Example 1 with EntityIndexDescriptor

use of org.kie.kogito.persistence.api.schema.EntityIndexDescriptor in project kogito-apps by kiegroup.

the class ProtoIndexParser method create.

@Override
public EntityIndexDescriptor create(Descriptor annotatedDescriptor, AnnotationElement.Annotation annotation) {
    String name = annotatedDescriptor.getFullName();
    List<IndexDescriptor> indexes = new ArrayList<>(annotatedDescriptor.getFields().size());
    List<AttributeDescriptor> fields = new ArrayList<>(annotatedDescriptor.getFields().size());
    for (FieldDescriptor fd : annotatedDescriptor.getFields()) {
        AnnotationElement.Annotation fieldAnnotation = fd.getAnnotations().get(FIELD_ANNOTATION);
        if (fieldAnnotation != null) {
            String fieldName = Optional.ofNullable((String) fieldAnnotation.getAttributeValue(FIELD_NAME_ATTRIBUTE).getValue()).filter(a -> !a.isEmpty()).orElseGet(fd::getName);
            boolean isIndexed = INDEX_YES.equals(fieldAnnotation.getAttributeValue(FIELD_INDEX_ATTRIBUTE).getValue());
            fields.add(createAttributeDescriptor(fd, fieldName));
            if (isIndexed) {
                indexes.add(new IndexDescriptor(fieldName, List.of(fieldName)));
            }
        }
    }
    return new EntityIndexDescriptor(name, indexes, fields);
}
Also used : FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) AnnotationMetadataCreator(org.infinispan.protostream.AnnotationMetadataCreator) Set(java.util.Set) Descriptor(org.infinispan.protostream.descriptors.Descriptor) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) Map(java.util.Map) Optional(java.util.Optional) Configuration(org.infinispan.protostream.config.Configuration) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) Collectors.toSet(java.util.stream.Collectors.toSet) ArrayList(java.util.ArrayList) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)

Example 2 with EntityIndexDescriptor

use of org.kie.kogito.persistence.api.schema.EntityIndexDescriptor in project kogito-apps by kiegroup.

the class TestUtils method getValidEntityIndexDescriptors.

static Map<String, EntityIndexDescriptor> getValidEntityIndexDescriptors(boolean includeUnindexedAttribute) {
    AttributeDescriptor flightNumber = new AttributeDescriptor("flightNumber", "string", true);
    EntityIndexDescriptor flightEntityIndexDescriptor = new EntityIndexDescriptor("org.acme.travels.travels.Flight", emptyList(), List.of(flightNumber));
    AttributeDescriptor hotelName = new AttributeDescriptor("name", "string", true);
    AttributeDescriptor hotelRoom = new AttributeDescriptor("room", "string", true);
    EntityIndexDescriptor hotelEntityIndexDescriptor = new EntityIndexDescriptor("org.acme.travels.travels.Hotel", emptyList(), includeUnindexedAttribute ? List.of(hotelName, hotelRoom) : List.of(hotelName));
    AttributeDescriptor flight = new AttributeDescriptor("flight", "Flight", false);
    AttributeDescriptor hotel = new AttributeDescriptor("hotel", "Hotel", false);
    AttributeDescriptor id = new AttributeDescriptor("id", "string", true);
    AttributeDescriptor metadata = new AttributeDescriptor("metadata", "string", true);
    IndexDescriptor idIndex = new IndexDescriptor("id", List.of("id"));
    EntityIndexDescriptor travelEntityIndexDescriptor = new EntityIndexDescriptor("org.acme.travels.travels.Travels", List.of(idIndex), List.of(flight, hotel, id, metadata));
    Map<String, EntityIndexDescriptor> entityIndexDescriptorMap = new HashMap<>();
    entityIndexDescriptorMap.put(flightEntityIndexDescriptor.getName(), flightEntityIndexDescriptor);
    entityIndexDescriptorMap.put(hotelEntityIndexDescriptor.getName(), hotelEntityIndexDescriptor);
    entityIndexDescriptorMap.put(travelEntityIndexDescriptor.getName(), travelEntityIndexDescriptor);
    return entityIndexDescriptorMap;
}
Also used : HashMap(java.util.HashMap) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor)

Example 3 with EntityIndexDescriptor

use of org.kie.kogito.persistence.api.schema.EntityIndexDescriptor in project kogito-apps by kiegroup.

the class IndexManagerIT method setup_all.

@BeforeAll
static void setup_all() {
    AttributeDescriptor flightNumber = new AttributeDescriptor("flightNumber", "string", true);
    IndexDescriptor flightNumberIndex = new IndexDescriptor("flightNumber", List.of("flightNumber"));
    flightEntityIndexDescriptor = new EntityIndexDescriptor("org.acme.travels.travels.Flight", List.of(flightNumberIndex), List.of(flightNumber));
    AttributeDescriptor hotelName = new AttributeDescriptor("name", "string", true);
    AttributeDescriptor hotelRoom = new AttributeDescriptor("room", "string", true);
    IndexDescriptor hotelNameIndex = new IndexDescriptor("name", List.of("name"));
    hotelEntityIndexDescriptor = new EntityIndexDescriptor("org.acme.travels.travels.Hotel", List.of(hotelNameIndex), List.of(hotelName, hotelRoom));
    AttributeDescriptor flight = new AttributeDescriptor("flight", "Flight", false);
    AttributeDescriptor hotel = new AttributeDescriptor("hotel", "org.acme.travels.travels.Hotel", false);
    AttributeDescriptor id = new AttributeDescriptor("id", "string", true);
    AttributeDescriptor metadata = new AttributeDescriptor("metadata", "string", true);
    IndexDescriptor flightIndex = new IndexDescriptor("flight", List.of("flight"));
    IndexDescriptor hotelIndex = new IndexDescriptor("hotel", List.of("hotel"));
    IndexDescriptor idIndex = new IndexDescriptor("id", List.of("id"));
    IndexDescriptor metadataIndex = new IndexDescriptor("metadata", List.of("metadata"));
    travelEntityIndexDescriptor = new EntityIndexDescriptor("org.acme.travels.travels.Travels", List.of(flightIndex, hotelIndex, idIndex, metadataIndex), List.of(flight, hotel, id, metadata));
    errorEntityIndexDescriptor = mockErrorIndexes();
}
Also used : AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with EntityIndexDescriptor

use of org.kie.kogito.persistence.api.schema.EntityIndexDescriptor in project kogito-apps by kiegroup.

the class IndexManagerIT method testOnSchemaRegisteredEvent.

@Test
void testOnSchemaRegisteredEvent() {
    Map<String, EntityIndexDescriptor> indexes = new HashMap<>();
    indexes.put("test", travelEntityIndexDescriptor);
    indexManager.getCollectionIndexMapping().put("test", travelEntityIndexDescriptor.getName());
    indexManager.onSchemaRegisteredEvent(new SchemaRegisteredEvent(new SchemaDescriptor("test", "test", indexes, new ProcessDescriptor("test", travelEntityIndexDescriptor.getName())), new SchemaType("test")));
    MongoCollection<Document> testCollection = indexManager.getCollection("test");
    collections.add(testCollection);
    Set<String> testIndexes = StreamSupport.stream(testCollection.listIndexes().spliterator(), false).map(document -> document.getString(INDEX_NAME_FIELD)).filter(name -> !DEFAULT_INDEX.equals(name)).collect(toSet());
    assertEquals(getTestIndexNames(), testIndexes);
    mockProcessIndexEventListener.assertFire("test", travelEntityIndexDescriptor.getName());
}
Also used : IntStream(java.util.stream.IntStream) Document(org.bson.Document) Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) MongoDBQuarkusTestResource(org.kie.kogito.testcontainers.quarkus.MongoDBQuarkusTestResource) ArgumentMatchers(org.mockito.ArgumentMatchers) IndexModel(com.mongodb.client.model.IndexModel) SchemaRegistrationException(org.kie.kogito.persistence.api.schema.SchemaRegistrationException) MongoCollection(com.mongodb.client.MongoCollection) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) HashMap(java.util.HashMap) QuarkusTest(io.quarkus.test.junit.QuarkusTest) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Bson(org.bson.conversions.Bson) QuarkusMock.installMockForType(io.quarkus.test.junit.QuarkusMock.installMockForType) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) INDEX_NAME_FIELD(org.kie.kogito.persistence.mongodb.index.IndexManager.INDEX_NAME_FIELD) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedList(java.util.LinkedList) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) MockProcessIndexEventListener(org.kie.kogito.persistence.mongodb.mock.MockProcessIndexEventListener) Collectors.toSet(java.util.stream.Collectors.toSet) QuarkusTestResource(io.quarkus.test.common.QuarkusTestResource) SchemaType(org.kie.kogito.persistence.api.schema.SchemaType) Set(java.util.Set) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) IndexOptions(com.mongodb.client.model.IndexOptions) Test(org.junit.jupiter.api.Test) Indexes(com.mongodb.client.model.Indexes) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Collectors.toList(java.util.stream.Collectors.toList) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) DEFAULT_INDEX(org.kie.kogito.persistence.mongodb.index.IndexManager.DEFAULT_INDEX) Mockito.mock(org.mockito.Mockito.mock) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) HashMap(java.util.HashMap) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) Document(org.bson.Document) SchemaType(org.kie.kogito.persistence.api.schema.SchemaType) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with EntityIndexDescriptor

use of org.kie.kogito.persistence.api.schema.EntityIndexDescriptor in project kogito-apps by kiegroup.

the class IndexManager method onIndexCreateOrUpdateEvent.

public void onIndexCreateOrUpdateEvent(@Observes IndexCreateOrUpdateEvent event) {
    String indexType = collectionIndexMapping.put(event.getCollection(), event.getIndex());
    if (!event.getIndex().equals(indexType)) {
        MongoCollection<Document> collection = this.getCollection(event.getCollection());
        EntityIndexDescriptor index = indexes.get(event.getIndex());
        updateCollection(collection, index);
    }
}
Also used : EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) Document(org.bson.Document)

Aggregations

EntityIndexDescriptor (org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)9 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)5 AttributeDescriptor (org.kie.kogito.persistence.api.schema.AttributeDescriptor)5 IndexDescriptor (org.kie.kogito.persistence.api.schema.IndexDescriptor)5 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors.toSet (java.util.stream.Collectors.toSet)3 Descriptor (org.infinispan.protostream.descriptors.Descriptor)3 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Objects (java.util.Objects)2 Document (org.bson.Document)2 AnnotationMetadataCreator (org.infinispan.protostream.AnnotationMetadataCreator)2 SerializationContext (org.infinispan.protostream.SerializationContext)2 Configuration (org.infinispan.protostream.config.Configuration)2 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2