Redo xbps_repository_pool internals, it's a bit faster now.

This commit is contained in:
Juan RP 2011-12-22 16:38:05 +01:00
parent 32597d85c1
commit 3d81d497bd
3 changed files with 65 additions and 74 deletions

View File

@ -56,7 +56,7 @@
*/ */
#define XBPS_PKGINDEX_VERSION "1.3" #define XBPS_PKGINDEX_VERSION "1.3"
#define XBPS_API_VERSION "20111222" #define XBPS_API_VERSION "20111222-1"
#define XBPS_VERSION "0.12" #define XBPS_VERSION "0.12"
/** /**
@ -425,6 +425,12 @@ struct xbps_handle {
* stored in XBPS_META_PATH/XBPS_REGPKGDB. * stored in XBPS_META_PATH/XBPS_REGPKGDB.
*/ */
prop_dictionary_t regpkgdb_dictionary; prop_dictionary_t regpkgdb_dictionary;
/**
* @private
*
* Array of dictionaries with all registered repositories.
*/
prop_array_t repo_pool;
/** /**
* @var xbps_state_cb * @var xbps_state_cb
* *

View File

@ -78,7 +78,7 @@ void HIDDEN xbps_regpkgdb_dictionary_release(void);
* @private * @private
* From lib/repository_pool.c * From lib/repository_pool.c
*/ */
int HIDDEN xbps_repository_pool_init(const struct xbps_handle *); int HIDDEN xbps_repository_pool_init(struct xbps_handle *);
void HIDDEN xbps_repository_pool_release(void); void HIDDEN xbps_repository_pool_release(void);
/** /**

View File

@ -38,16 +38,6 @@
* @defgroup repopool Repository pool functions * @defgroup repopool Repository pool functions
*/ */
struct repository_pool {
SIMPLEQ_ENTRY(repository_pool) rp_entries;
struct repository_pool_index *rpi;
};
static SIMPLEQ_HEAD(rpool_head, repository_pool) rpool_queue =
SIMPLEQ_HEAD_INITIALIZER(rpool_queue);
static bool repolist_initialized;
/* /*
* Returns true if repository URI contains "noarch" or matching architecture * Returns true if repository URI contains "noarch" or matching architecture
* in last component, false otherwise. * in last component, false otherwise.
@ -74,35 +64,25 @@ check_repo_arch(const char *uri)
} }
int HIDDEN int HIDDEN
xbps_repository_pool_init(const struct xbps_handle *xhp) xbps_repository_pool_init(struct xbps_handle *xhp)
{ {
struct repository_pool *rpool; prop_dictionary_t d = NULL;
size_t i, ntotal = 0, nmissing = 0, repocnt = 0; size_t i, ntotal = 0, nmissing = 0;
const char *repouri; const char *repouri;
char *plist; char *plist;
int rv = 0; int rv = 0;
bool duprepo;
if (repolist_initialized) if (prop_object_type(xhp->repo_pool) == PROP_TYPE_ARRAY)
return 0; return 0;
else if (xhp->cfg == NULL) else if (xhp->cfg == NULL)
return ENOTSUP; return ENOTSUP;
xhp->repo_pool = prop_array_create();
if (xhp->repo_pool == NULL)
return ENOMEM;
for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) { for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
repouri = cfg_getnstr(xhp->cfg, "repositories", i); repouri = cfg_getnstr(xhp->cfg, "repositories", i);
/*
* Check that we do not register duplicate repositories.
*/
duprepo = false;
SIMPLEQ_FOREACH(rpool, &rpool_queue, rp_entries) {
if (strcmp(rpool->rpi->rpi_uri, repouri) == 0) {
duprepo = true;
break;
}
}
if (duprepo)
continue;
ntotal++; ntotal++;
/* /*
* Check if repository doesn't match our architecture. * Check if repository doesn't match our architecture.
@ -129,50 +109,42 @@ xbps_repository_pool_init(const struct xbps_handle *xhp)
continue; continue;
} }
/* /*
* Internalize repository's index dictionary and add it * Register repository into the array.
* into the queue.
*/ */
rpool = malloc(sizeof(struct repository_pool)); d = prop_dictionary_create();
if (rpool == NULL) { if (d == NULL) {
rv = errno; rv = ENOMEM;
free(plist); free(plist);
goto out; goto out;
} }
if (!prop_dictionary_set_cstring_nocopy(d, "uri", repouri)) {
rpool->rpi = malloc(sizeof(struct repository_pool_index)); rv = EINVAL;
if (rpool->rpi == NULL) { prop_object_release(d);
rv = errno;
free(rpool);
free(plist); free(plist);
goto out; goto out;
} }
if (!xbps_add_obj_to_dict(d,
rpool->rpi->rpi_uri = repouri; prop_dictionary_internalize_from_zfile(plist),
rpool->rpi->rpi_repod = "index")) {
prop_dictionary_internalize_from_zfile(plist); rv = EINVAL;
if (rpool->rpi->rpi_repod == NULL) { prop_object_release(d);
rv = errno;
free(rpool->rpi);
free(rpool);
free(plist); free(plist);
xbps_dbg_printf("[rpool] cannot internalize plist %s: %s\n",
plist, strerror(rv));
goto out; goto out;
} }
free(plist); free(plist);
if (!prop_array_add(xhp->repo_pool, d)) {
rv = EINVAL;
prop_object_release(d);
goto out;
}
xbps_dbg_printf("[rpool] `%s' registered.\n", repouri); xbps_dbg_printf("[rpool] `%s' registered.\n", repouri);
rpool->rpi->rpi_index = repocnt;
SIMPLEQ_INSERT_TAIL(&rpool_queue, rpool, rp_entries);
repocnt++;
} }
if (ntotal - nmissing == 0) { if (ntotal - nmissing == 0) {
/* no repositories available, error out */ /* no repositories available, error out */
rv = ENOTSUP; rv = ENOTSUP;
goto out; goto out;
} }
repolist_initialized = true;
xbps_dbg_printf("[rpool] initialized ok.\n"); xbps_dbg_printf("[rpool] initialized ok.\n");
out: out:
if (rv != 0) if (rv != 0)
@ -185,21 +157,22 @@ out:
void HIDDEN void HIDDEN
xbps_repository_pool_release(void) xbps_repository_pool_release(void)
{ {
struct repository_pool *rpool; struct xbps_handle *xhp = xbps_handle_get();
prop_dictionary_t d;
size_t i;
const char *uri;
if (!repolist_initialized) if (xhp->repo_pool == NULL)
return; return;
while ((rpool = SIMPLEQ_FIRST(&rpool_queue)) != NULL) { for (i = 0; i < prop_array_count(xhp->repo_pool); i++) {
SIMPLEQ_REMOVE(&rpool_queue, rpool, repository_pool, rp_entries); d = prop_array_get(xhp->repo_pool, i);
xbps_dbg_printf("[rpool] unregistered repository '%s'\n", prop_dictionary_get_cstring_nocopy(d, "uri", &uri);
rpool->rpi->rpi_uri); xbps_dbg_printf("[rpool] unregistered repository '%s'\n", uri);
prop_object_release(rpool->rpi->rpi_repod); prop_object_release(d);
free(rpool->rpi);
free(rpool);
rpool = NULL;
} }
repolist_initialized = false; prop_object_release(xhp->repo_pool);
xhp->repo_pool = NULL;
xbps_dbg_printf("[rpool] released ok.\n"); xbps_dbg_printf("[rpool] released ok.\n");
} }
@ -242,15 +215,15 @@ xbps_repository_pool_foreach(
int (*fn)(struct repository_pool_index *, void *, bool *), int (*fn)(struct repository_pool_index *, void *, bool *),
void *arg) void *arg)
{ {
const struct xbps_handle *xhp = xbps_handle_get(); prop_dictionary_t d;
struct repository_pool *rpool, *rpool_new; struct xbps_handle *xhp = xbps_handle_get();
struct repository_pool_index *rpi;
size_t i;
int rv = 0; int rv = 0;
bool done = false; bool done = false;
assert(fn != NULL); assert(fn != NULL);
/* /* Initialize repository pool */
* Initialize repository pool.
*/
if ((rv = xbps_repository_pool_init(xhp)) != 0) { if ((rv = xbps_repository_pool_init(xhp)) != 0) {
if (rv == ENOTSUP) { if (rv == ENOTSUP) {
xbps_dbg_printf("[rpool] empty repository list.\n"); xbps_dbg_printf("[rpool] empty repository list.\n");
@ -260,11 +233,23 @@ xbps_repository_pool_foreach(
} }
return rv; return rv;
} }
/* Iterate over repository pool */
for (i = 0; i < prop_array_count(xhp->repo_pool); i++) {
rpi = malloc(sizeof(*rpi));
if (rpi == NULL)
return ENOMEM;
SIMPLEQ_FOREACH_SAFE(rpool, &rpool_queue, rp_entries, rpool_new) { d = prop_array_get(xhp->repo_pool, i);
rv = (*fn)(rpool->rpi, arg, &done); prop_dictionary_get_cstring_nocopy(d, "uri", &rpi->rpi_uri);
if (rv != 0 || done) rpi->rpi_repod = prop_dictionary_get(d, "index");
rpi->rpi_index = i;
rv = (*fn)(rpi, arg, &done);
if (rv != 0 || done) {
free(rpi);
break; break;
}
free(rpi);
} }
return rv; return rv;