]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/database.c
ruby: First attempt at fixing gc for ruby-1.9
[notmuch] / bindings / ruby / database.c
1 /* The Ruby interface to the notmuch mail library
2  *
3  * Copyright © 2010 Ali Polatel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Ali Polatel <alip@exherbo.org>
19  */
20
21 #include "defs.h"
22
23 /*
24  * call-seq: Notmuch::Database.new(path, [{:create => false, :mode => notmuch::MODE_READ_ONLY}]) => DB
25  *
26  * Create or open a notmuch database using the given path.
27  * If :create is +true+, create the database instead of opening.
28  * The argument :mode specifies the open mode of the database.
29  */
30 VALUE
31 notmuch_rb_database_new(int argc, VALUE *argv, VALUE klass)
32 {
33     const char *path;
34     int create, mode;
35     notmuch_rb_database_t *db;
36     VALUE modev, dbv;
37
38 #if !defined(RSTRING_PTR)
39 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
40 #endif /* !defined(RSTRING_PTR) */
41
42     /* Check arguments */
43     if (argc < 1 || argc > 2)
44         rb_raise(rb_eTypeError, "Wrong number of arguments");
45
46     SafeStringValue(argv[0]);
47     path = RSTRING_PTR(argv[0]);
48
49     if (argc == 2) {
50         Check_Type(argv[1], T_HASH);
51         create = RTEST(rb_hash_aref(argv[1], ID2SYM(ID_db_create)));
52         modev = rb_hash_aref(argv[1], ID2SYM(ID_db_mode));
53         if (NIL_P(modev))
54             mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
55         else if (!FIXNUM_P(modev))
56             rb_raise(rb_eTypeError, ":mode isn't a Fixnum");
57         else {
58             mode = FIX2INT(modev);
59             switch (mode) {
60             case NOTMUCH_DATABASE_MODE_READ_ONLY:
61             case NOTMUCH_DATABASE_MODE_READ_WRITE:
62                 break;
63             default:
64                 rb_raise(rb_eTypeError, "Invalid mode");
65             }
66         }
67     }
68     else {
69         create = 0;
70         mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
71     }
72
73     dbv = Data_Make_Struct(klass, notmuch_rb_database_t, NULL, notmuch_rb_database_free, db);
74     db->nm_db = create ? notmuch_database_create(path) : notmuch_database_open(path, mode);
75     if (!db->nm_db)
76         rb_raise(notmuch_rb_eDatabaseError, "failed to open database");
77
78     return dbv;
79 }
80
81 /*
82  * call-seq: DB.close => nil
83  *
84  * Close the notmuch database.
85  */
86 VALUE
87 notmuch_rb_database_close(VALUE self)
88 {
89     notmuch_rb_database_t *db;
90
91     Data_Get_Struct(self, notmuch_rb_database_t, db);
92     if (db->nm_db) {
93         notmuch_database_close(db->nm_db);
94         db->nm_db = NULL;
95     }
96
97     return Qnil;
98 }
99
100 /*
101  * call-seq: DB.path => String
102  *
103  * Return the path of the database
104  */
105 VALUE
106 notmuch_rb_database_path(VALUE self)
107 {
108     notmuch_rb_database_t *db;
109
110     Data_Get_Struct(self, notmuch_rb_database_t, db);
111     if (!db->nm_db)
112         rb_raise(rb_eRuntimeError, "Database closed");
113
114     return rb_str_new2(notmuch_database_get_path(db->nm_db));
115 }
116
117 /*
118  * call-seq: DB.version => Fixnum
119  *
120  * Return the version of the database
121  */
122 VALUE
123 notmuch_rb_database_version(VALUE self)
124 {
125     notmuch_rb_database_t *db;
126
127     Data_Get_Struct(self, notmuch_rb_database_t, db);
128     if (!db->nm_db)
129         rb_raise(rb_eRuntimeError, "Database closed");
130
131     return INT2FIX(notmuch_database_get_version(db->nm_db));
132 }
133
134 /*
135  * call-seq: DB.needs_upgrade? => true or false
136  *
137  * Return the +true+ if the database needs upgrading, +false+ otherwise
138  */
139 VALUE
140 notmuch_rb_database_needs_upgrade(VALUE self)
141 {
142     notmuch_rb_database_t *db;
143
144     Data_Get_Struct(self, notmuch_rb_database_t, db);
145     if (!db->nm_db)
146         rb_raise(rb_eRuntimeError, "Database closed");
147
148     return notmuch_database_needs_upgrade(db->nm_db) ? Qtrue : Qfalse;
149 }
150
151 static void
152 notmuch_rb_upgrade_notify(void *closure, double progress)
153 {
154     VALUE *block = (VALUE *)closure;
155     rb_funcall(*block, ID_call, 1, rb_float_new(progress));
156 }
157
158 /*
159  * call-seq: DB.upgrade! [{|progress| block }] => nil
160  *
161  * Upgrade the database.
162  *
163  * If a block is given the block is called with a progress indicator as a
164  * floating point value in the range of [0.0..1.0].
165  */
166 VALUE
167 notmuch_rb_database_upgrade(VALUE self)
168 {
169     notmuch_status_t ret;
170     void (*pnotify) (void *closure, double progress);
171     notmuch_rb_database_t *db;
172     VALUE block;
173
174     Data_Get_Struct(self, notmuch_rb_database_t, db);
175     if (!db->nm_db)
176         rb_raise(rb_eRuntimeError, "Database closed");
177
178     if (rb_block_given_p()) {
179         pnotify = notmuch_rb_upgrade_notify;
180         block = rb_block_proc();
181     }
182     else
183         pnotify = NULL;
184
185     ret = notmuch_database_upgrade(db->nm_db, pnotify, pnotify ? &block : NULL);
186     notmuch_rb_status_raise(ret);
187     return Qtrue;
188 }
189
190 /*
191  * call-seq: DB.get_directory(path) => DIR
192  *
193  * Retrieve a directory object from the database for 'path'
194  */
195 VALUE
196 notmuch_rb_database_get_directory(VALUE self, VALUE pathv)
197 {
198     const char *path;
199     notmuch_rb_directory_t *dir;
200     notmuch_rb_database_t *db;
201     VALUE dirv;
202
203     Data_Get_Struct(self, notmuch_rb_database_t, db);
204     if (!db->nm_db)
205         rb_raise(rb_eRuntimeError, "Database closed");
206
207 #if !defined(RSTRING_PTR)
208 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
209 #endif /* !defined(RSTRING_PTR) */
210
211     SafeStringValue(pathv);
212     path = RSTRING_PTR(pathv);
213
214     dirv = Data_Make_Struct(notmuch_rb_cDirectory, notmuch_rb_directory_t,
215             notmuch_rb_directory_mark, notmuch_rb_directory_free, dir);
216     dir->nm_dir = notmuch_database_get_directory(db->nm_db, path);
217     dir->db = self;
218     if (!dir->nm_dir)
219         rb_raise(notmuch_rb_eXapianError, "Xapian exception");
220
221     return dirv;
222 }
223
224 /*
225  * call-seq: DB.add_message(path) => MESSAGE, isdup
226  *
227  * Add a message to the database and return it
228  * +isdup+ is a boolean that specifies whether the added message was a
229  * duplicate.
230  */
231 VALUE
232 notmuch_rb_database_add_message(VALUE self, VALUE pathv)
233 {
234     const char *path;
235     notmuch_status_t ret;
236     notmuch_rb_message_t *message;
237     notmuch_rb_database_t *db;
238     VALUE messagev;
239
240     Data_Get_Struct(self, notmuch_rb_database_t, db);
241     if (!db->nm_db)
242         rb_raise(rb_eRuntimeError, "Database closed");
243
244 #if !defined(RSTRING_PTR)
245 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
246 #endif /* !defined(RSTRING_PTR) */
247
248     SafeStringValue(pathv);
249     path = RSTRING_PTR(pathv);
250
251     messagev = Data_Make_Struct(notmuch_rb_cMessage, notmuch_rb_message_t,
252             notmuch_rb_message_mark, notmuch_rb_message_free, message);
253     ret = notmuch_database_add_message(db->nm_db, path, &message->nm_message);
254     message->parent = self;
255     notmuch_rb_status_raise(ret);
256     return rb_assoc_new(messagev, (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
257 }
258
259 /*
260  * call-seq: DB.remove_message(path) => isdup
261  *
262  * Remove a message from the database.
263  * +isdup+ is a boolean that specifies whether the removed message was a
264  * duplicate.
265  */
266 VALUE
267 notmuch_rb_database_remove_message(VALUE self, VALUE pathv)
268 {
269     const char *path;
270     notmuch_status_t ret;
271     notmuch_rb_database_t *db;
272
273     Data_Get_Struct(self, notmuch_rb_database_t, db);
274     if (!db->nm_db)
275         rb_raise(rb_eRuntimeError, "Database closed");
276
277 #if !defined(RSTRING_PTR)
278 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
279 #endif /* !defined(RSTRING_PTR) */
280
281     SafeStringValue(pathv);
282     path = RSTRING_PTR(pathv);
283
284     ret = notmuch_database_remove_message(db->nm_db, path);
285     notmuch_rb_status_raise(ret);
286     return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
287 }
288
289 /*
290  * call-seq: DB.query(query) => QUERY
291  *
292  * Retrieve a query object for the query string 'query'
293  */
294 VALUE
295 notmuch_rb_database_query_create(VALUE self, VALUE qstrv)
296 {
297     const char *qstr;
298     notmuch_rb_query_t *query;
299     notmuch_rb_database_t *db;
300     VALUE queryv;
301
302     Data_Get_Struct(self, notmuch_rb_database_t, db);
303     if (!db->nm_db)
304         rb_raise(rb_eRuntimeError, "Database closed");
305
306 #if !defined(RSTRING_PTR)
307 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
308 #endif /* !defined(RSTRING_PTR) */
309
310     SafeStringValue(qstrv);
311     qstr = RSTRING_PTR(qstrv);
312
313     queryv = Data_Make_Struct(notmuch_rb_cQuery, notmuch_rb_query_t,
314             notmuch_rb_query_mark, notmuch_rb_query_free, query);
315     query->nm_query = notmuch_query_create(db->nm_db, qstr);
316     query->db = self;
317     if (!query->nm_query)
318         rb_raise(notmuch_rb_eMemoryError, "out of memory");
319
320     return queryv;
321 }