1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/*****************************************************************
* gmerlin - a general purpose multimedia framework and applications
*
* Copyright (c) 2001 - 2024 Members of the Gmerlin project
* http://github.com/bplaum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <config.h>
#include <gavl/gavl.h>
#include <gavl/metatags.h>
#include <gmerlin/bggavl.h>
#include <gmerlin/objectcache.h>
#include <gmerlin/utils.h>
#include <gmerlin/translation.h>
#include <gmerlin/log.h>
#define LOG_DOMAIN "objcache"
#define ROOT_NAME_ENTRY "cacheentry"
#define ROOT_NAME_INDEX "cacheindex"
#define INDEX_FILENAME "INDEX"
typedef struct
{
uint32_t md5[4];
gavl_value_t val;
char * id;
} mem_cache_entry_t;
typedef struct
{
uint32_t md5[4];
} disk_cache_entry_t;
struct bg_object_cache_s
{
int max_disk_cache_size;
int max_memory_cache_size;
int disk_cache_size;
int memory_cache_size;
mem_cache_entry_t * memory_cache;
disk_cache_entry_t * disk_cache;
char * directory;
};
static gavl_value_t * object_cache_put_nocopy(bg_object_cache_t * cache,
const char * id, gavl_value_t * val);
static gavl_value_t * object_cache_put(bg_object_cache_t * cache,
const char * id, const gavl_value_t * val);
static gavl_value_t * object_cache_prepend_nocopy(bg_object_cache_t * cache,
const char * id, gavl_value_t * val);
static void move_to_front_mem(mem_cache_entry_t * arr, int num, int idx)
{
mem_cache_entry_t sav;
memcpy(&sav, arr + idx, sizeof(sav));
if(idx < num - 1)
memmove(arr + idx, arr + (idx + 1), (num - 1 - idx) * sizeof(*arr));
if(num - 1 > 0)
memmove(arr + 1, arr, (num - 1) * sizeof(*arr));
memcpy(arr, &sav, sizeof(sav));
}
static void move_to_front_disk(disk_cache_entry_t * arr, int num, int idx)
{
disk_cache_entry_t sav;
memcpy(&sav, arr + idx, sizeof(sav));
if(idx < num - 1)
memmove(arr + idx, arr + (idx + 1), (num - 1 - idx) * sizeof(*arr));
if(num - 1 > 0)
memmove(arr + 1, arr, (num - 1) * sizeof(*arr));
memcpy(arr, &sav, sizeof(sav));
}
/* Utils */
/* md5 stuff */
static void id_2_md5(const char * id, void * ret)
{
gavl_md5_buffer(id, strlen(id), ret);
}
static char * create_filename(const bg_object_cache_t * cache, const uint32_t * md5)
{
char str[GAVL_MD5_LENGTH];
gavl_md5_2_string(md5, str);
return gavl_sprintf("%s/%s", cache->directory, str);
}
static void load_disk_index(bg_object_cache_t * cache)
{
char * filename;
const gavl_array_t * arr;
gavl_value_t val;
gavl_value_init(&val);
filename = gavl_sprintf("%s/%s", cache->directory, INDEX_FILENAME);
if(!access(filename, R_OK))
bg_value_load_xml(&val, filename, ROOT_NAME_INDEX);
free(filename);
cache->disk_cache_size = 0;
if((arr = gavl_value_get_array(&val)))
{
while((cache->disk_cache_size < cache->max_disk_cache_size) &&
(cache->disk_cache_size < arr->num_entries))
{
if(!gavl_string_2_md5(gavl_value_get_string(&arr->entries[cache->disk_cache_size]),
cache->disk_cache[cache->disk_cache_size].md5))
break;
cache->disk_cache_size++;
}
}
gavl_value_free(&val);
}
static void save_disk_index(const bg_object_cache_t * cache)
{
int i;
char * filename;
gavl_array_t * arr;
gavl_value_t val;
gavl_value_t el;
char md5_string[GAVL_MD5_LENGTH];
gavl_value_init(&val);
filename = gavl_sprintf("%s/%s", cache->directory, INDEX_FILENAME);
gavl_log(GAVL_LOG_INFO, LOG_DOMAIN, "Saving disk index %s", filename);
arr = gavl_value_set_array(&val);
for(i = 0; i < cache->disk_cache_size; i++)
{
gavl_value_init(&el);
gavl_md5_2_string(cache->disk_cache[i].md5, md5_string);
gavl_value_set_string(&el, md5_string);
gavl_array_splice_val_nocopy(arr, -1, 0, &el);
}
bg_value_save_xml(&val, filename, ROOT_NAME_INDEX);
gavl_value_free(&val);
free(filename);
}
static void delete_memory_cache_idx(bg_object_cache_t * cache,
int idx)
{
gavl_value_reset(&cache->memory_cache[idx].val);
if(idx < cache->memory_cache_size-1)
{
memmove(cache->memory_cache + idx,
cache->memory_cache + (idx + 1),
sizeof(*cache->memory_cache) * (cache->memory_cache_size-1-idx));
}
cache->memory_cache_size--;
}
static void delete_disk_cache_idx(bg_object_cache_t * cache,
int idx)
{
char * str = create_filename(cache, cache->disk_cache[idx].md5);
remove(str);
free(str);
if(idx < cache->disk_cache_size-1)
{
memmove(cache->disk_cache + idx,
cache->disk_cache + (idx + 1),
sizeof(*cache->disk_cache) * (cache->disk_cache_size-1-idx));
}
cache->disk_cache_size--;
}
/* object cache */
static int mem_cache_index(const bg_object_cache_t * cache, const uint32_t * md5)
{
int i;
for(i = 0; i < cache->memory_cache_size; i++)
{
if((cache->memory_cache[i].md5[0] == md5[0]) &&
(cache->memory_cache[i].md5[1] == md5[1]) &&
(cache->memory_cache[i].md5[2] == md5[2]) &&
(cache->memory_cache[i].md5[3] == md5[3]))
{
return i;
}
}
return -1;
}
static int disk_cache_index(const bg_object_cache_t * cache, const uint32_t * md5)
{
int i;
// fprintf(stderr, "disk_cache_index\n");
// gavl_hexdump((const uint8_t *)md5, 16, 16);
for(i = 0; i < cache->disk_cache_size; i++)
{
// gavl_hexdump((const uint8_t *)cache->disk_cache[i].md5, 16, 16);
if((cache->disk_cache[i].md5[0] == md5[0]) &&
(cache->disk_cache[i].md5[1] == md5[1]) &&
(cache->disk_cache[i].md5[2] == md5[2]) &&
(cache->disk_cache[i].md5[3] == md5[3]))
{
return i;
}
}
return -1;
}
gavl_value_t * bg_object_cache_get(bg_object_cache_t * cache, const char * id)
{
int idx;
int done = 0;
gavl_value_t * ret = NULL;
gavl_value_t * val = NULL;
uint32_t md5[4];
/* Calculate md5 of id */
id_2_md5(id, md5);
/* 1. Try memory cache */
if((idx = mem_cache_index(cache, md5)) >= 0)
{
move_to_front_mem(cache->memory_cache, cache->memory_cache_size, idx);
done = 1;
ret = &cache->memory_cache[0].val;
}
/* 2. Try disk cache */
if(!done)
{
if((idx = disk_cache_index(cache, md5)) >= 0)
{
gavl_dictionary_t dict;
char * filename = create_filename(cache, md5);
/* Load value from cache */
gavl_dictionary_init(&dict);
if((done = bg_dictionary_load_xml(&dict, filename, ROOT_NAME_ENTRY)))
{
val = gavl_dictionary_get_nc(&dict, "v");
ret = object_cache_prepend_nocopy(cache, id, val);
move_to_front_disk(cache->disk_cache, cache->disk_cache_size, idx);
}
free(filename);
gavl_dictionary_free(&dict);
}
}
return ret;
}
static void delete_memory_cache(bg_object_cache_t * cache,
uint32_t * md5)
{
int idx;
if((idx = mem_cache_index(cache, md5)) >= 0)
delete_memory_cache_idx(cache, idx);
}
static void delete_disk_cache(bg_object_cache_t * cache,
uint32_t * md5)
{
int idx;
if((idx = disk_cache_index(cache, md5)) >= 0)
delete_disk_cache_idx(cache, idx);
}
void bg_object_cache_delete(bg_object_cache_t * cache,
const char * id)
{
uint32_t md5[4];
id_2_md5(id, md5);
delete_memory_cache(cache, md5);
delete_disk_cache(cache, md5);
}
static void put_disk_cache_nocopy(bg_object_cache_t * cache, const uint32_t * md5,
gavl_value_t * val, const char * id)
{
int idx;
char * filename;
gavl_dictionary_t dict;
/* If entry is there, we don't rewrite it. Just move it to the front in the index */
if((idx = disk_cache_index(cache, md5)) >= 0)
{
disk_cache_entry_t tmp;
memcpy(&tmp, &cache->disk_cache[idx], sizeof(tmp));
if(idx < cache->disk_cache_size-1)
{
memmove(cache->disk_cache + idx,
cache->disk_cache + (idx + 1),
sizeof(*cache->disk_cache) * (cache->disk_cache_size-1-idx));
}
if(cache->disk_cache_size > 1)
memmove(&cache->disk_cache[1], &cache->disk_cache[0], (cache->disk_cache_size-1) * sizeof(*cache->disk_cache));
memcpy(&cache->disk_cache[0], &tmp, sizeof(tmp));
gavl_value_free(val);
return;
}
/* Make space in disk cache */
if(cache->disk_cache_size == cache->max_disk_cache_size)
delete_disk_cache_idx(cache, cache->disk_cache_size-1);
if(cache->disk_cache_size > 0)
memmove(&cache->disk_cache[1], &cache->disk_cache[0], cache->disk_cache_size * sizeof(*cache->disk_cache));
memcpy(cache->disk_cache[0].md5, md5, 4*sizeof(*md5));
cache->disk_cache_size++;
/* Save value */
gavl_dictionary_init(&dict);
gavl_dictionary_set_nocopy(&dict, "v", val);
filename = create_filename(cache, md5);
gavl_log(GAVL_LOG_INFO, LOG_DOMAIN, "Saving disk cache entry %s", filename);
bg_dictionary_save_xml(&dict, filename, ROOT_NAME_ENTRY);
free(filename);
gavl_dictionary_free(&dict);
}
static gavl_value_t * object_cache_prepend_nocopy(bg_object_cache_t * cache,
const char * id, gavl_value_t * val)
{
/* Make space in memory cache */
if(cache->memory_cache_size == cache->max_memory_cache_size)
{
mem_cache_entry_t * e = &cache->memory_cache[cache->max_memory_cache_size-1];
put_disk_cache_nocopy(cache, e->md5, &e->val, e->id);
cache->memory_cache_size--;
}
/* Move in front of the memory cache */
if(cache->memory_cache_size)
memmove(cache->memory_cache+1, cache->memory_cache, sizeof(*cache->memory_cache)*cache->memory_cache_size);
gavl_value_move(&cache->memory_cache[0].val, val);
gavl_md5_buffer(id, strlen(id), cache->memory_cache[0].md5);
cache->memory_cache_size++;
return &cache->memory_cache[0].val;
}
static gavl_value_t * object_cache_put_nocopy(bg_object_cache_t * cache,
const char * id, gavl_value_t * val)
{
bg_object_cache_delete(cache, id);
return object_cache_prepend_nocopy(cache, id, val);
}
static gavl_value_t * object_cache_put(bg_object_cache_t * cache,
const char * id, const gavl_value_t * val)
{
gavl_value_t v;
gavl_value_init(&v);
gavl_value_copy(&v, val);
return object_cache_put_nocopy(cache, id, &v);
}
gavl_value_t * bg_object_cache_put(bg_object_cache_t * cache,
const char * id, const gavl_value_t * val)
{
gavl_value_t * ret;
ret = object_cache_put(cache, id, val);
return ret;
}
gavl_value_t * bg_object_cache_put_nocopy(bg_object_cache_t * cache,
const char * id, gavl_value_t * val)
{
gavl_value_t * ret;
ret = object_cache_put_nocopy(cache, id, val);
return ret;
}
bg_object_cache_t * bg_object_cache_create(int max_disk_cache_size,
int max_memory_cache_size,
const char * directory)
{
bg_object_cache_t * ret = calloc(1, sizeof(*ret));
ret->max_disk_cache_size = max_disk_cache_size;
ret->max_memory_cache_size = max_memory_cache_size;
ret->disk_cache = calloc(ret->max_disk_cache_size, sizeof(*ret->disk_cache));
ret->memory_cache = calloc(ret->max_memory_cache_size, sizeof(*ret->memory_cache));
ret->directory = gavl_strdup(directory);
gavl_ensure_directory(ret->directory, 1);
load_disk_index(ret);
return ret;
}
void bg_object_cache_destroy(bg_object_cache_t * cache)
{
/* Move memory cache to disk.
We do this backwards such that the first entry in the memory cache becomes the
first entry in the disk cache */
int i;
for(i = cache->memory_cache_size-1; i >= 0; i--)
{
mem_cache_entry_t * e = cache->memory_cache + i;
put_disk_cache_nocopy(cache, e->md5, &e->val, e->id);
}
save_disk_index(cache);
/* Free stuff */
free(cache->memory_cache);
free(cache->disk_cache);
free(cache->directory);
free(cache);
}
|