2
* Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* General Public License for more details.
14
* You should have received a copy of the GNU General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
22
#include <glib/gstdio.h>
24
#include <libtracker-common/tracker-crc32.h>
26
#include <libtracker-data/tracker-db-journal.h>
28
#ifndef DISABLE_JOURNAL
31
test_init_and_shutdown (void)
36
/* check double init/shutdown */
37
tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
38
result = tracker_db_journal_init (NULL, FALSE, &error);
39
g_assert_no_error (error);
40
g_assert (result == TRUE);
42
result = tracker_db_journal_shutdown (&error);
43
g_assert_no_error (error);
44
g_assert (result == TRUE);
46
tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
47
result = tracker_db_journal_init (NULL, FALSE, &error);
48
g_assert_no_error (error);
49
g_assert (result == TRUE);
51
result = tracker_db_journal_shutdown (&error);
52
g_assert_no_error (error);
53
g_assert (result == TRUE);
57
test_write_functions (void)
60
const gchar *filename;
61
gsize initial_size, actual_size;
65
path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
68
tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
69
tracker_db_journal_init (path, FALSE, &error);
70
g_assert_no_error (error);
72
filename = tracker_db_journal_get_filename ();
73
g_assert (filename != NULL);
74
g_assert_cmpstr (filename, ==, path);
76
/* Size is 8 due to header */
77
actual_size = tracker_db_journal_get_size ();
78
g_assert_cmpint (actual_size, ==, 8);
80
/* Check with rollback, nothing is added */
81
initial_size = tracker_db_journal_get_size ();
82
result = tracker_db_journal_start_transaction (time (NULL));
83
g_assert_cmpint (result, ==, TRUE);
84
result = tracker_db_journal_append_resource (10, "http://resource");
85
g_assert_cmpint (result, ==, TRUE);
86
result = tracker_db_journal_append_resource (11, "http://predicate");
87
g_assert_cmpint (result, ==, TRUE);
88
result = tracker_db_journal_append_delete_statement (0, 10, 11, "test");
89
g_assert_cmpint (result, ==, TRUE);
90
result = tracker_db_journal_rollback_transaction (&error);
91
g_assert_no_error (error);
92
g_assert_cmpint (result, ==, TRUE);
93
actual_size = tracker_db_journal_get_size ();
94
g_assert_cmpint (initial_size, ==, actual_size);
96
/* Check with commit, somethign is added */
97
result = tracker_db_journal_start_transaction (time (NULL));
98
g_assert_cmpint (result, ==, TRUE);
99
result = tracker_db_journal_append_resource (12, "http://resource");
100
g_assert_cmpint (result, ==, TRUE);
101
result = tracker_db_journal_append_resource (13, "http://predicate");
102
g_assert_cmpint (result, ==, TRUE);
103
result = tracker_db_journal_append_resource (14, "http://resource");
104
g_assert_cmpint (result, ==, TRUE);
105
result = tracker_db_journal_append_delete_statement_id (0, 12, 13, 14);
106
g_assert_cmpint (result, ==, TRUE);
107
result = tracker_db_journal_commit_db_transaction (&error);
108
g_assert_no_error (error);
109
g_assert_cmpint (result, ==, TRUE);
110
actual_size = tracker_db_journal_get_size ();
111
g_assert_cmpint (initial_size, !=, actual_size);
113
/* Test insert statement */
114
result = tracker_db_journal_start_transaction (time (NULL));
115
g_assert_cmpint (result, ==, TRUE);
116
result = tracker_db_journal_append_resource (15, "http://resource");
117
g_assert_cmpint (result, ==, TRUE);
118
result = tracker_db_journal_append_resource (16, "http://predicate");
119
g_assert_cmpint (result, ==, TRUE);
120
result = tracker_db_journal_append_insert_statement (0, 15, 16, "test");
121
g_assert_cmpint (result, ==, TRUE);
122
result = tracker_db_journal_commit_db_transaction (&error);
123
g_assert_no_error (error);
124
g_assert_cmpint (result, ==, TRUE);
127
result = tracker_db_journal_start_transaction (time (NULL));
128
g_assert_cmpint (result, ==, TRUE);
129
result = tracker_db_journal_append_resource (17, "http://resource");
130
g_assert_cmpint (result, ==, TRUE);
131
result = tracker_db_journal_append_resource (18, "http://predicate");
132
g_assert_cmpint (result, ==, TRUE);
133
result = tracker_db_journal_append_resource (19, "http://resource");
134
g_assert_cmpint (result, ==, TRUE);
135
result = tracker_db_journal_append_insert_statement_id (0, 17, 18, 19);
136
g_assert_cmpint (result, ==, TRUE);
137
result = tracker_db_journal_commit_db_transaction (&error);
138
g_assert_no_error (error);
139
g_assert_cmpint (result, ==, TRUE);
142
result = tracker_db_journal_fsync ();
143
g_assert_cmpint (result, ==, TRUE);
145
tracker_db_journal_shutdown (&error);
146
g_assert_no_error (error);
152
test_read_functions (void)
154
GError *error = NULL;
157
TrackerDBJournalEntryType type;
158
gint id, s_id, p_id, o_id;
159
const gchar *uri, *str;
161
path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
163
/* NOTE: we don't unlink here so we can use the data from the write tests */
165
/* Create an iterator */
166
result = tracker_db_journal_reader_init (path, &error);
167
g_assert_no_error (error);
168
g_assert_cmpint (result, ==, TRUE);
170
type = tracker_db_journal_reader_get_type ();
171
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START);
173
/* First transaction */
174
result = tracker_db_journal_reader_next (&error);
175
g_assert_no_error (error);
176
g_assert_cmpint (result, ==, TRUE);
178
type = tracker_db_journal_reader_get_type ();
179
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
181
result = tracker_db_journal_reader_next (&error);
182
g_assert_no_error (error);
183
g_assert_cmpint (result, ==, TRUE);
185
type = tracker_db_journal_reader_get_type ();
186
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
188
result = tracker_db_journal_reader_get_resource (&id, &uri);
189
g_assert_cmpint (result, ==, TRUE);
190
g_assert_cmpint (id, ==, 12);
191
g_assert_cmpstr (uri, ==, "http://resource");
193
result = tracker_db_journal_reader_next (&error);
194
g_assert_no_error (error);
195
g_assert_cmpint (result, ==, TRUE);
197
type = tracker_db_journal_reader_get_type ();
198
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
200
result = tracker_db_journal_reader_get_resource (&id, &uri);
201
g_assert_cmpint (result, ==, TRUE);
202
g_assert_cmpint (id, ==, 13);
203
g_assert_cmpstr (uri, ==, "http://predicate");
205
result = tracker_db_journal_reader_next (&error);
206
g_assert_no_error (error);
207
g_assert_cmpint (result, ==, TRUE);
209
type = tracker_db_journal_reader_get_type ();
210
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
212
result = tracker_db_journal_reader_get_resource (&id, &uri);
213
g_assert_cmpint (result, ==, TRUE);
214
g_assert_cmpint (id, ==, 14);
215
g_assert_cmpstr (uri, ==, "http://resource");
217
result = tracker_db_journal_reader_next (&error);
218
g_assert_no_error (error);
219
g_assert_cmpint (result, ==, TRUE);
221
type = tracker_db_journal_reader_get_type ();
222
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_DELETE_STATEMENT_ID);
224
result = tracker_db_journal_reader_get_statement_id (NULL, &s_id, &p_id, &o_id);
225
g_assert_cmpint (result, ==, TRUE);
226
g_assert_cmpint (s_id, ==, 12);
227
g_assert_cmpint (p_id, ==, 13);
228
g_assert_cmpint (o_id, ==, 14);
230
result = tracker_db_journal_reader_next (&error);
231
g_assert_no_error (error);
232
g_assert_cmpint (result, ==, TRUE);
234
type = tracker_db_journal_reader_get_type ();
235
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
237
/* Second transaction */
238
result = tracker_db_journal_reader_next (&error);
239
g_assert_no_error (error);
240
g_assert_cmpint (result, ==, TRUE);
242
type = tracker_db_journal_reader_get_type ();
243
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
245
result = tracker_db_journal_reader_next (&error);
246
g_assert_no_error (error);
247
g_assert_cmpint (result, ==, TRUE);
249
type = tracker_db_journal_reader_get_type ();
250
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
252
result = tracker_db_journal_reader_get_resource (&id, &uri);
253
g_assert_cmpint (result, ==, TRUE);
254
g_assert_cmpint (id, ==, 15);
255
g_assert_cmpstr (uri, ==, "http://resource");
257
result = tracker_db_journal_reader_next (&error);
258
g_assert_no_error (error);
259
g_assert_cmpint (result, ==, TRUE);
261
type = tracker_db_journal_reader_get_type ();
262
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
264
result = tracker_db_journal_reader_get_resource (&id, &uri);
265
g_assert_cmpint (result, ==, TRUE);
266
g_assert_cmpint (id, ==, 16);
267
g_assert_cmpstr (uri, ==, "http://predicate");
269
result = tracker_db_journal_reader_next (&error);
270
g_assert_no_error (error);
271
g_assert_cmpint (result, ==, TRUE);
273
type = tracker_db_journal_reader_get_type ();
274
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_INSERT_STATEMENT);
276
result = tracker_db_journal_reader_get_statement (NULL, &s_id, &p_id, &str);
277
g_assert_cmpint (result, ==, TRUE);
278
g_assert_cmpint (s_id, ==, 15);
279
g_assert_cmpint (p_id, ==, 16);
280
g_assert_cmpstr (str, ==, "test");
282
result = tracker_db_journal_reader_next (&error);
283
g_assert_no_error (error);
284
g_assert_cmpint (result, ==, TRUE);
286
type = tracker_db_journal_reader_get_type ();
287
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
289
/* Third transaction */
290
result = tracker_db_journal_reader_next (&error);
291
g_assert_no_error (error);
292
g_assert_cmpint (result, ==, TRUE);
294
type = tracker_db_journal_reader_get_type ();
295
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
297
result = tracker_db_journal_reader_next (&error);
298
g_assert_no_error (error);
299
g_assert_cmpint (result, ==, TRUE);
301
type = tracker_db_journal_reader_get_type ();
302
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
304
result = tracker_db_journal_reader_get_resource (&id, &uri);
305
g_assert_cmpint (result, ==, TRUE);
306
g_assert_cmpint (id, ==, 17);
307
g_assert_cmpstr (uri, ==, "http://resource");
309
result = tracker_db_journal_reader_next (&error);
310
g_assert_no_error (error);
311
g_assert_cmpint (result, ==, TRUE);
313
type = tracker_db_journal_reader_get_type ();
314
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
316
result = tracker_db_journal_reader_get_resource (&id, &uri);
317
g_assert_cmpint (result, ==, TRUE);
318
g_assert_cmpint (id, ==, 18);
319
g_assert_cmpstr (uri, ==, "http://predicate");
321
result = tracker_db_journal_reader_next (&error);
322
g_assert_no_error (error);
323
g_assert_cmpint (result, ==, TRUE);
325
type = tracker_db_journal_reader_get_type ();
326
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
328
result = tracker_db_journal_reader_get_resource (&id, &uri);
329
g_assert_cmpint (result, ==, TRUE);
330
g_assert_cmpint (id, ==, 19);
331
g_assert_cmpstr (uri, ==, "http://resource");
333
result = tracker_db_journal_reader_next (&error);
334
g_assert_no_error (error);
335
g_assert_cmpint (result, ==, TRUE);
337
type = tracker_db_journal_reader_get_type ();
338
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_INSERT_STATEMENT_ID);
340
result = tracker_db_journal_reader_get_statement_id (NULL, &s_id, &p_id, &o_id);
341
g_assert_cmpint (result, ==, TRUE);
342
g_assert_cmpint (s_id, ==, 17);
343
g_assert_cmpint (p_id, ==, 18);
344
g_assert_cmpint (o_id, ==, 19);
346
result = tracker_db_journal_reader_next (&error);
347
g_assert_no_error (error);
348
g_assert_cmpint (result, ==, TRUE);
350
type = tracker_db_journal_reader_get_type ();
351
g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
354
result = tracker_db_journal_reader_shutdown ();
355
g_assert_cmpint (result, ==, TRUE);
360
#endif /* DISABLE_JOURNAL */
363
main (int argc, char **argv)
369
g_thread_init (NULL);
370
g_test_init (&argc, &argv, NULL);
372
#ifndef DISABLE_JOURNAL
373
/* None of these tests make sense in case of disabled journal */
374
g_test_add_func ("/libtracker-db/tracker-db-journal/init-and-shutdown",
375
test_init_and_shutdown);
376
g_test_add_func ("/libtracker-db/tracker-db-journal/write-functions",
377
test_write_functions);
378
g_test_add_func ("/libtracker-db/tracker-db-journal/read-functions",
379
test_read_functions);
380
#endif /* DISABLE_JOURNAL */
382
result = g_test_run ();
385
path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
389
path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-data", "dconf", "user", NULL);