summaryrefslogtreecommitdiff
path: root/src/tracker-store/tracker-writeback.c
blob: a183b00a3f3747df7408943a09b4bcec3c35fd54 (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
/*
 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *
 * Authors:
 *  Philip Van Hoof <philip@codeminded.be>
 */

#include "config.h"

#include <libtracker-data/tracker-data.h>

#include "tracker-writeback.h"

typedef struct {
	GHashTable *allowances;
	GHashTable *pending_events;
	GHashTable *ready_events;
} WritebackPrivate;

static WritebackPrivate *private;

static GArray*
rdf_types_to_array (GPtrArray *rdf_types)
{
	GArray *new_types;
	guint n;

	new_types =  g_array_sized_new (FALSE, FALSE, sizeof (gint), rdf_types->len);

	for (n = 0; n < rdf_types->len; n++) {
		gint id = tracker_class_get_id (rdf_types->pdata[n]);
		g_array_append_val (new_types, id);
	}

	return new_types;
}

static void
array_free (GArray *array)
{
	g_array_free (array, TRUE);
}

void
tracker_writeback_check (gint         graph_id,
                         const gchar *graph,
                         gint         subject_id,
                         const gchar *subject,
                         gint         pred_id,
                         gint         object_id,
                         const gchar *object,
                         GPtrArray   *rdf_types)
{
	/* When graph is NULL, the graph is the default one. We only do
	 * writeback reporting in the default graph (update queries that
	 * aren't coming from the miner)
	 */

	if (graph != NULL) {
		/* g_debug ("Not doing writeback check, no graph"); */
		return;
	}

	g_return_if_fail (private != NULL);

	if (g_hash_table_lookup (private->allowances, GINT_TO_POINTER (pred_id))) {
		if (!private->pending_events) {
			private->pending_events = g_hash_table_new_full (g_direct_hash, g_direct_equal,
			                                                 (GDestroyNotify) NULL,
			                                                 (GDestroyNotify) NULL);
		}

		g_hash_table_insert (private->pending_events,
		                     GINT_TO_POINTER (subject_id),
		                     rdf_types_to_array (rdf_types));
	}
}

void
tracker_writeback_reset_pending ()
{
	g_return_if_fail (private != NULL);

	if (private->pending_events) {
		g_hash_table_remove_all (private->pending_events);
	}
}

void
tracker_writeback_reset_ready ()
{
	g_return_if_fail (private != NULL);

	if (private->ready_events) {
		g_hash_table_unref (private->ready_events);
		private->ready_events = NULL;
	}
}

GHashTable *
tracker_writeback_get_ready (void)
{
	g_return_val_if_fail (private != NULL, NULL);

	return private->ready_events;
}

static void
free_private (gpointer user_data)
{
	WritebackPrivate *private;

	private = user_data;
	if (private->ready_events)
		g_hash_table_unref (private->ready_events);
	if (private->pending_events)
		g_hash_table_unref (private->pending_events);
	g_hash_table_unref (private->allowances);
	g_free (private);
}

void
tracker_writeback_init (TrackerDataManager                *data_manager,
                        TrackerWritebackGetPredicatesFunc  func)
{
	TrackerOntologies *ontologies;
	GStrv predicates_to_signal;
	gint i, count;

	g_return_if_fail (private == NULL);

	private = g_new0 (WritebackPrivate, 1);

	private->allowances = g_hash_table_new_full (g_direct_hash,
	                                             g_direct_equal,
	                                             NULL,
	                                             NULL);

	g_message ("Setting up predicates for writeback notification...");

	if (!func) {
		g_message ("  No predicates set, no TrackerWritebackGetPredicatesFunc");
		return;
	}

	predicates_to_signal = (*func)();

	if (!predicates_to_signal) {
		g_message ("  No predicates set, none are configured in ontology");
		return;
	}

	count = g_strv_length (predicates_to_signal);
	ontologies = tracker_data_manager_get_ontologies (data_manager);

	for (i = 0; i < count; i++) {
		TrackerProperty *predicate = tracker_ontologies_get_property_by_uri (ontologies, predicates_to_signal[i]);
		if (predicate) {
			gint id = tracker_property_get_id (predicate);
			g_message ("  Adding:'%s'", predicates_to_signal[i]);
			g_hash_table_insert (private->allowances,
			                     GINT_TO_POINTER (id),
			                     GINT_TO_POINTER (TRUE));
		}
	}

	g_strfreev (predicates_to_signal);
}

void
tracker_writeback_transact (void)
{
	GHashTableIter iter;
	gpointer key, value;

	if (!private->pending_events)
		return;

	if (!private->ready_events) {
		private->ready_events = g_hash_table_new_full (g_direct_hash, g_direct_equal,
		                                               (GDestroyNotify) NULL,
		                                               (GDestroyNotify) array_free);
	}

	g_hash_table_iter_init (&iter, private->pending_events);

	while (g_hash_table_iter_next (&iter, &key, &value)) {
		g_hash_table_insert (private->ready_events, key, value);
		g_hash_table_iter_remove (&iter);
	}
}

void
tracker_writeback_shutdown (void)
{
	g_return_if_fail (private != NULL);

	tracker_writeback_reset_pending ();

	/* Perhaps hurry an emit of the ready events here? We're shutting down,
	 * so I guess we're not required to do that here ... ? */
	tracker_writeback_reset_ready ();

	free_private (private);
	private = NULL;
}