Search in sources :

Example 1 with IngestionEntity

use of io.openk9.entity.manager.cache.model.IngestionEntity in project openk9 by smclab.

the class AssociateEntitiesRunnable method run_.

@Override
public void run_() {
    _log.info("start AssociateEntitiesRunnable");
    IMap<AssociableEntityKey, Entity> associableEntityMap = MapUtil.getAssociableEntityMap(_hazelcastInstance);
    Set<AssociableEntityKey> associableEntityKeys = associableEntityMap.localKeySet();
    Map<AssociableEntityKey, Entity> localEntityMap = associableEntityMap.getAll(associableEntityKeys);
    _log.info("ingestionKeys: " + localEntityMap.size());
    Map<String, List<Entity>> groupingByIngestionId = localEntityMap.entrySet().stream().collect(Collectors.groupingBy(e -> e.getKey().getIngestionId(), Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
    List<AssociableEntityKey> entitiesToRemove = new ArrayList<>();
    List<String> ingestionIds = new ArrayList<>();
    for (Map.Entry<String, List<Entity>> entry : groupingByIngestionId.entrySet()) {
        String ingestionId = entry.getKey();
        List<Entity> v = entry.getValue();
        if (v.isEmpty()) {
            continue;
        }
        DataService dataService = CDI.current().select(DataService.class).get();
        Long tenantId = v.stream().map(Entity::getTenantId).findFirst().get();
        try {
            boolean associated = dataService.associateEntities(tenantId, ingestionId, v.stream().map(IngestionEntity::fromEntity).collect(Collectors.toList()));
            if (associated) {
                for (Entity entity : v) {
                    entitiesToRemove.add(AssociableEntityKey.of(entity.getCacheId(), entity.getIngestionId()));
                    ingestionIds.add(ingestionId);
                }
            }
        } catch (Exception ioe) {
            _log.error(ioe.getMessage());
        }
    }
    _log.info("entities associated: " + entitiesToRemove.size() + " ingestionIds: " + ingestionIds);
    try {
        Pipelining pipelining = new Pipelining<>(10);
        for (AssociableEntityKey associateEntityKey : entitiesToRemove) {
            pipelining.add(associableEntityMap.removeAsync(associateEntityKey));
        }
        pipelining.results();
    } catch (Exception e) {
        _log.error(e.getMessage(), e);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Entity(io.openk9.entity.manager.cache.model.Entity) Logger(org.jboss.logging.Logger) CDI(javax.enterprise.inject.spi.CDI) Set(java.util.Set) DataService(io.openk9.entity.manager.service.index.DataService) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Pipelining(com.hazelcast.core.Pipelining) List(java.util.List) Map(java.util.Map) IngestionEntity(io.openk9.entity.manager.cache.model.IngestionEntity) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware) AssociableEntityKey(io.openk9.entity.manager.cache.model.AssociableEntityKey) MapUtil(io.openk9.entity.manager.util.MapUtil) IMap(com.hazelcast.map.IMap) Entity(io.openk9.entity.manager.cache.model.Entity) IngestionEntity(io.openk9.entity.manager.cache.model.IngestionEntity) ArrayList(java.util.ArrayList) DataService(io.openk9.entity.manager.service.index.DataService) IngestionEntity(io.openk9.entity.manager.cache.model.IngestionEntity) AssociableEntityKey(io.openk9.entity.manager.cache.model.AssociableEntityKey) ArrayList(java.util.ArrayList) List(java.util.List) Pipelining(com.hazelcast.core.Pipelining) Map(java.util.Map) IMap(com.hazelcast.map.IMap)

Aggregations

HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)1 Pipelining (com.hazelcast.core.Pipelining)1 IMap (com.hazelcast.map.IMap)1 AssociableEntityKey (io.openk9.entity.manager.cache.model.AssociableEntityKey)1 Entity (io.openk9.entity.manager.cache.model.Entity)1 IngestionEntity (io.openk9.entity.manager.cache.model.IngestionEntity)1 DataService (io.openk9.entity.manager.service.index.DataService)1 MapUtil (io.openk9.entity.manager.util.MapUtil)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CDI (javax.enterprise.inject.spi.CDI)1 Logger (org.jboss.logging.Logger)1