Extra DB debug functions and cleanup
authorSteve McIntyre <steve@einval.com>
Mon, 24 Jan 2011 01:47:46 +0000 (01:47 +0000)
committerSteve McIntyre <steve@einval.com>
Mon, 24 Jan 2011 01:47:46 +0000 (01:47 +0000)
Rearrange order of functions: keep cache and size functions together.

Add dump_size_entry(), db_dump_size() and db_count_size() for future
debug purposes.

C/fmdb.c

index 724fe12..4c482ad 100644 (file)
--- a/C/fmdb.c
+++ b/C/fmdb.c
@@ -50,77 +50,6 @@ struct results *res_head = NULL;
 struct results *res_current = NULL;
 struct results *res_tail = NULL;
 
-#ifdef DB_DEBUG
-static char *str_state(db_cache_state_e state)
-{
-    switch (state)
-    {
-        case FMDB_CACHE_FREE:
-            return "FMDB_CACHE_FREE";
-        case FMDB_CACHE_ENCODING:
-            return "FMDB_CACHE_ENCODING";
-        case FMDB_CACHE_READING:
-            return "FMDB_CACHE_READING";
-        case FMDB_CACHE_DELETING:
-            return "FMDB_CACHE_DELETING";
-        default:
-            return "BOGUS STATE";
-    }
-}
-#endif
-
-static int db_create_size_table(db_state_t *dbp)
-{
-    int error = 0;
-    char *open_error;
-    char sql_command[2 * PATH_MAX];
-    
-    /* Delete the table and create new */
-    error = sqlite3_exec(dbp->db, "DROP TABLE sizes;", NULL, NULL, NULL);
-    sprintf(sql_command, "CREATE TABLE sizes ("
-            "flac_path VARCHAR(%d),"
-            "format_choice VARCHAR(10),"
-            "format_quality VARCHAR(10),"
-            "size INTEGER,"
-            "mtime INTEGER);", PATH_MAX);
-    error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
-    if (error)
-    {
-        DBLOG((logfile, "%s: got error %d (%s) from create\n",
-               __func__, error, open_error));
-        if (open_error)
-            sqlite3_free(open_error);
-        return error;
-    }
-    return 0;
-}
-
-static int db_create_cache_table(db_state_t *dbp)
-{
-    int error = 0;
-    char *open_error;
-    char sql_command[2 * PATH_MAX];
-    
-    /* Delete the table and create new */
-    error = sqlite3_exec(dbp->db, "DROP TABLE cache;", NULL, NULL, NULL);
-    sprintf(sql_command, "CREATE TABLE cache ("
-            "flac_path VARCHAR(%d),"
-            "cache_path VARCHAR(%d),"
-            "state INTEGER,"
-            "size INTEGER,"
-            "mtime INTEGER);", PATH_MAX, PATH_MAX);
-    error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
-    if (error)
-    {
-        DBLOG((logfile, "%s: got error %d (%s) from create\n",
-               __func__, error, open_error));
-        if (open_error)
-            sqlite3_free(open_error);
-        return error;
-    }
-    return 0;
-}
-
 static void free_results(void)
 {
     struct results *entry = res_head;
@@ -184,6 +113,106 @@ static int results_callback(void *pArg, int argc, char **argv, char **columnName
     return 0;
 }
 
+static int db_create_size_table(db_state_t *dbp)
+{
+    int error = 0;
+    char *open_error;
+    char sql_command[2 * PATH_MAX];
+    
+    /* Delete the table and create new */
+    error = sqlite3_exec(dbp->db, "DROP TABLE sizes;", NULL, NULL, NULL);
+    sprintf(sql_command, "CREATE TABLE sizes ("
+            "flac_path VARCHAR(%d),"
+            "format_choice VARCHAR(10),"
+            "format_quality VARCHAR(10),"
+            "size INTEGER,"
+            "mtime INTEGER);", PATH_MAX);
+    error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
+    if (error)
+    {
+        DBLOG((logfile, "%s: got error %d (%s) from create\n",
+               __func__, error, open_error));
+        if (open_error)
+            sqlite3_free(open_error);
+        return error;
+    }
+    return 0;
+}
+
+static void dump_size_entry(const db_size_entry_t *entry)
+{
+    DBLOG((logfile, "  flac_path: %s\n", entry->flac_path));
+    DBLOG((logfile, "  format_choice: %s\n", entry->format_choice));
+    DBLOG((logfile, "  format_quality: %s\n", entry->format_quality));
+    DBLOG((logfile, "  size: %lld\n", entry->size));
+    DBLOG((logfile, "  mtime: %ld\n", entry->mtime));
+}
+
+static int db_dump_size(FMDB *dbp)
+{
+    int error = 0;
+    db_state_t *state = dbp;
+    char *open_error;
+    int result_type = RES_CACHE;
+    char sql_command[2 * PATH_MAX];
+    int i = 0;
+
+    free_results();
+
+    sprintf(sql_command, "SELECT * FROM sizes");
+    error = sqlite3_exec(state->db, sql_command, results_callback, &result_type, &open_error);
+    if (error)
+    {
+        DBLOG((logfile, "%s: Failed to lookup, error %d (%s)\n",
+               __func__, error, open_error));
+        if (open_error)
+            sqlite3_free(open_error);
+        return error;
+    }
+
+    res_current = res_head;
+    DBLOG((logfile, "%s: db dump\n", __func__));
+    while (res_current)
+    {
+        DBLOG((logfile, "entry %d:\n", i++));
+        dump_size_entry(&res_current->data.size);
+        res_current = res_current->next;
+    }
+
+    return error;
+}
+
+static int db_count_size(FMDB *dbp)
+{
+    int error = 0;
+    db_state_t *state = dbp;
+    char *open_error;
+    int result_type = RES_COUNT;
+
+    free_results();
+
+    error = sqlite3_exec(state->db, "SELECT COUNT(*) FROM sizes;", results_callback, &result_type, &open_error);
+    if (error)
+    {
+        DBLOG((logfile, "%s: Failed to lookup, error %d (%s)\n",
+               __func__, error, open_error));
+        if (open_error)
+            sqlite3_free(open_error);
+        return error;
+    }
+
+    res_current = res_head;
+    if (res_current)
+    {
+        DBLOG((logfile, "%s: %d entries in the sizes DB\n", 
+               __func__, res_current->data.count.count));
+    }
+    else
+        error = ENOENT;
+
+    return error;
+}
+
 int db_store_size_entry(FMDB *dbp, const db_size_entry_t *in)
 {
     int error = 0;
@@ -290,7 +319,52 @@ int db_remove_size_entry(FMDB *dbp, const db_size_entry_t *in)
     return error;
 }
 
-void dump_cache_entry(const db_cache_entry_t *entry)
+static int db_create_cache_table(db_state_t *dbp)
+{
+    int error = 0;
+    char *open_error;
+    char sql_command[2 * PATH_MAX];
+    
+    /* Delete the table and create new */
+    error = sqlite3_exec(dbp->db, "DROP TABLE cache;", NULL, NULL, NULL);
+    sprintf(sql_command, "CREATE TABLE cache ("
+            "flac_path VARCHAR(%d),"
+            "cache_path VARCHAR(%d),"
+            "state INTEGER,"
+            "size INTEGER,"
+            "mtime INTEGER);", PATH_MAX, PATH_MAX);
+    error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
+    if (error)
+    {
+        DBLOG((logfile, "%s: got error %d (%s) from create\n",
+               __func__, error, open_error));
+        if (open_error)
+            sqlite3_free(open_error);
+        return error;
+    }
+    return 0;
+}
+
+#ifdef DB_DEBUG
+static char *str_state(db_cache_state_e state)
+{
+    switch (state)
+    {
+        case FMDB_CACHE_FREE:
+            return "FMDB_CACHE_FREE";
+        case FMDB_CACHE_ENCODING:
+            return "FMDB_CACHE_ENCODING";
+        case FMDB_CACHE_READING:
+            return "FMDB_CACHE_READING";
+        case FMDB_CACHE_DELETING:
+            return "FMDB_CACHE_DELETING";
+        default:
+            return "BOGUS STATE";
+    }
+}
+#endif
+
+static void dump_cache_entry(const db_cache_entry_t *entry)
 {
     DBLOG((logfile, "  flac_path: %s\n", entry->flac_path));
     DBLOG((logfile, "  cache_path: %s\n", entry->cache_path));
@@ -299,7 +373,7 @@ void dump_cache_entry(const db_cache_entry_t *entry)
     DBLOG((logfile, "  mtime: %ld\n", entry->mtime));
 }
 
-int db_dump_cache(FMDB *dbp)
+static int db_dump_cache(FMDB *dbp)
 {
     int error = 0;
     db_state_t *state = dbp;