代码之家  ›  专栏  ›  技术社区  ›  quarks

getLinks方法返回删除的实体,如何防止?

  •  3
  • quarks  · 技术社区  · 7 年前

    Entity 的链接。但问题是即使是 实体 实体

    EntityId idOfEntity = txn.toEntityId(entityId);
    Entity txnEntity = txn.getEntity(idOfEntity);
    EntityIterable result = txnEntity.getLinks(Arrays.asList(new String[] {linkName}));
    for (Entity entity : result) {
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Vyacheslav Lukianov    7 年前

    删除实体时,您有责任检查是否有指向已删除实体的传入链接。否则会出现所谓的“幻影链接”。你可以设置 -Dexodus.entityStore.debug.searchForIncomingLinksOnDelete=true ( PersistentEntityStoreConfig#setDebugSearchForIncomingLinksOnDelete(true) )调试应用程序中的删除。使用此设置,Xodus搜索到每个已删除实体的传入链接并抛出 EntityStoreException 如果找到了。该设置不应在生产环境中使用,因为它会显著降低实体删除性能。

        2
  •  0
  •   quarks    6 年前

    下面是我想出的完整代码:

    @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];
      }
    
    推荐文章