[couchdb-glib] Add couchdb_session_get_all_documents call to get all documents in 1 HTTP request



commit 3681927842ffd4f26c1ccb308d1d3dc86adc2754
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Tue May 4 17:55:04 2010 +0200

    Add couchdb_session_get_all_documents call to get all documents in 1 HTTP request

 couchdb-glib/couchdb-document.c |   51 +++++++++++------
 couchdb-glib/couchdb-document.h |    4 +-
 couchdb-glib/couchdb-session.c  |  121 ++++++++++++++++++++++++++++++---------
 couchdb-glib/couchdb-session.h  |   39 +++++++------
 couchdb-glib/utils.h            |    1 +
 5 files changed, 150 insertions(+), 66 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 6d22e3a..75a29d8 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -31,7 +31,7 @@
 struct _CouchdbDocument {
 	GObject		parent;
 
-	CouchdbSession  *couchdb;
+	CouchdbSession  *session;
 	char		*dbname;
 	JsonNode	*root_node;
 };
@@ -60,7 +60,7 @@ couchdb_document_class_init (CouchdbDocumentClass *klass)
 static void
 couchdb_document_init (CouchdbDocument *document)
 {
-	document->couchdb = NULL;
+	document->session = NULL;
 	document->root_node = NULL;
 	document->dbname = NULL;
 }
@@ -75,12 +75,12 @@ couchdb_document_init (CouchdbDocument *document)
  * Return value: A newly-created #CouchdbDocument object, with no data on it.
  */
 CouchdbDocument *
-couchdb_document_new (CouchdbSession *couchdb)
+couchdb_document_new (CouchdbSession *session)
 {
 	CouchdbDocument *document;
 
 	document = g_object_new (COUCHDB_TYPE_DOCUMENT, NULL);
-	document->couchdb = couchdb;
+	document->session = session;
 	document->dbname = NULL;
 
 	document->root_node = json_node_new (JSON_NODE_OBJECT);
@@ -89,6 +89,21 @@ couchdb_document_new (CouchdbSession *couchdb)
 	return document;
 }
 
+CouchdbDocument *
+couchdb_document_new_from_json_object (CouchdbSession *session, JsonObject *json_object)
+{
+	CouchdbDocument *document;
+
+	document = g_object_new (COUCHDB_TYPE_DOCUMENT, NULL);
+	document->session = session;
+	document->dbname = NULL;
+
+	document->root_node = json_node_new (JSON_NODE_OBJECT);
+	json_node_set_object (document->root_node, json_object_ref (json_object));
+
+	return document;
+}
+
 /**
  * couchdb_document_get:
  * @couchdb: A #CouchdbSession object
@@ -102,7 +117,7 @@ couchdb_document_new (CouchdbSession *couchdb)
  * which case, the error argument will contain information about the error.
  */
 CouchdbDocument *
-couchdb_document_get (CouchdbSession *couchdb,
+couchdb_document_get (CouchdbSession *session,
 		      const char *dbname,
 		      const char *docid,
 		      GError **error)
@@ -111,16 +126,16 @@ couchdb_document_get (CouchdbSession *couchdb,
 	JsonParser *parser;
 	CouchdbDocument *document = NULL;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (couchdb), NULL);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (session), NULL);
 	g_return_val_if_fail (dbname != NULL, NULL);
 	g_return_val_if_fail (docid != NULL, NULL);
 
 	encoded_docid = soup_uri_encode (docid, NULL);
-	url = g_strdup_printf ("%s/%s/%s", couchdb_session_get_uri (couchdb), dbname, encoded_docid);
+	url = g_strdup_printf ("%s/%s/%s", couchdb_session_get_uri (session), dbname, encoded_docid);
 	parser = json_parser_new ();
-	if (couchdb_session_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
+	if (couchdb_session_send_message (session, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		document = g_object_new (COUCHDB_TYPE_DOCUMENT, NULL);
-		document->couchdb = couchdb;
+		document->session = session;
 		document->dbname = g_strdup (dbname);
 
 		document->root_node = json_node_copy (json_parser_get_root (parser));		
@@ -171,13 +186,13 @@ couchdb_document_put (CouchdbDocument *document,
 		char *encoded_docid;
 
 		encoded_docid = soup_uri_encode (id, NULL);
-		url = g_strdup_printf ("%s/%s/%s", couchdb_session_get_uri (document->couchdb), dbname, encoded_docid);		
-		send_ok = couchdb_session_send_message (document->couchdb, SOUP_METHOD_PUT, url, body, parser, error);
+		url = g_strdup_printf ("%s/%s/%s", couchdb_session_get_uri (document->session), dbname, encoded_docid);		
+		send_ok = couchdb_session_send_message (document->session, SOUP_METHOD_PUT, url, body, parser, error);
 
 		g_free (encoded_docid);
 	} else {
-		url = g_strdup_printf ("%s/%s/", couchdb_session_get_uri (document->couchdb), dbname);
-		send_ok = couchdb_session_send_message (document->couchdb, SOUP_METHOD_POST, url, body, parser, error);
+		url = g_strdup_printf ("%s/%s/", couchdb_session_get_uri (document->session), dbname);
+		send_ok = couchdb_session_send_message (document->session, SOUP_METHOD_POST, url, body, parser, error);
 	}
 
 	if (send_ok) {
@@ -193,9 +208,9 @@ couchdb_document_put (CouchdbDocument *document,
 		}
 
 		if (id)
-			g_signal_emit_by_name (document->couchdb, "document_updated", dbname, document);
+			g_signal_emit_by_name (document->session, "document_updated", dbname, document);
 		else
-			g_signal_emit_by_name (document->couchdb, "document_created", dbname, document);
+			g_signal_emit_by_name (document->session, "document_created", dbname, document);
 
 		result = TRUE;
 	}
@@ -241,12 +256,12 @@ couchdb_document_delete (CouchdbDocument *document, GError **error)
 	if (!id || !revision) /* we can't remove a document without an ID and/or a REVISION */
 		return FALSE;
 
-	url = g_strdup_printf ("%s/%s/%s?rev=%s", couchdb_session_get_uri (document->couchdb), document->dbname, id, revision);
+	url = g_strdup_printf ("%s/%s/%s?rev=%s", couchdb_session_get_uri (document->session), document->dbname, id, revision);
 	
 	/* We don't parse the http response, therefore the parser arg is NULL */
-	if (couchdb_session_send_message (document->couchdb, SOUP_METHOD_DELETE, url, NULL, NULL, error)) {
+	if (couchdb_session_send_message (document->session, SOUP_METHOD_DELETE, url, NULL, NULL, error)) {
 		result = TRUE;		
-		g_signal_emit_by_name (document->couchdb, "document_deleted", document->dbname, id);
+		g_signal_emit_by_name (document->session, "document_deleted", document->dbname, id);
 	}
 
 	g_free (url);
diff --git a/couchdb-glib/couchdb-document.h b/couchdb-glib/couchdb-document.h
index 73bf1fd..45668da 100644
--- a/couchdb-glib/couchdb-document.h
+++ b/couchdb-glib/couchdb-document.h
@@ -47,8 +47,8 @@ typedef struct {
 
 GType            couchdb_document_get_type (void);
 
-CouchdbDocument *couchdb_document_new (CouchdbSession  *couchdb);
-CouchdbDocument *couchdb_document_get (CouchdbSession  *couchdb,
+CouchdbDocument *couchdb_document_new (CouchdbSession  *session);
+CouchdbDocument *couchdb_document_get (CouchdbSession  *session,
 				       const char      *dbname,
 				       const char      *docid,
 				       GError          **error);
diff --git a/couchdb-glib/couchdb-session.c b/couchdb-glib/couchdb-session.c
index 49093df..41292a0 100644
--- a/couchdb-glib/couchdb-session.c
+++ b/couchdb-glib/couchdb-session.c
@@ -498,9 +498,27 @@ couchdb_session_free_database_list (GSList *dblist)
 	g_slist_free (dblist);
 }
 
+static JsonArray *
+call_all_docs_uri (CouchdbSession *session, const char *url, JsonParser *parser, GError **error)
+{
+	JsonArray *rows = NULL;
+
+	if (couchdb_session_send_message (session, SOUP_METHOD_GET, url, NULL, parser, error)) {
+		JsonNode *root_node;
+
+		root_node = json_parser_get_root (parser);
+		if (json_node_get_node_type (root_node) == JSON_NODE_OBJECT) {
+			rows = json_object_get_array_member (
+				json_node_get_object (root_node), "rows");
+		}
+	}
+
+	return rows;
+}
+
 /**
  * couchdb_session_list_documents:
- * @couchdb: A #CouchdbSession object
+ * @session: A #CouchdbSession object
  * @dbname: Name of the databases to retrieve documents from
  * @error: Placeholder for error information
  *
@@ -514,42 +532,91 @@ couchdb_session_free_database_list (GSList *dblist)
  * #couchdb_session_free_document_list.
  */
 GSList *
-couchdb_session_list_documents (CouchdbSession *couchdb, const char *dbname, GError **error)
+couchdb_session_list_documents (CouchdbSession *session, const char *dbname, GError **error)
 {
 	char *url;
 	JsonParser *parser;
+	JsonArray *rows;
 	GSList *doclist = NULL;
 
-	g_return_val_if_fail (COUCHDB_IS_SESSION (couchdb), NULL);
+	g_return_val_if_fail (COUCHDB_IS_SESSION (session), NULL);
 	g_return_val_if_fail (dbname != NULL, NULL);
 
-	url = g_strdup_printf ("%s/%s/_all_docs", couchdb->priv->uri, dbname);
+	url = g_strdup_printf ("%s/%s/_all_docs", session->priv->uri, dbname);
 	parser = json_parser_new ();
-	if (couchdb_session_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
-		JsonNode *root_node;
 
-		root_node = json_parser_get_root (parser);
-		if (json_node_get_node_type (root_node) == JSON_NODE_OBJECT) {
-			JsonArray *rows;
-			gint i;
+	rows = call_all_docs_uri (session, url, parser, error);
+	if (rows != NULL) {
+		gint i;
+		for (i = 0; i < json_array_get_length (rows); i++) {
+			JsonObject *doc;
+			CouchdbDocumentInfo *doc_info;
+
+			doc = json_array_get_object_element (rows, i);
+			if (!doc)
+				continue;
+
+			doc_info = couchdb_document_info_new (
+				json_object_get_string_member (doc, "id"),
+				json_object_get_string_member (
+					json_object_get_object_member (doc, "value"),
+					"rev"));
+			doclist = g_slist_append (doclist, doc_info);
+		}
+	}
 
-			rows = json_object_get_array_member (
-				json_node_get_object (root_node), "rows");
-			for (i = 0; i < json_array_get_length (rows); i++) {
-				JsonObject *doc;
-				CouchdbDocumentInfo *doc_info;
+	g_object_unref (G_OBJECT (parser));
+	g_free (url);
 
-				doc = json_array_get_object_element (rows, i);
-				if (!doc)
-					continue;
+	return doclist;
+}
 
-				doc_info = couchdb_document_info_new (
-					json_object_get_string_member (doc, "id"),
-					json_object_get_string_member (
-						json_object_get_object_member (doc, "value"),
-						"rev"));
-				doclist = g_slist_append (doclist, doc_info);
-			}
+/**
+ * couchdb_session_get_all_documents:
+ * @couchdb: A #CouchdbSession object
+ * @dbname: Name of the databases to retrieve documents from
+ * @error: Placeholder for error information
+ *
+ * Retrieve all documents from a database on a running CouchDB instance. For
+ * each document found in the database, a #CouchdbDocument object is returned
+ * on the list, which represents the document's contents as found on the
+ * underlying database.
+ *
+ * Return value: a list of #CouchdbDocument objects, or NULL if there are none
+ * or there was an error (in which case the error argument will contain information
+ * about the error). Once no longer needed, the list should be freed by calling
+ * #couchdb_session_free_document_list.
+ */
+GSList *
+couchdb_session_get_all_documents (CouchdbSession *session, const char *dbname, GError **error)
+{
+	char *url;
+	JsonParser *parser;
+	JsonArray *rows;
+	GSList *doclist = NULL;
+
+	g_return_val_if_fail (COUCHDB_IS_SESSION (session), NULL);
+	g_return_val_if_fail (dbname != NULL, NULL);
+
+	url = g_strdup_printf ("%s/%s/_all_docs?include_docs=true", session->priv->uri, dbname);
+	parser = json_parser_new ();
+
+	rows = call_all_docs_uri (session, url, parser, error);
+	if (rows != NULL) {
+		gint i;
+		for (i = 0; i < json_array_get_length (rows); i++) {
+			JsonObject *obj;
+			CouchdbDocument *document;
+
+			obj = json_array_get_object_element (rows, i);
+			if (!obj)
+				continue;
+
+			document = couchdb_document_new_from_json_object (
+				session,
+				json_object_get_object_member (obj, "doc"));
+			if (document != NULL)
+				doclist = g_slist_append (doclist, document);
 		}
 	}
 
diff --git a/couchdb-glib/couchdb-session.h b/couchdb-glib/couchdb-session.h
index 3a1a039..fd918ec 100644
--- a/couchdb-glib/couchdb-session.h
+++ b/couchdb-glib/couchdb-session.h
@@ -51,47 +51,48 @@ typedef struct {
 typedef struct {
 	GObjectClass parent_class;
 
-	void (* authentication_failed) (CouchdbSession *couchdb);
+	void (* authentication_failed) (CouchdbSession *session);
 
-	void (* database_created) (CouchdbSession *couchdb, const char *dbname);
-	void (* database_deleted) (CouchdbSession *couchdb, const char *dbname);
+	void (* database_created) (CouchdbSession *session, const char *dbname);
+	void (* database_deleted) (CouchdbSession *session, const char *dbname);
 
-	void (* document_created) (CouchdbSession *couchdb, const char *dbname, CouchdbDocument *document);
-	void (* document_updated) (CouchdbSession *couchdb, const char *dbname, CouchdbDocument *document);
-	void (* document_deleted) (CouchdbSession *couchdb, const char *dbname, const char *docid);
+	void (* document_created) (CouchdbSession *session, const char *dbname, CouchdbDocument *document);
+	void (* document_updated) (CouchdbSession *session, const char *dbname, CouchdbDocument *document);
+	void (* document_deleted) (CouchdbSession *session, const char *dbname, const char *docid);
 } CouchdbSessionClass;
 
 GType                couchdb_session_get_type (void);
 CouchdbSession      *couchdb_session_new (const char *uri);
 
-const char          *couchdb_session_get_uri (CouchdbSession *couchdb);
+const char          *couchdb_session_get_uri (CouchdbSession *session);
 
-GSList              *couchdb_session_list_databases (CouchdbSession *couchdb, GError **error);
+GSList              *couchdb_session_list_databases (CouchdbSession *session, GError **error);
 void                 couchdb_session_free_database_list (GSList *dblist);
 
-CouchdbDatabaseInfo *couchdb_session_get_database_info (CouchdbSession *couchdb, const char *dbname, GError **error);
+CouchdbDatabaseInfo *couchdb_session_get_database_info (CouchdbSession *session, const char *dbname, GError **error);
 
-gboolean             couchdb_session_create_database (CouchdbSession *couchdb, const char *dbname, GError **error);
-gboolean             couchdb_session_delete_database (CouchdbSession *couchdb, const char *dbname, GError **error);
-gboolean             couchdb_session_compact_database (CouchdbSession *couchdb, const char *dbname, GError **error);
+gboolean             couchdb_session_create_database (CouchdbSession *session, const char *dbname, GError **error);
+gboolean             couchdb_session_delete_database (CouchdbSession *session, const char *dbname, GError **error);
+gboolean             couchdb_session_compact_database (CouchdbSession *session, const char *dbname, GError **error);
 
-void                 couchdb_session_listen_for_changes (CouchdbSession *couchdb, const char *dbname);
+void                 couchdb_session_listen_for_changes (CouchdbSession *session, const char *dbname);
 
-void                 couchdb_session_enable_authentication (CouchdbSession *couchdb, CouchdbCredentials *credentials);
-void                 couchdb_session_disable_authentication (CouchdbSession *couchdb);
-gboolean             couchdb_session_is_authentication_enabled (CouchdbSession *couchdb);
+void                 couchdb_session_enable_authentication (CouchdbSession *session, CouchdbCredentials *credentials);
+void                 couchdb_session_disable_authentication (CouchdbSession *session);
+gboolean             couchdb_session_is_authentication_enabled (CouchdbSession *session);
 
-gboolean             couchdb_session_send_message (CouchdbSession *couchdb,
+gboolean             couchdb_session_send_message (CouchdbSession *session,
 						   const char *method,
 						   const char *url,
 						   const char *body,
 						   JsonParser *output,
 						   GError **error);
 
-GSList              *couchdb_session_list_documents (CouchdbSession *couchdb, const char *dbname, GError **error);
+GSList              *couchdb_session_list_documents (CouchdbSession *session, const char *dbname, GError **error);
+GSList              *couchdb_session_get_all_documents (CouchdbSession *session, const char *dbname, GError **error);
 void                 couchdb_session_free_document_list (GSList *doclist);
 
-gboolean             couchdb_session_replicate (CouchdbSession *couchdb,
+gboolean             couchdb_session_replicate (CouchdbSession *session,
 						const gchar *source,
 						const gchar *target,
 						gboolean continous,
diff --git a/couchdb-glib/utils.h b/couchdb-glib/utils.h
index 4118d9a..9ce438e 100644
--- a/couchdb-glib/utils.h
+++ b/couchdb-glib/utils.h
@@ -38,6 +38,7 @@ GQuark      couchdb_error_quark (void);
 char* generate_uuid (void);
 
 /* Private API */
+CouchdbDocument  *couchdb_document_new_from_json_object (CouchdbSession *session, JsonObject *json_object);
 JsonObject*	  couchdb_document_get_json_object	(CouchdbDocument *document);
 
 CouchdbArrayField  *couchdb_array_field_new_from_json_array (JsonArray *json_array);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]