File: gom-adapter.c

package info (click to toggle)
libgom 0.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 784 kB
  • sloc: ansic: 9,785; python: 75; javascript: 50; sh: 33; makefile: 9
file content (521 lines) | stat: -rw-r--r-- 14,319 bytes parent folder | download | duplicates (2)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* gom-adapter.c
 *
 * Copyright (C) 2011 Christian Hergert <chris@dronelabs.com>
 *
 * This file is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sqlite3.h>

#include "gom-adapter.h"
#include "gom-command.h"
#include "gom-error.h"

struct _GomAdapterPrivate
{
   sqlite3 *db;
   GThread *thread;
   GAsyncQueue *queue;
};

G_DEFINE_TYPE_WITH_PRIVATE(GomAdapter, gom_adapter, G_TYPE_OBJECT)

typedef enum {
   ASYNC_CMD_TYPE_OPEN,
   ASYNC_CMD_TYPE_READ,
   ASYNC_CMD_TYPE_WRITE,
   ASYNC_CMD_TYPE_CLOSE
} GomAsyncCmdType;

typedef struct {
  GomAdapter *adapter;
  GomAsyncCmdType type;
  GomAdapterCallback callback;
  gpointer callback_data;
} GomAsyncCmd;

static GomAsyncCmd *
_async_cmd_new(GomAdapter         *adapter,
               GomAsyncCmdType     type,
               GomAdapterCallback  callback,
               gpointer            callback_data)
{
   GomAsyncCmd *cmd;
   cmd = g_new0(GomAsyncCmd, 1);
   cmd->adapter = g_object_ref(adapter);
   cmd->type = type;
   cmd->callback = callback;
   cmd->callback_data = callback_data;
   return cmd;
}

GomAdapter *
gom_adapter_new (void)
{
   return g_object_new(GOM_TYPE_ADAPTER, NULL);
}

/**
 * gom_adapter_get_handle:
 * @adapter: (in): A #GomAdapter.
 *
 * Fetches the sqlite3 structure used by the adapter.
 *
 * Returns: (transfer none): A handle to the #sqlite3 structure.
 * Side effects: None.
 */
gpointer
gom_adapter_get_handle (GomAdapter *adapter)
{
   g_return_val_if_fail(GOM_IS_ADAPTER(adapter), NULL);
   g_return_val_if_fail(adapter->priv->thread != NULL, NULL);
   g_assert (g_thread_self () == adapter->priv->thread);
   return adapter->priv->db;
}

static gpointer
gom_adapter_worker (gpointer data)
{
   GomAsyncCmd *cmd;
   GAsyncQueue *queue = data;

   /*
    * First item is open request.
    */
   cmd = g_async_queue_pop(queue);
   g_assert (cmd->type == ASYNC_CMD_TYPE_OPEN);
   cmd->callback(cmd->adapter, cmd->callback_data);
   g_object_unref(cmd->adapter);
   g_free(cmd);

   /*
    * Handle additional requests.
    */
   while ((cmd = g_async_queue_pop(queue))) {
      /*
       * XXX: Right now, we synchronize all requests. I hope to make this
       *      more performant when necessary.
       */
      cmd->callback(cmd->adapter, cmd->callback_data);
      if (cmd->type == ASYNC_CMD_TYPE_CLOSE) {
         g_object_unref(cmd->adapter);
         g_free(cmd);
         break;
      }

      g_object_unref(cmd->adapter);
      g_free(cmd);
   }

   return NULL;
}

/**
 * gom_adapter_queue_write:
 * @adapter: (in): A #GomAdapter.
 * @callback: (in) (scope async): A callback to execute write queries on SQLite.
 * @user_data: (in): User data for @callback.
 *
 * Queues a callback to be executed within the SQLite thread. The callback can
 * perform reads and writes.
 */
void
gom_adapter_queue_write (GomAdapter         *adapter,
                         GomAdapterCallback  callback,
                         gpointer            user_data)
{
   GomAdapterPrivate *priv;
   GomAsyncCmd *cmd;

   g_return_if_fail(GOM_IS_ADAPTER(adapter));
   g_return_if_fail(callback != NULL);
   g_return_if_fail(adapter->priv->queue != NULL);

   priv = adapter->priv;

   cmd = _async_cmd_new(adapter, ASYNC_CMD_TYPE_WRITE, callback, user_data);

   g_async_queue_push(priv->queue, cmd);
}

/**
 * gom_adapter_queue_read:
 * @adapter: (in): A #GomAdapter.
 * @callback: (in) (scope async): A callback to execute read queries on SQLite.
 * @user_data: (in): User data for @callback.
 *
 * Queues a callback to be executed within the SQLite thread. The callback is
 * expected to perform reads only.
 */
void
gom_adapter_queue_read (GomAdapter         *adapter,
                        GomAdapterCallback  callback,
                        gpointer            user_data)
{
   GomAdapterPrivate *priv;
   GomAsyncCmd *cmd;

   g_return_if_fail(GOM_IS_ADAPTER(adapter));
   g_return_if_fail(callback != NULL);
   g_return_if_fail(adapter->priv->queue != NULL);

   priv = adapter->priv;

   cmd = _async_cmd_new(adapter, ASYNC_CMD_TYPE_READ, callback, user_data);

   g_async_queue_push(priv->queue, cmd);
}

static void
open_callback (GomAdapter *adapter,
               gpointer    user_data)
{
   GSimpleAsyncResult *simple = user_data;
   GAsyncQueue *queue;
   const char *uri;
   gint flags;
   gint ret;

   queue = g_object_get_data(G_OBJECT(simple), "queue");
   uri = g_object_get_data(G_OBJECT(simple), "uri");
   flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI | SQLITE_OPEN_NOMUTEX;
   ret = sqlite3_open_v2(uri, &adapter->priv->db, flags, NULL);
   if (ret != SQLITE_OK) {
      g_simple_async_result_set_error(simple, GOM_ERROR,
                                      GOM_ERROR_ADAPTER_OPEN,
                                      "Failed to open database at %s", uri);
   }
   g_simple_async_result_set_op_res_gboolean(simple, ret == SQLITE_OK);
   if (!queue)
      g_simple_async_result_complete_in_idle(simple);
   else
      g_async_queue_push(queue, GINT_TO_POINTER(TRUE));
}

gboolean
gom_adapter_open_sync (GomAdapter           *adapter,
                       const gchar          *uri,
                       GError              **error)
{
   GomAdapterPrivate *priv;
   GSimpleAsyncResult *simple;
   GomAsyncCmd *cmd;
   GAsyncQueue *queue;
   gboolean ret;

   g_return_val_if_fail(GOM_IS_ADAPTER(adapter), FALSE);
   g_return_val_if_fail(uri != NULL, FALSE);

   priv = adapter->priv;

   if (priv->thread) {
      g_warning("%s may only be called once per adapter.",
                G_STRFUNC);
      return FALSE;
   }

   priv->queue = g_async_queue_new();

#if GLIB_CHECK_VERSION(2, 32, 0)
   priv->thread = g_thread_new("gom-adapter-worker",
                               gom_adapter_worker,
                               priv->queue);
#else
   priv->thread = g_thread_create(gom_adapter_worker, priv->queue,
                                  TRUE, NULL);
#endif

   queue = g_async_queue_new();

   simple = g_simple_async_result_new(G_OBJECT(adapter), NULL, NULL,
                                      gom_adapter_open_sync);
   g_object_set_data_full(G_OBJECT(simple), "uri",
                          g_strdup(uri), g_free);
   g_object_set_data (G_OBJECT(simple), "queue", queue);

   cmd = _async_cmd_new(adapter, ASYNC_CMD_TYPE_OPEN, open_callback, simple);

   g_async_queue_push(priv->queue, cmd);
   g_async_queue_pop(queue);
   g_async_queue_unref(queue);

   if (!(ret = g_simple_async_result_get_op_res_gboolean(simple))) {
      g_simple_async_result_propagate_error(simple, error);
   }
   g_object_unref(simple);

   return ret;
}

/**
 * gom_adapter_open_async:
 * @adapter: a #GomAdapter
 * @uri: a URI understood by SQLite
 * @callback: the function to call when the operation finished, or %NULL
 * @user_data: the user data to pass to the callback function
 *
 * Opens the database pointed to by @uri. @uri can be in any format understood
 * by SQLite. See <ulink type="http" url="http://www.sqlite.org/c3ref/open.html">http://www.sqlite.org/c3ref/open.html</ulink>
 * for details.
 */
void
gom_adapter_open_async (GomAdapter          *adapter,
                        const gchar         *uri,
                        GAsyncReadyCallback  callback,
                        gpointer             user_data)
{
   GomAdapterPrivate *priv;
   GSimpleAsyncResult *simple;
   GomAsyncCmd *cmd;

   g_return_if_fail(GOM_IS_ADAPTER(adapter));
   g_return_if_fail(uri != NULL);
   g_return_if_fail(callback != NULL);

   priv = adapter->priv;

   if (priv->thread) {
      g_warning("%s may only be called once per adapter.",
                G_STRFUNC);
      return;
   }

   priv->queue = g_async_queue_new();

#if GLIB_CHECK_VERSION(2, 32, 0)
   priv->thread = g_thread_new("gom-adapter-worker",
                               gom_adapter_worker,
                               priv->queue);
#else
   priv->thread = g_thread_create(gom_adapter_worker, priv->queue,
                                  TRUE, NULL);
#endif

   simple = g_simple_async_result_new(G_OBJECT(adapter), callback, user_data,
                                      gom_adapter_open_async);
   g_object_set_data_full(G_OBJECT(simple), "uri",
                          g_strdup(uri), g_free);

   cmd = _async_cmd_new(adapter, ASYNC_CMD_TYPE_OPEN, open_callback, simple);

   g_async_queue_push(priv->queue, cmd);
}

gboolean
gom_adapter_open_finish (GomAdapter    *adapter,
                         GAsyncResult  *result,
                         GError       **error)
{
   GSimpleAsyncResult *simple = (GSimpleAsyncResult *)result;
   gboolean ret;

   g_return_val_if_fail(GOM_IS_ADAPTER(adapter), FALSE);
   g_return_val_if_fail(G_IS_SIMPLE_ASYNC_RESULT(simple), FALSE);

   if (!(ret = g_simple_async_result_get_op_res_gboolean(simple))) {
      g_simple_async_result_propagate_error(simple, error);
   }
   g_object_unref(simple);

   return ret;
}

static void
close_callback (GomAdapter *adapter,
                gpointer    user_data)
{
   GSimpleAsyncResult *simple = user_data;
   GAsyncQueue *queue;

   queue = g_object_get_data(user_data, "queue");

   sqlite3_close(adapter->priv->db);
   adapter->priv->db = NULL;

   g_simple_async_result_set_op_res_gboolean(simple, TRUE);
   if (!queue)
      g_simple_async_result_complete_in_idle(simple);
   else
      g_async_queue_push(queue, GINT_TO_POINTER(TRUE));
}

gboolean
gom_adapter_close_sync (GomAdapter    *adapter,
                        GError       **error)
{
   GomAsyncCmd *cmd;
   GomAdapterPrivate *priv;
   GSimpleAsyncResult *simple;
   GAsyncQueue *queue;
   gboolean ret;

   g_return_val_if_fail(GOM_IS_ADAPTER(adapter), FALSE);

   priv = adapter->priv;

   if (!priv->db) {
      return TRUE;
   }

   queue = g_async_queue_new();

   simple = g_simple_async_result_new(G_OBJECT(adapter), NULL, NULL,
                                      gom_adapter_close_sync);
   g_object_set_data(G_OBJECT(simple), "queue", queue);

   cmd = _async_cmd_new(adapter, ASYNC_CMD_TYPE_CLOSE, close_callback, simple);

   g_async_queue_push(priv->queue, cmd);
   g_async_queue_pop(queue);
   g_async_queue_unref(queue);

   if (!(ret = g_simple_async_result_get_op_res_gboolean(simple))) {
      g_simple_async_result_propagate_error(simple, error);
   }
   g_object_unref(simple);

   return ret;
}

void
gom_adapter_close_async (GomAdapter          *adapter,
                         GAsyncReadyCallback  callback,
                         gpointer             user_data)
{
   GomAdapterPrivate *priv;
   GSimpleAsyncResult *simple;
   GomAsyncCmd *cmd;

   g_return_if_fail(GOM_IS_ADAPTER(adapter));
   g_return_if_fail(callback != NULL);

   priv = adapter->priv;

   simple = g_simple_async_result_new(G_OBJECT(adapter), callback, user_data,
                                      gom_adapter_close_async);

   if (!priv->db) {
      g_simple_async_result_set_op_res_gboolean(simple, TRUE);
      g_simple_async_result_complete_in_idle(simple);
      g_object_unref(simple);
      return;
   }

   cmd = _async_cmd_new(adapter, ASYNC_CMD_TYPE_CLOSE, close_callback, simple);

   g_async_queue_push(priv->queue, cmd);
}

gboolean
gom_adapter_close_finish (GomAdapter    *adapter,
                          GAsyncResult  *result,
                          GError       **error)
{
   GSimpleAsyncResult *simple = (GSimpleAsyncResult *)result;
   gboolean ret;

   g_return_val_if_fail(GOM_IS_ADAPTER(adapter), FALSE);
   g_return_val_if_fail(G_IS_SIMPLE_ASYNC_RESULT(simple), FALSE);

   if (!(ret = g_simple_async_result_get_op_res_gboolean(simple))) {
      g_simple_async_result_propagate_error(simple, error);
   }
   g_object_unref(simple);

   return ret;
}

/**
 * gom_adapter_execute_sql:
 * @adapter: A #GomAdapter.
 * @sql: SQL to execute.
 * @error: a #GError
 *
 * This is a helper function to make simple execution of SQL easier.
 * It is primarily meant for things like "BEGIN;" and "COMMIT;".
 *
 * This MUST be called from within a write transaction using
 * gom_adapter_queue_write().
 *
 * Returns: %TRUE if successful;
 */
gboolean
gom_adapter_execute_sql (GomAdapter   *adapter,
                         const gchar  *sql,
                         GError      **error)
{
   GomCommand *command;
   gboolean ret;

   g_return_val_if_fail(GOM_IS_ADAPTER(adapter), FALSE);
   g_return_val_if_fail(sql, FALSE);
   g_assert (g_thread_self () == adapter->priv->thread);

   command = g_object_new(GOM_TYPE_COMMAND,
                          "adapter", adapter,
                          "sql", sql,
                          NULL);
   ret = gom_command_execute(command, NULL, error);
   g_object_unref(command);

   return ret;
}

/**
 * gom_adapter_finalize:
 * @object: (in): A #GomAdapter.
 *
 * Finalizer for a #GomAdapter instance.  Frees any resources held by
 * the instance.
 */
static void
gom_adapter_finalize (GObject *object)
{
   GomAdapterPrivate *priv = GOM_ADAPTER(object)->priv;

   if (priv->db)
      g_warning("Adapter not closed, leaking!");

   g_clear_pointer(&priv->queue, g_async_queue_unref);
   g_clear_pointer(&priv->thread, g_thread_unref);

   G_OBJECT_CLASS(gom_adapter_parent_class)->finalize(object);
}

/**
 * gom_adapter_class_init:
 * @klass: (in): A #GomAdapterClass.
 *
 * Initializes the #GomAdapterClass and prepares the vtable.
 */
static void
gom_adapter_class_init (GomAdapterClass *klass)
{
   GObjectClass *object_class;

   object_class = G_OBJECT_CLASS(klass);
   object_class->finalize = gom_adapter_finalize;
}

/**
 * gom_adapter_init:
 * @adapter: (in): A #GomAdapter.
 *
 * Initializes the newly created #GomAdapter instance.
 */
static void
gom_adapter_init (GomAdapter *adapter)
{
   adapter->priv = gom_adapter_get_instance_private(adapter);
}