-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWebSearchEngineExt.h
More file actions
414 lines (358 loc) · 12.5 KB
/
WebSearchEngineExt.h
File metadata and controls
414 lines (358 loc) · 12.5 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* Copyright (C) 2022-2026 Stefan-Mihai MOGA
This file is part of WebSearchEngine application developed by Stefan-Mihai MOGA.
WebSearchEngine is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Open
Source Initiative, either version 3 of the License, or any later version.
WebSearchEngine 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
WebSearchEngine. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>*/
/**
* @file WebSearchEngineExt.h
* @brief Declarations for URL frontier management, HTML processing, and database accessors
* for the WebSearchEngine application.
*/
#ifndef __WEBSEARCHENGINE_H__
#define __WEBSEARCHENGINE_H__
#pragma once
#include "stdafx.h"
#include "ODBCWrappers.h"
#include "WebSearchEngineDlg.h"
// Type aliases for core data structures used in the search engine
typedef std::vector<std::string> FrontierArray; ///< List of URLs (frontier)
typedef std::map<std::string, int> FrontierScore; ///< URL to score mapping
typedef std::map<std::wstring, __int64> WebpageIndex; ///< Webpage URL to ID mapping
typedef std::map<std::wstring, __int64> KeywordIndex; ///< Keyword to ID mapping
typedef std::vector<std::wstring> KeywordArray; ///< List of keywords
/**
* @brief Adds a new URL to the frontier if not already visited or present.
* @param lpszURL The URL to add.
* @return true if added or already present, false on error.
*/
bool AddURLToFrontier(const std::string& lpszURL);
/**
* @brief Extracts the next URL from the frontier, prioritizing by score.
* @param[out] lpszURL The extracted URL.
* @return true if a URL was extracted, false if the frontier is empty.
*/
bool ExtractURLFromFrontier(std::string& lpszURL);
/**
* @brief Downloads a web page from a URL and saves it to a temporary file.
* @param lpszURL The URL to download.
* @param[out] lpszFilename The path to the downloaded file.
* @return true if the download succeeded, false otherwise.
*/
bool DownloadURLToFile(const std::string& lpszURL, std::string& lpszFilename);
/**
* @brief Processes an HTML file: extracts title, hyperlinks, and plain text,
* updates the database with webpage and keyword information.
* @param pWebSearchEngineDlg Pointer to the main dialog for UI and database access.
* @param lpszFilename Path to the HTML file to process.
* @param lpszURL The URL of the processed page.
* @return true if processing succeeded, false otherwise.
*/
bool ProcessHTML(CWebSearchEngineDlg* pWebSearchEngineDlg, const std::string& lpszFilename, const std::string& lpszURL);
/**
* @class CGenericStatement
* @brief Executes a single SQL statement with no output.
*/
class CGenericStatement
{
public:
/**
* @brief Executes a SQL statement.
* @param pDbConnect Database connection.
* @param lpszSQL SQL statement to execute.
* @return true if successful, false otherwise.
*/
bool Execute(CODBC::CConnection& pDbConnect, LPCTSTR lpszSQL)
{
// Implementation in header for simplicity
CODBC::CStatement statement;
SQLRETURN nRet = statement.Create(pDbConnect);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Prepare(const_cast<SQLTCHAR*>(reinterpret_cast<const SQLTCHAR*>(lpszSQL)));
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Execute();
ODBC_CHECK_RETURN_FALSE(nRet, statement);
return true;
}
};
/**
* @class CWebpageInsertAccessor
* @brief Accessor for inserting a row into the WEBPAGE table.
*/
class CWebpageInsertAccessor
{
public:
TCHAR m_lpszURL[MAX_URL_LENGTH]; ///< Webpage URL
TCHAR m_lpszTitle[0x100]; ///< Webpage title
TCHAR m_lpszContent[0x10000]; ///< Webpage content
#pragma warning(suppress: 26429)
BEGIN_ODBC_PARAM_MAP(CWebpageInsertAccessor)
SET_ODBC_PARAM_TYPE(SQL_PARAM_INPUT)
#pragma warning(suppress: 26446 26485 26486 26489)
ODBC_PARAM_ENTRY(1, m_lpszURL)
ODBC_PARAM_ENTRY(2, m_lpszTitle)
ODBC_PARAM_ENTRY(3, m_lpszContent)
END_ODBC_PARAM_MAP()
DEFINE_ODBC_COMMAND(CWebpageInsertAccessor, _T("INSERT INTO `webpage` (`url`, `title`, `content`) VALUES (?, ?, ?);"))
/**
* @brief Clears all fields in the record.
*/
void ClearRecord() noexcept
{
memset(this, 0, sizeof(*this));
}
};
/**
* @class CWebpageInsert
* @brief Executes an INSERT statement for the WEBPAGE table.
*/
class CWebpageInsert : public CODBC::CAccessor<CWebpageInsertAccessor>
{
public:
/**
* @brief Inserts a webpage record into the database.
* @param pDbConnect Database connection.
* @param pURL Webpage URL.
* @param pTitle Webpage title.
* @param pContent Webpage content.
* @return true if successful, false otherwise.
*/
bool Execute(CODBC::CConnection& pDbConnect, const std::wstring& pURL, const std::wstring& pTitle, const std::wstring& pContent)
{
ClearRecord();
CODBC::CStatement statement;
SQLRETURN nRet = statement.Create(pDbConnect);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Prepare(GetDefaultCommand());
ODBC_CHECK_RETURN_FALSE(nRet, statement);
#pragma warning(suppress: 26485)
_tcscpy_s(m_lpszURL, _countof(m_lpszURL), pURL.c_str());
_tcscpy_s(m_lpszTitle, _countof(m_lpszTitle), pTitle.c_str());
_tcscpy_s(m_lpszContent, _countof(m_lpszContent), pContent.c_str());
nRet = BindParameters(statement);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Execute();
ODBC_CHECK_RETURN_FALSE(nRet, statement);
return true;
}
};
/**
* @class CKeywordInsertAccessor
* @brief Accessor for inserting a row into the KEYWORD table.
*/
class CKeywordInsertAccessor
{
public:
TCHAR m_lpszName[0x100]; ///< Keyword name
#pragma warning(suppress: 26429)
BEGIN_ODBC_PARAM_MAP(CKeywordInsertAccessor)
SET_ODBC_PARAM_TYPE(SQL_PARAM_INPUT)
#pragma warning(suppress: 26446 26485 26486 26489)
ODBC_PARAM_ENTRY(1, m_lpszName)
END_ODBC_PARAM_MAP()
DEFINE_ODBC_COMMAND(CKeywordInsertAccessor, _T("INSERT INTO `keyword` (`name`) VALUES (?);"))
void ClearRecord() noexcept
{
memset(this, 0, sizeof(*this));
}
};
/**
* @class CKeywordInsert
* @brief Executes an INSERT statement for the KEYWORD table.
*/
class CKeywordInsert : public CODBC::CAccessor<CKeywordInsertAccessor>
{
public:
/**
* @brief Inserts a keyword record into the database.
* @param pDbConnect Database connection.
* @param pKeyword Keyword to insert.
* @return true if successful, false otherwise.
*/
bool Execute(CODBC::CConnection& pDbConnect, const std::wstring& pKeyword)
{
ClearRecord();
CODBC::CStatement statement;
SQLRETURN nRet = statement.Create(pDbConnect);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Prepare(GetDefaultCommand());
ODBC_CHECK_RETURN_FALSE(nRet, statement);
#pragma warning(suppress: 26485)
_tcscpy_s(m_lpszName, _countof(m_lpszName), pKeyword.c_str());
nRet = BindParameters(statement);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Execute();
ODBC_CHECK_RETURN_FALSE(nRet, statement);
return true;
}
};
/**
* @class COccurrenceInsertAccessor
* @brief Accessor for inserting a row into the OCCURRENCE table.
*/
class COccurrenceInsertAccessor
{
public:
__int64 m_nWebpageID; ///< Webpage ID
__int64 m_nKeywordID; ///< Keyword ID
__int64 m_nCounter; ///< Occurrence count
double m_rPageRank; ///< PageRank value
#pragma warning(suppress: 26429)
BEGIN_ODBC_PARAM_MAP(COccurrenceInsertAccessor)
SET_ODBC_PARAM_TYPE(SQL_PARAM_INPUT)
#pragma warning(suppress: 26446 26485 26486 26489)
ODBC_PARAM_ENTRY(1, m_nWebpageID)
ODBC_PARAM_ENTRY(2, m_nKeywordID)
ODBC_PARAM_ENTRY(3, m_nCounter)
ODBC_PARAM_ENTRY(4, m_rPageRank)
END_ODBC_PARAM_MAP()
DEFINE_ODBC_COMMAND(COccurrenceInsertAccessor, _T("INSERT INTO `occurrence` (`webpage_id`, `keyword_id`, `counter`, `pagerank`) VALUES (?, ?, ?, ?);"))
void ClearRecord() noexcept
{
memset(this, 0, sizeof(*this));
}
};
/**
* @class COccurrenceInsert
* @brief Executes an INSERT statement for the OCCURRENCE table.
*/
class COccurrenceInsert : public CODBC::CAccessor<COccurrenceInsertAccessor>
{
public:
/**
* @brief Inserts an occurrence record into the database.
* @param pDbConnect Database connection.
* @param nWebpageID Webpage ID.
* @param nKeywordID Keyword ID.
* @param nCounter Occurrence count.
* @return true if successful, false otherwise.
*/
bool Execute(CODBC::CConnection& pDbConnect, const __int64& nWebpageID, const __int64& nKeywordID, const __int64& nCounter)
{
ClearRecord();
CODBC::CStatement statement;
SQLRETURN nRet = statement.Create(pDbConnect);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Prepare(GetDefaultCommand());
ODBC_CHECK_RETURN_FALSE(nRet, statement);
#pragma warning(suppress: 26485)
m_nWebpageID = nWebpageID;
m_nKeywordID = nKeywordID;
m_nCounter = nCounter;
m_rPageRank = 0.0;
nRet = BindParameters(statement);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Execute();
ODBC_CHECK_RETURN_FALSE(nRet, statement);
return true;
}
};
/**
* @class COccurrenceUpdateAccessor
* @brief Accessor for updating a row in the OCCURRENCE table.
*/
class COccurrenceUpdateAccessor
{
public:
__int64 m_nWebpageID; ///< Webpage ID
__int64 m_nKeywordID; ///< Keyword ID
#pragma warning(suppress: 26429)
BEGIN_ODBC_PARAM_MAP(COccurrenceUpdateAccessor)
SET_ODBC_PARAM_TYPE(SQL_PARAM_INPUT)
#pragma warning(suppress: 26446 26485 26486 26489)
ODBC_PARAM_ENTRY(1, m_nWebpageID)
ODBC_PARAM_ENTRY(2, m_nKeywordID)
END_ODBC_PARAM_MAP()
DEFINE_ODBC_COMMAND(COccurrenceUpdateAccessor, _T("UPDATE `occurrence` SET `counter` = `counter` + 1 WHERE `webpage_id` = ? AND `keyword_id` = ?;"))
void ClearRecord() noexcept
{
memset(this, 0, sizeof(*this));
}
};
/**
* @class COccurrenceUpdate
* @brief Executes an UPDATE statement for the OCCURRENCE table.
*/
class COccurrenceUpdate : public CODBC::CAccessor<COccurrenceUpdateAccessor>
{
public:
/**
* @brief Updates the occurrence counter for a webpage-keyword pair.
* @param pDbConnect Database connection.
* @param nWebpageID Webpage ID.
* @param nKeywordID Keyword ID.
* @return true if successful, false otherwise.
*/
bool Execute(CODBC::CConnection& pDbConnect, const __int64& nWebpageID, const __int64& nKeywordID)
{
ClearRecord();
CODBC::CStatement statement;
SQLRETURN nRet = statement.Create(pDbConnect);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Prepare(GetDefaultCommand());
ODBC_CHECK_RETURN_FALSE(nRet, statement);
#pragma warning(suppress: 26485)
m_nWebpageID = nWebpageID;
m_nKeywordID = nKeywordID;
nRet = BindParameters(statement);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Execute();
ODBC_CHECK_RETURN_FALSE(nRet, statement);
return true;
}
};
/**
* @class CDataMiningUpdateAccessor
* @brief Accessor for applying data mining updates to the OCCURRENCE table.
*/
class CDataMiningUpdateAccessor
{
public:
TCHAR m_lpszName[0x100]; ///< Keyword name
#pragma warning(suppress: 26429)
BEGIN_ODBC_PARAM_MAP(CDataMiningUpdateAccessor)
SET_ODBC_PARAM_TYPE(SQL_PARAM_INPUT)
#pragma warning(suppress: 26446 26485 26486 26489)
ODBC_PARAM_ENTRY(1, m_lpszName)
END_ODBC_PARAM_MAP()
DEFINE_ODBC_COMMAND(CDataMiningUpdateAccessor, _T("UPDATE `occurrence` INNER JOIN `keyword` USING(`keyword_id`) SET `pagerank` = data_mining(`webpage_id`, `name`) WHERE `name` = ?;"))
void ClearRecord() noexcept
{
memset(this, 0, sizeof(*this));
}
};
/**
* @class CDataMiningUpdate
* @brief Executes a data mining UPDATE statement for the OCCURRENCE table.
*/
class CDataMiningUpdate : public CODBC::CAccessor<CDataMiningUpdateAccessor>
{
public:
/**
* @brief Applies data mining update for a given keyword.
* @param pDbConnect Database connection.
* @param pKeyword Keyword to update.
* @return true if successful, false otherwise.
*/
bool Execute(CODBC::CConnection& pDbConnect, const std::wstring& pKeyword)
{
ClearRecord();
CODBC::CStatement statement;
SQLRETURN nRet = statement.Create(pDbConnect);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Prepare(GetDefaultCommand());
ODBC_CHECK_RETURN_FALSE(nRet, statement);
#pragma warning(suppress: 26485)
_tcscpy_s(m_lpszName, _countof(m_lpszName), pKeyword.c_str());
nRet = BindParameters(statement);
ODBC_CHECK_RETURN_FALSE(nRet, statement);
nRet = statement.Execute();
ODBC_CHECK_RETURN_FALSE(nRet, statement);
return true;
}
};
#endif