File: ActionQueue.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 (322 lines) | stat: -rw-r--r-- 6,923 bytes parent folder | download | duplicates (3)
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
/*
 *  Copyright 2005-2013 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <set>
#include <iostream>
#include <sstream>

#include "Url.h"
#include "StringManip.h"
#include "TimeConverter.h"
#include "ActionQueue.h"

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

ActionQueue::ActionQueue(const string &database, const string queueId) :
	SQLiteBase(database, false, false),
	m_queueId(queueId)
{
	prepareStatement("select-url",
		"SELECT Url FROM ActionQueue WHERE QueueId=? AND Url=?;");
	prepareStatement("push-item-insert",
		"INSERT INTO ActionQueue VALUES(?, ?, ?, ?, ?);");
	prepareStatement("push-item-update",
		"UPDATE ActionQueue SET Type=?, Date=?, Info=? WHERE QueueId=? AND Url=?;");
	prepareStatement("pop-item",
		"DELETE FROM ActionQueue WHERE QueueId=? AND Url=?;");
	prepareStatement("select-oldest-url",
		"SELECT Type, Info FROM ActionQueue "
		"WHERE QueueId=? ORDER BY Date DESC LIMIT 1;");
	prepareStatement("expire-items",
		"DELETE FROM ActionQueue WHERE QueueId=? AND Date<?;");
}

ActionQueue::~ActionQueue()
{
}

string ActionQueue::typeToText(ActionType type)
{
	string text;

	switch (type)
	{
		case INDEX:
			text = "INDEX";
			break;
		case UNINDEX:
			text = "UNINDEX";
			break;
		default:
			break;
	}

	return text;
}

ActionQueue::ActionType ActionQueue::textToType(const string &text)
{
	ActionType type = INDEX;

	if (text == "UNINDEX")
	{
		type = UNINDEX;
	}

	return type;
}

/// Creates the ActionQueue table in the database.
bool ActionQueue::create(const string &database)
{
	bool success = true;

	// The specified path must be a file
	if (SQLiteBase::check(database) == false)
	{
		return false;
	}

	SQLiteBase db(database);

	// Does ActionQueue exist ?
	if (db.executeSimpleStatement("SELECT * FROM ActionQueue LIMIT 1;") == false)
	{
#ifdef DEBUG
		clog << "ActionQueue::create: ActionQueue doesn't exist" << endl;
#endif
		// Create the table
		if (db.executeSimpleStatement("CREATE TABLE ActionQueue (QueueId VARCHAR(255), \
			Url VARCHAR(255), Type VARCHAR(255), Date INTEGER, Info TEXT, \
			PRIMARY KEY(QueueId, Url));") == false)
		{
			success = false;
		}
	}

	return success;
}

/// Pushes an item.
bool ActionQueue::pushItem(ActionType type, const DocumentInfo &docInfo)
{
	vector<string> values;
	string url(docInfo.getLocation());
	string info(docInfo.serialize());
	stringstream numStr;
	bool update = false, success = false;

	// Is there already an item for this URL ?
	values.push_back(m_queueId);
	values.push_back(Url::escapeUrl(url));
	SQLResults *results = executePreparedStatement("select-url", values);
	if (results != NULL)
	{
		SQLRow *row = results->nextRow();
		if (row != NULL)
		{
#ifdef DEBUG
			clog << "ActionQueue::pushItem: item "
				<< Url::unescapeUrl(row->getColumn(0)) << " exists" << endl;
#endif
			update = true;

			delete row;
		}

		delete results;
	}

	numStr << time(NULL);

	if (update == false)
	{
		values.push_back(typeToText(type));
		values.push_back(numStr.str());
		values.push_back(info);

		results = executePreparedStatement("push-item-insert", values);
	}
	else
	{
		values.clear();
		values.push_back(typeToText(type));
		values.push_back(numStr.str());
		values.push_back(info);
		values.push_back(m_queueId);
		values.push_back(Url::escapeUrl(url));

		results = executePreparedStatement("push-item-update", values);
	}
	if (results != NULL)
	{
#ifdef DEBUG
		clog << "ActionQueue::pushItem: queue " << m_queueId
			<< ": " << type << " on " << url << ", " << update << endl;
#endif
		success = true;

		delete results;
	}

	return success;
}

/// Pops and deletes the oldest item.
bool ActionQueue::popItem(ActionType &type, DocumentInfo &docInfo)
{
	vector<string> values;
	string url;
	bool success = false;

	if (getOldestItem(type, docInfo) == false)
	{
		return false;
	}
	url = docInfo.getLocation();
#ifdef DEBUG
	clog << "ActionQueue::popItem: queue " << m_queueId
		<< ": " << type << " on " << url << endl;
#endif

	values.push_back(m_queueId);
	values.push_back(Url::escapeUrl(url));

	// Delete from ActionQueue
	SQLResults *results = executePreparedStatement("pop-item", values);
	if (results != NULL)
	{
		success = true;
		delete results;
	}

	return success;
}

bool ActionQueue::getOldestItem(ActionType &type, DocumentInfo &docInfo)
{
	vector<string> values;
	bool success = false;

	values.push_back(m_queueId);
	SQLResults *results = executePreparedStatement("select-oldest-url", values);
	if (results != NULL)
	{
		SQLRow *row = results->nextRow();
		if (row != NULL)
		{
			type = textToType(row->getColumn(0));
			success = true;

			// Deserialize DocumentInfo
			docInfo.deserialize(row->getColumn(1));

			delete row;
		}

		delete results;
	}

	return success;
}

/// Returns the number of items of a particular type.
unsigned int ActionQueue::getItemsCount(ActionType type)
{
	unsigned int count = 0;

	SQLResults *results = executeStatement("SELECT COUNT(*) FROM ActionQueue \
		WHERE Type='%q';",
		typeToText(type).c_str());
	if (results != NULL)
	{
		count = (unsigned int)results->getIntCount();

		delete results;
	}

	return count;
}

/// Deletes all items under a given URL.
bool ActionQueue::deleteItems(const string &url)
{
	bool success = false;

	if (beginTransaction() == false)
	{
		return false;
	}

	SQLResults *results = executeStatement("DELETE FROM ActionQueue \
		WHERE Url LIKE '%q%%';", Url::escapeUrl(url).c_str());
	if (results != NULL)
	{
		success = true;
		delete results;
	}

	if (endTransaction() == false)
	{
		return false;
	}

	return success;
}

/// Expires items older than the given date.
bool ActionQueue::expireItems(time_t expiryDate)
{
	vector<string> values;
	stringstream numStr;
	bool success = false;

	if (beginTransaction() == false)
	{
		return false;
	}

	values.push_back(m_queueId);
	numStr << expiryDate;
	values.push_back(numStr.str());

	SQLResults *results = executePreparedStatement("expire-items", values);
	if (results != NULL)
	{
		success = true;
		delete results;
	}

	if (endTransaction() == false)
	{
		return false;
	}

	return success;
}