summaryrefslogtreecommitdiff
path: root/ept/popcon/maint/popconindexer.cc
blob: a8dea69417d75764b020ceb44ff0a2e4f242941c (plain)
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
#include <ept/popcon/popcon.h>
#include <ept/popcon/maint/popconindexer.h>
#include <ept/popcon/maint/path.h>

#include <wibble/exception.h>
#include <wibble/sys/fs.h>

#include <tagcoll/diskindex/mmap.h>

#include <unistd.h>
#include <set>
#include <string>
#include <cstdio>
#include <cstring>

using namespace std;

namespace ept {
namespace popcon {

template<typename STRUCT>
struct StructIndexer : public tagcoll::diskindex::MMapIndexer
{
	const STRUCT& data;
	StructIndexer(const STRUCT& data) : data(data) {}

	int encodedSize() const { return sizeof(STRUCT); }
	void encode(char* buf) const { *(STRUCT*)buf = data; }
};

/// MMapIndexer that indexes the package names
struct PopconGenerator : public tagcoll::diskindex::MMapIndexer
{
	// Sorted set of all available package names and data
	std::map<std::string, Score> data;

	int encodedSize() const
	{
		int size = data.size() * sizeof(Score);
		for (std::map<std::string, Score>::const_iterator i = data.begin();
				i != data.end(); ++i)
			size += i->first.size() + 1;
		return tagcoll::diskindex::MMap::align(size);
	}

	void encode(char* buf) const
	{
		int pos = data.size() * sizeof(Score);
		int idx = 0;
		for (std::map<std::string, Score>::const_iterator i = data.begin();
				i != data.end(); ++i)
		{
			((Score*)buf)[idx] = i->second;
			((Score*)buf)[idx].offset = pos;
			memcpy(buf + pos, i->first.c_str(), i->first.size() + 1);
			pos += i->first.size() + 1;
			++idx;
		}
	}
};


PopconIndexer::PopconIndexer()
	: mainSource(Path::popconSourceDir()),
	  userSource(Path::popconUserSourceDir())
{
	rescan();
}

void PopconIndexer::rescan()
{
	ts_main_src = mainSource.timestamp();
	ts_user_src = userSource.timestamp();
	ts_main_sco = Path::timestamp(Path::scores());
	ts_user_sco = Path::timestamp(Path::userScores());
	ts_main_idx = Path::timestamp(Path::scoresIndex());
	ts_user_idx = Path::timestamp(Path::userScoresIndex());
}

bool PopconIndexer::needsRebuild() const
{
	// If there are no indexes of any kind, then we need rebuilding
	if (ts_user_sco == 0 || ts_main_sco == 0 || ts_user_idx == 0 && ts_main_idx == 0)
		return true;

	// If the user index is ok, then we are fine
	if (ts_user_sco >= sourceTimestamp() && ts_user_idx >= sourceTimestamp())
		return false;

	// If there are user sources, then we cannot use the system index
	if (ts_user_src > 0)
		return true;

	// If there are no user sources, then we can fallback on the system
	// indexes in case the user indexes are not up to date
	if (ts_main_sco >= sourceTimestamp() && ts_main_idx >= sourceTimestamp())
		return false;

	return true;
}

bool PopconIndexer::userIndexIsRedundant() const
{
	// If there is no user index, then it is not redundant
	if (ts_user_idx == 0)
		return false;

	// If the system index is not up to date, then the user index is not
	// redundant
	if (ts_main_idx < sourceTimestamp())
		return false;

	return true;
}

bool PopconIndexer::rebuild(const std::string& scofname, const std::string& idxfname)
{
	PopconGenerator gen;
	InfoStruct is;
	is.submissions = 0;
	if (!mainSource.readScores(gen.data, is.submissions))
		userSource.readScores(gen.data, is.submissions);
	if (gen.data.empty())
		return false;

	StructIndexer<InfoStruct> infoStruct(is);

	// Create the index
	tagcoll::diskindex::MasterMMapIndexer master(idxfname);
	master.append(gen);
	master.append(infoStruct);
	master.commit();

//	for (map<string, Score>::const_iterator i = gen.data.begin(); i != gen.data.end(); ++i)
//	{
//		fprintf(stderr, "%s   %d   %f\n", i->first.c_str(), i->second.offset, i->second.score);
//	}

	// Create the score file
	FILE* out = fopen(scofname.c_str(), "wt");
	if (out == NULL)
		throw wibble::exception::File(scofname, "opening and truncating file for writing");
	for (map<string, Score>::const_iterator i = gen.data.begin();
			i != gen.data.end(); ++i)
	{
		fprintf(out, "%s %f\n", i->first.c_str(), i->second.score);
	}
	fclose(out);
	return true;
}

bool PopconIndexer::rebuildIfNeeded()
{
	if (needsRebuild())
	{
		// Decide if we rebuild the user index or the system index
		if (Path::access(Path::popconIndexDir(), W_OK) == 0)
		{
			// Since we can write on the system index directory, we rebuild
			// the system index
			if (!rebuild(Path::scores(), Path::scoresIndex()))
				return false;
			ts_main_sco = Path::timestamp(Path::scores());
			ts_main_idx = Path::timestamp(Path::scoresIndex());
			if (Path::scores() == Path::userScores())
				ts_user_sco = ts_main_sco;
			if (Path::scoresIndex() == Path::userScoresIndex())
				ts_user_idx = ts_main_idx;
		} else {
			wibble::sys::fs::mkFilePath(Path::userScores());
			wibble::sys::fs::mkFilePath(Path::userScoresIndex());
			if (!rebuild(Path::userScores(), Path::userScoresIndex()))
				return false;
			ts_user_sco = Path::timestamp(Path::userScores());
			ts_user_idx = Path::timestamp(Path::userScoresIndex());
		}
		return true;
	}
	return false;
}

bool PopconIndexer::deleteRedundantUserIndex()
{
	if (userIndexIsRedundant())
	{
		// Delete the user indexes if they exist
		if (Path::scores() != Path::userScores())
		{
			unlink(Path::userScores().c_str());
			ts_user_sco = 0;
		}
		if (Path::scoresIndex() != Path::userScoresIndex())
		{
			unlink(Path::userScoresIndex().c_str());
			ts_user_idx = 0;
		}
		return true;
	}
	return false;
}

bool PopconIndexer::getUpToDatePopcon(std::string& scofname, std::string& idxfname)
{
	// If there are no indexes of any kind, then we have nothing to return
	if (ts_user_sco == 0 && ts_main_sco == 0 && ts_user_idx == 0 && ts_main_idx == 0)
		return false;

	// If the user index is up to date, use it
	if (ts_user_sco >= sourceTimestamp() &&
	    ts_user_idx >= sourceTimestamp())
	{
		scofname = Path::userScores();
		idxfname = Path::userScoresIndex();
		return true;
	}

	// If the user index is not up to date and we have user sources, we cannot
	// fall back to the system index
	if (ts_user_src != 0)
		return false;

	// Fallback to the system index
	if (ts_main_sco >= sourceTimestamp() &&
	    ts_main_idx >= sourceTimestamp())
	{
		scofname = Path::scores();
		idxfname = Path::scoresIndex();
		return true;
	}

	return false;
}


bool PopconIndexer::obtainWorkingPopcon(std::string& scofname, std::string& idxfname)
{
	PopconIndexer indexer;

	indexer.rebuildIfNeeded();
	indexer.deleteRedundantUserIndex();
	return indexer.getUpToDatePopcon(scofname, idxfname);
}


}
}

// vim:set ts=4 sw=4: