| 1 | /* |
| 2 | Copyright (C) 2009-2017 Brazil |
| 3 | Copyright (C) 2021-2023 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn_cache.h" |
| 21 | #include "grn_ctx.h" |
| 22 | #include "grn_ctx_impl.h" |
| 23 | #include "grn_hash.h" |
| 24 | #include "grn_pat.h" |
| 25 | #include "grn_store.h" |
| 26 | #include "grn_db.h" |
| 27 | #include "grn_file_lock.h" |
| 28 | |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | typedef uint8_t grn_cache_flags; |
| 32 | #define GRN_CACHE_FLAG_WITH_TRACE_LOG (1 << 0) |
| 33 | |
| 34 | typedef struct _grn_cache_entry_memory grn_cache_entry_memory; |
| 35 | |
| 36 | struct _grn_cache_entry_memory { |
| 37 | grn_cache_entry_memory *next; |
| 38 | grn_cache_entry_memory *prev; |
| 39 | grn_obj *value; |
| 40 | grn_timeval tv; |
| 41 | grn_id id; |
| 42 | }; |
| 43 | |
| 44 | typedef struct _grn_cache_entry_persistent_data { |
| 45 | grn_id next; |
| 46 | grn_id prev; |
| 47 | grn_timeval modified_time; |
| 48 | } grn_cache_entry_persistent_data; |
| 49 | |
| 50 | /* |
| 51 | sizeof(grn_cache_entry_persistent_metadata) should be equal or smaller |
| 52 | than sizeof(grn_cache_entry_persistent_data). |
| 53 | */ |
| 54 | typedef struct _grn_cache_entry_persistent_metadata { |
| 55 | uint32_t max_nentries; |
| 56 | uint32_t nfetches; |
| 57 | uint32_t nhits; |
| 58 | uint32_t flags; |
| 59 | } grn_cache_entry_persistent_metadata; |
| 60 | |
| 61 | typedef union _grn_cache_entry_persistent { |
| 62 | grn_cache_entry_persistent_data data; |
| 63 | grn_cache_entry_persistent_metadata metadata; |
| 64 | } grn_cache_entry_persistent; |
| 65 | |
| 66 | struct _grn_cache { |
| 67 | union { |
| 68 | struct { |
| 69 | /* next and prev is the same layout as grn_cache_entry_memory to |
| 70 | * use this struct as the root entry. */ |
| 71 | grn_cache_entry_memory *next; |
| 72 | grn_cache_entry_memory *prev; |
| 73 | grn_hash *hash; |
| 74 | grn_mutex mutex; |
| 75 | uint32_t max_nentries; |
| 76 | uint32_t nfetches; |
| 77 | uint32_t nhits; |
| 78 | } memory; |
| 79 | struct { |
| 80 | grn_hash *keys; |
| 81 | grn_ja *values; |
| 82 | int timeout; |
| 83 | } persistent; |
| 84 | } impl; |
| 85 | bool is_memory; |
| 86 | grn_ctx *ctx; |
| 87 | }; |
| 88 | |
| 89 | #define GRN_CACHE_PERSISTENT_ROOT_ID 1 |
| 90 | #define GRN_CACHE_PERSISTENT_ROOT_KEY "\0" |
| 91 | #define GRN_CACHE_PERSISTENT_ROOT_KEY_LEN \ |
| 92 | (sizeof(GRN_CACHE_PERSISTENT_ROOT_KEY) - 1) |
| 93 | #define GRN_CACHE_PERSISTENT_METADATA_ID 2 |
| 94 | #define GRN_CACHE_PERSISTENT_METADATA_KEY "\1" |
| 95 | #define GRN_CACHE_PERSISTENT_METADATA_KEY_LEN \ |
| 96 | (sizeof(GRN_CACHE_PERSISTENT_METADATA_KEY) - 1) |
| 97 | |
| 98 | static grn_ctx grn_cache_ctx; |
| 99 | static grn_cache *grn_cache_current = NULL; |
| 100 | static grn_cache *grn_cache_default = NULL; |
| 101 | static char grn_cache_default_base_path[PATH_MAX]; |
| 102 | |
| 103 | void |
| 104 | grn_set_default_cache_base_path(const char *base_path) |
| 105 | { |
| 106 | if (base_path) { |
| 107 | grn_strcpy(grn_cache_default_base_path, |
| 108 | PATH_MAX, |
| 109 | base_path); |
| 110 | } else { |
| 111 | grn_cache_default_base_path[0] = '\0'; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | const char * |
| 116 | grn_get_default_cache_base_path(void) |
| 117 | { |
| 118 | if (grn_cache_default_base_path[0] == '\0') { |
| 119 | return NULL; |
| 120 | } else { |
| 121 | return grn_cache_default_base_path; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | grn_cache_open_memory(grn_ctx *ctx, grn_cache *cache) |
| 127 | { |
| 128 | cache->impl.memory.next = (grn_cache_entry_memory *)&(cache->impl.memory); |
| 129 | cache->impl.memory.prev = (grn_cache_entry_memory *)&(cache->impl.memory); |
| 130 | cache->impl.memory.hash = grn_hash_create(cache->ctx, |
| 131 | NULL, |
| 132 | GRN_CACHE_MAX_KEY_SIZE, |
| 133 | sizeof(grn_cache_entry_memory), |
| 134 | GRN_OBJ_KEY_VAR_SIZE); |
| 135 | if (!cache->impl.memory.hash) { |
| 136 | ERR(GRN_NO_MEMORY_AVAILABLE, "[cache] failed to create hash table"); |
| 137 | return; |
| 138 | } |
| 139 | MUTEX_INIT(cache->impl.memory.mutex); |
| 140 | |
| 141 | cache->impl.memory.max_nentries = GRN_CACHE_DEFAULT_MAX_N_ENTRIES; |
| 142 | cache->impl.memory.nfetches = 0; |
| 143 | cache->impl.memory.nhits = 0; |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | grn_cache_open_persistent(grn_ctx *ctx, |
| 148 | grn_cache *cache, |
| 149 | const char *base_path) |
| 150 | { |
| 151 | grn_file_lock file_lock; |
| 152 | char *keys_path = NULL; |
| 153 | char *values_path = NULL; |
| 154 | char lock_path_buffer[PATH_MAX]; |
| 155 | char keys_path_buffer[PATH_MAX]; |
| 156 | char values_path_buffer[PATH_MAX]; |
| 157 | |
| 158 | cache->impl.persistent.timeout = 1000; |
| 159 | |
| 160 | if (base_path) { |
| 161 | struct stat stat_buffer; |
| 162 | |
| 163 | grn_snprintf(lock_path_buffer, PATH_MAX, PATH_MAX, "%s.lock", base_path); |
| 164 | grn_file_lock_init(ctx, &file_lock, lock_path_buffer); |
| 165 | |
| 166 | grn_snprintf(keys_path_buffer, PATH_MAX, PATH_MAX, "%s.keys", base_path); |
| 167 | grn_snprintf(values_path_buffer, PATH_MAX, PATH_MAX, "%s.values", base_path); |
| 168 | keys_path = keys_path_buffer; |
| 169 | values_path = values_path_buffer; |
| 170 | |
| 171 | if (!grn_file_lock_acquire(ctx, |
| 172 | &file_lock, |
| 173 | cache->impl.persistent.timeout, |
| 174 | "[cache][persistent][open]")) { |
| 175 | goto exit; |
| 176 | } |
| 177 | |
| 178 | if (stat(keys_path, &stat_buffer) == 0) { |
| 179 | cache->impl.persistent.keys = grn_hash_open(ctx, keys_path); |
| 180 | if (cache->impl.persistent.keys) { |
| 181 | cache->impl.persistent.values = grn_ja_open(ctx, values_path); |
| 182 | } |
| 183 | } |
| 184 | if (!cache->impl.persistent.keys) { |
| 185 | if (cache->impl.persistent.values) { |
| 186 | grn_ja_close(ctx, cache->impl.persistent.values); |
| 187 | cache->impl.persistent.values = NULL; |
| 188 | } |
| 189 | if (stat(keys_path, &stat_buffer) == 0) { |
| 190 | if (grn_hash_remove(ctx, keys_path) != GRN_SUCCESS) { |
| 191 | ERRNO_ERR("[cache][persistent] " |
| 192 | "failed to remove path for cache keys: <%s>", |
| 193 | keys_path); |
| 194 | goto exit; |
| 195 | } |
| 196 | } |
| 197 | if (stat(values_path, &stat_buffer) == 0) { |
| 198 | if (grn_ja_remove(ctx, values_path) != GRN_SUCCESS) { |
| 199 | ERRNO_ERR("[cache][persistent] " |
| 200 | "failed to remove path for cache values: <%s>", |
| 201 | values_path); |
| 202 | goto exit; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (!cache->impl.persistent.keys) { |
| 209 | cache->impl.persistent.keys = |
| 210 | grn_hash_create(ctx, |
| 211 | keys_path, |
| 212 | GRN_CACHE_MAX_KEY_SIZE, |
| 213 | sizeof(grn_cache_entry_persistent), |
| 214 | GRN_OBJ_KEY_VAR_SIZE); |
| 215 | if (!cache->impl.persistent.keys) { |
| 216 | ERR(ctx->rc == GRN_SUCCESS ? GRN_FILE_CORRUPT : ctx->rc, |
| 217 | "[cache][persistent] failed to create cache keys storage: <%s>", |
| 218 | keys_path ? keys_path : "(memory)"); |
| 219 | goto exit; |
| 220 | } |
| 221 | cache->impl.persistent.values = |
| 222 | grn_ja_create(ctx, |
| 223 | values_path, |
| 224 | 1 << 16, |
| 225 | 0); |
| 226 | if (!cache->impl.persistent.values) { |
| 227 | grn_hash_close(ctx, cache->impl.persistent.keys); |
| 228 | ERR(ctx->rc == GRN_SUCCESS ? GRN_FILE_CORRUPT : ctx->rc, |
| 229 | "[cache][persistent] failed to create cache values storage: <%s>", |
| 230 | values_path ? values_path : "(memory)"); |
| 231 | goto exit; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | { |
| 236 | grn_cache_entry_persistent *entry; |
| 237 | grn_id root_id; |
| 238 | int added; |
| 239 | |
| 240 | root_id = grn_hash_add(ctx, |
| 241 | cache->impl.persistent.keys, |
| 242 | GRN_CACHE_PERSISTENT_ROOT_KEY, |
| 243 | GRN_CACHE_PERSISTENT_ROOT_KEY_LEN, |
| 244 | (void **)&entry, |
| 245 | &added); |
| 246 | if (root_id != GRN_CACHE_PERSISTENT_ROOT_ID) { |
| 247 | grn_ja_close(ctx, cache->impl.persistent.values); |
| 248 | grn_hash_close(ctx, cache->impl.persistent.keys); |
| 249 | if (values_path) { |
| 250 | grn_ja_remove(ctx, values_path); |
| 251 | } |
| 252 | if (keys_path) { |
| 253 | grn_hash_remove(ctx, keys_path); |
| 254 | } |
| 255 | ERR(ctx->rc == GRN_SUCCESS ? GRN_FILE_CORRUPT : ctx->rc, |
| 256 | "[cache][persistent] broken cache keys storage: broken root: <%s>", |
| 257 | keys_path ? keys_path : "(memory)"); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (added) { |
| 262 | entry->data.next = root_id; |
| 263 | entry->data.prev = root_id; |
| 264 | entry->data.modified_time.tv_sec = 0; |
| 265 | entry->data.modified_time.tv_nsec = 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | { |
| 270 | grn_cache_entry_persistent *entry; |
| 271 | grn_id metadata_id; |
| 272 | int added; |
| 273 | |
| 274 | metadata_id = grn_hash_add(ctx, |
| 275 | cache->impl.persistent.keys, |
| 276 | GRN_CACHE_PERSISTENT_METADATA_KEY, |
| 277 | GRN_CACHE_PERSISTENT_METADATA_KEY_LEN, |
| 278 | (void **)&entry, |
| 279 | &added); |
| 280 | if (metadata_id != GRN_CACHE_PERSISTENT_METADATA_ID) { |
| 281 | grn_ja_close(ctx, cache->impl.persistent.values); |
| 282 | grn_hash_close(ctx, cache->impl.persistent.keys); |
| 283 | if (values_path) { |
| 284 | grn_ja_remove(ctx, values_path); |
| 285 | } |
| 286 | if (keys_path) { |
| 287 | grn_hash_remove(ctx, keys_path); |
| 288 | } |
| 289 | ERR(ctx->rc == GRN_SUCCESS ? GRN_FILE_CORRUPT : ctx->rc, |
| 290 | "[cache][persistent] broken cache keys storage: broken metadata: <%s>", |
| 291 | keys_path ? keys_path : "(memory)"); |
| 292 | goto exit; |
| 293 | } |
| 294 | |
| 295 | if (added) { |
| 296 | entry->metadata.max_nentries = GRN_CACHE_DEFAULT_MAX_N_ENTRIES; |
| 297 | entry->metadata.nfetches = 0; |
| 298 | entry->metadata.nhits = 0; |
| 299 | entry->metadata.flags = GRN_CACHE_FLAG_WITH_TRACE_LOG; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | exit : |
| 304 | if (base_path) { |
| 305 | grn_file_lock_release(ctx, &file_lock); |
| 306 | grn_file_lock_fin(ctx, &file_lock); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | static grn_cache * |
| 311 | grn_cache_open_raw(grn_ctx *ctx, |
| 312 | bool is_memory, |
| 313 | const char *base_path) |
| 314 | { |
| 315 | grn_cache *cache = NULL; |
| 316 | |
| 317 | GRN_API_ENTER; |
| 318 | cache = GRN_CALLOC(sizeof(grn_cache)); |
| 319 | if (!cache) { |
| 320 | ERR(GRN_NO_MEMORY_AVAILABLE, "[cache] failed to allocate grn_cache"); |
| 321 | goto exit; |
| 322 | } |
| 323 | |
| 324 | cache->ctx = ctx; |
| 325 | cache->is_memory = is_memory; |
| 326 | if (cache->is_memory) { |
| 327 | grn_cache_open_memory(ctx, cache); |
| 328 | } else { |
| 329 | grn_cache_open_persistent(ctx, cache, base_path); |
| 330 | } |
| 331 | if (ctx->rc != GRN_SUCCESS) { |
| 332 | GRN_FREE(cache); |
| 333 | cache = NULL; |
| 334 | goto exit; |
| 335 | } |
| 336 | |
| 337 | exit : |
| 338 | GRN_API_RETURN(cache); |
| 339 | } |
| 340 | |
| 341 | grn_cache * |
| 342 | grn_cache_open(grn_ctx *ctx) |
| 343 | { |
| 344 | const char *base_path = NULL; |
| 345 | bool is_memory; |
| 346 | |
| 347 | if (grn_cache_default_base_path[0] != '\0') { |
| 348 | base_path = grn_cache_default_base_path; |
| 349 | } |
| 350 | |
| 351 | if (base_path) { |
| 352 | is_memory = false; |
| 353 | } else { |
| 354 | char grn_cache_type_env[GRN_ENV_BUFFER_SIZE]; |
| 355 | grn_getenv("GRN_CACHE_TYPE", grn_cache_type_env, GRN_ENV_BUFFER_SIZE); |
| 356 | if (strcmp(grn_cache_type_env, "persistent") == 0) { |
| 357 | is_memory = false; |
| 358 | } else { |
| 359 | is_memory = true; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return grn_cache_open_raw(ctx, is_memory, base_path); |
| 364 | } |
| 365 | |
| 366 | grn_cache * |
| 367 | grn_persistent_cache_open(grn_ctx *ctx, const char *base_path) |
| 368 | { |
| 369 | bool is_memory = false; |
| 370 | return grn_cache_open_raw(ctx, is_memory, base_path); |
| 371 | } |
| 372 | |
| 373 | |
| 374 | static void |
| 375 | grn_cache_close_memory(grn_ctx *ctx, grn_cache *cache) |
| 376 | { |
| 377 | GRN_HASH_EACH_BEGIN(ctx, cache->impl.memory.hash, cursor, id) { |
| 378 | void *value; |
| 379 | grn_hash_cursor_get_value(ctx, cursor, &value); |
| 380 | grn_cache_entry_memory *entry = value; |
| 381 | grn_obj_close(ctx, entry->value); |
| 382 | } GRN_HASH_EACH_END(ctx, cursor); |
| 383 | grn_hash_close(ctx, cache->impl.memory.hash); |
| 384 | MUTEX_FIN(cache->impl.memory.mutex); |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | grn_cache_close_persistent(grn_ctx *ctx, grn_cache *cache) |
| 389 | { |
| 390 | grn_hash_close(ctx, cache->impl.persistent.keys); |
| 391 | grn_ja_close(ctx, cache->impl.persistent.values); |
| 392 | } |
| 393 | |
| 394 | grn_rc |
| 395 | grn_cache_close(grn_ctx *ctx_not_used, grn_cache *cache) |
| 396 | { |
| 397 | grn_ctx *ctx = cache->ctx; |
| 398 | |
| 399 | GRN_API_ENTER; |
| 400 | |
| 401 | if (cache->is_memory) { |
| 402 | grn_cache_close_memory(ctx, cache); |
| 403 | } else { |
| 404 | grn_cache_close_persistent(ctx, cache); |
| 405 | } |
| 406 | GRN_FREE(cache); |
| 407 | |
| 408 | GRN_API_RETURN(ctx->rc); |
| 409 | } |
| 410 | |
| 411 | grn_rc |
| 412 | grn_cache_current_set(grn_ctx *ctx, grn_cache *cache) |
| 413 | { |
| 414 | grn_cache_current = cache; |
| 415 | return GRN_SUCCESS; |
| 416 | } |
| 417 | |
| 418 | grn_cache * |
| 419 | grn_cache_current_get(grn_ctx *ctx) |
| 420 | { |
| 421 | if (ctx && ctx->impl && ctx->impl->db && ((grn_db *)ctx->impl->db)->cache) { |
| 422 | return ((grn_db *)ctx->impl->db)->cache; |
| 423 | } |
| 424 | return grn_cache_current; |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | grn_cache_init(void) |
| 429 | { |
| 430 | grn_ctx *ctx = &grn_cache_ctx; |
| 431 | |
| 432 | grn_ctx_init(ctx, 0); |
| 433 | |
| 434 | grn_cache_default = grn_cache_open(ctx); |
| 435 | grn_cache_current_set(ctx, grn_cache_default); |
| 436 | } |
| 437 | |
| 438 | grn_rc |
| 439 | grn_cache_default_reopen(void) |
| 440 | { |
| 441 | grn_ctx *ctx = &grn_cache_ctx; |
| 442 | grn_cache *new_default; |
| 443 | bool default_is_current; |
| 444 | |
| 445 | GRN_API_ENTER; |
| 446 | |
| 447 | new_default = grn_cache_open(ctx); |
| 448 | if (!new_default) { |
| 449 | GRN_API_RETURN(ctx->rc); |
| 450 | } |
| 451 | |
| 452 | default_is_current = (grn_cache_default == grn_cache_current_get(ctx)); |
| 453 | if (default_is_current) { |
| 454 | grn_cache_current_set(ctx, new_default); |
| 455 | } |
| 456 | |
| 457 | if (grn_cache_default) { |
| 458 | grn_cache_close(ctx, grn_cache_default); |
| 459 | } |
| 460 | grn_cache_default = new_default; |
| 461 | |
| 462 | GRN_API_RETURN(ctx->rc); |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | grn_cache_expire_entry_memory(grn_cache *cache, grn_cache_entry_memory *ce) |
| 467 | { |
| 468 | ce->prev->next = ce->next; |
| 469 | ce->next->prev = ce->prev; |
| 470 | grn_obj_close(cache->ctx, ce->value); |
| 471 | grn_hash_delete_by_id(cache->ctx, cache->impl.memory.hash, ce->id, NULL); |
| 472 | } |
| 473 | |
| 474 | static void |
| 475 | grn_cache_entry_persistent_delete_link(grn_cache *cache, |
| 476 | grn_cache_entry_persistent *entry) |
| 477 | { |
| 478 | grn_ctx *ctx = cache->ctx; |
| 479 | grn_hash *keys = cache->impl.persistent.keys; |
| 480 | grn_cache_entry_persistent *prev_entry; |
| 481 | grn_cache_entry_persistent *next_entry; |
| 482 | |
| 483 | prev_entry = |
| 484 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 485 | keys, |
| 486 | entry->data.prev, |
| 487 | NULL); |
| 488 | next_entry = |
| 489 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 490 | keys, |
| 491 | entry->data.next, |
| 492 | NULL); |
| 493 | prev_entry->data.next = entry->data.next; |
| 494 | next_entry->data.prev = entry->data.prev; |
| 495 | } |
| 496 | |
| 497 | static void |
| 498 | grn_cache_entry_persistent_prepend_link(grn_cache *cache, |
| 499 | grn_cache_entry_persistent *entry, |
| 500 | grn_id entry_id, |
| 501 | grn_cache_entry_persistent *head_entry, |
| 502 | grn_id head_entry_id) |
| 503 | { |
| 504 | grn_ctx *ctx = cache->ctx; |
| 505 | grn_hash *keys = cache->impl.persistent.keys; |
| 506 | grn_cache_entry_persistent *head_next_entry; |
| 507 | |
| 508 | entry->data.next = head_entry->data.next; |
| 509 | entry->data.prev = head_entry_id; |
| 510 | head_next_entry = |
| 511 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 512 | keys, |
| 513 | head_entry->data.next, |
| 514 | NULL); |
| 515 | head_next_entry->data.prev = entry_id; |
| 516 | head_entry->data.next = entry_id; |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | grn_cache_expire_entry_persistent(grn_cache *cache, |
| 521 | grn_cache_entry_persistent *entry, |
| 522 | grn_id cache_id) |
| 523 | { |
| 524 | grn_hash *keys = cache->impl.persistent.keys; |
| 525 | grn_ja *values = cache->impl.persistent.values; |
| 526 | |
| 527 | grn_cache_entry_persistent_delete_link(cache, entry); |
| 528 | grn_ja_put(cache->ctx, values, cache_id, NULL, 0, GRN_OBJ_SET, NULL); |
| 529 | grn_hash_delete_by_id(cache->ctx, keys, cache_id, NULL); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | grn_cache_expire_memory_without_lock(grn_cache *cache, uint32_t size) |
| 534 | { |
| 535 | grn_cache_entry_memory *ce0 = |
| 536 | (grn_cache_entry_memory *)(&(cache->impl.memory)); |
| 537 | while (ce0 != ce0->prev && size--) { |
| 538 | grn_cache_expire_entry_memory(cache, ce0->prev); |
| 539 | } |
| 540 | if (size == 0) { |
| 541 | grn_hash_truncate(cache->ctx, cache->impl.memory.hash); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | static void |
| 546 | grn_cache_expire_persistent_without_lock(grn_cache *cache, uint32_t size) |
| 547 | { |
| 548 | grn_ctx *ctx = cache->ctx; |
| 549 | grn_hash *keys = cache->impl.persistent.keys; |
| 550 | grn_cache_entry_persistent *head_entry; |
| 551 | |
| 552 | head_entry = |
| 553 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 554 | keys, |
| 555 | GRN_CACHE_PERSISTENT_ROOT_ID, |
| 556 | NULL); |
| 557 | while (head_entry->data.prev != GRN_CACHE_PERSISTENT_ROOT_ID && |
| 558 | size > 0) { |
| 559 | grn_cache_entry_persistent *tail_entry; |
| 560 | tail_entry = |
| 561 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 562 | keys, |
| 563 | head_entry->data.prev, |
| 564 | NULL); |
| 565 | grn_cache_expire_entry_persistent(cache, tail_entry, head_entry->data.prev); |
| 566 | size--; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | static grn_rc |
| 571 | grn_cache_set_max_n_entries_memory(grn_ctx *ctx, |
| 572 | grn_cache *cache, |
| 573 | unsigned int n) |
| 574 | { |
| 575 | uint32_t current_max_n_entries; |
| 576 | |
| 577 | MUTEX_LOCK(cache->impl.memory.mutex); |
| 578 | current_max_n_entries = cache->impl.memory.max_nentries; |
| 579 | cache->impl.memory.max_nentries = n; |
| 580 | if (n < current_max_n_entries) { |
| 581 | grn_cache_expire_memory_without_lock(cache, current_max_n_entries - n); |
| 582 | } |
| 583 | MUTEX_UNLOCK(cache->impl.memory.mutex); |
| 584 | |
| 585 | return GRN_SUCCESS; |
| 586 | } |
| 587 | |
| 588 | static grn_rc |
| 589 | grn_cache_set_max_n_entries_persistent(grn_ctx *ctx, |
| 590 | grn_cache *cache, |
| 591 | unsigned int n) |
| 592 | { |
| 593 | grn_rc rc; |
| 594 | grn_hash *keys = cache->impl.persistent.keys; |
| 595 | grn_cache_entry_persistent *metadata_entry; |
| 596 | uint32_t current_max_n_entries; |
| 597 | |
| 598 | rc = grn_io_lock(ctx, keys->io, cache->impl.persistent.timeout); |
| 599 | if (rc != GRN_SUCCESS) { |
| 600 | return rc; |
| 601 | } |
| 602 | |
| 603 | metadata_entry = |
| 604 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 605 | keys, |
| 606 | GRN_CACHE_PERSISTENT_METADATA_ID, |
| 607 | NULL); |
| 608 | |
| 609 | current_max_n_entries = metadata_entry->metadata.max_nentries; |
| 610 | metadata_entry->metadata.max_nentries = n; |
| 611 | if (n < current_max_n_entries) { |
| 612 | grn_cache_expire_persistent_without_lock(cache, current_max_n_entries - n); |
| 613 | } |
| 614 | grn_io_unlock(ctx, keys->io); |
| 615 | |
| 616 | return GRN_SUCCESS; |
| 617 | } |
| 618 | |
| 619 | grn_rc |
| 620 | grn_cache_set_max_n_entries(grn_ctx *ctx, grn_cache *cache, unsigned int n) |
| 621 | { |
| 622 | if (!cache) { |
| 623 | return GRN_INVALID_ARGUMENT; |
| 624 | } |
| 625 | |
| 626 | if (cache->is_memory) { |
| 627 | return grn_cache_set_max_n_entries_memory(cache->ctx, cache, n); |
| 628 | } else { |
| 629 | return grn_cache_set_max_n_entries_persistent(cache->ctx, cache, n); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | static uint32_t |
| 634 | grn_cache_get_max_n_entries_memory(grn_ctx *ctx, grn_cache *cache) |
| 635 | { |
| 636 | return cache->impl.memory.max_nentries; |
| 637 | } |
| 638 | |
| 639 | static uint32_t |
| 640 | grn_cache_get_max_n_entries_persistent(grn_ctx *ctx, grn_cache *cache) |
| 641 | { |
| 642 | grn_rc rc; |
| 643 | grn_hash *keys = cache->impl.persistent.keys; |
| 644 | grn_cache_entry_persistent *metadata_entry; |
| 645 | uint32_t current_max_n_entries; |
| 646 | |
| 647 | rc = grn_io_lock(ctx, keys->io, cache->impl.persistent.timeout); |
| 648 | if (rc != GRN_SUCCESS) { |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | metadata_entry = |
| 653 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 654 | keys, |
| 655 | GRN_CACHE_PERSISTENT_METADATA_ID, |
| 656 | NULL); |
| 657 | current_max_n_entries = metadata_entry->metadata.max_nentries; |
| 658 | grn_io_unlock(ctx, keys->io); |
| 659 | |
| 660 | return current_max_n_entries; |
| 661 | } |
| 662 | |
| 663 | uint32_t |
| 664 | grn_cache_get_max_n_entries(grn_ctx *ctx, grn_cache *cache) |
| 665 | { |
| 666 | if (!cache) { |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | if (cache->is_memory) { |
| 671 | return grn_cache_get_max_n_entries_memory(cache->ctx, cache); |
| 672 | } else { |
| 673 | return grn_cache_get_max_n_entries_persistent(cache->ctx, cache); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static void |
| 678 | grn_cache_get_statistics_memory(grn_ctx *ctx, grn_cache *cache, |
| 679 | grn_cache_statistics *statistics) |
| 680 | { |
| 681 | MUTEX_LOCK(cache->impl.memory.mutex); |
| 682 | statistics->nentries = GRN_HASH_SIZE(cache->impl.memory.hash); |
| 683 | statistics->max_nentries = cache->impl.memory.max_nentries; |
| 684 | statistics->nfetches = cache->impl.memory.nfetches; |
| 685 | statistics->nhits = cache->impl.memory.nhits; |
| 686 | MUTEX_UNLOCK(cache->impl.memory.mutex); |
| 687 | } |
| 688 | |
| 689 | static void |
| 690 | grn_cache_get_statistics_persistent(grn_ctx *ctx, grn_cache *cache, |
| 691 | grn_cache_statistics *statistics) |
| 692 | { |
| 693 | grn_rc rc = GRN_INVALID_ARGUMENT; |
| 694 | grn_hash *keys = cache->impl.persistent.keys; |
| 695 | grn_cache_entry_persistent *metadata_entry; |
| 696 | |
| 697 | rc = grn_io_lock(ctx, keys->io, cache->impl.persistent.timeout); |
| 698 | if (rc != GRN_SUCCESS) { |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | metadata_entry = |
| 703 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 704 | keys, |
| 705 | GRN_CACHE_PERSISTENT_METADATA_ID, |
| 706 | NULL); |
| 707 | |
| 708 | statistics->nentries = GRN_HASH_SIZE(keys); |
| 709 | statistics->max_nentries = metadata_entry->metadata.max_nentries; |
| 710 | statistics->nfetches = metadata_entry->metadata.nfetches; |
| 711 | statistics->nhits = metadata_entry->metadata.nhits; |
| 712 | |
| 713 | grn_io_unlock(ctx, keys->io); |
| 714 | } |
| 715 | |
| 716 | void |
| 717 | grn_cache_get_statistics(grn_ctx *ctx, grn_cache *cache, |
| 718 | grn_cache_statistics *statistics) |
| 719 | { |
| 720 | if (cache->is_memory) { |
| 721 | grn_cache_get_statistics_memory(ctx, cache, statistics); |
| 722 | } else { |
| 723 | grn_cache_get_statistics_persistent(ctx, cache, statistics); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static grn_rc |
| 728 | grn_cache_fetch_memory(grn_ctx *ctx, grn_cache *cache, |
| 729 | const char *key, uint32_t key_len, |
| 730 | grn_obj *output) |
| 731 | { |
| 732 | /* TODO: How about GRN_NOT_FOUND? */ |
| 733 | grn_rc rc = GRN_INVALID_ARGUMENT; |
| 734 | grn_cache_entry_memory *ce; |
| 735 | |
| 736 | MUTEX_LOCK(cache->impl.memory.mutex); |
| 737 | cache->impl.memory.nfetches++; |
| 738 | if (grn_hash_get(cache->ctx, cache->impl.memory.hash, key, key_len, |
| 739 | (void **)&ce)) { |
| 740 | if (ce->tv.tv_sec <= grn_db_get_last_modified(ctx, ctx->impl->db)) { |
| 741 | grn_cache_expire_entry_memory(cache, ce); |
| 742 | goto exit; |
| 743 | } |
| 744 | rc = GRN_SUCCESS; |
| 745 | const char *value = GRN_TEXT_VALUE(ce->value); |
| 746 | size_t value_size = GRN_TEXT_LEN(ce->value); |
| 747 | grn_cache_flags flags = 0; |
| 748 | if (value_size > sizeof(uint8_t)) { |
| 749 | flags = *((uint8_t *)value); |
| 750 | value += sizeof(uint8_t); |
| 751 | value_size -= sizeof(uint8_t); |
| 752 | } |
| 753 | if (flags & GRN_CACHE_FLAG_WITH_TRACE_LOG) { |
| 754 | size_t used_size = grn_ctx_trace_log_restore(ctx, value, value_size); |
| 755 | value += used_size; |
| 756 | value_size -= used_size; |
| 757 | } |
| 758 | GRN_TEXT_PUT(ctx, output, value, value_size); |
| 759 | ce->prev->next = ce->next; |
| 760 | ce->next->prev = ce->prev; |
| 761 | { |
| 762 | grn_cache_entry_memory *ce0 = |
| 763 | (grn_cache_entry_memory *)(&(cache->impl.memory)); |
| 764 | ce->next = ce0->next; |
| 765 | ce->prev = ce0; |
| 766 | ce0->next->prev = ce; |
| 767 | ce0->next = ce; |
| 768 | } |
| 769 | cache->impl.memory.nhits++; |
| 770 | } |
| 771 | exit : |
| 772 | MUTEX_UNLOCK(cache->impl.memory.mutex); |
| 773 | return rc; |
| 774 | } |
| 775 | |
| 776 | static grn_rc |
| 777 | grn_cache_fetch_persistent(grn_ctx *ctx, grn_cache *cache, |
| 778 | const char *key, uint32_t key_len, |
| 779 | grn_obj *output) |
| 780 | { |
| 781 | /* TODO: How about GRN_NOT_FOUND? */ |
| 782 | grn_rc rc = GRN_INVALID_ARGUMENT; |
| 783 | grn_hash *keys = cache->impl.persistent.keys; |
| 784 | grn_ja *values = cache->impl.persistent.values; |
| 785 | grn_id cache_id; |
| 786 | grn_cache_entry_persistent *entry; |
| 787 | grn_cache_entry_persistent *metadata_entry; |
| 788 | |
| 789 | if (key_len == GRN_CACHE_PERSISTENT_ROOT_KEY_LEN && |
| 790 | memcmp(key, |
| 791 | GRN_CACHE_PERSISTENT_ROOT_KEY, |
| 792 | GRN_CACHE_PERSISTENT_ROOT_KEY_LEN) == 0) { |
| 793 | return rc; |
| 794 | } |
| 795 | |
| 796 | rc = grn_io_lock(ctx, keys->io, cache->impl.persistent.timeout); |
| 797 | if (rc != GRN_SUCCESS) { |
| 798 | return rc; |
| 799 | } |
| 800 | |
| 801 | /* TODO: How about GRN_NOT_FOUND? */ |
| 802 | rc = GRN_INVALID_ARGUMENT; |
| 803 | |
| 804 | metadata_entry = |
| 805 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 806 | keys, |
| 807 | GRN_CACHE_PERSISTENT_METADATA_ID, |
| 808 | NULL); |
| 809 | metadata_entry->metadata.nfetches++; |
| 810 | |
| 811 | cache_id = grn_hash_get(cache->ctx, keys, key, key_len, (void **)&entry); |
| 812 | if (cache_id == GRN_ID_NIL) { |
| 813 | goto exit; |
| 814 | } |
| 815 | |
| 816 | if (cache_id != GRN_ID_NIL) { |
| 817 | if (entry->data.modified_time.tv_sec <= |
| 818 | grn_db_get_last_modified(ctx, ctx->impl->db)) { |
| 819 | grn_cache_expire_entry_persistent(cache, entry, cache_id); |
| 820 | goto exit; |
| 821 | } |
| 822 | |
| 823 | rc = GRN_SUCCESS; |
| 824 | if (metadata_entry->metadata.flags & GRN_CACHE_FLAG_WITH_TRACE_LOG) { |
| 825 | const uint8_t *data; |
| 826 | uint32_t data_size; |
| 827 | grn_io_win iw; |
| 828 | data = grn_ja_ref(ctx, values, cache_id, &iw, &data_size); |
| 829 | if (data) { |
| 830 | grn_cache_flags flags = 0; |
| 831 | if (data_size > sizeof(uint8_t)) { |
| 832 | flags = *data; |
| 833 | data += sizeof(uint8_t); |
| 834 | data_size -= sizeof(uint8_t); |
| 835 | } |
| 836 | if (flags & GRN_CACHE_FLAG_WITH_TRACE_LOG) { |
| 837 | size_t used_size = grn_ctx_trace_log_restore(ctx, data, data_size); |
| 838 | data += used_size; |
| 839 | data_size -= used_size; |
| 840 | } |
| 841 | GRN_TEXT_PUT(ctx, output, data, data_size); |
| 842 | grn_ja_unref(ctx, &iw); |
| 843 | } |
| 844 | } else { |
| 845 | grn_ja_get_value(ctx, values, cache_id, output); |
| 846 | } |
| 847 | grn_cache_entry_persistent_delete_link(cache, entry); |
| 848 | { |
| 849 | grn_cache_entry_persistent *head_entry; |
| 850 | head_entry = |
| 851 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 852 | keys, |
| 853 | GRN_CACHE_PERSISTENT_ROOT_ID, |
| 854 | NULL); |
| 855 | grn_cache_entry_persistent_prepend_link(cache, |
| 856 | entry, |
| 857 | cache_id, |
| 858 | head_entry, |
| 859 | GRN_CACHE_PERSISTENT_ROOT_ID); |
| 860 | } |
| 861 | metadata_entry->metadata.nhits++; |
| 862 | } |
| 863 | |
| 864 | exit : |
| 865 | grn_io_unlock(ctx, keys->io); |
| 866 | |
| 867 | return rc; |
| 868 | } |
| 869 | |
| 870 | grn_rc |
| 871 | grn_cache_fetch(grn_ctx *ctx, grn_cache *cache, |
| 872 | const char *key, uint32_t key_len, |
| 873 | grn_obj *output) |
| 874 | { |
| 875 | if (!ctx->impl || !ctx->impl->db) { return GRN_INVALID_ARGUMENT; } |
| 876 | |
| 877 | if (cache->is_memory) { |
| 878 | return grn_cache_fetch_memory(ctx, cache, key, key_len, output); |
| 879 | } else { |
| 880 | return grn_cache_fetch_persistent(ctx, cache, key, key_len, output); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | static void |
| 885 | grn_cache_update_memory(grn_ctx *ctx, grn_cache *cache, |
| 886 | const char *key, uint32_t key_len, |
| 887 | grn_obj *value) |
| 888 | { |
| 889 | grn_id id; |
| 890 | int added = 0; |
| 891 | grn_cache_entry_memory *ce; |
| 892 | grn_rc rc = GRN_SUCCESS; |
| 893 | grn_obj *old = NULL; |
| 894 | grn_obj *obj = NULL; |
| 895 | |
| 896 | if (cache->impl.memory.max_nentries == 0) { |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | MUTEX_LOCK(cache->impl.memory.mutex); |
| 901 | obj = grn_obj_open(cache->ctx, GRN_BULK, 0, GRN_DB_TEXT); |
| 902 | if (!obj) { |
| 903 | goto exit; |
| 904 | } |
| 905 | grn_cache_flags flags = 0; |
| 906 | if (grn_ctx_trace_log_is_enabled(ctx)) { |
| 907 | flags |= GRN_CACHE_FLAG_WITH_TRACE_LOG; |
| 908 | } |
| 909 | GRN_UINT8_PUT(cache->ctx, obj, flags); |
| 910 | if (flags & GRN_CACHE_FLAG_WITH_TRACE_LOG) { |
| 911 | grn_ctx_trace_log_dump(ctx, cache->ctx, obj); |
| 912 | } |
| 913 | GRN_TEXT_PUT(cache->ctx, obj, GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value)); |
| 914 | id = grn_hash_add(cache->ctx, cache->impl.memory.hash, key, key_len, |
| 915 | (void **)&ce, &added); |
| 916 | if (id) { |
| 917 | if (!added) { |
| 918 | old = ce->value; |
| 919 | ce->prev->next = ce->next; |
| 920 | ce->next->prev = ce->prev; |
| 921 | } |
| 922 | ce->id = id; |
| 923 | ce->value = obj; |
| 924 | ce->tv = ctx->impl->tv; |
| 925 | { |
| 926 | grn_cache_entry_memory *ce0 = |
| 927 | (grn_cache_entry_memory *)(&(cache->impl.memory)); |
| 928 | ce->next = ce0->next; |
| 929 | ce->prev = ce0; |
| 930 | ce0->next->prev = ce; |
| 931 | ce0->next = ce; |
| 932 | } |
| 933 | if (GRN_HASH_SIZE(cache->impl.memory.hash) > |
| 934 | cache->impl.memory.max_nentries) { |
| 935 | grn_cache_expire_entry_memory(cache, cache->impl.memory.prev); |
| 936 | } |
| 937 | } else { |
| 938 | rc = GRN_NO_MEMORY_AVAILABLE; |
| 939 | } |
| 940 | exit : |
| 941 | if (rc) { grn_obj_close(cache->ctx, obj); } |
| 942 | if (old) { grn_obj_close(cache->ctx, old); } |
| 943 | MUTEX_UNLOCK(cache->impl.memory.mutex); |
| 944 | } |
| 945 | |
| 946 | static void |
| 947 | grn_cache_update_persistent(grn_ctx *ctx, grn_cache *cache, |
| 948 | const char *key, uint32_t key_len, |
| 949 | grn_obj *value) |
| 950 | { |
| 951 | grn_rc rc; |
| 952 | grn_hash *keys = cache->impl.persistent.keys; |
| 953 | grn_ja *values = cache->impl.persistent.values; |
| 954 | grn_cache_entry_persistent *metadata_entry; |
| 955 | grn_id cache_id; |
| 956 | grn_cache_entry_persistent *entry; |
| 957 | int added; |
| 958 | |
| 959 | if (key_len == GRN_CACHE_PERSISTENT_ROOT_KEY_LEN && |
| 960 | memcmp(key, |
| 961 | GRN_CACHE_PERSISTENT_ROOT_KEY, |
| 962 | GRN_CACHE_PERSISTENT_ROOT_KEY_LEN) == 0) { |
| 963 | return; |
| 964 | } |
| 965 | |
| 966 | if (key_len == GRN_CACHE_PERSISTENT_METADATA_KEY_LEN && |
| 967 | memcmp(key, |
| 968 | GRN_CACHE_PERSISTENT_METADATA_KEY, |
| 969 | GRN_CACHE_PERSISTENT_METADATA_KEY_LEN) == 0) { |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | rc = grn_io_lock(ctx, keys->io, cache->impl.persistent.timeout); |
| 974 | if (rc != GRN_SUCCESS) { |
| 975 | return; |
| 976 | } |
| 977 | |
| 978 | metadata_entry = |
| 979 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 980 | keys, |
| 981 | GRN_CACHE_PERSISTENT_METADATA_ID, |
| 982 | NULL); |
| 983 | if (metadata_entry->metadata.max_nentries == 0) { |
| 984 | goto exit; |
| 985 | } |
| 986 | |
| 987 | cache_id = grn_hash_add(cache->ctx, keys, key, key_len, (void **)&entry, |
| 988 | &added); |
| 989 | if (cache_id) { |
| 990 | grn_cache_entry_persistent *head_entry; |
| 991 | |
| 992 | if (!added) { |
| 993 | grn_cache_entry_persistent_delete_link(cache, entry); |
| 994 | } |
| 995 | entry->data.modified_time = ctx->impl->tv; |
| 996 | |
| 997 | if (metadata_entry->metadata.flags & GRN_CACHE_FLAG_WITH_TRACE_LOG) { |
| 998 | grn_obj data; |
| 999 | GRN_TEXT_INIT(&data, 0); |
| 1000 | grn_cache_flags flags = 0; |
| 1001 | if (grn_ctx_trace_log_is_enabled(ctx)) { |
| 1002 | flags |= GRN_CACHE_FLAG_WITH_TRACE_LOG; |
| 1003 | } |
| 1004 | GRN_UINT8_PUT(cache->ctx, &data, flags); |
| 1005 | if (flags & GRN_CACHE_FLAG_WITH_TRACE_LOG) { |
| 1006 | grn_ctx_trace_log_dump(ctx, cache->ctx, &data); |
| 1007 | } |
| 1008 | GRN_TEXT_PUT(cache->ctx, &data, GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value)); |
| 1009 | grn_ja_put(cache->ctx, |
| 1010 | values, |
| 1011 | cache_id, |
| 1012 | GRN_TEXT_VALUE(&data), |
| 1013 | (uint32_t)GRN_TEXT_LEN(&data), |
| 1014 | GRN_OBJ_SET, |
| 1015 | NULL); |
| 1016 | GRN_OBJ_FIN(cache->ctx, &data); |
| 1017 | } else { |
| 1018 | grn_ja_put(cache->ctx, |
| 1019 | values, |
| 1020 | cache_id, |
| 1021 | GRN_TEXT_VALUE(value), |
| 1022 | (uint32_t)GRN_TEXT_LEN(value), |
| 1023 | GRN_OBJ_SET, |
| 1024 | NULL); |
| 1025 | } |
| 1026 | |
| 1027 | head_entry = |
| 1028 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 1029 | keys, |
| 1030 | GRN_CACHE_PERSISTENT_ROOT_ID, |
| 1031 | NULL); |
| 1032 | grn_cache_entry_persistent_prepend_link(cache, |
| 1033 | entry, |
| 1034 | cache_id, |
| 1035 | head_entry, |
| 1036 | GRN_CACHE_PERSISTENT_ROOT_ID); |
| 1037 | if (GRN_HASH_SIZE(keys) > metadata_entry->metadata.max_nentries) { |
| 1038 | grn_cache_entry_persistent *tail_entry; |
| 1039 | tail_entry = |
| 1040 | (grn_cache_entry_persistent *)grn_hash_get_value_(ctx, |
| 1041 | keys, |
| 1042 | head_entry->data.prev, |
| 1043 | NULL); |
| 1044 | grn_cache_expire_entry_persistent(cache, |
| 1045 | tail_entry, |
| 1046 | head_entry->data.prev); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | exit : |
| 1051 | grn_io_unlock(ctx, keys->io); |
| 1052 | } |
| 1053 | |
| 1054 | void |
| 1055 | grn_cache_update(grn_ctx *ctx, grn_cache *cache, |
| 1056 | const char *key, uint32_t key_len, grn_obj *value) |
| 1057 | { |
| 1058 | if (!ctx->impl) { return; } |
| 1059 | |
| 1060 | if (cache->is_memory) { |
| 1061 | grn_cache_update_memory(ctx, cache, key, key_len, value); |
| 1062 | } else { |
| 1063 | grn_cache_update_persistent(ctx, cache, key, key_len, value); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | static void |
| 1068 | grn_cache_expire_memory(grn_cache *cache, uint32_t size) |
| 1069 | { |
| 1070 | MUTEX_LOCK(cache->impl.memory.mutex); |
| 1071 | grn_cache_expire_memory_without_lock(cache, size); |
| 1072 | MUTEX_UNLOCK(cache->impl.memory.mutex); |
| 1073 | } |
| 1074 | |
| 1075 | static void |
| 1076 | grn_cache_expire_persistent(grn_cache *cache, uint32_t size) |
| 1077 | { |
| 1078 | grn_rc rc; |
| 1079 | grn_ctx *ctx = cache->ctx; |
| 1080 | grn_hash *keys = cache->impl.persistent.keys; |
| 1081 | |
| 1082 | rc = grn_io_lock(ctx, keys->io, cache->impl.persistent.timeout); |
| 1083 | if (rc != GRN_SUCCESS) { |
| 1084 | return; |
| 1085 | } |
| 1086 | |
| 1087 | grn_cache_expire_persistent_without_lock(cache, size); |
| 1088 | |
| 1089 | grn_io_unlock(ctx, keys->io); |
| 1090 | } |
| 1091 | |
| 1092 | void |
| 1093 | grn_cache_expire(grn_cache *cache, uint32_t size) |
| 1094 | { |
| 1095 | if (cache->is_memory) { |
| 1096 | grn_cache_expire_memory(cache, size); |
| 1097 | } else { |
| 1098 | grn_cache_expire_persistent(cache, size); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | void |
| 1103 | grn_cache_fin(void) |
| 1104 | { |
| 1105 | grn_ctx *ctx = &grn_cache_ctx; |
| 1106 | |
| 1107 | grn_cache_current_set(ctx, NULL); |
| 1108 | |
| 1109 | if (grn_cache_default) { |
| 1110 | grn_cache_close(ctx, grn_cache_default); |
| 1111 | grn_cache_default = NULL; |
| 1112 | } |
| 1113 | |
| 1114 | grn_ctx_fin(ctx); |
| 1115 | } |