Search in sources :

Example 6 with EntityIndexDescriptor

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

the class ProtoIndexParser method createEntityIndexDescriptors.

static Map<String, EntityIndexDescriptor> createEntityIndexDescriptors(FileDescriptor desc, Map<String, EntityIndexDescriptor> entityIndexes) {
    desc.getMessageTypes().forEach(mDesc -> {
        String typeName = mDesc.getFullName();
        EntityIndexDescriptor entityIndex = entityIndexes.get(typeName);
        if (entityIndex != null) {
            // Add the fields without index
            Set<String> fieldNames = entityIndex.getAttributeDescriptors().stream().map(AttributeDescriptor::getName).collect(toSet());
            mDesc.getFields().stream().filter(fDesc -> !fieldNames.contains(fDesc.getName())).forEach(fDesc -> entityIndex.getAttributeDescriptors().add(createAttributeDescriptor(fDesc, null)));
        }
    });
    return entityIndexes;
}
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) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)

Example 7 with EntityIndexDescriptor

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

the class ProtobufService method onStart.

void onStart(@Observes StartupEvent ev) {
    kogitoDescriptors.getFileDescriptors().forEach((name, bytes) -> {
        LOGGER.info("Registering Kogito ProtoBuffer file: {}", name);
        String content = new String(bytes);
        try {
            SerializationContext ctx = createSerializationContext(FileDescriptorSource.fromString(name, content));
            FileDescriptor desc = ctx.getFileDescriptors().get(name);
            Map<String, EntityIndexDescriptor> entityIndexes = desc.getMessageTypes().stream().map(t -> t.<EntityIndexDescriptor>getProcessedAnnotation(INDEXED_ANNOTATION)).filter(Objects::nonNull).collect(toMap(EntityIndexDescriptor::getName, Function.identity()));
            Map<String, EntityIndexDescriptor> entityIndexDescriptors = createEntityIndexDescriptors(desc, entityIndexes);
            schemaEvent.fire(new SchemaRegisteredEvent(new SchemaDescriptor(name, content, entityIndexDescriptors, null), SCHEMA_TYPE));
        } catch (ProtobufValidationException e) {
            throw new ProtobufFileRegistrationException(e);
        }
    });
    protobufMonitorService.startMonitoring();
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor)

Example 8 with EntityIndexDescriptor

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

the class ProtobufService method registerProtoBufferType.

public void registerProtoBufferType(String content) throws ProtobufValidationException {
    LOGGER.debug("Registering new ProtoBuffer file with content: \n{}", content);
    content = content.replaceAll("kogito.Date", "string");
    SerializationContext ctx = createSerializationContext(kogitoDescriptors, FileDescriptorSource.fromString(DOMAIN_MODEL_PROTO_NAME, content));
    FileDescriptor desc = ctx.getFileDescriptors().get(DOMAIN_MODEL_PROTO_NAME);
    Option processIdOption = desc.getOption("kogito_id");
    if (processIdOption == null || processIdOption.getValue() == null) {
        throw new ProtobufValidationException("Missing marker for process id in proto file, please add option kogito_id=\"processid\"");
    }
    String processId = (String) processIdOption.getValue();
    Option model = desc.getOption("kogito_model");
    if (model == null || model.getValue() == null) {
        throw new ProtobufValidationException("Missing marker for main message type in proto file, please add option kogito_model=\"messagename\"");
    }
    String messageName = (String) model.getValue();
    String fullTypeName = desc.getPackage() == null ? messageName : desc.getPackage() + "." + messageName;
    Descriptor descriptor;
    try {
        descriptor = ctx.getMessageDescriptor(fullTypeName);
    } catch (IllegalArgumentException ex) {
        throw new ProtobufValidationException(format("Could not find message with name: %s in proto file, e, please review option kogito_model", fullTypeName));
    }
    validateDescriptorField(messageName, descriptor, Constants.KOGITO_DOMAIN_ATTRIBUTE);
    Map<String, EntityIndexDescriptor> entityIndexes = desc.getMessageTypes().stream().map(t -> t.<EntityIndexDescriptor>getProcessedAnnotation(INDEXED_ANNOTATION)).filter(Objects::nonNull).collect(toMap(EntityIndexDescriptor::getName, Function.identity()));
    Map<String, EntityIndexDescriptor> entityIndexedDescriptors = createEntityIndexDescriptors(desc, entityIndexes);
    try {
        schemaEvent.fire(new SchemaRegisteredEvent(new SchemaDescriptor(processId + ".proto", content, entityIndexedDescriptors, new ProcessDescriptor(processId, fullTypeName)), SCHEMA_TYPE));
    } catch (RuntimeException ex) {
        throw new ProtobufValidationException(ex.getMessage(), ex);
    }
    domainModelEvent.fire(new FileDescriptorRegisteredEvent(desc));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) Descriptor(org.infinispan.protostream.descriptors.Descriptor) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Option(org.infinispan.protostream.descriptors.Option) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)

Example 9 with EntityIndexDescriptor

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

the class ProtoIndexParserTest method testCreateEntityIndexeDescriptors.

@Test
void testCreateEntityIndexeDescriptors() {
    FileDescriptor fileDescriptor = createFileDescriptor();
    Map<String, EntityIndexDescriptor> entityIndexes = createFileDescriptor().getMessageTypes().stream().map(t -> t.<EntityIndexDescriptor>getProcessedAnnotation(INDEXED_ANNOTATION)).filter(Objects::nonNull).collect(toMap(EntityIndexDescriptor::getName, Function.identity()));
    Map<String, EntityIndexDescriptor> indexDescriptor = createEntityIndexDescriptors(fileDescriptor, entityIndexes);
    assertEquals(getValidEntityIndexDescriptors(true), indexDescriptor);
}
Also used : EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.jupiter.api.Test)

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