mirror of
https://github.com/pachli/pachli-android.git
synced 2025-02-08 07:58:55 +01:00
The PageCache implementation wasn't properly dealing with timelines that could return statuses in non-chronological order. For example, if you bookmark a recent status, then go back in the timeline and bookmark an older status; the bookmarks timeline is ordered by the time of the bookmark event, not the creation time of the status that was bookmarked. If a sufficiently old status was bookmarked so it straddled a page boundary you could have a situation where the range of status IDs in two different cached pages overlapped. E.g., this log extract: ``` 0: k: 110912736679636090, prev: 3521487, next: 3057175, size: 40, range: 112219564107059218..110912736679636090 1: k: 111651744569170291, prev: 3049659, next: 2710596, size: 40, range: 111926741634665808..111651744569170291 ``` The range of IDs in page 0 overlaps with the range of IDs in page 1. The previous `PageCache` assumed this couldn't happen, and broke in various interesting ways when it did. E.g., you can't find the page that contains a given status by looking for the largest key less than the needle's status id. Given the pages above looking for ID 112219564107059218 (first status in page 0) would suggest page 1 as having the greatest key less than that ID. This manifested as the correct page briefly appearing in the UI (page 0), then being completely replaced with page 1. Rewrite PageCache to fix this. The previous implementation used a single `TreeMap` assuming items were always sorted by ID. The new code keeps an unordered map from status IDs to the page that contains that status, and a separate `LinkedList` that contains the pages in order they're provided by the API. This decouples the ordering of pages in the cache with the overall ordering of items within the pages.