summaryrefslogtreecommitdiff
path: root/src/libtracker-data/tracker-vtab-triples.c
blob: a81709cd0e41b59334bac44845e61dc1e6e968ed (plain)
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
 * Copyright (C) 2019, Red Hat Inc.
 *
 * This library 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 library 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 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.
 *
 * Author: Carlos Garnacho <carlosg@gnome.org>
 */
#include "config.h"

#include "tracker-ontologies.h"
#include "tracker-vtab-triples.h"

/* Define some constraints for older SQLite, we will never get
 * those in older versions, and simplifies checks in code.
 */
#if SQLITE_VERSION_NUMBER<=3021000
#define SQLITE_INDEX_CONSTRAINT_NE        68
#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
#endif

enum {
	COL_ROWID,
	COL_GRAPH,
	COL_SUBJECT,
	COL_PREDICATE,
	COL_OBJECT,
	COL_OBJECT_TYPE,
	N_COLS
};

enum {
	IDX_COL_GRAPH           = 1 << 0,
	IDX_COL_SUBJECT         = 1 << 1,
	IDX_COL_PREDICATE       = 1 << 2,
	IDX_MATCH_GRAPH_NEG     = 1 << 3,
	IDX_MATCH_SUBJECT_NEG   = 1 << 4,
	IDX_MATCH_PREDICATE_NEG = 1 << 5,
};

typedef struct {
	sqlite3 *db;
	TrackerOntologies *ontologies;
} TrackerTriplesModule;

typedef struct {
	struct sqlite3_vtab parent;
	TrackerTriplesModule *module;
	GList *cursors;
} TrackerTriplesVTab;

typedef struct {
	struct sqlite3_vtab_cursor parent;
	TrackerTriplesVTab *vtab;
	struct sqlite3_stmt *stmt;

	struct {
		sqlite3_value *graph;
		sqlite3_value *subject;
		sqlite3_value *predicate;
		sqlite3_value *object;
		guint idxFlags;
	} match;

	GHashTable *query_graphs;
	GList *properties;
	GList *graphs;

	guint64 rowid;
	guint finished : 1;
} TrackerTriplesCursor;

static void
tracker_triples_module_free (gpointer data)
{
	TrackerTriplesModule *module = data;

	g_clear_object (&module->ontologies);
	g_free (module);
}

static void
tracker_triples_vtab_free (gpointer data)
{
	TrackerTriplesVTab *vtab = data;

	g_list_free (vtab->cursors);
	g_free (vtab);
}

static void
tracker_triples_cursor_reset (TrackerTriplesCursor *cursor)
{
	g_clear_pointer (&cursor->stmt, sqlite3_finalize);
	g_clear_pointer (&cursor->match.graph, sqlite3_value_free);
	g_clear_pointer (&cursor->match.subject, sqlite3_value_free);
	g_clear_pointer (&cursor->match.predicate, sqlite3_value_free);
	g_clear_pointer (&cursor->properties, g_list_free);
	g_clear_pointer (&cursor->graphs, g_list_free);
	g_clear_pointer (&cursor->query_graphs, g_hash_table_unref);
	cursor->match.idxFlags = 0;
	cursor->rowid = 0;
	cursor->finished = FALSE;
}

static void
tracker_triples_cursor_free (gpointer data)
{
	TrackerTriplesCursor *cursor = data;

	tracker_triples_cursor_reset (cursor);
	g_free (cursor);
}

static int
triples_connect (sqlite3            *db,
                 gpointer            data,
                 int                 argc,
                 const char *const  *argv,
                 sqlite3_vtab      **vtab_out,
                 char              **err_out)
{
	TrackerTriplesModule *module = data;
	TrackerTriplesVTab *vtab;
	int rc;

	vtab = g_new0 (TrackerTriplesVTab, 1);
	vtab->module = module;

	rc = sqlite3_declare_vtab (module->db,
	                           "CREATE TABLE x("
	                           "    ID INTEGER,"
	                           "    graph INTEGER,"
	                           "    subject INTEGER, "
	                           "    predicate INTEGER, "
	                           "    object INTEGER, "
	                           "    object_type INTEGER"
	                           ")");

	if (rc == SQLITE_OK) {
		*vtab_out = &vtab->parent;
	} else {
		g_free (vtab);
	}

	return rc;
}

static int
triples_best_index (sqlite3_vtab       *vtab,
                    sqlite3_index_info *info)
{
	gboolean order_by_consumed = FALSE;
	int i, argv_idx = 1, idx = 0;
	char *idx_str;

	idx_str = sqlite3_malloc (sizeof (char) * N_COLS);
	bzero (idx_str, sizeof (char) * N_COLS);

	for (i = 0; i < info->nConstraint; i++) {
		struct {
			int mask;
			int negated_mask;
		} masks [] = {
			{ IDX_COL_GRAPH, IDX_MATCH_GRAPH_NEG },
			{ IDX_COL_SUBJECT, IDX_MATCH_SUBJECT_NEG },
			{ IDX_COL_PREDICATE, IDX_MATCH_PREDICATE_NEG },
			{ 0, 0 },
		};

		if (!info->aConstraint[i].usable)
			continue;

		/* We let object be matched in upper layers, where proper
		 * translation to strings can be done.
		 */
		if (info->aConstraint[i].iColumn == COL_OBJECT ||
		    info->aConstraint[i].iColumn == COL_OBJECT_TYPE)
			continue;

		if (info->aConstraint[i].iColumn == COL_ROWID) {
			g_free (idx_str);
			return SQLITE_ERROR;
		}

		/* We can only check for (in)equality */
		if (info->aConstraint[i].op != SQLITE_INDEX_CONSTRAINT_EQ &&
		    info->aConstraint[i].op != SQLITE_INDEX_CONSTRAINT_NE &&
		    info->aConstraint[i].op != SQLITE_INDEX_CONSTRAINT_ISNULL &&
		    info->aConstraint[i].op != SQLITE_INDEX_CONSTRAINT_ISNOTNULL) {
			g_free (idx_str);
			return SQLITE_ERROR;
		}

		/* idxNum encodes the used columns and their operators */
		idx |= masks[info->aConstraint[i].iColumn - 1].mask;

		if (info->aConstraint[i].op == SQLITE_INDEX_CONSTRAINT_NE ||
		    info->aConstraint[i].op == SQLITE_INDEX_CONSTRAINT_ISNOTNULL)
			idx |= masks[info->aConstraint[i].iColumn - 1].negated_mask;

		/* idxStr stores the mapping between columns and filter arguments */
		idx_str[info->aConstraint[i].iColumn] = argv_idx - 1;
		info->aConstraintUsage[i].argvIndex = argv_idx;
		info->aConstraintUsage[i].omit = FALSE;
		argv_idx++;
	}

	info->idxNum = idx;
	info->orderByConsumed = order_by_consumed;
	info->idxStr = idx_str;
	info->needToFreeIdxStr = TRUE;

	return SQLITE_OK;
}

static int
triples_disconnect (sqlite3_vtab *vtab)
{
	return SQLITE_OK;
}

static int
triples_destroy (sqlite3_vtab *vtab)
{
	tracker_triples_vtab_free (vtab);
	return SQLITE_OK;
}

static int
triples_open (sqlite3_vtab         *vtab_sqlite,
	      sqlite3_vtab_cursor **cursor_ret)
{
	TrackerTriplesVTab *vtab = (TrackerTriplesVTab *) vtab_sqlite;
	TrackerTriplesCursor *cursor;

	cursor = g_new0 (TrackerTriplesCursor, 1);
	cursor->vtab = vtab;
	vtab->cursors = g_list_prepend (vtab->cursors, cursor);

	*cursor_ret = &cursor->parent;
	return SQLITE_OK;
}

static int
triples_close (sqlite3_vtab_cursor *vtab_cursor)
{
	TrackerTriplesCursor *cursor = (TrackerTriplesCursor *) vtab_cursor;
	TrackerTriplesVTab *vtab = cursor->vtab;

	vtab->cursors = g_list_remove (vtab->cursors, cursor);
	tracker_triples_cursor_free (cursor);
	return SQLITE_OK;
}

static void
collect_properties (TrackerTriplesCursor *cursor)
{
	TrackerProperty **properties;
	guint n_properties, i;

	properties = tracker_ontologies_get_properties (cursor->vtab->module->ontologies,
							&n_properties);
	for (i = 0; i < n_properties; i++) {
		if (cursor->match.predicate) {
			gboolean negated = !!(cursor->match.idxFlags & IDX_MATCH_PREDICATE_NEG);
			gboolean equals =
				(sqlite3_value_int64 (cursor->match.predicate) ==
				 tracker_property_get_id (properties[i]));

			if (equals == negated)
				continue;
		}

		cursor->properties = g_list_prepend (cursor->properties,
		                                     properties[i]);
	}
}

static int
collect_graphs (TrackerTriplesCursor *cursor)
{
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2 (cursor->vtab->module->db,
	                         "SELECT 0, \"main\" "
	                         "UNION ALL "
	                         "SELECT ID, "
	                         "       (SELECT Uri from Resource where Resource.ID = Graph.ID) "
	                         "FROM Graph",
	                         -1, &stmt, 0);
	if (rc != SQLITE_OK)
		return rc;

	cursor->query_graphs = g_hash_table_new_full (NULL, NULL, NULL, g_free);

	while ((rc = sqlite3_step (stmt)) == SQLITE_ROW) {
		const gchar *uri;
		gint id;

		id = sqlite3_column_int (stmt, 0);
		uri = sqlite3_column_text (stmt, 1);

		if (cursor->match.graph) {
			gboolean negated = !!(cursor->match.idxFlags & IDX_MATCH_GRAPH_NEG);
			gboolean equals = (sqlite3_value_int64 (cursor->match.graph) == id);

			if (equals == negated)
				continue;
		}

		g_hash_table_insert (cursor->query_graphs,
		                     GINT_TO_POINTER (id),
		                     g_strdup (uri));
	}

	if (rc == SQLITE_DONE)
		cursor->graphs = g_hash_table_get_keys (cursor->query_graphs);

	sqlite3_finalize (stmt);

	return rc;
}

static gchar *
convert_to_string (const gchar         *table_name,
		   TrackerPropertyType  type)
{
	switch (type) {
	case TRACKER_PROPERTY_TYPE_STRING:
	case TRACKER_PROPERTY_TYPE_LANGSTRING:
	case TRACKER_PROPERTY_TYPE_INTEGER:
		return g_strdup_printf ("t.\"%s\"", table_name);
	case TRACKER_PROPERTY_TYPE_RESOURCE:
		return g_strdup_printf ("(SELECT Uri FROM Resource WHERE ID = t.\"%s\")",
		                        table_name);
	case TRACKER_PROPERTY_TYPE_BOOLEAN:
		return g_strdup_printf ("CASE t.\"%s\" "
		                        "WHEN 1 THEN 'true' "
		                        "WHEN 0 THEN 'false' "
		                        "ELSE NULL END",
		                        table_name);
	case TRACKER_PROPERTY_TYPE_DATE:
		return g_strdup_printf ("strftime (\"%%Y-%%m-%%d\", t.\"%s\", \"unixepoch\")",
		                        table_name);
	case TRACKER_PROPERTY_TYPE_DATETIME:
		return g_strdup_printf ("SparqlFormatTime (t.\"%s\")",
		                        table_name);
	default:
		/* Let sqlite convert the expression to string */
		return g_strdup_printf ("CAST (t.\"%s\" AS TEXT)",
		                        table_name);
	}
}

static void
add_arg_check (GString       *str,
	       sqlite3_value *value,
	       gboolean       negated,
	       const gchar   *var_name)
{
	if (sqlite3_value_type (value) == SQLITE_NULL) {
		if (negated)
			g_string_append (str, "IS NOT NULL ");
		else
			g_string_append (str, "IS NULL ");
	} else {
		if (negated)
			g_string_append_printf (str, "!= %s ", var_name);
		else
			g_string_append_printf (str, "= %s ", var_name);
	}
}

static void
bind_arg (sqlite3_stmt  *stmt,
	  sqlite3_value *value,
	  const gchar   *var_name)
{
	gint idx;

	if (sqlite3_value_type (value) == SQLITE_NULL)
		return;

	idx = sqlite3_bind_parameter_index (stmt, var_name);
	if (idx == 0)
		return;

	sqlite3_bind_value (stmt, idx, value);
}

static gboolean
iterate_next_stmt (TrackerTriplesCursor  *cursor,
                   const gchar          **graph,
                   gint                  *graph_id,
                   TrackerProperty      **property)
{
	while (cursor->properties && !cursor->graphs) {
		/* Iterate to next property, and redo graph list */
		cursor->properties = g_list_remove (cursor->properties,
		                                    cursor->properties->data);
		cursor->graphs = g_hash_table_get_keys (cursor->query_graphs);
	}

	if (!cursor->properties)
		return FALSE;

	*property = cursor->properties->data;
	*graph_id = GPOINTER_TO_INT (cursor->graphs->data);
	*graph = g_hash_table_lookup (cursor->query_graphs,
	                              cursor->graphs->data);

	cursor->graphs = g_list_remove (cursor->graphs, cursor->graphs->data);

	return TRUE;
}

static int
init_stmt (TrackerTriplesCursor *cursor)
{
	TrackerProperty *property;
	const gchar *graph;
	gint graph_id;
	GString *sql;
	int rc;

	while (iterate_next_stmt (cursor, &graph, &graph_id, &property)) {
		gchar *string_expr;

		string_expr = convert_to_string (tracker_property_get_name (property),
						 tracker_property_get_data_type (property));

		sql = g_string_new (NULL);
		g_string_append_printf (sql,
		                        "SELECT %d, t.ID, "
		                        "       (SELECT ID From Resource WHERE Uri = \"%s\"), "
		                        "       %s, "
		                        "       %d "
		                        "FROM \"%s\".\"%s\" AS t "
		                        "WHERE 1 ",
		                        graph_id,
		                        tracker_property_get_uri (property),
		                        string_expr,
		                        tracker_property_get_data_type (property),
		                        graph,
		                        tracker_property_get_table_name (property));

		if (cursor->match.subject) {
			g_string_append (sql, "AND t.ID ");
			add_arg_check (sql, cursor->match.subject,
			               !!(cursor->match.idxFlags & IDX_MATCH_SUBJECT_NEG),
			               "@s");
		}

		rc = sqlite3_prepare_v2 (cursor->vtab->module->db,
					 sql->str, -1, &cursor->stmt, 0);
		g_string_free (sql, TRUE);
		g_free (string_expr);

		if (rc == SQLITE_OK) {
			if (cursor->match.graph)
				bind_arg (cursor->stmt, cursor->match.graph, "@g");
			if (cursor->match.subject)
				bind_arg (cursor->stmt, cursor->match.subject, "@s");

			rc = sqlite3_step (cursor->stmt);
		}

		if (rc != SQLITE_DONE)
			return rc;

		g_clear_pointer (&cursor->stmt, sqlite3_finalize);
	}

	return SQLITE_DONE;
}

static int
triples_filter (sqlite3_vtab_cursor  *vtab_cursor,
                int                   idx,
                const char           *idx_str,
                int                   argc,
                sqlite3_value       **argv)
{
	TrackerTriplesCursor *cursor = (TrackerTriplesCursor *) vtab_cursor;
	int rc;

	tracker_triples_cursor_reset (cursor);

	if (idx & IDX_COL_GRAPH) {
		int idx = idx_str[COL_GRAPH];
		cursor->match.graph = sqlite3_value_dup (argv[idx]);
	}

	if (idx & IDX_COL_SUBJECT) {
		int idx = idx_str[COL_SUBJECT];
		cursor->match.subject = sqlite3_value_dup (argv[idx]);
	}

	if (idx & IDX_COL_PREDICATE) {
		int idx = idx_str[COL_PREDICATE];
		cursor->match.predicate = sqlite3_value_dup (argv[idx]);
	}

	cursor->match.idxFlags = idx;

	if ((rc = collect_graphs (cursor)) != SQLITE_DONE)
		return rc;

	collect_properties (cursor);
	rc = init_stmt (cursor);

	if (rc == SQLITE_DONE)
		cursor->finished = TRUE;

	if (rc == SQLITE_ROW || rc == SQLITE_DONE)
		return SQLITE_OK;

	return rc;
}

static int
triples_next (sqlite3_vtab_cursor *vtab_cursor)
{
	TrackerTriplesCursor *cursor = (TrackerTriplesCursor *) vtab_cursor;
	int rc;

	rc = sqlite3_step (cursor->stmt);

	if (rc == SQLITE_DONE) {
		g_clear_pointer (&cursor->stmt, sqlite3_finalize);
		rc = init_stmt (cursor);
	}

	if (rc == SQLITE_ROW) {
		cursor->rowid++;
	} else {
		cursor->finished = TRUE;
	}

	if (rc != SQLITE_ROW && rc != SQLITE_DONE)
		return rc;

	return SQLITE_OK;
}

static int
triples_eof (sqlite3_vtab_cursor *vtab_cursor)
{
	TrackerTriplesCursor *cursor = (TrackerTriplesCursor *) vtab_cursor;

	return cursor->finished;
}

static int
triples_column (sqlite3_vtab_cursor *vtab_cursor,
                sqlite3_context     *context,
                int                  n_col)
{
	TrackerTriplesCursor *cursor = (TrackerTriplesCursor *) vtab_cursor;
	sqlite3_value *value;

	if (n_col == COL_ROWID) {
		sqlite3_result_int64 (context, cursor->rowid);
	} else {
		value = sqlite3_column_value (cursor->stmt, n_col - 1);
		sqlite3_result_value (context, value);
	}

	return SQLITE_OK;
}

static int
triples_rowid (sqlite3_vtab_cursor *vtab_cursor,
               sqlite_int64        *rowid_out)
{
	TrackerTriplesCursor *cursor = (TrackerTriplesCursor *) vtab_cursor;

	*rowid_out = cursor->rowid;
	return SQLITE_OK;
}

void
tracker_vtab_triples_init (sqlite3            *db,
                           TrackerDataManager *data_manager)
{
	TrackerTriplesModule *module;
	TrackerOntologies *ontologies;
	static const sqlite3_module triples_module = {
		2, /* version */
		NULL, /* create(), null because this is an eponymous-only table */
		triples_connect,
		triples_best_index,
		triples_disconnect,
		triples_destroy,
		triples_open,
		triples_close,
		triples_filter,
		triples_next,
		triples_eof,
		triples_column,
		triples_rowid,
		NULL, /* update */
		NULL, /* begin */
		NULL, /* sync */
		NULL, /* commit */
		NULL, /* rollback */
		NULL, /* find function */
		NULL, /* rename */
		NULL, /* savepoint */
		NULL, /* release */
		NULL, /* rollback to */
	};

	module = g_new0 (TrackerTriplesModule, 1);
	module->db = db;
	ontologies = tracker_data_manager_get_ontologies (data_manager);
	g_set_object (&module->ontologies, ontologies);
	sqlite3_create_module_v2 (db, "tracker_triples", &triples_module,
	                          module, tracker_triples_module_free);
}