From bb106c3bf8daf366a4b4ca7229010c038f89755c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 12 Oct 2008 03:35:45 -0700 Subject: directory: always maintain sorted properties vectors This allows clients to see sorted results while we're updating the DB and removes the need for us to have to sort manually. We'll have to write separate routines for managing stored playlists with songvecs eventually; but that's for another day. --- src/dirvec.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'src/dirvec.c') diff --git a/src/dirvec.c b/src/dirvec.c index 228c54be9..91b371b8a 100644 --- a/src/dirvec.c +++ b/src/dirvec.c @@ -18,13 +18,6 @@ static int dirvec_cmp(const void *d1, const void *d2) return strcmp(a->path, b->path); } -void dirvec_sort(struct dirvec *dv) -{ - pthread_mutex_lock(&nr_lock); - qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp); - pthread_mutex_unlock(&nr_lock); -} - struct directory *dirvec_find(const struct dirvec *dv, const char *path) { int i; @@ -69,10 +62,14 @@ int dirvec_delete(struct dirvec *dv, struct directory *del) void dirvec_add(struct dirvec *dv, struct directory *add) { + size_t old_nr; + pthread_mutex_lock(&nr_lock); - ++dv->nr; + old_nr = dv->nr++; dv->base = xrealloc(dv->base, dv_size(dv)); - dv->base[dv->nr - 1] = add; + dv->base[old_nr] = add; + if (old_nr && dirvec_cmp(&dv->base[old_nr - 1], &add) >= 0) + qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp); pthread_mutex_unlock(&nr_lock); } -- cgit v1.2.3