Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 568d7397 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

CACHE: Get ride of long and complicated macro

parent 46de53cd
Branches
Étiquettes
1 requête de fusion!157Caching for karas & refactor & others
......@@ -5,15 +5,6 @@
#include <lektor/internal/dbmacro.h>
#include <math.h>
/* Cache operations */
#define _get_ 1
#define _set_ 2
/* The 'set' operation are not needed outside of this file, make them private */
#define DEF_PRIVATE(op) DEF_PRIVATE_##op
#define DEF_PRIVATE_2 PRIVATE_FUNCTION /* Set => private */
#define DEF_PRIVATE_1 /* Get => public */
/* Forward declarations for set operations */
PRIVATE_FUNCTION bool database_set_kara_mtime_id(volatile sqlite3 *, int, uint64_t);
PRIVATE_FUNCTION bool database_set_kara_duration_id(volatile sqlite3 *, int, uint64_t);
......@@ -76,40 +67,37 @@ database_cache_kara(volatile sqlite3 *db, int id)
LOG_INFO("CACHE", "Cached mtime %ld and duration %ld for '%s' kara %d", mtime, duration_uint, filepath, id);
}
#pragma message(TODO "Cache the 'get' with the next `N` karas. Use static __thread variables for that.")
#pragma message(TODO "Add private 'packed' get for cache values")
/* Cache and retry if the kara was not present in the cache */
#define ___cache_and_retry(what, pointer) \
if ((pointer what) == 0 && !retry_once) { \
database_cache_kara(db, id); \
retry_once = true; \
goto retry; \
#define ___cache_set_from_id(what) \
PRIVATE_FUNCTION bool database_set_kara_##what##_id(volatile sqlite3 *db, int id, uint64_t what) \
{ \
bool ret_code = false; \
sqlite3_stmt *stmt = NULL; \
SQLITE_PREPARE(db, stmt, "UPDATE kara SET cached_" #what " = ? WHERE id = ?;", error); \
SQLITE_BIND_INT(db, stmt, 1, what, error); \
SQLITE_BIND_INT(db, stmt, 2, id, error); \
SQLITE_STEP_OK(db, stmt, error); \
ret_code = true; \
error: \
sqlite3_finalize(stmt); \
return ret_code; \
}
/* Define a get/set operation on the cache */
#define ___cache_operation_db_function(op, what, pointer) \
DEF_PRIVATE(op) bool database##op##kara_##what##_id(volatile sqlite3 *db, int id, uint64_t pointer what) \
#define ___cache_get_from_id(what) \
bool database_get_kara_##what##_id(volatile sqlite3 *db, int id, uint64_t *what) \
{ \
/* The 'if true' / 'if false' will be suppressed by the compilator */ \
bool ret_code = false; \
bool retry_once = false; \
sqlite3_stmt *stmt = NULL; \
/* If define a GET operation */ \
if (op == _get_) { \
retry: \
pointer what = 0; \
*what = 0; \
SQLITE_PREPARE(db, stmt, "SELECT cached_" #what " FROM kara WHERE id = ?;", error); \
SQLITE_BIND_INT(db, stmt, 1, id, error); \
SQLITE_STEP_ROW(db, stmt, error); \
pointer what = sqlite3_column_int64(stmt, 0); \
___cache_and_retry(what, pointer); \
} /* If define a SET operation */ \
else if (op == _set_) { \
SQLITE_PREPARE(db, stmt, "UPDATE kara SET cached_" #what " = ? WHERE id = ?;", error); \
SQLITE_BIND_INT(db, stmt, 1, pointer what, error); \
SQLITE_BIND_INT(db, stmt, 2, id, error); \
SQLITE_STEP_OK(db, stmt, error); \
*what = sqlite3_column_int64(stmt, 0); \
if ((*what) == 0 && !retry_once) { \
database_cache_kara(db, id); \
retry_once = true; \
goto retry; \
} \
ret_code = true; \
error: \
......@@ -129,9 +117,9 @@ database_cache_kara(volatile sqlite3 *db, int id)
return database_get_kara_##what##_id(db, id, what); \
}
___cache_operation_db_function(_get_, mtime, *);
___cache_operation_db_function(_get_, duration, *);
___cache_operation_db_function(_set_, mtime, NOTHING);
___cache_operation_db_function(_set_, duration, NOTHING);
___cache_set_from_id(mtime);
___cache_set_from_id(duration);
___cache_get_from_id(mtime);
___cache_get_from_id(duration);
___cache_get_from_path(mtime);
___cache_get_from_path(duration);
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter