C Collections

clrucache · Section 3

clrucache_get_full

clrucache_get_full - get a value by key

C Collections (libccollections, -lccollections)

#include <ccollections/clrucache.h>
 
ccol_retval clrucache_get_full(ccache cache,
                                 const cmap_pair *key_pair,
                                 cmap_pair *val_out);

Looks up key_pair in the cache. On success it allocates a heap copy of
the value paired with it, with the cache's custom allocator if the
caller gave one at construction and with malloc() if not, assigns that
pointer to val_out->ptr and sets val_out->size to the stored byte
size. The caller must free val_out->ptr with the matching allocator:
the custom free function if the caller configured a custom allocator,
and free() if not.
 
Cache hit: The entry moves to the most recently used position, and a
heap copy of its value is written into val_out.
 
Cache miss and no remote getter: The function returns
ccol_key_not_found.
 
Cache miss with a remote getter: A placeholder entry is inserted and
the remote getter is called outside the segment's lock. Concurrent get
calls for the same key coalesce. Only the first caller runs the fetch,
and all the others block until the result is ready and then get the
same result as the caller that ran it. When the getter succeeds the
fetched value is cached and a heap copy of it is written into val_out
for every waiter. When the getter fails in an ordinary way, every
waiter gets ccol_key_not_found. The fetched value can also be rejected
because its size is wrong for the fixed-size buffer of a concurrent,
coalesced ccache_get() caller. Every waiter then gets
ccol_unexpected_failure, and so does this call when it is one of those
waiters.
 
The call blocks while a set of the same key is in progress, and waits
until the set has completed and the cache is consistent again. If the
set fails and the key is then absent from the cache, the call goes on
exactly like a get that started after the failed set: It runs the
remote getter (or joins a fetch of the same key that has already
started), and it reports ccol_key_not_found only when the cache has no
remote getter.

cache     Cache handle.
key_pair  The key to look up. The ptr and size fields must be set.
val_out   The output pair that the function fills on success.
          val_out->ptr becomes a heap copy of the value, which the
          caller must free with the cache's custom allocator, or with
          free() when the caller gave no custom allocator.
          val_out->size becomes the size of the stored value. val_out
          must not be NULL.

ccol_success           The function filled val_out. The caller owns
                       val_out->ptr.
ccol_key_not_found     The key is absent and there is no getter, or
                       the getter failed.
ccol_invalid_args      cache is CCACHE_NULL, or cache is a
                       stale handle that a call already destroyed, or
                       key_pair is NULL, or val_out is NULL, or
                       key_pair->size does not agree with the width of
                       the cache's key type.
ccol_not_enough_memory An allocation failed, either the one of the
                       placeholder entry or the one of the output copy.
ccol_unexpected_failure
                       This call coalesced onto another, concurrent
                       cache-miss fetch of the same key, and the caller
                       that owns that fetch rejected the fetched value
                       because its size is wrong for that caller's own
                       fixed-size destination buffer. Every caller that
                       coalesces onto the same fetch gets this same
                       result.
ccol_container_full    The cache's internal bookkeeping reached its
                       maximum element count on a cache miss. A real
                       program cannot reach this.

cache is an opaque VALUE handle, which the library resolves through a
slot table of its own before it touches the cache object behind it.
The resolve takes a pin, and the call holds it for its whole duration,
including any time spent blocked inside a remote getter callback, so
a concurrent ccache_destroy() on the same handle waits for this call to
finish instead of freeing the cache under it.
The val_out->ptr that comes back is allocated with the cache's custom
allocator if the caller gave one at construction, and with the
standard malloc() if not; free it with the matching allocator. When
the value type is char*, the copy is a null-terminated string on the
heap, which also comes from the custom allocator, or from malloc()
when the caller configured none.
The cache never hands out a pointer into its internal storage, because
such a pointer would dangle after an eviction.
The average-case complexity of the hash map lookup is O(1).

ccache_get(3), clrucache_set_full(3), clrucache_create_full(3)