Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
libpkg: Introduce pkgdb_all_search() function.
Gleb Popov committed 3 years ago
commit ffdb473e704a48abca70446a5f025aba2eec8ca8
parent f8cdf1d
5 files changed +87 -76
modified libpkg/libpkg.ver
@@ -212,6 +212,7 @@ global:
	pkgdb_repo_query;
	pkgdb_repo_query_cond;
	pkgdb_repo_search;
+
	pkgdb_all_search;
	pkgdb_rquery_provide;
	pkgdb_set2;
	pkgdb_set_case_sensitivity;
modified libpkg/pkg.h.in
@@ -921,6 +921,8 @@ struct pkgdb_it *pkgdb_repo_query_cond(struct pkgdb *db, const char *cond,
	const char *pattern, match_t type, const char *reponame);
struct pkgdb_it * pkgdb_repo_search(struct pkgdb *db, const char *pattern,
    match_t type, pkgdb_field field, pkgdb_field sort, const char *reponame);
+
struct pkgdb_it * pkgdb_all_search(struct pkgdb *db, const char *pattern,
+
    match_t type, pkgdb_field field, pkgdb_field sort, const char *reponame);

/**
 * @todo Return directly the struct pkg?
modified libpkg/pkgdb_iterator.c
@@ -1116,47 +1116,42 @@ int
pkgdb_it_next(struct pkgdb_it *it, struct pkg **pkg_p, unsigned flags)
{
	struct pkg_repo_it *rit;
-
	int ret;
+
	int ret = EPKG_END;

	assert(it != NULL);

-
	switch (it->type) {
-
	case PKGDB_IT_LOCAL:
-
		return (pkgdb_sqlite_it_next(&it->un.local, pkg_p, flags));
-
		break;
-
	case PKGDB_IT_REPO:
-
		if (it->un.remote != NULL) {
-
			rit = it->un.remote->it;
-
			ret = rit->ops->next(rit, pkg_p, flags);
-
			if (ret != EPKG_OK) {
-
				/*
-
				 * Detach this iterator from list and switch to another
-
				 */
-
				struct _pkg_repo_it_set *tmp;
-

-
				rit->ops->free(rit);
-
				tmp = it->un.remote;
-
				it->un.remote = tmp->next;
-
				free(tmp);
-

-
				return (pkgdb_it_next(it, pkg_p, flags));
-
			}
-

-
			if (*pkg_p != NULL)
-
				(*pkg_p)->repo = rit->repo;
+
	if (it->local != NULL && !it->local->finished) {
+
		if (pkgdb_sqlite_it_next(it->local, pkg_p, flags) == EPKG_OK)
+
			return EPKG_OK;
+
	}

-
			return (EPKG_OK);
+
	if (it->remote != NULL) {
+
		rit = it->remote->it;
+
		ret = rit->ops->next(rit, pkg_p, flags);
+
		if (ret != EPKG_OK) {
+
			/*
+
			* Detach this iterator from list and switch to another
+
			*/
+
			struct _pkg_repo_it_set *tmp;
+

+
			rit->ops->free(rit);
+
			tmp = it->remote;
+
			it->remote = tmp->next;
+
			free(tmp);
+

+
			return (pkgdb_it_next(it, pkg_p, flags));
		}
-
		/*
-
		 * All done
-
		 */
-
		return (EPKG_END);
-
		break;
+

+
		if (*pkg_p != NULL)
+
			(*pkg_p)->repo = rit->repo;
+

+
		return (EPKG_OK);
	}

-
	return (EPKG_FATAL);
+
	return ret;
}

+
// TODO: Why doesn't this function handle remote?
int
pkgdb_it_count(struct pkgdb_it *it)
{
@@ -1167,7 +1162,7 @@ pkgdb_it_count(struct pkgdb_it *it)
	assert(it != NULL);

	i = 0;
-
	sit = &it->un.local;
+
	sit = it->local;

	if (sit == NULL)
		return (0);
@@ -1197,15 +1192,13 @@ pkgdb_it_reset(struct pkgdb_it *it)

	assert(it != NULL);

-
	switch (it->type) {
-
		case PKGDB_IT_LOCAL:
-
			pkgdb_sqlite_it_reset(&it->un.local);
-
			break;
-
		case PKGDB_IT_REPO:
-
			LL_FOREACH(it->un.remote, cur) {
-
				cur->it->ops->reset(cur->it);
-
			}
-
			break;
+
	if (it->local != NULL) {
+
		pkgdb_sqlite_it_reset(it->local);
+
	}
+
	if (it->remote != NULL) {
+
		LL_FOREACH(it->remote, cur) {
+
			cur->it->ops->reset(cur->it);
+
		}
	}
}

@@ -1217,16 +1210,15 @@ pkgdb_it_free(struct pkgdb_it *it)
	if (it == NULL)
		return;

-
	switch (it->type) {
-
		case PKGDB_IT_LOCAL:
-
			pkgdb_sqlite_it_free(&it->un.local);
-
			break;
-
		case PKGDB_IT_REPO:
-
			LL_FOREACH_SAFE(it->un.remote, cur, tmp) {
-
				cur->it->ops->free(cur->it);
-
				free(cur);
-
			}
-
			break;
+
	if (it->local != NULL) {
+
		pkgdb_sqlite_it_free(it->local);
+
		free(it->local);
+
	}
+
	if (it->remote != NULL) {
+
		LL_FOREACH_SAFE(it->remote, cur, tmp) {
+
			cur->it->ops->free(cur->it);
+
			free(cur);
+
		}
	}

	free(it);
@@ -1243,15 +1235,16 @@ pkgdb_it_new_sqlite(struct pkgdb *db, sqlite3_stmt *s, int type, short flags)

	it = xmalloc(sizeof(struct pkgdb_it));

-
	it->type = PKGDB_IT_LOCAL;
-

	it->db = db;
-
	it->un.local.sqlite = db->sqlite;
-
	it->un.local.stmt = s;
-
	it->un.local.pkg_type = type;
+
	it->local = xmalloc(sizeof(struct pkgdb_sqlite_it));
+
	it->local->sqlite = db->sqlite;
+
	it->local->stmt = s;
+
	it->local->pkg_type = type;
+

+
	it->local->flags = flags;
+
	it->local->finished = 0;

-
	it->un.local.flags = flags;
-
	it->un.local.finished = 0;
+
	it->remote = NULL;

	return (it);
}
@@ -1263,11 +1256,10 @@ pkgdb_it_new_repo(struct pkgdb *db)

	it = xmalloc(sizeof(struct pkgdb_it));

-
	it->type = PKGDB_IT_REPO;
-

	it->db = db;

-
	it->un.remote = NULL;
+
	it->local = NULL;
+
	it->remote = NULL;

	return (it);
}
@@ -1279,7 +1271,7 @@ pkgdb_it_repo_attach(struct pkgdb_it *it, struct pkg_repo_it *rit)

	item = xmalloc(sizeof(struct _pkg_repo_it_set));
	item->it = rit;
-
	LL_PREPEND(it->un.remote, item);
+
	LL_PREPEND(it->remote, item);
}

int
modified libpkg/pkgdb_query.c
@@ -497,6 +497,7 @@ pkgdb_repo_provide(struct pkgdb *db, const char *require, const char *repo)

	return (it);
}
+

struct pkgdb_it *
pkgdb_repo_search(struct pkgdb *db, const char *pattern, match_t match,
    pkgdb_field field, pkgdb_field sort, const char *repo)
@@ -521,3 +522,26 @@ pkgdb_repo_search(struct pkgdb *db, const char *pattern, match_t match,

	return (it);
}
+

+
struct pkgdb_it *
+
pkgdb_all_search(struct pkgdb *db, const char *pattern, match_t match,
+
    pkgdb_field field, pkgdb_field sort, const char *repo)
+
{
+
	struct pkgdb_it *it;
+
	struct pkg_repo_it *rit;
+

+
	it = pkgdb_query(db, pattern, match);
+

+
	tll_foreach(db->repos, cur) {
+
		if (repo == NULL || strcasecmp(cur->item->name, repo) == 0) {
+
			if (cur->item->ops->search != NULL) {
+
				rit = cur->item->ops->search(cur->item, pattern, match,
+
					field, sort);
+
				if (rit != NULL)
+
					pkgdb_it_repo_attach(it, rit);
+
			}
+
		}
+
	}
+

+
	return (it);
+
}
modified libpkg/private/pkgdb.h
@@ -40,11 +40,6 @@ struct pkgdb {
	tll(struct pkg_repo *) repos;
};

-
enum pkgdb_iterator_type {
-
	PKGDB_IT_LOCAL = 0,
-
	PKGDB_IT_REPO
-
};
-

struct pkgdb_sqlite_it {
	sqlite3	*sqlite;
	sqlite3_stmt	*stmt;
@@ -56,15 +51,12 @@ struct pkgdb_sqlite_it {
struct pkg_repo_it;

struct pkgdb_it {
-
	enum pkgdb_iterator_type type;
	struct pkgdb *db;
-
	union _un_pkg_it {
-
		struct _pkg_repo_it_set {
-
			struct pkg_repo_it *it;
-
			struct _pkg_repo_it_set *next;
-
		} *remote;
-
		struct pkgdb_sqlite_it local;
-
	} un;
+
	struct _pkg_repo_it_set {
+
		struct pkg_repo_it *it;
+
		struct _pkg_repo_it_set *next;
+
	} *remote;
+
	struct pkgdb_sqlite_it *local;
};

#define PKGDB_IT_FLAG_CYCLED (0x1)