libept
popcon.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*-
2 #ifndef EPT_POPCON_POPCON_H
3 #define EPT_POPCON_POPCON_H
4 
10 /*
11  * Copyright (C) 2007 Enrico Zini <enrico@debian.org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  */
27 
28 #include <tagcoll/diskindex/mmap.h>
29 #include <string>
30 #include <time.h>
31 
32 namespace ept {
33 namespace apt {
34 class Apt;
35 }
36 
37 namespace popcon {
38 
44 class Score
45 {
46 protected:
47  unsigned offset;
48 
49 public:
50  float score;
51 
52  Score(float score) : offset(offset), score(score) {}
53 
54  friend class Popcon;
55  friend class PopconIndexer;
56  friend class PopconGenerator;
57 };
58 
73 class Popcon : public tagcoll::diskindex::MMap
74 {
75  struct GeneralInfo : public tagcoll::diskindex::MMap
76  {
77  size_t submissions() const;
78  };
79 
80  tagcoll::diskindex::MasterMMap mastermmap;
81  time_t m_timestamp;
82 
83  GeneralInfo m_info;
84 
86  const Score* structByIndex(size_t idx) const
87  {
88  if (idx >= 0 && idx < size())
89  return (Score*)m_buf + idx;
90  return 0;
91  }
92 
93 public:
94  Popcon();
95 
97  time_t timestamp() const { return m_timestamp; }
98 
100  bool hasData() const { return m_timestamp != 0; }
101 
103  size_t submissions() const { return m_info.submissions(); }
104 
106  size_t size() const
107  {
108  if (m_buf)
109  return ((Score*)m_buf)->offset / sizeof(Score);
110  else
111  return 0;
112  }
113 
119  std::string name(size_t idx) const
120  {
121  const Score* s = structByIndex(idx);
122  if (s == 0) return std::string();
123  return std::string(m_buf + s->offset);
124  }
125 
127  float scoreByIndex(size_t idx) const
128  {
129  const Score* s = structByIndex(idx);
130  if (!s) return 0;
131  return s->score;
132  }
133 
135  float scoreByName(const std::string& name) const;
136 
138  float score(size_t idx) const { return scoreByIndex(idx); }
139 
141  float operator[](int idx) const { return scoreByIndex(idx); }
142 
144  float score(const std::string& name) const { return scoreByName(name); }
145 
147  float operator[](const std::string& name) const { return scoreByName(name); }
148 };
149 
150 }
151 }
152 
153 // vim:set ts=4 sw=4:
154 #endif
float operator[](const std::string &name) const
Get the score structure by package name.
Definition: popcon.h:147
Definition: popconindexer.h:38
size_t submissions() const
Return the total number of popcon submissions.
Definition: popcon.h:103
Maps Packages to IDs and vice-versa.
Definition: popcon.h:73
size_t size() const
Get the number of packages in the index.
Definition: popcon.h:106
float scoreByName(const std::string &name) const
Get the score structure by package name.
Definition: popcon.cc:69
Definition: apt.cc:42
float scoreByIndex(size_t idx) const
Get the score by index.
Definition: popcon.h:127
Popcon()
Definition: popcon.cc:43
Score(float score)
Definition: popcon.h:52
bool hasData() const
Return true if this data source has data, false if it's empty.
Definition: popcon.h:100
float score
Definition: popcon.h:50
float score(size_t idx) const
Get the score by index.
Definition: popcon.h:138
float operator[](int idx) const
Get the score by index.
Definition: popcon.h:141
time_t timestamp() const
Get the timestamp of when the index was last updated.
Definition: popcon.h:97
unsigned offset
Definition: popcon.h:47
std::string name(size_t idx) const
Get a package name by index.
Definition: popcon.h:119
Store the score information in the popcon cache.
Definition: popcon.h:44
friend class PopconGenerator
Definition: popcon.h:56
float score(const std::string &name) const
Get the score by name.
Definition: popcon.h:144