aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-10-12 03:35:45 -0700
committerEric Wong <normalperson@yhbt.net>2008-10-12 05:27:34 -0700
commitbb106c3bf8daf366a4b4ca7229010c038f89755c (patch)
tree2db77aeed63d095a5533fb6f5d175af4cb10dade /src
parentd60f11020f4901b806c822ef5ab2d87bbb9e4077 (diff)
downloadmpd-bb106c3bf8daf366a4b4ca7229010c038f89755c.tar.gz
mpd-bb106c3bf8daf366a4b4ca7229010c038f89755c.tar.xz
mpd-bb106c3bf8daf366a4b4ca7229010c038f89755c.zip
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.
Diffstat (limited to '')
-rw-r--r--src/database.c4
-rw-r--r--src/directory.c13
-rw-r--r--src/directory.h2
-rw-r--r--src/dirvec.c15
-rw-r--r--src/dirvec.h2
-rw-r--r--src/songvec.c15
-rw-r--r--src/songvec.h2
7 files changed, 12 insertions, 41 deletions
diff --git a/src/database.c b/src/database.c
index a9e80f425..733180fea 100644
--- a/src/database.c
+++ b/src/database.c
@@ -183,10 +183,6 @@ int db_save(void)
DEBUG("removing empty directories from DB\n");
directory_prune_empty(&music_root);
- DEBUG("sorting DB\n");
-
- directory_sort(&music_root);
-
DEBUG("writing DB\n");
fd = open(dbFile, O_WRONLY|O_TRUNC|O_CREAT, 0666);
diff --git a/src/directory.c b/src/directory.c
index 709353476..3ebac6830 100644
--- a/src/directory.c
+++ b/src/directory.c
@@ -110,19 +110,6 @@ directory_get_subdir(struct directory *dir, const char *name)
return found;
}
-static int directory_sort_x(struct directory *dir, mpd_unused void *arg)
-{
- directory_sort(dir);
- return 0;
-}
-
-void directory_sort(struct directory *dir)
-{
- dirvec_sort(&dir->children);
- dirvec_for_each(&dir->children, directory_sort_x, NULL);
- songvec_sort(&dir->songs);
-}
-
struct dirwalk_arg {
int (*each_song) (struct mpd_song *, void *);
int (*each_dir) (struct directory *, void *);
diff --git a/src/directory.h b/src/directory.h
index bb2888034..8f0d797a8 100644
--- a/src/directory.h
+++ b/src/directory.h
@@ -92,8 +92,6 @@ int directory_save(int fd, struct directory *dir);
void directory_load(FILE *fp, struct directory *dir);
-void directory_sort(struct directory *dir);
-
int db_walk(const char *name,
int (*forEachSong) (struct mpd_song *, void *),
int (*forEachDir) (struct directory *, void *), void *data);
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);
}
diff --git a/src/dirvec.h b/src/dirvec.h
index b820e8739..6709537b8 100644
--- a/src/dirvec.h
+++ b/src/dirvec.h
@@ -8,8 +8,6 @@ struct dirvec {
size_t nr;
};
-void dirvec_sort(struct dirvec *dv);
-
struct directory *dirvec_find(const struct dirvec *dv, const char *path);
int dirvec_delete(struct dirvec *dv, struct directory *del);
diff --git a/src/songvec.c b/src/songvec.c
index 3d512e917..1a9635044 100644
--- a/src/songvec.c
+++ b/src/songvec.c
@@ -17,13 +17,6 @@ static size_t sv_size(struct songvec *sv)
return sv->nr * sizeof(struct mpd_song *);
}
-void songvec_sort(struct songvec *sv)
-{
- pthread_mutex_lock(&nr_lock);
- qsort(sv->base, sv->nr, sizeof(struct mpd_song *), songvec_cmp);
- pthread_mutex_unlock(&nr_lock);
-}
-
struct mpd_song *songvec_find(const struct songvec *sv, const char *url)
{
int i;
@@ -68,10 +61,14 @@ int songvec_delete(struct songvec *sv, const struct mpd_song *del)
void songvec_add(struct songvec *sv, struct mpd_song *add)
{
+ size_t old_nr;
+
pthread_mutex_lock(&nr_lock);
- ++sv->nr;
+ old_nr = sv->nr++;
sv->base = xrealloc(sv->base, sv_size(sv));
- sv->base[sv->nr - 1] = add;
+ sv->base[old_nr] = add;
+ if (old_nr && songvec_cmp(&sv->base[old_nr - 1], &add) >= 0)
+ qsort(sv->base, sv->nr, sizeof(struct mpd_song *), songvec_cmp);
pthread_mutex_unlock(&nr_lock);
}
diff --git a/src/songvec.h b/src/songvec.h
index 633cf8d66..10a896052 100644
--- a/src/songvec.h
+++ b/src/songvec.h
@@ -9,8 +9,6 @@ struct songvec {
size_t nr;
};
-void songvec_sort(struct songvec *sv);
-
struct mpd_song *songvec_find(const struct songvec *sv, const char *url);
int songvec_delete(struct songvec *sv, const struct mpd_song *del);