use of io.openk9.entity.manager.cache.model.EntityRelationKey in project openk9 by smclab.
the class EntityManagerBus method run.
@SneakyThrows
public void run() {
while (true) {
Payload request = _entityManagerQueue.take();
TransactionContext transactionContext = _hazelcastInstance.newTransactionContext();
transactionContext.beginTransaction();
try {
TransactionalMap<EntityKey, Entity> entityTransactionalMap = transactionContext.getMap("entityMap");
TransactionalMap<EntityRelationKey, EntityRelation> transactionalEntityRelationMap = transactionContext.getMap("entityRelationMap");
TransactionalMultiMap<DocumentKey, String> documentEntityMap = transactionContext.getMultiMap("documentEntityMap");
EntityManagerRequest payload = request.getPayload();
_loggerAggregator.emitLog("process ingestionId", payload.getIngestionId());
long tenantId = payload.getTenantId();
String ingestionId = payload.getIngestionId();
List<EntityRequest> entities = request.getEntities();
Map<EntityKey, Entity> localEntityMap = new HashMap<>(entities.size());
for (EntityRequest entityRequest : entities) {
String name = entityRequest.getName();
String type = entityRequest.getType();
String cacheId = Long.toString(_entityFlakeId.newId());
EntityKey key = EntityKey.of(tenantId, name, type, cacheId, ingestionId);
Entity entity = new Entity(null, cacheId, tenantId, name, type, null, ingestionId, false, true, entityRequest.getContext());
entityTransactionalMap.set(key, entity);
localEntityMap.put(key, entity);
for (EntityRequest entityRequest2 : entities) {
for (RelationRequest relation : entityRequest2.getRelations()) {
if (relation.getTo().equals(entityRequest.getTmpId())) {
relation.setTo(entity.getCacheId());
}
}
}
}
for (EntityRequest entity : entities) {
List<RelationRequest> relations = entity.getRelations();
if (relations == null || relations.isEmpty()) {
continue;
}
Collection<Entity> values = localEntityMap.values();
Entity current = values.stream().filter(e -> e.getName().equals(entity.getName()) && e.getType().equals(entity.getType())).findFirst().orElse(null);
if (current == null) {
continue;
}
for (RelationRequest relation : relations) {
String to = relation.getTo();
String name = relation.getName();
for (Entity value : values) {
if (value.getCacheId().equals(to)) {
long entityRelationId = _entityRelationFlakeId.newId();
EntityRelation entityRelation = new EntityRelation(entityRelationId, current.getCacheId(), ingestionId, name, value.getCacheId());
transactionalEntityRelationMap.set(EntityRelationKey.of(entityRelationId, current.getCacheId(), ingestionId), entityRelation);
}
}
}
}
if (!localEntityMap.isEmpty()) {
DocumentKey key = DocumentKey.of(payload.getDatasourceId(), payload.getContentId(), tenantId);
for (Entity value : localEntityMap.values()) {
documentEntityMap.put(key, value.getCacheId());
}
}
} catch (Exception e) {
_log.error(e.getMessage(), e);
transactionContext.rollbackTransaction();
} finally {
transactionContext.commitTransaction();
}
}
}
use of io.openk9.entity.manager.cache.model.EntityRelationKey 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);
}
}
Aggregations