diff options
author | Sean Bright <sean.bright@gmail.com> | 2017-11-09 09:21:38 -0500 |
---|---|---|
committer | Sean Bright <sean.bright@gmail.com> | 2017-11-13 15:15:33 -0500 |
commit | ffccce76d9c85fb09a28883452cf9df318c3d050 (patch) | |
tree | 21cfa458a58160fc2eb994906d7bdeb4e899bf8b /res/res_sorcery_memory_cache.c | |
parent | 9eacd55c7191a460fe8704f81b9e5e3beaf014b9 (diff) |
sorcery: Add ast_sorcery_retrieve_by_prefix()
Some consumers of the sorcery API use ast_sorcery_retrieve_by_regex
only so that they can anchor the potential match as a prefix and not
because they truly need regular expressions.
Rather than using regular expressions for simple prefix lookups, add
a new operation - ast_sorcery_retrieve_by_prefix - that does them.
Change-Id: I56f4e20ba1154bd52281f995c27a429a854f6a79
Diffstat (limited to 'res/res_sorcery_memory_cache.c')
-rw-r--r-- | res/res_sorcery_memory_cache.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/res/res_sorcery_memory_cache.c b/res/res_sorcery_memory_cache.c index bf2347ccd..30e6ef04b 100644 --- a/res/res_sorcery_memory_cache.c +++ b/res/res_sorcery_memory_cache.c @@ -185,6 +185,10 @@ struct sorcery_memory_cache_fields_cmp_params { const struct ast_variable *fields; /*! \brief Regular expression for checking object id */ regex_t *regex; + /*! \brief Prefix for matching object id */ + const char *prefix; + /*! \brief Prefix length in bytes for matching object id */ + const size_t prefix_len; /*! \brief Optional container to put object into */ struct ao2_container *container; }; @@ -201,6 +205,8 @@ static void sorcery_memory_cache_retrieve_multiple(const struct ast_sorcery *sor struct ao2_container *objects, const struct ast_variable *fields); static void sorcery_memory_cache_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex); +static void sorcery_memory_cache_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, + struct ao2_container *objects, const char *prefix, const size_t prefix_len); static int sorcery_memory_cache_delete(const struct ast_sorcery *sorcery, void *data, void *object); static void sorcery_memory_cache_close(void *data); @@ -216,6 +222,7 @@ static struct ast_sorcery_wizard memory_cache_object_wizard = { .retrieve_fields = sorcery_memory_cache_retrieve_fields, .retrieve_multiple = sorcery_memory_cache_retrieve_multiple, .retrieve_regex = sorcery_memory_cache_retrieve_regex, + .retrieve_prefix = sorcery_memory_cache_retrieve_prefix, .close = sorcery_memory_cache_close, }; @@ -1253,6 +1260,11 @@ static int sorcery_memory_cache_fields_cmp(void *obj, void *arg, int flags) ao2_link(params->container, cached->object); } return 0; + } else if (params->prefix) { + if (!strncmp(params->prefix, ast_sorcery_object_get_id(cached->object), params->prefix_len)) { + ao2_link(params->container, cached->object); + } + return 0; } else if (params->fields && (!ast_variable_lists_match(cached->objectset, params->fields, 0))) { /* If we can't turn the object into an object set OR if differences exist between the fields @@ -1378,6 +1390,40 @@ static void sorcery_memory_cache_retrieve_regex(const struct ast_sorcery *sorcer /*! * \internal + * \brief Callback function to retrieve multiple objects whose id matches a prefix + * + * \param sorcery The sorcery instance + * \param data The sorcery memory cache + * \param type The type of the object to retrieve + * \param objects Container to place the objects into + * \param prefix Prefix to match against the object id + */ +static void sorcery_memory_cache_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, + struct ao2_container *objects, const char *prefix, const size_t prefix_len) +{ + struct sorcery_memory_cache *cache = data; + struct sorcery_memory_cache_fields_cmp_params params = { + .sorcery = sorcery, + .cache = cache, + .container = objects, + .prefix = prefix, + .prefix_len = prefix_len, + }; + + if (is_passthru_update() || !cache->full_backend_cache) { + return; + } + + memory_cache_full_update(sorcery, type, cache); + ao2_callback(cache->objects, 0, sorcery_memory_cache_fields_cmp, ¶ms); + + if (ao2_container_count(objects)) { + memory_cache_stale_check(sorcery, cache); + } +} + +/*! + * \internal * \brief Callback function to finish configuring the memory cache * * \param data The sorcery memory cache |