|
| 1 | +# URL Pattern Application Strategies |
| 2 | + |
| 3 | +## Strategy 1: Exclusive Patterns |
| 4 | + |
| 5 | +Patterns have exclusive ownership of URLs they match. System prevents creation of overlapping patterns. |
| 6 | + |
| 7 | +Example: |
| 8 | +``` |
| 9 | +Pattern A: */docs/* # Matches 100 URLs |
| 10 | +Pattern B: */docs/api/* # Rejected - overlaps with Pattern A |
| 11 | +Pattern C: */blog/* # Accepted - no overlap |
| 12 | +``` |
| 13 | + |
| 14 | +Benefits: |
| 15 | +- Clear ownership |
| 16 | +- Predictable effects |
| 17 | +- Simple conflict resolution |
| 18 | +- Easy to debug |
| 19 | + |
| 20 | +Drawbacks: |
| 21 | +- Less flexible |
| 22 | +- May require many specific patterns |
| 23 | +- May need pattern deletion/recreation to modify rules |
| 24 | + |
| 25 | +## Strategy 2: Smallest Set Priority |
| 26 | + |
| 27 | +Multiple patterns can match same URLs. Pattern affecting smallest URL set takes precedence. |
| 28 | + |
| 29 | +Example: |
| 30 | +``` |
| 31 | +Pattern A: */docs/* # Matches 100 URLs |
| 32 | +Pattern B: */docs/api/* # Matches 20 URLs |
| 33 | +Pattern C: */docs/api/v2/* # Matches 5 URLs |
| 34 | +
|
| 35 | +For URL "/docs/api/v2/users": |
| 36 | +- All patterns match |
| 37 | +- Pattern C wins (5 URLs < 20 URLs < 100 URLs) |
| 38 | +``` |
| 39 | + |
| 40 | +Benefits: |
| 41 | +- More flexible rule creation |
| 42 | +- Natural handling of specificity |
| 43 | + |
| 44 | +Drawbacks: |
| 45 | +- Complex precedence rules |
| 46 | +- Pattern effects can change as URL sets grow |
| 47 | +- Harder to predict/debug |
| 48 | +- Performance impact from URL set size calculations |
| 49 | + |
| 50 | +## Implementation Notes |
| 51 | + |
| 52 | +Strategy 1: |
| 53 | +```python |
| 54 | +def save(self, *args, **kwargs): |
| 55 | + # Check for overlapping patterns |
| 56 | + overlapping = self.get_matching_delta_urls().filter( |
| 57 | + deltapatterns__isnull=False |
| 58 | + ).exists() |
| 59 | + if overlapping: |
| 60 | + raise ValidationError("Pattern would overlap existing pattern") |
| 61 | + super().save(*args, **kwargs) |
| 62 | +``` |
| 63 | + |
| 64 | +Strategy 2: |
| 65 | +```python |
| 66 | +def apply(self): |
| 67 | + matching_urls = self.get_matching_delta_urls() |
| 68 | + my_url_count = matching_urls.count() |
| 69 | + |
| 70 | + # Only apply if this pattern matches fewer URLs than other matching patterns |
| 71 | + for url in matching_urls: |
| 72 | + other_patterns_min_count = url.deltapatterns.annotate( |
| 73 | + url_count=Count('delta_urls') |
| 74 | + ).aggregate(Min('url_count'))['url_count__min'] or float('inf') |
| 75 | + |
| 76 | + if my_url_count <= other_patterns_min_count: |
| 77 | + self.apply_to_url(url) |
| 78 | +``` |
0 commit comments