Initial commit: Test GLM project
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
# Subtask: Unique Short Code Generation with Collision Handling
|
||||
|
||||
**Parent Task**: Decomp/001
|
||||
**Subtask ID**: Decomp/001-03
|
||||
**Dependencies**: Decomp/001-01 (Database Schema), Decomp/001-02 (Base62)
|
||||
|
||||
## Description
|
||||
|
||||
Generate unique short codes for URLs, handling duplicate URLs and hash collisions. Implements idempotent URL shortening (same URL → same short code when possible).
|
||||
|
||||
## Requirements
|
||||
|
||||
- Generate short code from auto-increment ID using Base62
|
||||
- Detect duplicate URLs (return existing short code)
|
||||
- Handle hash collisions gracefully (multiple codes for same checksum)
|
||||
- Support custom user-defined aliases
|
||||
- Ensure uniqueness at database level (UNIQUE constraint)
|
||||
|
||||
## Technical Specifications
|
||||
|
||||
**Algorithm**: Counter-based + duplicate detection
|
||||
|
||||
```python
|
||||
def create_short_url(original_url: str, custom_alias: str = None) -> str:
|
||||
"""
|
||||
Create a short URL. Returns existing short code if URL already shortened.
|
||||
"""
|
||||
if custom_alias:
|
||||
# Validate custom alias format
|
||||
if not valid_alias(custom_alias):
|
||||
raise ValueError("Invalid custom alias")
|
||||
# Check for conflicts
|
||||
if exists_short_code(custom_alias):
|
||||
raise ValueError("Alias already taken")
|
||||
short_code = custom_alias
|
||||
else:
|
||||
# Check for duplicate URL
|
||||
checksum = sha256(original_url)
|
||||
existing = find_by_checksum(checksum)
|
||||
if existing and not existing.expires_at < now():
|
||||
return existing.short_code # Return existing
|
||||
|
||||
# Generate new code from auto-increment ID
|
||||
url_id = insert_url(original_url, checksum)
|
||||
short_code = encode_id(url_id)
|
||||
|
||||
return short_code
|
||||
```
|
||||
|
||||
**Collision handling strategy**:
|
||||
- Primary: database UNIQUE constraint on short_code
|
||||
- Secondary: checksum collision → append random suffix
|
||||
- Fallback: retry with new ID (incremental)
|
||||
|
||||
**API signatures**:
|
||||
```python
|
||||
def create_short_url(original_url: str, custom_alias: Optional[str] = None,
|
||||
ttl: Optional[int] = None) -> str
|
||||
def resolve_short_code(short_code: str) -> Optional[str]
|
||||
def check_duplicate(original_url: str) -> Optional[str]
|
||||
```
|
||||
|
||||
**Error handling**:
|
||||
- `ValueError`: invalid custom alias format
|
||||
- `ConflictError`: custom alias already exists
|
||||
- `DatabaseError`: constraint violation (should not happen with proper logic)
|
||||
|
||||
## Acceptance Criteria
|
||||
|
||||
- [ ] Same URL → same short code (idempotent)
|
||||
- [ ] Custom aliases override auto-generated codes
|
||||
- [ ] Collisions handled without data loss
|
||||
- [ ] Expired duplicate URLs generate new codes
|
||||
- [ ] Custom alias validation: 3-8 chars, alphanumeric + hyphen
|
||||
- [ ] Database constraint violations never cause silent failures
|
||||
|
||||
## Notes
|
||||
|
||||
**Trade-offs**:
|
||||
- Duplicate detection on checksum: fast but theoretically possible collisions
|
||||
- SHA-256 chosen over MD5: collision resistance (astronomically improbable)
|
||||
- Custom alias override: user choice trumps deduplication
|
||||
|
||||
**Edge cases**:
|
||||
- Same URL with different TTL: return existing, update TTL
|
||||
- Collision after 10M URLs (Base62 wrap): increment ID length
|
||||
- Custom alias matching auto-generated format: allow but warn
|
||||
|
||||
**Alternatives considered**:
|
||||
- True hash-based codes: rejected, not sortable, collision-prone
|
||||
- UUID prefix: rejected, longer codes, no ordering
|
||||
Reference in New Issue
Block a user