Cache lms_db_* per DB instance instead of globally.
authorGustavo Sverzut Barbieri <barbieri@gmail.com>
Fri, 7 Dec 2007 14:35:37 +0000 (14:35 +0000)
committerGustavo Sverzut Barbieri <barbieri@gmail.com>
Fri, 7 Dec 2007 14:35:37 +0000 (14:35 +0000)
This change enable us to have various lms instances running on
multiple database connections at the same time.

src/lib/lightmediascanner_db_audio.c
src/lib/lightmediascanner_db_image.c
src/lib/lightmediascanner_db_playlist.c
src/lib/lightmediascanner_db_video.c

index b137e8b..5d3a1c5 100644 (file)
@@ -16,7 +16,7 @@ struct lms_db_audio {
     unsigned int _is_started:1;
 };
 
-static lms_db_audio_t *_singleton = NULL;
+static struct lms_db_cache _cache = {0, NULL};
 
 static int
 _db_create(sqlite3 *db, const char *name, const char *sql)
@@ -234,9 +234,9 @@ lms_db_audio_new(sqlite3 *db)
 {
     lms_db_audio_t *lda;
 
-    if (_singleton) {
-        _singleton->_references++;
-        return _singleton;
+    if (lms_db_cache_get(&_cache, db, (void**)&lda) == 0) {
+        lda->_references++;
+        return lda;
     }
 
     if (!db)
@@ -251,6 +251,11 @@ lms_db_audio_new(sqlite3 *db)
     lda->_references = 1;
     lda->db = db;
 
+    if (lms_db_cache_add(&_cache, db, lda) != 0) {
+        lms_db_audio_free(lda);
+        return NULL;
+    }
+
     return lda;
 }
 
@@ -306,6 +311,8 @@ lms_db_audio_start(lms_db_audio_t *lda)
 int
 lms_db_audio_free(lms_db_audio_t *lda)
 {
+    int r;
+
     if (!lda)
         return -1;
     if (lda->_references == 0) {
@@ -338,10 +345,10 @@ lms_db_audio_free(lms_db_audio_t *lda)
     if (lda->get_genre)
         lms_db_finalize_stmt(lda->get_genre, "get_genre");
 
+    r = lms_db_cache_del(&_cache, lda->db, lda);
     free(lda);
-    _singleton = NULL;
 
-    return 0;
+    return r;
 }
 
 static int
index 5eda552..2fddb84 100644 (file)
@@ -10,7 +10,7 @@ struct lms_db_image {
     unsigned int _is_started:1;
 };
 
-static lms_db_image_t *_singleton = NULL;
+static struct lms_db_cache _cache = {0, NULL};
 
 static int
 _db_table_updater_images_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
@@ -84,9 +84,9 @@ lms_db_image_new(sqlite3 *db)
 {
     lms_db_image_t *ldi;
 
-    if (_singleton) {
-        _singleton->_references++;
-        return _singleton;
+    if (lms_db_cache_get(&_cache, db, (void**)&ldi) == 0) {
+        ldi->_references++;
+        return ldi;
     }
 
     if (!db)
@@ -101,6 +101,11 @@ lms_db_image_new(sqlite3 *db)
     ldi->_references = 1;
     ldi->db = db;
 
+    if (lms_db_cache_add(&_cache, db, ldi) != 0) {
+        lms_db_image_free(ldi);
+        return NULL;
+    }
+
     return ldi;
 }
 
@@ -127,6 +132,8 @@ lms_db_image_start(lms_db_image_t *ldi)
 int
 lms_db_image_free(lms_db_image_t *ldi)
 {
+    int r;
+
     if (!ldi)
         return -1;
     if (ldi->_references == 0) {
@@ -141,10 +148,10 @@ lms_db_image_free(lms_db_image_t *ldi)
     if (ldi->insert)
         lms_db_finalize_stmt(ldi->insert, "insert");
 
+    r = lms_db_cache_del(&_cache, ldi->db, ldi);
     free(ldi);
-    _singleton = NULL;
 
-    return 0;
+    return r;
 }
 
 static int
index 696db80..60d3333 100644 (file)
@@ -10,7 +10,7 @@ struct lms_db_playlist {
     unsigned int _is_started:1;
 };
 
-static lms_db_playlist_t *_singleton = NULL;
+static struct lms_db_cache _cache = {0, NULL};
 
 static int
 _db_table_updater_playlists_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
@@ -78,9 +78,9 @@ lms_db_playlist_new(sqlite3 *db)
 {
     lms_db_playlist_t *ldp;
 
-    if (_singleton) {
-        _singleton->_references++;
-        return _singleton;
+    if (lms_db_cache_get(&_cache, db, (void**)&ldp) == 0) {
+        ldp->_references++;
+        return ldp;
     }
 
     if (!db)
@@ -95,6 +95,11 @@ lms_db_playlist_new(sqlite3 *db)
     ldp->_references = 1;
     ldp->db = db;
 
+    if (lms_db_cache_add(&_cache, db, ldp) != 0) {
+        lms_db_playlist_free(ldp);
+        return NULL;
+    }
+
     return ldp;
 }
 
@@ -119,6 +124,8 @@ lms_db_playlist_start(lms_db_playlist_t *ldp)
 int
 lms_db_playlist_free(lms_db_playlist_t *ldp)
 {
+    int r;
+
     if (!ldp)
         return -1;
     if (ldp->_references == 0) {
@@ -133,10 +140,10 @@ lms_db_playlist_free(lms_db_playlist_t *ldp)
     if (ldp->insert)
         lms_db_finalize_stmt(ldp->insert, "insert");
 
+    r = lms_db_cache_del(&_cache, ldp->db, ldp);
     free(ldp);
-    _singleton = NULL;
 
-    return 0;
+    return r;
 }
 
 static int
index 8cae823..f2c8903 100644 (file)
@@ -10,7 +10,7 @@ struct lms_db_video {
     unsigned int _is_started:1;
 };
 
-static lms_db_video_t *_singleton = NULL;
+static struct lms_db_cache _cache = {0, NULL};
 
 static int
 _db_table_updater_videos_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
@@ -91,9 +91,9 @@ lms_db_video_new(sqlite3 *db)
 {
     lms_db_video_t *ldv;
 
-    if (_singleton) {
-        _singleton->_references++;
-        return _singleton;
+    if (lms_db_cache_get(&_cache, db, (void**)&ldv) == 0) {
+        ldv->_references++;
+        return ldv;
     }
 
     if (!db)
@@ -108,6 +108,11 @@ lms_db_video_new(sqlite3 *db)
     ldv->_references = 1;
     ldv->db = db;
 
+    if (lms_db_cache_add(&_cache, db, ldv) != 0) {
+        lms_db_video_free(ldv);
+        return NULL;
+    }
+
     return ldv;
 }
 
@@ -131,6 +136,8 @@ lms_db_video_start(lms_db_video_t *ldv)
 int
 lms_db_video_free(lms_db_video_t *ldv)
 {
+    int r;
+
     if (!ldv)
         return -1;
     if (ldv->_references == 0) {
@@ -145,10 +152,10 @@ lms_db_video_free(lms_db_video_t *ldv)
     if (ldv->insert)
         lms_db_finalize_stmt(ldv->insert, "insert");
 
+    r = lms_db_cache_del(&_cache, ldv->db, ldv);
     free(ldv);
-    _singleton = NULL;
 
-    return 0;
+    return r;
 }
 
 static int