Skip to content

Commit 2d3d702

Browse files
committed
Experiment: Rework filtering to preserve artist/album/track objects
Fairly limited benefits... - Makes track_exists (ignore_duplicates) aware of filtered out tracks - Fixes %{albumtracks} being reduced while tracks are filtered out - Slightly faster to clear filter if library is large enough to notice - With a further small change, we could avoid resetting the shuffle list order for the use case of quickly filtering down to find a track/album and then clearing again Changed behavior (not sure if this is a TODO or it's better this way): - :clear, remove_sel_artist, remove_sel_album only remove tracks that are not currently filtered. This change seems to work but I haven't tested extensively, and looking at the churn to benefit ratio I'm inclined to think it's not worth it.
1 parent 6bfd963 commit 2d3d702

4 files changed

Lines changed: 271 additions & 223 deletions

File tree

‎lib.c‎

Lines changed: 50 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ char *lib_live_filter = NULL;
4141

4242
struct rb_root lib_shuffle_root;
4343
struct rb_root lib_album_shuffle_root;
44+
LIST_HEAD(lib_all_tracks);
4445
static struct expr *filter = NULL;
4546
static struct expr *add_filter = NULL;
46-
static int remove_from_hash = 1;
47+
int lib_filter_action = 0;
4748

4849
static struct expr *live_filter_expr = NULL;
4950
static struct track_info *cur_track_ti = NULL;
@@ -74,34 +75,21 @@ static void all_wins_changed(void)
7475
lib_editable.shared->win->changed = 1;
7576
}
7677

77-
static void shuffle_add(struct tree_track *track)
78-
{
79-
shuffle_list_add(&track->simple_track.shuffle_info, &lib_shuffle_root, track->album);
80-
}
81-
82-
static void album_shuffle_list_add(struct album *album)
83-
{
84-
shuffle_list_add(&album->shuffle_info, &lib_album_shuffle_root, album);
85-
}
86-
87-
static void album_shuffle_list_remove(struct album *album)
88-
{
89-
rb_erase(&album->shuffle_info.tree_node, &lib_album_shuffle_root);
90-
}
91-
9278
static void views_add_track(struct track_info *ti)
9379
{
9480
struct tree_track *track = xnew(struct tree_track, 1);
81+
track->in_tree = false;
9582

9683
/* NOTE: does not ref ti */
9784
simple_track_init((struct simple_track *)track, ti);
9885

9986
/* both the hash table and views have refs */
10087
track_info_ref(ti);
10188

102-
tree_add_track(track, album_shuffle_list_add);
103-
shuffle_add(track);
104-
editable_add(&lib_editable, (struct simple_track *)track);
89+
tree_add_track(track);
90+
91+
if (!lib_is_filtered(ti))
92+
editable_add(&lib_editable, (struct simple_track *)track);
10593
}
10694

10795
struct fh_entry {
@@ -160,7 +148,7 @@ static void hash_remove(struct track_info *ti)
160148
}
161149
}
162150

163-
static int is_filtered(struct track_info *ti)
151+
int lib_is_filtered(struct track_info *ti)
164152
{
165153
if (live_filter_expr && !expr_eval(live_filter_expr, ti))
166154
return 1;
@@ -182,7 +170,7 @@ static bool track_exists(struct track_info *ti)
182170
return false;
183171

184172
char *artist_collkey_name = u_strcasecoll_key(tree_artist_name(ti));
185-
rb_for_each_entry(artist, node, &lib_artist_root, tree_node) {
173+
rb_for_each_entry(artist, node, &lib_all_artists_root, all_artists_node) {
186174
if (strcmp(artist->collkey_name, artist_collkey_name) == 0)
187175
break;
188176
}
@@ -192,16 +180,16 @@ static bool track_exists(struct track_info *ti)
192180
return false;
193181

194182
char *album_collkey_name = u_strcasecoll_key(tree_album_name(ti));
195-
rb_for_each_entry(album, node, &artist->album_root, tree_node) {
183+
list_for_each_entry(album, &artist->all_albums, all_albums_node) {
196184
if (strcmp(album->collkey_name, album_collkey_name) == 0)
197185
break;
198186
}
199187
free(album_collkey_name);
200188

201-
if (!album)
189+
if (&album->all_albums_node == &artist->all_albums) // looped back to list head
202190
return false;
203191

204-
rb_for_each_entry(track, node, &album->track_root, tree_node) {
192+
list_for_each_entry(track, &album->all_tracks, all_tracks_node) {
205193
struct track_info *iter_ti = tree_track_info(track);
206194
if (iter_ti->tracknumber == ti->tracknumber
207195
&& iter_ti->discnumber == ti->discnumber
@@ -226,8 +214,7 @@ void lib_add_track(struct track_info *ti, void *opaque)
226214
return;
227215
}
228216

229-
if (!is_filtered(ti))
230-
views_add_track(ti);
217+
views_add_track(ti);
231218
}
232219

233220
static struct tree_track *album_first_track(const struct album *album)
@@ -442,7 +429,7 @@ void lib_reshuffle(void)
442429
}
443430

444431
void lib_sort_artists(void) {
445-
tree_sort_artists(album_shuffle_list_add, album_shuffle_list_remove);
432+
tree_sort_artists();
446433
}
447434

448435
static void free_lib_track(struct editable *e, struct list_head *item)
@@ -453,14 +440,13 @@ static void free_lib_track(struct editable *e, struct list_head *item)
453440
if (track == lib_cur_track)
454441
lib_cur_track = NULL;
455442

456-
if (remove_from_hash)
457-
hash_remove(ti);
458-
459-
rb_erase(&track->simple_track.shuffle_info.tree_node, &lib_shuffle_root);
460-
tree_remove(track, album_shuffle_list_remove);
443+
tree_remove(track);
461444

462-
track_info_unref(ti);
463-
free(track);
445+
if (!lib_filter_action) {
446+
hash_remove(ti);
447+
track_info_unref(ti);
448+
free(track);
449+
}
464450
}
465451

466452
void lib_init(void)
@@ -621,23 +607,6 @@ struct track_info *sorted_activate_selected(void)
621607
return lib_set_track(sorted_get_selected());
622608
}
623609

624-
static void hash_add_to_views(void)
625-
{
626-
int i;
627-
for (i = 0; i < FH_SIZE; i++) {
628-
struct fh_entry *e;
629-
630-
e = ti_hash[i];
631-
while (e) {
632-
struct track_info *ti = e->ti;
633-
634-
if (!is_filtered(ti))
635-
views_add_track(ti);
636-
e = e->next;
637-
}
638-
}
639-
}
640-
641610
struct tree_track *lib_find_track(struct track_info *ti)
642611
{
643612
struct simple_track *track;
@@ -675,25 +644,37 @@ static void restore_cur_track(struct track_info *ti)
675644

676645
static int is_filtered_cb(void *data, struct track_info *ti)
677646
{
678-
return is_filtered(ti);
647+
return lib_is_filtered(ti);
679648
}
680649

681-
static void do_lib_filter(int clear_before)
650+
static void do_lib_filter(int may_grow)
682651
{
652+
struct tree_track *track;
653+
683654
/* try to save cur_track */
684655
if (lib_cur_track)
685656
lib_store_cur_track(tree_track_info(lib_cur_track));
686657

687-
if (clear_before)
688-
d_print("filter results could grow, clear tracks and re-add (slow)\n");
658+
lib_filter_action = 1;
659+
660+
editable_remove_matching_tracks(&lib_editable, is_filtered_cb, NULL);
661+
662+
if (may_grow) {
663+
/* ensure an album isn't selected when we collapse the artists */
664+
window_goto_top(lib_tree_win);
689665

690-
remove_from_hash = 0;
691-
if (clear_before) {
692-
editable_clear(&lib_editable);
693-
hash_add_to_views();
694-
} else
695-
editable_remove_matching_tracks(&lib_editable, is_filtered_cb, NULL);
696-
remove_from_hash = 1;
666+
list_for_each_entry(track, &lib_all_tracks, lib_all_tracks_node) {
667+
/* collapse all artists for now, live filter matches will be
668+
* expanded again later */
669+
track->album->artist->expanded = false;
670+
671+
if (!track->in_tree && !lib_is_filtered(tree_track_info(track))) {
672+
tree_readd_track(track);
673+
editable_add(&lib_editable, (struct simple_track *)track);
674+
}
675+
}
676+
}
677+
lib_filter_action = 0;
697678

698679
window_changed(lib_editable.shared->win);
699680
window_goto_top(lib_editable.shared->win);
@@ -715,12 +696,12 @@ static void unset_live_filter(void)
715696

716697
void lib_set_filter(struct expr *expr)
717698
{
718-
int clear_before = lib_live_filter || filter;
699+
int may_grow = lib_live_filter || filter;
719700
unset_live_filter();
720701
if (filter)
721702
expr_free(filter);
722703
filter = expr;
723-
do_lib_filter(clear_before);
704+
do_lib_filter(may_grow);
724705
}
725706

726707
void lib_set_add_filter(struct expr *expr)
@@ -777,8 +758,8 @@ static void restore_sel_track(void)
777758
}
778759
}
779760

780-
/* determine if filter results could grow, in which case all tracks must be cleared and re-added */
781-
static int do_clear_before(const char *str, struct expr *expr)
761+
/* determine if filter results could grow */
762+
static int expr_may_grow(const char *str, struct expr *expr)
782763
{
783764
if (!lib_live_filter)
784765
return 0;
@@ -793,7 +774,7 @@ static int do_clear_before(const char *str, struct expr *expr)
793774

794775
void lib_set_live_filter(const char *str)
795776
{
796-
int clear_before;
777+
int may_grow;
797778
struct expr *expr = NULL;
798779

799780
if (strcmp0(str, lib_live_filter) == 0)
@@ -805,15 +786,15 @@ void lib_set_live_filter(const char *str)
805786
return;
806787
}
807788

808-
clear_before = do_clear_before(str, expr);
789+
may_grow = expr_may_grow(str, expr);
809790

810791
if (!str)
811792
store_sel_track();
812793

813794
unset_live_filter();
814795
lib_live_filter = str ? xstrdup(str) : NULL;
815796
live_filter_expr = expr;
816-
do_lib_filter(clear_before);
797+
do_lib_filter(may_grow);
817798

818799
if (expr) {
819800
unsigned int match_type = expr_get_match_type(expr);

‎lib.h‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ struct tree_track {
3030

3131
/* position in track search tree */
3232
struct rb_node tree_node;
33+
struct list_head lib_all_tracks_node;
34+
struct list_head all_tracks_node; // within album
3335

3436
struct album *album;
37+
38+
bool in_tree;
3539
};
3640

3741
static inline struct track_info *tree_track_info(const struct tree_track *track)
@@ -50,9 +54,11 @@ struct album {
5054

5155
/* position in album search tree */
5256
struct rb_node tree_node;
57+
struct list_head all_albums_node;
5358

5459
/* root of track tree */
5560
struct rb_root track_root;
61+
struct list_head all_tracks;
5662

5763
struct artist *artist;
5864
char *name;
@@ -64,14 +70,18 @@ struct album {
6470
/* min date of the tracks added to this album */
6571
int min_date;
6672
int num_tracks;
73+
74+
bool in_tree;
6775
};
6876

6977
struct artist {
7078
/* position in artist search tree */
7179
struct rb_node tree_node;
80+
struct rb_node all_artists_node;
7281

7382
/* root of album tree */
7483
struct rb_root album_root;
84+
struct list_head all_albums;
7585

7686
char *name;
7787
char *sort_name;
@@ -83,6 +93,7 @@ struct artist {
8393
/* albums visible for this artist in the tree_win? */
8494
unsigned int expanded : 1;
8595
unsigned int is_compilation : 1;
96+
unsigned int in_tree : 1;
8697
};
8798

8899
const char *artist_sort_name(const struct artist *);
@@ -97,6 +108,7 @@ extern struct editable lib_editable;
97108
extern struct tree_track *lib_cur_track;
98109
extern struct rb_root lib_shuffle_root;
99110
extern struct rb_root lib_album_shuffle_root;
111+
extern struct list_head lib_all_tracks;
100112
extern enum aaa_mode aaa_mode;
101113
extern unsigned int play_sorted;
102114
extern char *lib_live_filter;
@@ -106,12 +118,16 @@ extern struct window *lib_tree_win;
106118
extern struct window *lib_track_win;
107119
extern struct window *lib_cur_win;
108120
extern struct rb_root lib_artist_root;
121+
extern struct rb_root lib_all_artists_root;
122+
123+
extern int lib_filter_action;
109124

110125
#define CUR_ALBUM (lib_cur_track->album)
111126
#define CUR_ARTIST (lib_cur_track->album->artist)
112127

113128
void lib_init(void);
114129
void tree_init(void);
130+
int lib_is_filtered(struct track_info *ti);
115131
struct track_info *lib_goto_next(void);
116132
struct track_info *lib_goto_prev(void);
117133
struct track_info *lib_goto_next_album(void);
@@ -139,9 +155,10 @@ struct tree_track *tree_get_selected(void);
139155
struct track_info *tree_activate_selected(void);
140156
const char *tree_artist_name(const struct track_info* ti);
141157
const char *tree_album_name(const struct track_info* ti);
142-
void tree_sort_artists(void (*add_album_cb)(struct album *), void (*remove_album_cb)(struct album *));
143-
void tree_add_track(struct tree_track *track, void (*add_album_cb)(struct album *));
144-
void tree_remove(struct tree_track *track, void (*remove_album_cb)(struct album *));
158+
void tree_sort_artists(void);
159+
void tree_add_track(struct tree_track *track);
160+
void tree_readd_track(struct tree_track *track);
161+
void tree_remove(struct tree_track *track);
145162
void tree_remove_sel(void);
146163
void tree_toggle_active_window(void);
147164
void tree_toggle_expand_artist(void);
@@ -182,6 +199,11 @@ static inline struct artist *to_artist(const struct rb_node *node)
182199
return container_of(node, struct artist, tree_node);
183200
}
184201

202+
static inline struct artist *all_artists_node_to_artist(const struct rb_node *node)
203+
{
204+
return container_of(node, struct artist, all_artists_node);
205+
}
206+
185207
static inline struct album *to_album(const struct rb_node *node)
186208
{
187209
return container_of(node, struct album, tree_node);

‎list.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct list_head {
4646
struct list_head *next, *prev;
4747
};
4848

49-
#define LIST_HEAD_INIT(name) { &(name), &(name) }
49+
#define LIST_HEAD_INIT(name) (struct list_head){ &(name), &(name) }
5050

5151
#define LIST_HEAD(name) \
5252
struct list_head name = LIST_HEAD_INIT(name)

0 commit comments

Comments
 (0)