3

Below is my code to get the list of Entity's linked. It works but the problem is that even the deleted Entity is returned, although the Entity is already emptied out and it is the only property set. Is there a way to not return the deleted entities at all? Or is there a way to filter it out?

EntityId idOfEntity = txn.toEntityId(entityId);
Entity txnEntity = txn.getEntity(idOfEntity);
EntityIterable result = txnEntity.getLinks(Arrays.asList(new String[] {linkName}));
for (Entity entity : result) {
}
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
quarks
  • 33,478
  • 73
  • 290
  • 513

2 Answers2

1

When you delete an entity, it's your responsibility to check if there are incoming links to the deleted entity. Otherwise so called "phantom links" can appear. You can set -Dexodus.entityStore.debug.searchForIncomingLinksOnDelete=true (PersistentEntityStoreConfig#setDebugSearchForIncomingLinksOnDelete(true)) to debug deletion in your application. With this setting, Xodus searches for incoming links to each deleted entity and throws EntityStoreException if it finds. The setting should not be used in production environment as it significantly slows down entity deletion performance.

Vyacheslav Lukianov
  • 1,913
  • 8
  • 12
  • Thanks for this I have managed to fix the issue (bug) although I understand that there is a performance penalty for that. – quarks Jul 01 '19 at 01:36
0

Here's the complete code that I have come up with:

@Override
  public boolean deleteEntities(String instance, String namespace, final String entityType) {
    final boolean[] success = {false};
    final PersistentEntityStore entityStore = manager.getPersistentEntityStore(xodusRoot, instance);
    try {
      entityStore.executeInTransaction(
          new StoreTransactionalExecutable() {
            @Override
            public void execute(@NotNull final StoreTransaction txn) {
              EntityIterable result = null;
              if (namespace != null && !namespace.isEmpty()) {
                result =
                    txn.findWithProp(entityType, namespaceProperty)
                        .intersect(txn.find(entityType, namespaceProperty, namespace));
              } else {
                result =
                    txn.getAll(entityType).minus(txn.findWithProp(entityType, namespaceProperty));
              }
              final boolean[] hasError = {false};
              for (Entity entity : result) {

                entity.getLinkNames().forEach(linkName -> {
                  Entity linked = entity.getLink(linkName);
                  entity.deleteLink(linkName, linked);
                });


                // TODO: This is a performance issue
                final List<String> allLinkNames = ((PersistentEntityStoreImpl) entityStore).getAllLinkNames((PersistentStoreTransaction) entityStore.getCurrentTransaction());
                for (final String entityType : txn.getEntityTypes()) {
                  for (final String linkName : allLinkNames) {
                    for (final Entity referrer : txn.findLinks(entityType, entity, linkName)) {
                      referrer.deleteLink(linkName, entity);
                    }
                  }
                }

                if (!entity.delete()) {
                  hasError[0] = true;
                }
              }
              success[0] = !hasError[0];
            }
          });
    } finally {
      // entityStore.close();
    }

    return success[0];
  }
quarks
  • 33,478
  • 73
  • 290
  • 513