GNOME GIT source code repository

summaryrefslogtreecommitdiff
path: root/tests/test-list-databases.c (plain)
blob: a8eb502cf6a15d77189fd37e6062c81e072d5c1b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 Canonical Services Ltd (www.canonical.com)
 *
 * Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <couchdb-glib.h>

int
main (int argc, char *argv[])
{
	CouchDB *couchdb;
	GSList *dblist, *sl;
	GError *error = NULL;


	g_type_init ();
	g_thread_init (NULL);

	/* Initialize CouchDB */
	couchdb =  argc > 1 ? couchdb_new (argv[1]) : couchdb_new (NULL);
	g_printf ("Connecting to CouchDB at %s\n", couchdb_get_hostname (couchdb));
	
	if (!couchdb) {
		g_print ("Could not create CouchDB object\n");
		return -1;
	}

	dblist = couchdb_list_databases (couchdb, &error);
	if (error != NULL) {
		g_critical ("Error listing databases: %s", error->message);
		g_error_free (error);
	}
	
	for (sl = dblist; sl != NULL; sl = sl->next) {
		CouchDBDatabaseInfo *dbinfo;
		GSList *doclist;

		error = NULL;
		g_print ("Found database %s\n", (const char *) sl->data);
		dbinfo = couchdb_get_database_info (couchdb, (const char *) sl->data, &error);
		if (dbinfo) {
			g_print ("\tDatabase name: %s\n",
				 couchdb_database_info_get_dbname (dbinfo));
			g_print ("\t# of documents: %d\n",
				 couchdb_database_info_get_documents_count (dbinfo));
			g_print ("\t# of deleted documents: %d\n",
				 couchdb_database_info_get_deleted_documents_count (dbinfo));
			g_print ("\tUpdate sequence: %d\n",
				 couchdb_database_info_get_update_sequence (dbinfo));
			g_print ("\tCompact running?: %s\n",
				 couchdb_database_info_is_compact_running (dbinfo) ? "True" : "False");
			g_print ("\tDisk size: %d\n",
				 couchdb_database_info_get_disk_size (dbinfo));

			couchdb_database_info_unref (dbinfo);
		} else {
			g_print ("Could not retrieve info for database %s\n", (const char *) sl->data);
		}

		/* now, get list of documents */
		error = NULL;
		doclist = couchdb_list_documents (couchdb, (const char *) sl->data, &error);
		if (doclist) {
			GSList *sl_doc;

			g_print ("\tDocuments:\n");
			for (sl_doc = doclist; sl_doc != NULL; sl_doc = sl_doc->next) {
				CouchDBDocumentInfo *doc_info = sl_doc->data;
				CouchDBDocument *document;

				error = NULL;
				document = couchdb_document_get (couchdb,
								 (const char *) sl->data,
								 couchdb_document_info_get_docid (doc_info),
								 &error);
				if (document) {
					char *json;

					g_print ("\t\t%s\t(rev: %s)\n",
						 couchdb_document_get_id (document),
						 couchdb_document_get_revision (document));

					json = couchdb_document_to_string (document);
					g_print ("\t\t\t%s\n", json);
					g_free (json);

					g_object_unref (G_OBJECT (document));
				} else {
					g_print ("\t\t%s\t(rev: %s)\n",
						 couchdb_document_info_get_docid (doc_info),
						 couchdb_document_info_get_revision (doc_info));
				}
			}
		} else {
			g_print ("\tNo documents\n");
		}
	}

	couchdb_free_database_list (dblist);
	g_object_unref (G_OBJECT (couchdb));

	return 0;
}