File: QueryHistory.cpp

package info (click to toggle)
pinot 1.05-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,848 kB
  • ctags: 3,572
  • sloc: cpp: 39,255; sh: 10,481; ansic: 3,049; makefile: 620; xml: 379
file content (392 lines) | stat: -rw-r--r-- 8,954 bytes parent folder | download | duplicates (5)
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
/*
 *  Copyright 2005-2009 Fabrice Colin
 *
 *  This program 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 program 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 program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <iostream>

#include "TimeConverter.h"
#include "Url.h"
#include "QueryHistory.h"

using std::clog;
using std::endl;
using std::string;
using std::set;
using std::vector;

QueryHistory::QueryHistory(const string &database) :
	SQLiteBase(database)
{
}

QueryHistory::~QueryHistory()
{
}

/// Creates the QueryHistory table in the database.
bool QueryHistory::create(const string &database)
{
	// The specified path must be a file
	if (SQLiteBase::check(database) == false)
	{
		return false;
	}

	SQLiteBase db(database);
	string tableDefinition("QueryName VARCHAR(255), EngineName VARCHAR(255), HostName VARCHAR(255), \
		Url VARCHAR(255), Title VARCHAR(255), Extract VARCHAR(255), Score FLOAT, Date INTEGER, \
		PRIMARY KEY(QueryName, EngineName, Url, Date)");

	// Does QueryHistory exist ?
	if (db.executeSimpleStatement("SELECT * FROM QueryHistory LIMIT 1;") == false)
	{
		// Create the table
		if (db.executeSimpleStatement("CREATE TABLE QueryHistory (" + tableDefinition + ");") == false)
		{
			return false;
		}
	}
	else
	{
		// Previous versions had PrevScore and Language columns, so check for one of them
		if (db.executeSimpleStatement("SELECT Language FROM QueryHistory LIMIT 1;") == true)
		{
#ifdef DEBUG
			clog << "QueryHistory::create: QueryHistory needs updating" << endl;
#endif
			db.alterTable("QueryHistory",
				"QueryName, EngineName, HostName, Url, Title, Extract, Score, Date",
				tableDefinition);
		}
	}

	return true;
}

/// Inserts an URL.
bool QueryHistory::insertItem(const string &queryName, const string &engineName,
	const string &url, const string &title, const string &extract, float score)
{
	Url urlObj(url);
	string hostName(urlObj.getHost());
	bool success = false;

	SQLResults *results = executeStatement("INSERT INTO QueryHistory \
		VALUES('%q', '%q', '%q', '%q', '%q', '%q', '%f', '%d');",
		queryName.c_str(), engineName.c_str(), hostName.c_str(),
		Url::escapeUrl(url).c_str(), title.c_str(), extract.c_str(),
		score, time(NULL));
	if (results != NULL)
	{
		success = true;
		delete results;
	}

	return success;
}

/// Checks if an URL is in the history; returns its current score or 0 if not found.
float QueryHistory::hasItem(const string &queryName, const string &engineName, const string &url,
	float &previousScore)
{
	float score = 0;

	SQLResults *results = executeStatement("SELECT Score FROM QueryHistory \
		WHERE QueryName='%q' AND EngineName='%q' AND Url='%q' ORDER BY Date DESC;",
		queryName.c_str(), engineName.c_str(), Url::escapeUrl(url).c_str());
	if (results != NULL)
	{
		previousScore = 0;

		SQLRow *row = results->nextRow();
		if (row != NULL)
		{
			score = (float)atof(row->getColumn(0).c_str());

			delete row;

			// Get the score of the second last run
			SQLRow *row = results->nextRow();
			if (row != NULL)
			{
				previousScore = (float)atof(row->getColumn(0).c_str());

				delete row;
			}
		}

		delete results;
	}

	return score;
}

/// Gets the list of engines the query was run on.
bool QueryHistory::getEngines(const string &queryName, set<string> &enginesList)
{
	bool success = false;

	SQLResults *results = executeStatement("SELECT EngineName FROM QueryHistory \
		WHERE QueryName='%q' GROUP BY EngineName;",
		queryName.c_str());
	if (results != NULL)
	{
		while (results->hasMoreRows() == true)
		{
			SQLRow *row = results->nextRow();
			if (row == NULL)
			{
				break;
			}

			enginesList.insert(row->getColumn(0));
			success = true;

			delete row;
		}

		delete results;
	}

	return success;
}

/// Gets the first max items for the given query, engine pair.
bool QueryHistory::getItems(const string &queryName, const string &engineName,
	unsigned int max, vector<DocumentInfo> &resultsList)
{
	bool success = false;

	SQLResults *results = executeStatement("SELECT Title, Url, Extract, Score, Date \
		FROM QueryHistory WHERE QueryName='%q' AND EngineName='%q' \
		ORDER BY Date DESC, Score DESC LIMIT %u;",
		queryName.c_str(), engineName.c_str(), max);
	if (results != NULL)
	{
		while (results->hasMoreRows() == true)
		{
			SQLRow *row = results->nextRow();
			if (row == NULL)
			{
				break;
			}

			DocumentInfo result(row->getColumn(0),
				Url::unescapeUrl(row->getColumn(1)).c_str(),
				"", "");
			result.setExtract(row->getColumn(2));
			result.setScore((float)atof(row->getColumn(3).c_str()));
			int runDate = atoi(row->getColumn(4).c_str());
			result.setTimestamp(TimeConverter::toTimestamp((time_t)runDate));

			resultsList.push_back(result);
			success = true;

			delete row;
		}

		delete results;
	}

	return success;
}

/// Gets an item's extract.
string QueryHistory::getItemExtract(const string &queryName, const string &engineName,
	const string &url)
{
	string extract;

	SQLResults *results = executeStatement("SELECT Extract FROM QueryHistory \
		WHERE QueryName='%q' AND EngineName='%q' AND Url='%q' ORDER BY Date DESC;",
		queryName.c_str(), engineName.c_str(), Url::escapeUrl(url).c_str());
	if (results != NULL)
	{
		SQLRow *row = results->nextRow();
		if (row != NULL)
		{
			extract = row->getColumn(0);

			delete row;
		}

		delete results;
	}

	return extract;
}

/// Finds URLs.
bool QueryHistory::findUrlsLike(const string &url, unsigned int count, set<string> &urls)
{
	bool success = false;

	if (url.empty() == true)
	{
		return false; 
	}

	SQLResults *results = executeStatement("SELECT Url FROM QueryHistory \
		WHERE Url LIKE '%q%%' ORDER BY Url LIMIT %u;",
		Url::escapeUrl(url).c_str(), count);
	if (results != NULL)
	{
		while (results->hasMoreRows() == true)
		{
			SQLRow *row = results->nextRow();
			if (row == NULL)
			{
				break;
			}

			urls.insert(Url::unescapeUrl(row->getColumn(0)));
			success = true;

			delete row;
		}

		delete results;
	}

	return success;
}

/// Gets a query's latest run times.
bool QueryHistory::getLatestRuns(const string &queryName, const string &engineName,
	unsigned int runCount, set<time_t> &runTimes)
{
	SQLResults *results = NULL;
	bool success = false;

	if (queryName.empty() == true)
	{
		return false;
	}

	if (engineName.empty() == true)
	{
		results = executeStatement("SELECT Date FROM QueryHistory \
			WHERE QueryName='%q' GROUP BY EngineName ORDER By Date DESC LIMIT %u;",
			queryName.c_str(), runCount);
	}
	else
	{
		results = executeStatement("SELECT Date FROM QueryHistory \
			WHERE QueryName='%q' AND EngineName='%q' GROUP BY Date ORDER By Date DESC LIMIT %u;",
			queryName.c_str(), engineName.c_str(), runCount);
	}

	if (results != NULL)
	{
		while (results->hasMoreRows() == true)
		{
			SQLRow *row = results->nextRow();
			if (row == NULL)
			{
				break;
			}

			int runDate = atoi(row->getColumn(0).c_str());
			if (runDate > 0)
			{
				runTimes.insert((time_t)runDate);
			}
			success = true;

			delete row;
		}

		delete results;
	}

	return success;
}

/// Deletes items at least as old as the given date.
bool QueryHistory::deleteItems(const string &queryName, const string &engineName,
	time_t cutOffDate)
{
	if (cutOffDate == 0)
	{
		// Nothing to delete
		return true;
	}

	SQLResults *results = executeStatement("DELETE FROM QueryHistory \
		WHERE QueryName='%q' AND EngineName='%q' AND Date<'%d';",
		queryName.c_str(), engineName.c_str(), cutOffDate);
	if (results != NULL)
	{
		delete results;

		return true;
	}

	return false;
}

/// Deletes items.
bool QueryHistory::deleteItems(const string &name, bool isQueryName)
{
	SQLResults *results = NULL;

	if (isQueryName == true)
	{
		results = executeStatement("DELETE FROM QueryHistory \
			WHERE QueryName='%q';", name.c_str());
	}
	else
	{
		results = executeStatement("DELETE FROM QueryHistory \
			WHERE EngineName='%q';", name.c_str());
	}

	if (results != NULL)
	{
		delete results;

		return true;
	}

	return false;
}

/// Expires items older than the given date.
bool QueryHistory::expireItems(time_t expiryDate)
{
	if (expiryDate == 0)
	{
		// Nothing to delete
		return true;
	}

	SQLResults *results = executeStatement("DELETE FROM QueryHistory \
		WHERE Date<'%d';", expiryDate);
	if (results != NULL)
	{
		delete results;

		return true;
	}

	return false;
}