Performance improvements for xbps_find_pkg_in_dict_by_* and xbps_find_*_in_array().

This commit is contained in:
Juan RP 2011-01-28 12:11:43 +01:00
parent 4438fd1183
commit f7f3cfcb9c

View File

@ -233,31 +233,21 @@ out:
return rpkgd; return rpkgd;
} }
static prop_dictionary_t static bool
find_virtual_pkg_in_dict(prop_object_iterator_t iter, find_virtual_pkg_in_dict(prop_dictionary_t d,
const char *str, const char *str,
bool bypattern) bool bypattern)
{ {
prop_object_t obj;
prop_array_t provides; prop_array_t provides;
bool found = false; bool found = false;
prop_object_iterator_reset(iter); if ((provides = prop_dictionary_get(d, "provides"))) {
while ((obj = prop_object_iterator_next(iter))) {
if ((provides = prop_dictionary_get(obj, "provides")) == NULL)
continue;
if (bypattern) if (bypattern)
found = xbps_find_pkgpattern_in_array(provides, str); found = xbps_find_pkgpattern_in_array(provides, str);
else else
found = xbps_find_pkgname_in_array(provides, str); found = xbps_find_pkgname_in_array(provides, str);
if (found)
break;
} }
prop_object_iterator_release(iter); return found;
return obj;
} }
static prop_dictionary_t static prop_dictionary_t
@ -283,29 +273,22 @@ find_pkg_in_dict(prop_dictionary_t d,
"pkgver", &pkgver); "pkgver", &pkgver);
if (xbps_pkgpattern_match(pkgver, __UNCONST(str))) if (xbps_pkgpattern_match(pkgver, __UNCONST(str)))
break; break;
if (find_virtual_pkg_in_dict(obj, str, true))
break;
} else { } else {
prop_dictionary_get_cstring_nocopy(obj, prop_dictionary_get_cstring_nocopy(obj,
"pkgname", &dpkgn); "pkgname", &dpkgn);
if (strcmp(dpkgn, str) == 0) if (strcmp(dpkgn, str) == 0)
break; break;
if (find_virtual_pkg_in_dict(obj, str, false))
break;
} }
} }
if (obj == NULL) { prop_object_iterator_release(iter);
/*
* No pkg was found, try virtual package by name
* or by pattern.
*/
if (bypattern)
obj = find_virtual_pkg_in_dict(iter, str, true);
else
obj = find_virtual_pkg_in_dict(iter, str, false);
if (obj == NULL) { if (obj == NULL) {
errno = ENOENT; errno = ENOENT;
return NULL; return NULL;
} }
}
prop_object_iterator_release(iter);
return obj; return obj;
} }
@ -439,17 +422,13 @@ xbps_get_pkg_dict_from_metadata_plist(const char *pkgn, const char *plist)
static bool static bool
remove_string_from_array(prop_array_t array, const char *str, bool byname) remove_string_from_array(prop_array_t array, const char *str, bool byname)
{ {
prop_object_iterator_t iter;
prop_object_t obj; prop_object_t obj;
const char *curname; const char *curname;
size_t idx = 0; size_t i, idx = 0;
bool found = false; bool found = false;
iter = prop_array_iterator(array); for (i = 0; i < prop_array_count(array); i++) {
if (iter == NULL) obj = prop_array_get(array, i);
return false;
while ((obj = prop_object_iterator_next(iter))) {
if (byname) { if (byname) {
/* match by name */ /* match by name */
prop_dictionary_get_cstring_nocopy(obj, prop_dictionary_get_cstring_nocopy(obj,
@ -467,11 +446,8 @@ remove_string_from_array(prop_array_t array, const char *str, bool byname)
} }
idx++; idx++;
} }
prop_object_iterator_release(iter); if (!found)
if (!found) {
errno = ENOENT;
return false; return false;
}
prop_array_remove(array, idx); prop_array_remove(array, idx);
return true; return true;