dect
/
libnl
Archived
13
0
Fork 0

cache: Add new nl_cache_find api

This patch adds new cache find api

nl_cache_find api was suggested by Thomas.

Unlike nl_cache_search, this patch uses
nl_object_match_filter() to look for an
object match.

Am not sure this matches what was decided
on the list few weeks back. I will be happy
to make any changes.

Signed-off-by: Roopa Prabhu <roopa@cumulusnetworks.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
This commit is contained in:
roopa 2013-01-08 04:33:29 -08:00 committed by Thomas Graf
parent 3540e44b15
commit b1ebda9241
3 changed files with 62 additions and 0 deletions

View File

@ -64,6 +64,7 @@ extern struct nl_cache * nl_object_get_cache(struct nl_object *);
extern const char * nl_object_get_type(const struct nl_object *);
extern int nl_object_get_msgtype(const struct nl_object *);
struct nl_object_ops * nl_object_get_ops(const struct nl_object *);
uint32_t nl_object_get_id_attrs(struct nl_object *obj);
static inline void * nl_object_priv(struct nl_object *obj)

View File

@ -1022,6 +1022,45 @@ struct nl_object *nl_cache_search(struct nl_cache *cache,
return NULL;
}
/**
* Find object in cache
* @arg cache Cache
* @arg filter object acting as a filter
*
* Searches the cache for an object which matches the object filter.
* If the filter attributes matches the object type id attributes,
* and the cache supports hash lookups, a faster hashtable lookup
* is used to return the object. Else, function nl_object_match_filter() is
* used to determine if the objects match. If a matching object is
* found, the reference counter is incremented and the object is returned.
*
* Therefore, if an object is returned, the reference to the object
* must be returned by calling nl_object_put() after usage.
*
* @return Reference to object or NULL if not found.
*/
struct nl_object *nl_cache_find(struct nl_cache *cache,
struct nl_object *filter)
{
struct nl_object *obj;
if (cache->c_ops == NULL)
BUG();
if ((nl_object_get_id_attrs(filter) == filter->ce_mask)
&& cache->hashtable)
return __cache_fast_lookup(cache, filter);
nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
if (nl_object_match_filter(obj, filter)) {
nl_object_get(obj);
return obj;
}
}
return NULL;
}
/**
* Mark all objects of a cache
* @arg cache Cache

View File

@ -506,6 +506,28 @@ struct nl_object_ops *nl_object_get_ops(const struct nl_object *obj)
return obj->ce_ops;
}
/**
* Return object id attribute mask
* @arg obj object
*
* @return object id attribute mask
*/
uint32_t nl_object_get_id_attrs(struct nl_object *obj)
{
struct nl_object_ops *ops = obj_ops(obj);
uint32_t id_attrs;
if (!ops)
return 0;
if (ops->oo_id_attrs_get)
id_attrs = ops->oo_id_attrs_get(obj);
else
id_attrs = ops->oo_id_attrs;
return id_attrs;
}
/** @} */
/** @} */