File: DocumentInfo.cpp

package info (click to toggle)
pinot 0.85-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 5,524 kB
  • ctags: 3,868
  • sloc: cpp: 33,107; sh: 8,801; ansic: 3,049; makefile: 557; xml: 366; python: 250
file content (341 lines) | stat: -rw-r--r-- 8,028 bytes parent folder | download
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
/*
 *  Copyright 2005,2006 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 Library 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 <stdlib.h>
#include <algorithm>

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

using std::string;
using std::map;
using std::set;
using std::copy;
using std::inserter;

DocumentInfo::DocumentInfo() :
	m_score(0.0),
	m_indexId(0),
	m_docId(0)
{
	setField("modtime", TimeConverter::toTimestamp(time(NULL)));
}

DocumentInfo::DocumentInfo(const string &title, const string &location,
	const string &type, const string &language) :
	m_score(0.0),
	m_indexId(0),
	m_docId(0)
{
	setField("caption", title);
	setField("url", location);
	setField("type", type);
	setField("language", language);
	setField("modtime", TimeConverter::toTimestamp(time(NULL)));
}

DocumentInfo::DocumentInfo(const DocumentInfo &other) :
	m_extract(other.m_extract),
	m_score(other.m_score),
	m_indexId(other.m_indexId),
	m_docId(other.m_docId)
{
	copy(other.m_fields.begin(), other.m_fields.end(),
		inserter(m_fields, m_fields.begin()));
	copy(other.m_labels.begin(), other.m_labels.end(),
		inserter(m_labels, m_labels.begin()));
}

DocumentInfo::~DocumentInfo()
{
}

DocumentInfo& DocumentInfo::operator=(const DocumentInfo& other)
{
	if (this != &other)
	{
		m_fields.clear();
		copy(other.m_fields.begin(), other.m_fields.end(),
			inserter(m_fields, m_fields.begin()));
		m_extract = other.m_extract;
		m_score = other.m_score;
		m_labels.clear();
		copy(other.m_labels.begin(), other.m_labels.end(),
			inserter(m_labels, m_labels.begin()));
		m_indexId = other.m_indexId;
		m_docId = other.m_docId;
	}

	return *this;
}

bool DocumentInfo::operator<(const DocumentInfo& other) const
{
	if (getField("url") < other.getField("url"))
	{
		return true;
	}

	return false;
}

/// Serializes the document.
string DocumentInfo::serialize(void) const
{
	string info;
	char numStr[64];

	// Serialize DocumentInfo into a string
	for (map<string, string>::const_iterator fieldIter = m_fields.begin();
		fieldIter != m_fields.end(); ++fieldIter)
	{
		info += "\n";
		info += fieldIter->first;
		info += "=";
		info += fieldIter->second;
	}
	info += "\nlabels=";
	for (set<string>::const_iterator labelIter = m_labels.begin();
		labelIter != m_labels.end(); ++labelIter)
	{
		info += "[" + Url::escapeUrl(*labelIter) + "]";
	}
	info += "\nextract=";
	info += m_extract;
	info += "\nscore=";
	snprintf(numStr, 64, "%f", m_score);
	info += numStr;
	info += "\nindexid=";
	snprintf(numStr, 64, "%u", m_indexId);
	info += numStr;
	info += "\ndocid=";
	snprintf(numStr, 64, "%u", m_docId);
	info += numStr;
	info += "\n";

	return Url::escapeUrl(info);
}

/// Deserializes the document.
void DocumentInfo::deserialize(const string &info)
{
	string unescapedInfo(Url::unescapeUrl(info));

	// Deserialize DocumentInfo
	setField("caption", StringManip::extractField(unescapedInfo, "caption=", "\n"));
	setField("url", StringManip::extractField(unescapedInfo, "url=", "\n"));
	setField("type", StringManip::extractField(unescapedInfo, "type=", "\n"));
	setField("language", StringManip::extractField(unescapedInfo, "language=", "\n"));
	setField("modtime", StringManip::extractField(unescapedInfo, "modtime=", "\n"));
	setField("size", StringManip::extractField(unescapedInfo, "size=", "\n"));
	string labelsString(StringManip::extractField(unescapedInfo, "labels=", "\n"));
	if (labelsString.empty() == false)
	{
		string::size_type endPos = 0;
		string labelName(StringManip::extractField(labelsString, "[", "]", endPos));

		m_labels.clear();

		// Parse labels
		while (labelName.empty() == false)
		{
			m_labels.insert(Url::unescapeUrl(labelName));

			if (endPos == string::npos)
			{
				break;
			}
			labelName = StringManip::extractField(labelsString, "[", "]", endPos);
		}
	}
	m_extract = StringManip::extractField(unescapedInfo, "extract=", "\n");
	m_score = (float)atof(StringManip::extractField(unescapedInfo, "score=", "\n").c_str());
	m_indexId = (unsigned int)atoi(StringManip::extractField(unescapedInfo, "indexid=", "\n").c_str());
	m_docId = (unsigned int)atoi(StringManip::extractField(unescapedInfo, "docid=", "\n").c_str());
}

/// Sets the title of the document.
void DocumentInfo::setTitle(const string &title)
{
	setField("caption", title);
}

/// Returns the title of the document.
string DocumentInfo::getTitle(void) const
{
	return getField("caption");
}

/// Sets the original location of the document.
void DocumentInfo::setLocation(const string &location)
{
	setField("url", location);
}

/// Returns the original location of the document.
string DocumentInfo::getLocation(void) const
{
	return getField("url");
}

/// Sets the type of the document.
void DocumentInfo::setType(const string &type)
{
	setField("type", type);
}

/// Returns the type of the document.
string DocumentInfo::getType(void) const
{
	return getField("type");
}

/// Sets the language of the document.
void DocumentInfo::setLanguage(const string &language)
{
	setField("language", language);
}

/// Returns the document's language.
string DocumentInfo::getLanguage(void) const
{
	return getField("language");
}

/// Sets the document's timestamp.
void DocumentInfo::setTimestamp(const string &timestamp)
{
	setField("modtime", timestamp);
}

/// Returns the document's timestamp.
string DocumentInfo::getTimestamp(void) const
{
	return getField("modtime");
}

/// Sets the document's size in bytes.
void DocumentInfo::setSize(off_t size)
{
	char sizeStr[64];

	snprintf(sizeStr, 64, "%u", size);
	setField("size", sizeStr);
}

/// Returns the document's size in bytes.
off_t DocumentInfo::getSize(void) const
{
	string sizeStr(getField("size"));

	if (sizeStr.empty() == true)
	{
		return 0;
	}

	return (off_t)atoi(sizeStr.c_str());
}

/// Sets the document's extract.
void DocumentInfo::setExtract(const string &extract)
{
	m_extract = extract;
}

/// Returns the document's extract.
string DocumentInfo::getExtract(void) const
{
	return m_extract;
}

/// Sets the document's score.
void DocumentInfo::setScore(float score)
{
	m_score = score;
}

/// Returns the document's score.
float DocumentInfo::getScore(void) const
{
	return m_score;
}

/// Sets the document's labels.
void DocumentInfo::setLabels(const set<string> &labels)
{
	copy(labels.begin(), labels.end(),
		inserter(m_labels, m_labels.begin()));
}

/// Returns the document's labels.
const set<string> &DocumentInfo::getLabels(void) const
{
	return m_labels;
}

/// Sets that the document is indexed.
void DocumentInfo::setIsIndexed(unsigned int indexId, unsigned int docId)
{
	m_indexId = indexId;
	m_docId = docId;
}

/// Sets that the document is not indexed.
void DocumentInfo::setIsNotIndexed(void)
{
	m_indexId = 0;
	m_docId = 0;
}

/// Gets whether the document is indexed.
bool DocumentInfo::getIsIndexed(void) const
{
	if (m_docId > 0)
	{
		return true;
	}

	return false;
}

/// Gets whether the documentt is indexed.
unsigned int DocumentInfo::getIsIndexed(unsigned int &indexId) const
{
	indexId = m_indexId;

	return m_docId;
}

void DocumentInfo::setField(const string &name, const string &value)
{
	m_fields[name] = value;
}

string DocumentInfo::getField(const string &name) const
{
	map<string, string>::const_iterator fieldIter = m_fields.find(name);
	if (fieldIter != m_fields.end())
	{
		return fieldIter->second;
	}

	return "";
}