1 : // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*-
2 : #ifndef EPT_DEBTAGS_PKGID_H
3 : #define EPT_DEBTAGS_PKGID_H
4 :
5 : /** @file
6 : * @author Enrico Zini <enrico@enricozini.org>
7 : * Quick map from package IDs to package names
8 : */
9 :
10 : /*
11 : * Copyright (C) 2003-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 :
31 : namespace ept {
32 : namespace debtags {
33 :
34 : /**
35 : * Maps Packages to IDs and vice-versa.
36 : *
37 : * This is used in building the Debtags fast index, which works representing
38 : * tags and packages as int IDs
39 : */
40 : class PkgId : public tagcoll::diskindex::MMap
41 11 : {
42 : tagcoll::diskindex::MasterMMap mastermmap;
43 : time_t m_timestamp;
44 :
45 : public:
46 : PkgId();
47 : PkgId(const tagcoll::diskindex::MasterMMap& master, size_t idx);
48 : PkgId(const char* buf, int size);
49 :
50 : /// Get the timestamp of when the index was last updated
51 : time_t timestamp() const { return m_timestamp; }
52 :
53 : /// Get the number of packages in the index
54 63445 : size_t size() const { return m_buf ? *(int*)m_buf / sizeof(int) : 0; }
55 :
56 : /**
57 : * Get the ID of a package given its name.
58 : *
59 : * If not found, returns -1.
60 : */
61 : int byName(const std::string& name) const;
62 :
63 : /**
64 : * Get a package name given its ID.
65 : *
66 : * If not found, returns the empty string.
67 : */
68 1128365 : std::string byID(int id) const
69 : {
70 1128365 : if (id >= 0 || static_cast<unsigned>(id) < size())
71 1128365 : return std::string(m_buf + ((int*)m_buf)[id]);
72 0 : return std::string();
73 : }
74 :
75 : /// Get the number of packages in the index
76 : int size(int id) const
77 : {
78 : if (id < 0 || (unsigned)id >= size())
79 : return 0;
80 : if ((unsigned)id == size() - 1)
81 : return m_size - ((int*)m_buf)[id] - 1;
82 : else
83 : return ((int*)m_buf)[id + 1] - ((int*)m_buf)[id] - 1;
84 : }
85 : };
86 :
87 : }
88 : }
89 :
90 : // vim:set ts=4 sw=4:
91 : #endif
|