Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3
* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsPlacesTables.h"
8
9
#ifndef __nsPlacesTriggers_h__
10
# define __nsPlacesTriggers_h__
11
12
/**
13
* Exclude these visit types:
14
* 0 - invalid
15
* 4 - EMBED
16
* 7 - DOWNLOAD
17
* 8 - FRAMED_LINK
18
* 9 - RELOAD
19
**/
20
# define EXCLUDED_VISIT_TYPES "0, 4, 7, 8, 9"
21
22
/**
23
* This triggers update visit_count and last_visit_date based on historyvisits
24
* table changes.
25
*/
26
# define CREATE_HISTORYVISITS_AFTERINSERT_TRIGGER \
27
NS_LITERAL_CSTRING( \
28
"CREATE TEMP TRIGGER moz_historyvisits_afterinsert_v2_trigger " \
29
"AFTER INSERT ON moz_historyvisits FOR EACH ROW " \
30
"BEGIN " \
31
"SELECT store_last_inserted_id('moz_historyvisits', NEW.id); " \
32
"UPDATE moz_places SET " \
33
"visit_count = visit_count + (SELECT NEW.visit_type NOT IN " \
34
"(" EXCLUDED_VISIT_TYPES \
35
")), " \
36
"last_visit_date = MAX(IFNULL(last_visit_date, 0), NEW.visit_date) " \
37
"WHERE id = NEW.place_id;" \
38
"END")
39
40
# define CREATE_HISTORYVISITS_AFTERDELETE_TRIGGER \
41
NS_LITERAL_CSTRING( \
42
"CREATE TEMP TRIGGER moz_historyvisits_afterdelete_v2_trigger " \
43
"AFTER DELETE ON moz_historyvisits FOR EACH ROW " \
44
"BEGIN " \
45
"UPDATE moz_places SET " \
46
"visit_count = visit_count - (SELECT OLD.visit_type NOT IN " \
47
"(" EXCLUDED_VISIT_TYPES \
48
")), " \
49
"last_visit_date = (SELECT visit_date FROM moz_historyvisits " \
50
"WHERE place_id = OLD.place_id " \
51
"ORDER BY visit_date DESC LIMIT 1) " \
52
"WHERE id = OLD.place_id;" \
53
"END")
54
55
// This macro is a helper for the next several triggers. It updates the origin
56
// frecency stats. Use it as follows. Before changing an origin's frecency,
57
// call the macro and pass "-" (subtraction) as the argument. That will update
58
// the stats by deducting the origin's current contribution to them. And then
59
// after you change the origin's frecency, call the macro again, this time
60
// passing "+" (addition) as the argument. That will update the stats by adding
61
// the origin's new contribution to them.
62
# define UPDATE_ORIGIN_FRECENCY_STATS(op) \
63
"INSERT OR REPLACE INTO moz_meta(key, value) " \
64
"SELECT '" MOZ_META_KEY_ORIGIN_FRECENCY_COUNT \
65
"', " \
66
"IFNULL((SELECT value FROM moz_meta WHERE key = " \
67
"'" MOZ_META_KEY_ORIGIN_FRECENCY_COUNT "'), 0) " op \
68
" CAST(frecency > 0 AS INT) " \
69
"FROM moz_origins WHERE prefix = OLD.prefix AND host = OLD.host " \
70
"UNION " \
71
"SELECT '" MOZ_META_KEY_ORIGIN_FRECENCY_SUM \
72
"', " \
73
"IFNULL((SELECT value FROM moz_meta WHERE key = " \
74
"'" MOZ_META_KEY_ORIGIN_FRECENCY_SUM "'), 0) " op \
75
" MAX(frecency, 0) " \
76
"FROM moz_origins WHERE prefix = OLD.prefix AND host = OLD.host " \
77
"UNION " \
78
"SELECT '" MOZ_META_KEY_ORIGIN_FRECENCY_SUM_OF_SQUARES \
79
"', " \
80
"IFNULL((SELECT value FROM moz_meta WHERE key = " \
81
"'" MOZ_META_KEY_ORIGIN_FRECENCY_SUM_OF_SQUARES "'), 0) " op \
82
" (MAX(frecency, 0) * MAX(frecency, 0)) " \
83
"FROM moz_origins WHERE prefix = OLD.prefix AND host = OLD.host "
84
85
// The next several triggers are a workaround for the lack of FOR EACH STATEMENT
86
// in Sqlite, until bug 871908 can be fixed properly.
87
//
88
// While doing inserts or deletes into moz_places, we accumulate the affected
89
// origins into a temp table. Afterwards, we delete everything from the temp
90
// table, causing the AFTER DELETE trigger to fire for it, which will then
91
// update moz_origins and the origin frecency stats. As a consequence, we also
92
// do this for updates to moz_places.frecency in order to make sure that changes
93
// to origins are serialized.
94
//
95
// Note this way we lose atomicity, crashing between the 2 queries may break the
96
// tables' coherency. So it's better to run those DELETE queries in a single
97
// transaction. Regardless, this is still better than hanging the browser for
98
// several minutes on a fast machine.
99
100
// This trigger runs on inserts into moz_places.
101
# define CREATE_PLACES_AFTERINSERT_TRIGGER \
102
NS_LITERAL_CSTRING( \
103
"CREATE TEMP TRIGGER moz_places_afterinsert_trigger " \
104
"AFTER INSERT ON moz_places FOR EACH ROW " \
105
"BEGIN " \
106
"SELECT store_last_inserted_id('moz_places', NEW.id); " \
107
"INSERT OR IGNORE INTO moz_updateoriginsinsert_temp (place_id, " \
108
"prefix, " \
109
"host, frecency) " \
110
"VALUES (NEW.id, get_prefix(NEW.url), get_host_and_port(NEW.url), " \
111
"NEW.frecency); " \
112
"END")
113
// This trigger corresponds to the previous trigger. It runs on deletes on
114
// moz_updateoriginsinsert_temp -- logically, after inserts on moz_places.
115
# define CREATE_UPDATEORIGINSINSERT_AFTERDELETE_TRIGGER \
116
NS_LITERAL_CSTRING( \
117
"CREATE TEMP TRIGGER moz_updateoriginsinsert_afterdelete_trigger " \
118
"AFTER DELETE ON moz_updateoriginsinsert_temp FOR EACH ROW " \
119
"BEGIN " \
120
/* Deduct the origin's current contribution to frecency stats */ \
121
UPDATE_ORIGIN_FRECENCY_STATS("-") "; " \
122
"INSERT INTO moz_origins (prefix, host, frecency) " \
123
"VALUES (OLD.prefix, OLD.host, MAX(OLD.frecency, 0)) " \
124
"ON CONFLICT(prefix, host) DO UPDATE " \
125
"SET frecency = frecency + OLD.frecency " \
126
"WHERE OLD.frecency > 0; " \
127
/* Add the origin's new contribution to frecency stats */ \
128
UPDATE_ORIGIN_FRECENCY_STATS("+") "; " \
129
"UPDATE moz_places SET origin_id = ( " \
130
"SELECT id " \
131
"FROM moz_origins " \
132
"WHERE prefix = OLD.prefix AND host = OLD.host " \
133
") " \
134
"WHERE id = OLD.place_id; " \
135
"END" \
136
)
137
138
// This trigger runs on deletes on moz_places.
139
# define CREATE_PLACES_AFTERDELETE_TRIGGER \
140
NS_LITERAL_CSTRING( \
141
"CREATE TEMP TRIGGER moz_places_afterdelete_trigger " \
142
"AFTER DELETE ON moz_places FOR EACH ROW " \
143
"BEGIN " \
144
"INSERT INTO moz_updateoriginsdelete_temp (prefix, host, " \
145
"frecency_delta) " \
146
"VALUES (get_prefix(OLD.url), get_host_and_port(OLD.url), " \
147
"-MAX(OLD.frecency, 0)) " \
148
"ON CONFLICT(prefix, host) DO UPDATE " \
149
"SET frecency_delta = frecency_delta - OLD.frecency " \
150
"WHERE OLD.frecency > 0; " \
151
"END ")
152
// This trigger corresponds to the previous trigger. It runs on deletes on
153
// moz_updateoriginsdelete_temp -- logically, after deletes on moz_places.
154
# define CREATE_UPDATEORIGINSDELETE_AFTERDELETE_TRIGGER \
155
NS_LITERAL_CSTRING( \
156
"CREATE TEMP TRIGGER moz_updateoriginsdelete_afterdelete_trigger " \
157
"AFTER DELETE ON moz_updateoriginsdelete_temp FOR EACH ROW " \
158
"BEGIN " \
159
/* Deduct the origin's current contribution to frecency stats */ \
160
UPDATE_ORIGIN_FRECENCY_STATS("-") "; " \
161
"UPDATE moz_origins SET frecency = frecency + OLD.frecency_delta " \
162
"WHERE prefix = OLD.prefix AND host = OLD.host; " \
163
"DELETE FROM moz_origins " \
164
"WHERE prefix = OLD.prefix AND host = OLD.host AND NOT EXISTS ( " \
165
"SELECT id FROM moz_places " \
166
"WHERE origin_id = moz_origins.id " \
167
"LIMIT 1 " \
168
"); " \
169
/* Add the origin's new contribution to frecency stats */ \
170
UPDATE_ORIGIN_FRECENCY_STATS("+") "; " \
171
"DELETE FROM moz_icons WHERE id IN ( " \
172
"SELECT id FROM moz_icons " \
173
"WHERE fixed_icon_url_hash = hash(fixup_url(OLD.host || '/favicon.ico')) " \
174
"AND fixup_url(icon_url) = fixup_url(OLD.host || '/favicon.ico') " \
175
"AND NOT EXISTS (SELECT 1 FROM moz_origins WHERE host = OLD.host " \
176
"OR host = fixup_url(OLD.host)) " \
177
"EXCEPT " \
178
"SELECT icon_id FROM moz_icons_to_pages " \
179
"); " \
180
"END" \
181
)
182
183
// This trigger runs on updates to moz_places.frecency.
184
//
185
// However, we skip this when frecency changes are due to frecency decay since
186
// (1) decay updates all frecencies at once, so this trigger would run for each
187
// moz_place, which would be expensive; and (2) decay does not change the
188
// ordering of frecencies since all frecencies decay by the same percentage.
189
# define CREATE_PLACES_AFTERUPDATE_FRECENCY_TRIGGER \
190
NS_LITERAL_CSTRING( \
191
"CREATE TEMP TRIGGER moz_places_afterupdate_frecency_trigger " \
192
"AFTER UPDATE OF frecency ON moz_places FOR EACH ROW " \
193
"WHEN NOT is_frecency_decaying() " \
194
"BEGIN " \
195
"INSERT INTO moz_updateoriginsupdate_temp (prefix, host, " \
196
"frecency_delta) " \
197
"VALUES (get_prefix(NEW.url), get_host_and_port(NEW.url), " \
198
"MAX(NEW.frecency, 0) - MAX(OLD.frecency, 0)) " \
199
"ON CONFLICT(prefix, host) DO UPDATE " \
200
"SET frecency_delta = frecency_delta + EXCLUDED.frecency_delta; " \
201
"END ")
202
// This trigger corresponds to the previous trigger. It runs on deletes on
203
// moz_updateoriginsupdate_temp -- logically, after updates to
204
// moz_places.frecency.
205
# define CREATE_UPDATEORIGINSUPDATE_AFTERDELETE_TRIGGER \
206
NS_LITERAL_CSTRING( \
207
"CREATE TEMP TRIGGER moz_updateoriginsupdate_afterdelete_trigger " \
208
"AFTER DELETE ON moz_updateoriginsupdate_temp FOR EACH ROW " \
209
"BEGIN " \
210
/* Deduct the origin's current contribution to frecency stats */ \
211
UPDATE_ORIGIN_FRECENCY_STATS("-") "; " \
212
"UPDATE moz_origins " \
213
"SET frecency = frecency + OLD.frecency_delta " \
214
"WHERE prefix = OLD.prefix AND host = OLD.host; " \
215
/* Add the origin's new contribution to frecency stats */ \
216
UPDATE_ORIGIN_FRECENCY_STATS("+") "; " \
217
"END" \
218
)
219
220
/**
221
* This trigger removes a row from moz_openpages_temp when open_count reaches 0.
222
*
223
* @note this should be kept up-to-date with the definition in
224
* nsPlacesAutoComplete.js
225
*/
226
# define CREATE_REMOVEOPENPAGE_CLEANUP_TRIGGER \
227
NS_LITERAL_CSTRING( \
228
"CREATE TEMPORARY TRIGGER moz_openpages_temp_afterupdate_trigger " \
229
"AFTER UPDATE OF open_count ON moz_openpages_temp FOR EACH ROW " \
230
"WHEN NEW.open_count = 0 " \
231
"BEGIN " \
232
"DELETE FROM moz_openpages_temp " \
233
"WHERE url = NEW.url " \
234
"AND userContextId = NEW.userContextId;" \
235
"END")
236
237
# define CREATE_BOOKMARKS_FOREIGNCOUNT_AFTERDELETE_TRIGGER \
238
NS_LITERAL_CSTRING( \
239
"CREATE TEMP TRIGGER moz_bookmarks_foreign_count_afterdelete_trigger " \
240
"AFTER DELETE ON moz_bookmarks FOR EACH ROW " \
241
"BEGIN " \
242
"UPDATE moz_places " \
243
"SET foreign_count = foreign_count - 1 " \
244
"WHERE id = OLD.fk;" \
245
"END")
246
247
# define CREATE_BOOKMARKS_FOREIGNCOUNT_AFTERINSERT_TRIGGER \
248
NS_LITERAL_CSTRING( \
249
"CREATE TEMP TRIGGER moz_bookmarks_foreign_count_afterinsert_trigger " \
250
"AFTER INSERT ON moz_bookmarks FOR EACH ROW " \
251
"BEGIN " \
252
"SELECT store_last_inserted_id('moz_bookmarks', NEW.id); " \
253
"SELECT note_sync_change() WHERE NEW.syncChangeCounter > 0; " \
254
"UPDATE moz_places " \
255
"SET foreign_count = foreign_count + 1 " \
256
"WHERE id = NEW.fk;" \
257
"END")
258
259
# define CREATE_BOOKMARKS_FOREIGNCOUNT_AFTERUPDATE_TRIGGER \
260
NS_LITERAL_CSTRING( \
261
"CREATE TEMP TRIGGER moz_bookmarks_foreign_count_afterupdate_trigger " \
262
"AFTER UPDATE OF fk, syncChangeCounter ON moz_bookmarks FOR EACH ROW " \
263
"BEGIN " \
264
"SELECT note_sync_change() " \
265
"WHERE NEW.syncChangeCounter <> OLD.syncChangeCounter; " \
266
"UPDATE moz_places " \
267
"SET foreign_count = foreign_count + 1 " \
268
"WHERE OLD.fk <> NEW.fk AND id = NEW.fk;" \
269
"UPDATE moz_places " \
270
"SET foreign_count = foreign_count - 1 " \
271
"WHERE OLD.fk <> NEW.fk AND id = OLD.fk;" \
272
"END")
273
274
# define CREATE_KEYWORDS_FOREIGNCOUNT_AFTERDELETE_TRIGGER \
275
NS_LITERAL_CSTRING( \
276
"CREATE TEMP TRIGGER moz_keywords_foreign_count_afterdelete_trigger " \
277
"AFTER DELETE ON moz_keywords FOR EACH ROW " \
278
"BEGIN " \
279
"UPDATE moz_places " \
280
"SET foreign_count = foreign_count - 1 " \
281
"WHERE id = OLD.place_id;" \
282
"END")
283
284
# define CREATE_KEYWORDS_FOREIGNCOUNT_AFTERINSERT_TRIGGER \
285
NS_LITERAL_CSTRING( \
286
"CREATE TEMP TRIGGER moz_keywords_foreign_count_afterinsert_trigger " \
287
"AFTER INSERT ON moz_keywords FOR EACH ROW " \
288
"BEGIN " \
289
"UPDATE moz_places " \
290
"SET foreign_count = foreign_count + 1 " \
291
"WHERE id = NEW.place_id;" \
292
"END")
293
294
# define CREATE_KEYWORDS_FOREIGNCOUNT_AFTERUPDATE_TRIGGER \
295
NS_LITERAL_CSTRING( \
296
"CREATE TEMP TRIGGER moz_keywords_foreign_count_afterupdate_trigger " \
297
"AFTER UPDATE OF place_id ON moz_keywords FOR EACH ROW " \
298
"BEGIN " \
299
"UPDATE moz_places " \
300
"SET foreign_count = foreign_count + 1 " \
301
"WHERE id = NEW.place_id; " \
302
"UPDATE moz_places " \
303
"SET foreign_count = foreign_count - 1 " \
304
"WHERE id = OLD.place_id; " \
305
"END")
306
307
# define CREATE_ICONS_AFTERINSERT_TRIGGER \
308
NS_LITERAL_CSTRING( \
309
"CREATE TEMP TRIGGER moz_icons_afterinsert_v1_trigger " \
310
"AFTER INSERT ON moz_icons FOR EACH ROW " \
311
"BEGIN " \
312
"SELECT store_last_inserted_id('moz_icons', NEW.id); " \
313
"END")
314
315
# define CREATE_BOOKMARKS_DELETED_AFTERINSERT_TRIGGER \
316
NS_LITERAL_CSTRING( \
317
"CREATE TEMP TRIGGER moz_bookmarks_deleted_afterinsert_v1_trigger " \
318
"AFTER INSERT ON moz_bookmarks_deleted FOR EACH ROW " \
319
"BEGIN " \
320
"SELECT note_sync_change(); " \
321
"END")
322
323
# define CREATE_BOOKMARKS_DELETED_AFTERDELETE_TRIGGER \
324
NS_LITERAL_CSTRING( \
325
"CREATE TEMP TRIGGER moz_bookmarks_deleted_afterdelete_v1_trigger " \
326
"AFTER DELETE ON moz_bookmarks_deleted FOR EACH ROW " \
327
"BEGIN " \
328
"SELECT note_sync_change(); " \
329
"END")
330
331
#endif // __nsPlacesTriggers_h__