alexcrawford/lexorank-php
Simple PHP implementation of Atlassian JIRA’s LexoRank for ordering database lists. Reorder items in O(1) by updating only the moved row’s rank value, avoiding mass updates and costly transactions.
lexorank-php package provides a lightweight, efficient way to implement lexical ranking (a gap-encoding scheme for ordered sequences), ideal for:
VARCHAR).list type or MongoDB’s $sort with custom logic, but more portable across databases.| Risk Area | Description | Mitigation Strategy |
|---|---|---|
| Sorting Overhead | Lexorank strings require application-side sorting (not database-native). | Pre-sort in-memory or use a database with full-text search (e.g., PostgreSQL tsvector). |
| Concurrency | Race conditions if multiple processes insert/delete ranks simultaneously. | Use atomic database transactions or distributed locks (Redis). |
| String Bloat | Lexorank strings grow with list size (though compact for most cases). | Monitor string lengths; consider hybrid approaches (e.g., lexorank for dynamic sections, IDs for static). |
| Migration Complexity | Retrofitting lexorank into an existing ordered system may require rewrites. | Start with a shadow mode (dual-write lexorank alongside existing IDs). |
pg_trgm extension)?Lexorank as a singleton in AppServiceProvider for global access.scopeOrderedByRank()) for Eloquent models.VARCHAR; sort in application code or use a computed column (PostgreSQL).ORDER BY created_at).// Model: Post.php
public function scopeTrending($query) {
return $query->where('is_trending', true)
->orderByRaw("lexorank(post.rank_string) ASC");
}
insert($list, $item, $rankString)remove($list, $rankString)getRank($list, $rankString)How can I help you explore Laravel packages today?