socsvn commit: r254247 - in soc2013/mattbw/backend: . actions

mattbw at FreeBSD.org mattbw at FreeBSD.org
Sat Jul 6 17:34:34 UTC 2013


Author: mattbw
Date: Sat Jul  6 17:34:33 2013
New Revision: 254247
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=254247

Log:
  tidy up query system interface

Modified:
  soc2013/mattbw/backend/actions/get-details.c
  soc2013/mattbw/backend/actions/get-files.c
  soc2013/mattbw/backend/actions/install-packages.c
  soc2013/mattbw/backend/query.c
  soc2013/mattbw/backend/query.h

Modified: soc2013/mattbw/backend/actions/get-details.c
==============================================================================
--- soc2013/mattbw/backend/actions/get-details.c	Sat Jul  6 16:51:30 2013	(r254246)
+++ soc2013/mattbw/backend/actions/get-details.c	Sat Jul  6 17:34:33 2013	(r254247)
@@ -30,7 +30,6 @@
 
 static const int LOAD_FLAGS = PKG_LOAD_BASIC | PKG_LOAD_LICENSES;
 
-static gboolean	body(struct query *q);
 static void	emit(struct pkg *pkg, const gchar *id, struct query *q);
 
 /*
@@ -40,17 +39,7 @@
 gboolean
 get_details_thread(PkBackend *backend)
 {
-	return query_for_all_ids(backend, LOAD_FLAGS, body);
-}
-
-/*
- * Look up and emit package details for the given PackageID, if it can be
- * found.
- */
-static gboolean
-body(struct query *q)
-{
-	return query_emit_match(q, emit);
+	return query_match_id_to_emitter(backend, LOAD_FLAGS, emit);
 }
 
 /*

Modified: soc2013/mattbw/backend/actions/get-files.c
==============================================================================
--- soc2013/mattbw/backend/actions/get-files.c	Sat Jul  6 16:51:30 2013	(r254246)
+++ soc2013/mattbw/backend/actions/get-files.c	Sat Jul  6 17:34:33 2013	(r254247)
@@ -34,7 +34,6 @@
 static const int FILE_NAME_STEP = 10;
 static const int LOAD_FLAGS = PKG_LOAD_BASIC | PKG_LOAD_FILES;
 
-static gboolean	body(struct query *q);
 static void	emit(struct pkg *pkg, const gchar *id, struct query *q);
 
 /*
@@ -44,17 +43,8 @@
 gboolean
 get_files_thread(PkBackend *backend)
 {
-	return query_for_all_ids(backend, LOAD_FLAGS, body);
-}
 
-/*
- * Look up and emit package details for the given PackageID, if it can be
- * found.
- */
-static gboolean
-body(struct query *q)
-{
-	return query_emit_match(q, emit);
+	return query_match_id_to_emitter(backend, LOAD_FLAGS, emit);
 }
 
 /*
@@ -66,27 +56,28 @@
 	struct pkg_file *file;
 	int		err;
 	int		sb_err;
-	struct sbuf *sb;
+	struct sbuf    *sb;
 
-	/* Construct a string of the form ";file1;file2;file3;...".
-	 * We'll get rid of the initial ; later.
- 	 */
+	/*
+	 * Construct a string of the form ";file1;file2;file3;...". We'll get
+	 * rid of the initial ; later.
+	 */
 	sb = sbuf_new_auto();
 	for (HASH_FOR(err, pkg_files, pkg, &file))
 		sbuf_printf(sb, ";%s", pkg_file_path(file));
 	sb_err = sbuf_finish(sb);
 	if (sb_err)
 		pk_backend_error_code(query_backend(q),
-			PK_ERROR_ENUM_INTERNAL_ERROR,
-			"couldn't construct filename string");
+				      PK_ERROR_ENUM_INTERNAL_ERROR,
+				      "couldn't construct filename string");
 	else {
-		char *filenames;
+		char           *filenames;
 
 		filenames = sbuf_data(sb);
-		/* Skip over any initial ;*/
+		/* Skip over any initial ; */
 		if (filenames[0] == ';')
 			filenames++;
-		
+
 		pk_backend_files(query_backend(q), id, filenames);
 	}
 	sbuf_delete(sb);

Modified: soc2013/mattbw/backend/actions/install-packages.c
==============================================================================
--- soc2013/mattbw/backend/actions/install-packages.c	Sat Jul  6 16:51:30 2013	(r254246)
+++ soc2013/mattbw/backend/actions/install-packages.c	Sat Jul  6 17:34:33 2013	(r254247)
@@ -29,15 +29,11 @@
 
 #include "actions.h"		/* install_packages_thread prototype */
 
-static gboolean	body(struct query *q);
-static gboolean	sim_body(struct query *q);
 static int	install_event_cb(void *backend_v, struct pkg_event *event);
 static void	do_install_packages(struct pkg_jobs *jobs, struct query *q, gboolean simulate);
 static void	do_install_solved_job(struct pkg_jobs *jobs, PkBackend *backend, gboolean simulate);
 static void	do_install_solved_job_real(struct pkg_jobs *jobs, PkBackend *backend);
-static void	emit(struct pkg *pkg, const gchar *id, struct query *q);
-static void	job (struct pkg_jobs *jobs, struct query *q);
-static void	sim_emit(struct pkg *pkg, const gchar *id, struct query *q);
+static void	job(struct pkg_jobs *jobs, struct query *q);
 static void	sim_job(struct pkg_jobs *jobs, struct query *q);
 
 
@@ -49,7 +45,7 @@
 install_packages_thread(PkBackend *backend)
 {
 
-	return query_for_all_ids(backend, PKG_LOAD_BASIC, body);
+	return query_match_id_to_job(backend, PKG_JOBS_INSTALL, job);
 }
 
 /*
@@ -60,28 +56,7 @@
 simulate_install_packages_thread(PkBackend *backend)
 {
 
-	return query_for_all_ids(backend, PKG_LOAD_BASIC, sim_body);
-}
-
-/*
- * Look up and attempt to install the given PackageID, if it can be found.
- */
-static gboolean
-body(struct query *q)
-{
-
-	return query_emit_match(q, emit);
-}
-
-/*
- * Look up and attempt to simulate installing the given PackageID, if it can
- * be found.
- */
-static gboolean
-sim_body(struct query *q)
-{
-
-	return query_emit_match(q, sim_emit);
+	return query_match_id_to_job(backend, PKG_JOBS_INSTALL, sim_job);
 }
 
 /*
@@ -210,17 +185,6 @@
 }
 
 /*
- * Tries to install the given package.
- */
-static void
-emit(struct pkg *pkg, const gchar *id, struct query *q)
-{
-
-	INTENTIONALLY_IGNORE(id);
-	return query_emit_to_job(pkg, q, PKG_JOBS_INSTALL, job);
-}
-
-/*
  * Tries to process the given solved installation jobs.
  */
 static void
@@ -231,17 +195,6 @@
 }
 
 /*
- * Tries to install the given package.
- */
-static void
-sim_emit(struct pkg *pkg, const gchar *id, struct query *q)
-{
-
-	INTENTIONALLY_IGNORE(id);
-	return query_emit_to_job(pkg, q, PKG_JOBS_INSTALL, sim_job);
-}
-
-/*
  * Tries to simulate processing the given installation jobs.
  */
 static void

Modified: soc2013/mattbw/backend/query.c
==============================================================================
--- soc2013/mattbw/backend/query.c	Sat Jul  6 16:51:30 2013	(r254246)
+++ soc2013/mattbw/backend/query.c	Sat Jul  6 17:34:33 2013	(r254247)
@@ -42,12 +42,20 @@
 
 	gboolean	any_repo;
 	gboolean	local_repo;
+
+	/* Query result routing */
+	emit_ptr	emitter;
+	job_emit_ptr	job_emitter;
+	pkg_jobs_t	job_type;
 };
 
 static gboolean	match(struct query *q, gchar **match_id_p, struct pkg **match_pkg_p);
 static gboolean	match_id_in_it(struct pkgdb_it *it, struct query *q, gchar **match_id_p, struct pkg **match_pkg_p);
 static gboolean	try_id_match(struct pkg *pkg, struct query *q, gchar **match_id);
+static gboolean query_do(PkBackend *backend, int load_flags, emit_ptr emitter, pkg_jobs_t job_type, job_emit_ptr job_emitter, query_body_ptr body);
+static gboolean query_match(struct query *q);
 static int	jobs_add_pkg(struct pkg_jobs *jobs, match_t type, struct pkg *pkg);
+static void	query_emit_to_job(struct pkg *pkg, const gchar *id, struct query *q);
 static void	query_free_contents(struct query *q);
 
 /* Returns the backend stored inside the struct query. */
@@ -57,80 +65,15 @@
 	return (q == NULL ? NULL : q->backend);
 }
 
-/*
- * Performs a package database query against a PackageID, and then hands the
- * matching result to an emitter function.
- */
-gboolean
-query_emit_match(struct query *q, emit_ptr emitter)
-{
-	gboolean	success;
-	gchar          *match_id;
-	struct pkg     *match_pkg;
-
-	match_id = NULL;
-	match_pkg = NULL;
-	success = match(q, &match_id, &match_pkg);
-	if (success == TRUE && match_id != NULL && match_pkg != NULL)
-		emitter(match_pkg, match_id, q);
-
-	pkg_free(match_pkg);
-	g_free(match_id);
-
-	return success;
-}
-
-/*
- * Iterates a query function over all PackageIDs provided for this job.
- * 
- * This provides each iterating function call with a query structure ready to
- * run and updates the percentage after each iteration.
- * 
- * It also *finishes* the backend job.
- */
-gboolean
-query_for_all_ids(PkBackend *backend, int load_flags, query_body_ptr body)
-{
-	gboolean	no_error_yet;
-	gchar         **package_ids;
-	guint		len;
-	guint		i;
-	struct pkgdb   *db;
-	struct query   *q;
-
-	package_ids = pk_backend_get_strv(backend, "package_ids");
-	len = g_strv_length(package_ids);
-
-	db = NULL;
-	q = NULL;
-
-	no_error_yet = open_remote_db(&db, backend);
-
-	pk_backend_set_percentage(backend, 0);
-	for (i = 0; i < len && no_error_yet; i++) {
-		no_error_yet = query_init(package_ids[i],
-					  backend,
-					  db,
-					  load_flags,
-					  &q);
-		if (no_error_yet == TRUE)
-			no_error_yet = body(q);
-		pk_backend_set_percentage(backend, ((i * 100) / len));
-	}
-	query_free(q);
-	pkgdb_close(db);
-
-	pk_backend_finished(backend);
-
-	return no_error_yet;
-}
-
 /* Creates a struct query for the given backend and target ID. */
 gboolean
 query_init(const gchar *id,
 	   PkBackend *backend,
 	   struct pkgdb *db,
 	   int load_flags,
+	   emit_ptr emitter,
+	   pkg_jobs_t job_type,
+	   job_emit_ptr job_emitter,
 	   struct query **q_p)
 {
 	gboolean	success;
@@ -146,6 +89,9 @@
 	q->backend = backend;
 	q->load_flags = load_flags;
 	q->db = db;
+	q->emitter = emitter;
+	q->job_type = job_type;
+	q->job_emitter = job_emitter;
 
 	success = split_id(id,
 			   &(q->strv),
@@ -194,26 +140,134 @@
 }
 
 /*
+ * Runs a query over the PackageIDs selected in the backend that sends the first
+ * match to an emitting function.
+ */
+gboolean
+query_match_id_to_emitter(PkBackend *backend, int load_flags, emit_ptr emitter)
+{
+
+	return query_do(backend,
+			load_flags,
+			emitter,
+			PKG_JOBS_INSTALL,
+			NULL,
+			query_match);
+}
+
+/*
+ * Runs a query over the PackageIDs selected in the backend that converts the
+ * first match into a job of the given type and hands it to a function for
+ * solving and applying.
+ */
+gboolean
+query_match_id_to_job(PkBackend *backend, pkg_jobs_t type, job_emit_ptr emitter)
+{
+
+	return query_do(backend,
+			PKG_LOAD_BASIC,
+			query_emit_to_job,
+			type,
+			emitter,
+			query_match);
+}
+
+/*
+ * Performs a package database query against a PackageID, and then emits
+ * the first matching result.
+ */
+static gboolean
+query_match(struct query *q)
+{
+	gboolean	success;
+	gchar          *match_id;
+	struct pkg     *match_pkg;
+
+	match_id = NULL;
+	match_pkg = NULL;
+	success = match(q, &match_id, &match_pkg);
+	if (success == TRUE && match_id != NULL && match_pkg != NULL)
+		q->emitter(match_pkg, match_id, q);
+
+	pkg_free(match_pkg);
+	g_free(match_id);
+
+	return success;
+}
+
+/*
+ * Iterates a query function over all PackageIDs provided for this job.
+ * 
+ * This provides each iterating function call with a query structure ready to
+ * run and updates the percentage after each iteration.
+ * 
+ * It also *finishes* the backend job.
+ */
+static gboolean
+query_do(PkBackend *backend,
+         int load_flags,
+         emit_ptr emitter,
+	 pkg_jobs_t job_type,
+         job_emit_ptr job_emitter,
+         query_body_ptr body)
+{
+	gboolean	no_error_yet;
+	gchar         **package_ids;
+	guint		len;
+	guint		i;
+	struct pkgdb   *db;
+	struct query   *q;
+
+	package_ids = pk_backend_get_strv(backend, "package_ids");
+	len = g_strv_length(package_ids);
+
+	db = NULL;
+	q = NULL;
+
+	no_error_yet = open_remote_db(&db, backend);
+
+	pk_backend_set_percentage(backend, 0);
+	for (i = 0; i < len && no_error_yet; i++) {
+		no_error_yet = query_init(package_ids[i],
+					  backend,
+					  db,
+					  load_flags,
+					  emitter,
+					  job_type,
+					  job_emitter,
+					  &q);
+		if (no_error_yet == TRUE)
+			no_error_yet = body(q);
+		pk_backend_set_percentage(backend, ((i * 100) / len));
+	}
+	query_free(q);
+	pkgdb_close(db);
+
+	pk_backend_finished(backend);
+
+	return no_error_yet;
+}
+
+/*
  * For adapting an emitter function into one that solves and applies a job.
  */
-void
-query_emit_to_job(struct pkg *pkg,
-		  struct query *q,
-		  pkg_jobs_t type,
-		  job_emit_ptr job_emitter)
+static void
+query_emit_to_job(struct pkg *pkg, const gchar *id, struct query *q)
 {
 	int		err;
 	struct pkg_jobs *jobs;
 
+	INTENTIONALLY_IGNORE(id);
+
 	jobs = NULL;
-	err = pkg_jobs_new(&jobs, type, q->db);
+	err = pkg_jobs_new(&jobs, q->job_type, q->db);
 	if (err == EPKG_OK) {
 		if (q->data != NULL)
 			err = pkg_jobs_set_repository(jobs, q->data);
 		if (err == EPKG_OK) {
 			err = jobs_add_pkg(jobs, MATCH_EXACT, pkg);
 			if (err == EPKG_OK)
-				job_emitter(jobs, q);
+				q->job_emitter(jobs, q);
 			else
 				pk_backend_error_code(q->backend,
 					       PK_ERROR_ENUM_INTERNAL_ERROR,
@@ -222,7 +276,6 @@
 			pk_backend_error_code(q->backend,
 					      PK_ERROR_ENUM_REPO_NOT_FOUND,
 					      "could not set repo");
-
 	} else
 		pk_backend_error_code(q->backend,
 				      PK_ERROR_ENUM_INTERNAL_ERROR,

Modified: soc2013/mattbw/backend/query.h
==============================================================================
--- soc2013/mattbw/backend/query.h	Sat Jul  6 16:51:30 2013	(r254246)
+++ soc2013/mattbw/backend/query.h	Sat Jul  6 17:34:33 2013	(r254247)
@@ -32,10 +32,11 @@
 typedef gboolean (*query_body_ptr) (struct query *q);
 
 PkBackend      *query_backend(struct query *q);
-gboolean	query_emit_match(struct query *q, emit_ptr emitter);
-gboolean	query_for_all_ids(PkBackend *backend, int load_flags, query_body_ptr body);
-gboolean	query_init(const gchar *id, PkBackend *backend, struct pkgdb *db, int load_flags, struct query **q_p);
-void		query_emit_to_job(struct pkg *pkg, struct query *q, pkg_jobs_t type, job_emit_ptr job_emitter);
+gboolean	query_init(const gchar *id, PkBackend *backend, struct pkgdb *db, int load_flags, emit_ptr emitter, pkg_jobs_t job_type, job_emit_ptr job_emitter, struct query **q_p);
+
+gboolean	query_match_id_to_emitter(PkBackend *backend, int load_flags, emit_ptr emitter);
+gboolean	query_match_id_to_job(PkBackend *backend, pkg_jobs_t type, job_emit_ptr job_emitter);
+
 void		query_free(struct query *q);
 void		query_pkg_to_id(struct pkg *pkg, gchar **id_p);
 


More information about the svn-soc-all mailing list