private boolean performCompaction(DegreeCachingNode node) {
    Map<DetachedRelationshipDescription, Integer> cachedDegrees = node.getCachedDegrees();

    // Not above the threshold => no need for compaction
    if (cachedDegrees.size() <= compactionThreshold) {
      return true;
    }

    // Not suitable generalization => bad luck
    DetachedRelationshipDescription generalization =
        generalizationStrategy.produceGeneralization(cachedDegrees);
    if (generalization == null) {
      return false;
    }

    // Find all the candidates to be eliminated by the generalization
    Set<DetachedRelationshipDescription> candidates = new HashSet<>();
    for (DetachedRelationshipDescription potentialCandidate : cachedDegrees.keySet()) {
      if (generalization.isMoreGeneralThan(potentialCandidate)) {
        candidates.add(potentialCandidate);
      }
    }

    int candidateCachedCount = 0;
    for (DetachedRelationshipDescription candidate : candidates) {
      int count = cachedDegrees.get(candidate);
      candidateCachedCount += count;
      node.decrementDegree(candidate, count);
    }

    node.incrementDegree(generalization, candidateCachedCount, true);

    // enough? => return, otherwise try again
    return cachedDegrees.size() <= compactionThreshold || performCompaction(node);
  }
 /** {@inheritDoc} */
 @Override
 public void compactRelationshipCounts(DegreeCachingNode node) {
   if (!performCompaction(node)) {
     LOG.warn(
         "The desired threshold ("
             + compactionThreshold
             + ") could not be achieved using the current compaction strategy "
             + "on node "
             + node.getId()
             + ". This is potentially due to the fact that there are more than "
             + compactionThreshold
             + " distinct relationship type - direction pairs being cached for the node. If that's what's desired,"
             + " increase the threshold. If not, implement a RelationshipInclusionPolicy that does not include"
             + " the unwanted relationships.");
   }
 }