Changeset 8089 for branches/3.19-stable


Ignore:
Timestamp:
Nov 1, 2022, 2:15:01 PM (7 months ago)
Author:
Nicklas Nordborg
Message:

References #2286: Improve performance for multi-hop child->parent->child kolumns

Implemented support also when filtering on such columns. Ther was an issue was with the caching feature that only considered the set of source items. The "doubling back" prevention can now yield different results with the same set of source items, depending on the path that was used while reaching the source items. The caching issue was solved by using the filter string as part of the cache key.

Location:
branches/3.19-stable/src/core/net/sf/basedb
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.19-stable/src/core/net/sf/basedb/core/PropertyFilter.java

    r8065 r8089  
    120120  private Unit unit;
    121121  private boolean temporary;
     122  private TransformContext transformContext;
    122123 
    123124  /**
     
    866867      String[] parts = property.split("/", 5);
    867868      SourceItemTransform direction = SourceItemTransform.PARENT_TO_CHILD;
     869      boolean allowDoublingBack = false;
    868870      int baseIndex = 1;
    869871      if (parts.length == 5) // The /DIRECTION is optional
    870872      {
    871873        baseIndex = 2;
    872         direction = "CHILD".equals(parts[1]) ? SourceItemTransform.CHILD_TO_PARENT : SourceItemTransform.PARENT_TO_CHILD;
     874        if (parts[1].startsWith("CHILD")) direction = SourceItemTransform.CHILD_TO_PARENT;
     875        allowDoublingBack = parts[1].endsWith("+");
    873876      }
    874877      Item targetType = Item.valueOf(parts[baseIndex]);
     
    896899      }
    897900      PropertyFilter pp = new PropertyFilter(targetProperty, operator, getValue(), getValueType());
     901      // Create a TransformContext unless we already have one
     902      // The subquery must use the same TransformContext in case we need the "doubling back" feature
     903      TransformContext tCtx = transformContext == null ? new TransformContext(dc) : transformContext;
     904      pp.transformContext = tCtx;
    898905      ClientContext subContext = new ClientContext(dc);
    899906      if (context != null) subContext.linkAttributes(context);
     
    907914        cc.setException(targetItemContext.getException());
    908915      }
    909 
     916     
    910917      // We then transform the list if ID:s to a list of child item ID:s using ItemList functionality
    911918      SortedSet<Integer> parentIds = new TreeSet<>(subquery.idList(dc));
    912       TransformContext tCtx = new TransformContext(dc);
    913919
    914920      SourceItemTransformerFactory factory = ListableUtil.getTransformerFactory(query.getItemType());
    915       factory = new SourceItemTransformerWithCache(factory);
     921      factory = new SourceItemTransformerWithCache(factory, tCtx.isAvoiding() ? property+operator+value : null);
     922      // If we are in a subquery the main query may need this for the "doubling back" feature
     923      if (transformContext != null && !allowDoublingBack) transformContext.setCollecting();
    916924      Set<Integer> myIds = factory.create(targetType, direction).transform(tCtx, parentIds);
     925      // If we are in a subquery the main query may need this for the "doubling back" feature
     926      if (transformContext != null && !allowDoublingBack) transformContext.setAvoiding();
    917927     
    918928      // The final ID list is returned as a restriction
  • branches/3.19-stable/src/core/net/sf/basedb/util/listable/SourceItemTransformerWithCache.java

    r7772 r8089  
    4747
    4848  private final SourceItemTransformerFactory factory;
     49  private final String cacheRegion;
    4950 
    5051  /**
     
    5758  public SourceItemTransformerWithCache(SourceItemTransformerFactory factory)
    5859  {
    59     this.factory = factory;
     60    this(factory, null);
    6061  }
    6162
     63  public SourceItemTransformerWithCache(SourceItemTransformerFactory factory, String cacheRegion)
     64  {
     65    this.factory = factory;
     66    this.cacheRegion = cacheRegion;
     67  }
     68 
     69 
    6270  @Override
    6371  public Item getTargetItem()
     
    7583  public SourceItemTransformer create(Item sourceItemType, SourceItemTransform transform)
    7684  {
    77     return new TransformerWithCache(factory.create(sourceItemType, transform), transform);
     85    return new TransformerWithCache(factory.create(sourceItemType, transform), transform, cacheRegion);
    7886  }
    7987 
     
    8593    private final SourceItemTransformer transformer;
    8694    private final SourceItemTransform transform;
     95    private final String cacheRegion;
    8796   
    88     TransformerWithCache(SourceItemTransformer transformer, SourceItemTransform transform)
     97    TransformerWithCache(SourceItemTransformer transformer, SourceItemTransform transform, String cacheRegion)
    8998    {
    9099      this.transformer = transformer;
    91100      this.transform = transform;
     101      this.cacheRegion = cacheRegion;
    92102    }
    93103   
     
    117127      if (cache != null)
    118128      {
    119         key = TransformCache.getKey(getSourceItemType(), getTargetItemType(), transform, source);
     129        key = TransformCache.getKey(cacheRegion, getSourceItemType(), getTargetItemType(), transform, source);
    120130        target = cache.get(key);
    121131      }
  • branches/3.19-stable/src/core/net/sf/basedb/util/listable/TransformCache.java

    r7770 r8089  
    3030  public static CacheKey getKey(Item sourceType, Item targetType, SyncFilter.SourceItemTransform transform, Set<Integer> sourceIds)
    3131  {
     32    return getKey(null, sourceType, targetType, transform, sourceIds);
     33  }
     34 
     35  public static CacheKey getKey(String cacheRegion, Item sourceType, Item targetType, SyncFilter.SourceItemTransform transform, Set<Integer> sourceIds)
     36  {
    3237    SortedSet<Integer> sortedSrc = sourceIds instanceof SortedSet ? (SortedSet<Integer>)sourceIds : new TreeSet<>(sourceIds);
    33     return new CacheKey(sourceType, targetType, transform, sortedSrc);
     38    return new CacheKey(cacheRegion, sourceType, targetType, transform, sortedSrc);
    3439  }
    3540 
     
    96101  public static class CacheKey
    97102  {
     103    private final String cacheRegion;
    98104    private final Item sourceType;
    99105    private final Item targetType;
     
    101107    private final String sourceIdHash;
    102108   
    103     CacheKey(Item sourceType, Item targetType, SyncFilter.SourceItemTransform transform, SortedSet<Integer> sourceIds)
     109    CacheKey(String cacheRegion, Item sourceType, Item targetType, SyncFilter.SourceItemTransform transform, SortedSet<Integer> sourceIds)
    104110    {
     111      this.cacheRegion = cacheRegion==null?"":cacheRegion;
    105112      this.sourceType = sourceType;
    106113      this.targetType = targetType;
     
    112119    public int hashCode()
    113120    {
    114       return sourceType.hashCode() + 3 * targetType.hashCode() + 7 * transform.hashCode() + 11 * sourceIdHash.hashCode();
     121      return cacheRegion.hashCode()+sourceType.hashCode() + 3 * targetType.hashCode() + 7 * transform.hashCode() + 11 * sourceIdHash.hashCode();
    115122    }
    116123
     
    120127      if (!(obj instanceof CacheKey)) return false;
    121128      CacheKey other = (CacheKey)obj;
    122       return sourceType == other.sourceType && targetType == other.targetType &&
     129      return cacheRegion.equals(other.cacheRegion) &&
     130        sourceType == other.sourceType && targetType == other.targetType &&
    123131        transform == other.transform && sourceIdHash.equals(other.sourceIdHash);
    124132    }
     
    127135    public String toString()
    128136    {
    129       return "CacheKey[" + sourceType.name() + "->" + targetType.name() + "; " + transform.name() + "; " + sourceIdHash + "]";
    130     }   
     137      return "CacheKey["+cacheRegion + ";" + sourceType.name() + "->" + targetType.name() + "; " + transform.name() + "; " + sourceIdHash + "]";
     138    }
    131139  }
    132140 
Note: See TracChangeset for help on using the changeset viewer.