summaryrefslogtreecommitdiff
path: root/src/libtracker-sparql/tracker-serializer-turtle.c
blob: 2202c9c1b710b04bb74e4f6ac381a6ce17a1f951 (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
/*
 * Copyright (C) 2021, 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>
 */

/* Serialization of cursors to the turtle format defined at:
 *  https://www.w3.org/TR/turtle/
 */

#include "config.h"

#include "tracker-serializer-turtle.h"

typedef struct _TrackerTriple TrackerTriple;

struct _TrackerTriple
{
	gchar *subject;
	gchar *predicate;
	gchar *object;
	TrackerSparqlValueType subject_type;
	TrackerSparqlValueType object_type;
};

struct _TrackerSerializerTurtle
{
	TrackerSerializer parent_instance;
	TrackerTriple last_triple;
	GString *data;
	guint stream_closed : 1;
	guint cursor_started : 1;
	guint cursor_finished : 1;
	guint head_printed : 1;
	guint has_triples : 1;
};

G_DEFINE_TYPE (TrackerSerializerTurtle, tracker_serializer_turtle,
               TRACKER_TYPE_SERIALIZER)

typedef enum
{
	TRACKER_TRIPLE_BREAK_NONE,
	TRACKER_TRIPLE_BREAK_SUBJECT,
	TRACKER_TRIPLE_BREAK_PREDICATE,
	TRACKER_TRIPLE_BREAK_OBJECT,
} TrackerTripleBreak;

static void
tracker_triple_init_from_cursor (TrackerTriple       *triple,
                                 TrackerSparqlCursor *cursor)
{
	triple->subject_type = tracker_sparql_cursor_get_value_type (cursor, 0);
	triple->object_type = tracker_sparql_cursor_get_value_type (cursor, 2);
	triple->subject = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
	triple->predicate = g_strdup (tracker_sparql_cursor_get_string (cursor, 1, NULL));
	triple->object = g_strdup (tracker_sparql_cursor_get_string (cursor, 2, NULL));

	if (triple->subject_type == TRACKER_SPARQL_VALUE_TYPE_STRING) {
		if (g_str_has_prefix (triple->subject, "urn:bnode:")) {
			triple->subject_type = TRACKER_SPARQL_VALUE_TYPE_BLANK_NODE;
		} else {
			triple->subject_type = TRACKER_SPARQL_VALUE_TYPE_URI;
		}
	}

	if (triple->object_type == TRACKER_SPARQL_VALUE_TYPE_STRING) {
		if (g_str_has_prefix (triple->object, "urn:bnode:")) {
			triple->object_type = TRACKER_SPARQL_VALUE_TYPE_BLANK_NODE;
		}
	}
}

static void
tracker_triple_clear (TrackerTriple *triple)
{
	g_clear_pointer (&triple->subject, g_free);
	g_clear_pointer (&triple->predicate, g_free);
	g_clear_pointer (&triple->object, g_free);
}

static TrackerTripleBreak
tracker_triple_get_break (TrackerTriple *last,
                          TrackerTriple *cur)
{
	if (!last->subject)
		return TRACKER_TRIPLE_BREAK_NONE;

	if (g_strcmp0 (last->subject, cur->subject) != 0)
		return TRACKER_TRIPLE_BREAK_SUBJECT;

	if (g_strcmp0 (last->predicate, cur->predicate) != 0)
		return TRACKER_TRIPLE_BREAK_PREDICATE;

	return TRACKER_TRIPLE_BREAK_OBJECT;
}

static void
tracker_serializer_turtle_finalize (GObject *object)
{
	g_input_stream_close (G_INPUT_STREAM (object), NULL, NULL);

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

static void
print_value (GString                 *str,
             const gchar             *value,
             TrackerSparqlValueType   value_type,
             TrackerNamespaceManager *namespaces)
{
	switch (value_type) {
	case TRACKER_SPARQL_VALUE_TYPE_URI: {
		gchar *shortname;

		shortname = tracker_namespace_manager_compress_uri (namespaces, value);

		if (shortname) {
			g_string_append (str, shortname);
		} else {
			g_string_append_c (str, '<');
			g_string_append (str, value);
			g_string_append_c (str, '>');
		}

		g_free (shortname);
		break;
	}
	case TRACKER_SPARQL_VALUE_TYPE_BLANK_NODE: {
		gchar *bnode_label;

		bnode_label = g_strdelimit (g_strdup (value), ":", '_');
		g_string_append (str, "_:");
		g_string_append (str, bnode_label);
		g_free (bnode_label);
		break;
	}
	case TRACKER_SPARQL_VALUE_TYPE_STRING:
	case TRACKER_SPARQL_VALUE_TYPE_DATETIME: {
		gchar *escaped;

		escaped = tracker_sparql_escape_string (value);
		g_string_append_c (str, '"');
		g_string_append (str, escaped);
		g_string_append_c (str, '"');
		g_free (escaped);
		break;
	}
	case TRACKER_SPARQL_VALUE_TYPE_INTEGER:
	case TRACKER_SPARQL_VALUE_TYPE_DOUBLE:
		g_string_append (str, value);
		break;
	case TRACKER_SPARQL_VALUE_TYPE_BOOLEAN:
		g_string_append (str,
		                 (value[0] == 't' || value[0] == 'T') ?
		                 "true" : "false");
		break;
	default:
		g_assert_not_reached ();
	}
}

static gboolean
serialize_up_to_size (TrackerSerializerTurtle *serializer_ttl,
                      gsize                    size,
                      GCancellable            *cancellable,
                      GError                 **error)
{
	TrackerSparqlCursor *cursor;
	TrackerNamespaceManager *namespaces;
	GError *inner_error = NULL;
	TrackerTriple cur;

	if (!serializer_ttl->data)
		serializer_ttl->data = g_string_new (NULL);

	cursor = tracker_serializer_get_cursor (TRACKER_SERIALIZER (serializer_ttl));
	namespaces = tracker_serializer_get_namespaces (TRACKER_SERIALIZER (serializer_ttl));

	if (!serializer_ttl->head_printed) {
		gchar *str;

		str = tracker_namespace_manager_print_turtle (namespaces);

		g_string_append (serializer_ttl->data, str);
		g_string_append_c (serializer_ttl->data, '\n');
		g_free (str);
		serializer_ttl->head_printed = TRUE;
	}

	while (!serializer_ttl->cursor_finished &&
	       serializer_ttl->data->len < size) {
		TrackerTripleBreak br;

		if (!tracker_sparql_cursor_next (cursor, cancellable, &inner_error)) {
			if (inner_error) {
				g_propagate_error (error, inner_error);
				return FALSE;
			} else {
				serializer_ttl->cursor_finished = TRUE;
				break;
			}
		} else {
			serializer_ttl->cursor_started = TRUE;
		}

		tracker_triple_init_from_cursor (&cur, cursor);

		if (!cur.subject || !cur.predicate || !cur.object) {
			g_set_error (error,
			             TRACKER_SPARQL_ERROR,
			             TRACKER_SPARQL_ERROR_INTERNAL,
			             "Cursor has no subject/predicate/object columns");
			return FALSE;
		}

		br = tracker_triple_get_break (&serializer_ttl->last_triple, &cur);

		if (br <= TRACKER_TRIPLE_BREAK_SUBJECT) {
			if (br == TRACKER_TRIPLE_BREAK_SUBJECT)
				g_string_append (serializer_ttl->data, " .\n\n");
			print_value (serializer_ttl->data, cur.subject, cur.subject_type, namespaces);
		}

		if (br <= TRACKER_TRIPLE_BREAK_PREDICATE) {
			if (br == TRACKER_TRIPLE_BREAK_PREDICATE)
				g_string_append (serializer_ttl->data, " ;\n  ");
			else
				g_string_append_c (serializer_ttl->data, ' ');

			print_value (serializer_ttl->data, cur.predicate,
			             TRACKER_SPARQL_VALUE_TYPE_URI, namespaces);
		}

		if (br <= TRACKER_TRIPLE_BREAK_OBJECT) {
			if (br == TRACKER_TRIPLE_BREAK_OBJECT)
				g_string_append (serializer_ttl->data, ",");

			g_string_append_c (serializer_ttl->data, ' ');
			print_value (serializer_ttl->data, cur.object, cur.object_type, namespaces);
		}

		serializer_ttl->has_triples = TRUE;
		tracker_triple_clear (&serializer_ttl->last_triple);
		memcpy (&serializer_ttl->last_triple, &cur, sizeof (TrackerTriple));
	}

	/* Print dot for the last triple */
	if (serializer_ttl->cursor_finished &&
	    serializer_ttl->has_triples)
		g_string_append (serializer_ttl->data, " .\n");

	return TRUE;
}

static gssize
tracker_serializer_turtle_read (GInputStream  *istream,
                                gpointer       buffer,
                                gsize          count,
                                GCancellable  *cancellable,
                                GError       **error)
{
	TrackerSerializerTurtle *serializer_ttl = TRACKER_SERIALIZER_TURTLE (istream);
	gsize bytes_copied;

	if (serializer_ttl->stream_closed ||
	    (serializer_ttl->cursor_finished &&
	     serializer_ttl->data->len == 0))
		return 0;

	if (!serialize_up_to_size (serializer_ttl,
				   count,
				   cancellable,
				   error))
		return -1;

	bytes_copied = MIN (count, serializer_ttl->data->len);

	memcpy (buffer,
	        serializer_ttl->data->str,
	        bytes_copied);
	g_string_erase (serializer_ttl->data, 0, bytes_copied);

	return bytes_copied;
}

static gboolean
tracker_serializer_turtle_close (GInputStream  *istream,
                                 GCancellable  *cancellable,
                                 GError       **error)
{
	TrackerSerializerTurtle *serializer_ttl = TRACKER_SERIALIZER_TURTLE (istream);

	tracker_triple_clear (&serializer_ttl->last_triple);

	if (serializer_ttl->data) {
		g_string_free (serializer_ttl->data, TRUE);
		serializer_ttl->data = NULL;
	}

	return TRUE;
}

static void
tracker_serializer_turtle_class_init (TrackerSerializerTurtleClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GInputStreamClass *istream_class = G_INPUT_STREAM_CLASS (klass);

	object_class->finalize = tracker_serializer_turtle_finalize;

	istream_class->read_fn = tracker_serializer_turtle_read;
	istream_class->close_fn = tracker_serializer_turtle_close;
}

static void
tracker_serializer_turtle_init (TrackerSerializerTurtle *serializer)
{
}