core: Defragment databases on startup

Databases can change either as part of an upgrade or through adding and
removing entries.  We can save a bit of memory by removing unallocated
entries during startup.

Implements #47: Automatic database defragment
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-12 14:00:11 -04:00
parent 85bfe717dd
commit d6e5e6c773
9 changed files with 49 additions and 0 deletions

View File

@ -9,6 +9,15 @@
#include <core/tags/tags.h>
static bool core_defragment(void *data)
{
if (tags_defragment()) {
playlist_save();
audio_save();
}
return true;
}
void core_init(int *argc, char ***argv, struct core_init_data *init)
{
#ifdef CONFIG_TESTING
@ -20,6 +29,8 @@ void core_init(int *argc, char ***argv, struct core_init_data *init)
tags_init();
playlist_init(init->playlist_ops);
audio_init(argc, argv, init->audio_ops);
idle_schedule(IDLE_SYNC, core_defragment, NULL);
}
void core_deinit()

View File

@ -300,6 +300,11 @@ void album_db_deinit()
db_deinit(&album_db);
}
bool album_db_defrag()
{
return db_defrag(&album_db);
}
bool album_db_upgrade_done()
{
struct db_entry *dbe, *next;

View File

@ -73,6 +73,11 @@ void library_db_deinit()
db_deinit(&library_db);
}
bool library_db_defrag()
{
return db_defrag(&library_db);
}
const struct database *library_db_get()
{
return &library_db;

View File

@ -35,3 +35,14 @@ void tags_deinit()
album_db_deinit();
artist_db_deinit();
}
bool tags_defragment(void *data)
{
bool album = album_db_defrag();
bool library = library_db_defrag();
bool track = track_db_defrag();
if (album || library || track)
track_db_commit();
return track;
}

View File

@ -212,6 +212,11 @@ void track_db_commit()
db_save(&track_db);
}
bool track_db_defrag()
{
return db_defrag(&track_db);
}
const struct database *track_db_get()
{
return &track_db;

View File

@ -36,6 +36,9 @@ void album_db_init();
/* Called to clean up the album database. */
void album_db_deinit();
/* Called to defragment the album database. */
bool album_db_defrag();
/* Called to clean up the album database after an upgrade. */
bool album_db_upgrade_done();

View File

@ -30,6 +30,9 @@ void library_db_init();
/* Called to clean up the library database. */
void library_db_deinit();
/* Called to defragment the library database. */
bool library_db_defrag();
/* Called to access the library database. */
const struct database *library_db_get();

View File

@ -11,4 +11,7 @@ void tags_init();
/* Called to clean up all databases. */
void tags_deinit();
/* Called to defragment all databases. */
bool tags_defragment();
#endif /* OCARINA_CORE_TAGS_TAGS_H */

View File

@ -69,6 +69,9 @@ void track_db_deinit();
/* Called to commit the track database. */
void track_db_commit();
/* Called to defragment the track database. */
bool track_db_defrag();
/* Called to access the track database. */
const struct database *track_db_get();