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
|
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*-
#ifndef EPT_POPCON_POPCON_H
#define EPT_POPCON_POPCON_H
/** @file
* @author Enrico Zini <enrico@enricozini.org>
* Access popcon data
*/
/*
* Copyright (C) 2007 Enrico Zini <enrico@debian.org>
*
* 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 <tagcoll/diskindex/mmap.h>
#include <string>
namespace ept {
namespace apt {
class Apt;
}
namespace popcon {
/**
* Store the score information in the popcon cache.
*
* Currently, this is only one float; more can be added in the future.
*/
class Score
{
protected:
unsigned offset;
public:
float score;
Score(float score) : offset(offset), score(score) {}
friend class Popcon;
friend class PopconIndexer;
friend class PopconGenerator;
};
/**
* Maps Packages to IDs and vice-versa.
*
* This is used in building the Debtags fast index, which works representing
* tags and packages as int IDs.
*
* Index building works like this:
* 1. The file all-popcon-results.txt.gz is downloaded from
* http://popcon.debian.org/all-popcon-results.txt.gz
* 2. The file is put in either ~/.popcon/all-popcon-results.txt.gz
* or in /var/lib/popcon/all-popcon-results.txt.gz
* 3. If the file is newer than the index, it will be automatically used to
* recompute the scores and rebuild the index.
*/
class Popcon : public tagcoll::diskindex::MMap
{
struct GeneralInfo : public tagcoll::diskindex::MMap
{
size_t submissions() const;
};
tagcoll::diskindex::MasterMMap mastermmap;
time_t m_timestamp;
GeneralInfo m_info;
/// Get the score structure by index
const Score* structByIndex(size_t idx) const
{
if (idx >= 0 && idx < size())
return (Score*)m_buf + idx;
return 0;
}
public:
Popcon();
/// Get the timestamp of when the index was last updated
time_t timestamp() const { return m_timestamp; }
/// Return true if this data source has data, false if it's empty
bool hasData() const { return m_timestamp != 0; }
/// Return the total number of popcon submissions
size_t submissions() const { return m_info.submissions(); }
/// Get the number of packages in the index
size_t size() const
{
if (m_buf)
return ((Score*)m_buf)->offset / sizeof(Score);
else
return 0;
}
/**
* Get a package name by index
*
* If the index is not valid, returns the empty string.
*/
std::string name(size_t idx) const
{
const Score* s = structByIndex(idx);
if (s == 0) return std::string();
return std::string(m_buf + s->offset);
}
/// Get the score by index
float scoreByIndex(size_t idx) const
{
const Score* s = structByIndex(idx);
if (!s) return 0;
return s->score;
}
/// Get the score structure by package name
float scoreByName(const std::string& name) const;
/// Get the score by index
float score(size_t idx) const { return scoreByIndex(idx); }
/// Get the score by index
float operator[](int idx) const { return scoreByIndex(idx); }
/// Get the score by name
float score(const std::string& name) const { return scoreByName(name); }
/// Get the score structure by package name
float operator[](const std::string& name) const { return scoreByName(name); }
};
}
}
// vim:set ts=4 sw=4:
#endif
|