Search in sources :

Example 1 with Pipelining

use of com.hazelcast.core.Pipelining 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)

Example 2 with Pipelining

use of com.hazelcast.core.Pipelining in project openk9 by smclab.

the class CreateRelationRunnable method run_.

@Override
public void run_() {
    IMap<EntityKey, Entity> entityIMap = MapUtil.getEntityMap(_hazelcastInstance);
    Set<EntityKey> entityKeys = entityIMap.localKeySet(Predicates.and(Predicates.notEqual("id", null), Predicates.notEqual("graphId", null)));
    _log.info("entityKeys: " + entityKeys.size());
    Map<EntityKey, Entity> entityIMapAll = entityIMap.getAll(entityKeys);
    _log.info("entityIMapAll: " + entityIMapAll.size());
    IMap<EntityRelationKey, EntityRelation> entityRelationMap = MapUtil.getEntityRelationMap(_hazelcastInstance);
    Map<String, String> collect = entityIMapAll.values().stream().collect(Collectors.toMap(Entity::getCacheId, Entity::getId));
    String[] cacheIds = entityKeys.stream().map(EntityKey::getCacheId).distinct().toArray(String[]::new);
    Map<EntityRelationKey, EntityRelation> entries = entityRelationMap.getAll(entityRelationMap.keySet(Predicates.in("__key.entityId", cacheIds)));
    _log.info("entityRelations: " + entries.size());
    EntityGraphService entityGraphService = CDI.current().select(EntityGraphService.class).get();
    List<EntityRelationKey> entityRelationKeysToDelete = new ArrayList<>();
    for (Map.Entry<EntityRelationKey, EntityRelation> entry : entries.entrySet()) {
        EntityRelationKey key = entry.getKey();
        EntityRelation value = entry.getValue();
        String from = collect.get(value.getEntityCacheId());
        String to = collect.get(value.getTo());
        if (from != null && to != null) {
            try {
                entityGraphService.createRelationship(from, to, value.getName());
                entityRelationKeysToDelete.add(key);
            } catch (Exception e) {
                _log.error(e.getMessage(), e);
            }
        }
    }
    try {
        Pipelining pipelining = new Pipelining<>(10);
        for (EntityRelationKey entityRelationKey : entityRelationKeysToDelete) {
            pipelining.add(entityRelationMap.removeAsync(entityRelationKey));
        }
        pipelining.results();
    } catch (Exception e) {
        _log.error(e.getMessage(), e);
    }
}
Also used : Entity(io.openk9.entity.manager.cache.model.Entity) EntityGraphService(io.openk9.entity.manager.service.graph.EntityGraphService) ArrayList(java.util.ArrayList) EntityRelationKey(io.openk9.entity.manager.cache.model.EntityRelationKey) EntityKey(io.openk9.entity.manager.cache.model.EntityKey) EntityRelation(io.openk9.entity.manager.cache.model.EntityRelation) Pipelining(com.hazelcast.core.Pipelining) Map(java.util.Map) IMap(com.hazelcast.map.IMap)

Aggregations

Pipelining (com.hazelcast.core.Pipelining)2 IMap (com.hazelcast.map.IMap)2 Entity (io.openk9.entity.manager.cache.model.Entity)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)1 AssociableEntityKey (io.openk9.entity.manager.cache.model.AssociableEntityKey)1 EntityKey (io.openk9.entity.manager.cache.model.EntityKey)1 EntityRelation (io.openk9.entity.manager.cache.model.EntityRelation)1 EntityRelationKey (io.openk9.entity.manager.cache.model.EntityRelationKey)1 IngestionEntity (io.openk9.entity.manager.cache.model.IngestionEntity)1 EntityGraphService (io.openk9.entity.manager.service.graph.EntityGraphService)1 DataService (io.openk9.entity.manager.service.index.DataService)1 MapUtil (io.openk9.entity.manager.util.MapUtil)1 Serializable (java.io.Serializable)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CDI (javax.enterprise.inject.spi.CDI)1