-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_format.h
122 lines (98 loc) · 3.45 KB
/
db_format.h
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
#ifndef PKGDEPDB_DB_FORMAT_H__
#define PKGDEPDB_DB_FORMAT_H__
namespace pkgdepdb {
using PkgOutMap = std::map<Package*, size_t>;
using ObjOutMap = std::map<Elf*, size_t>;
using PkgInMap = std::map<size_t, Package*>;
using ObjInMap = std::map<size_t, Elf*>;
class SerialStream {
public:
virtual ~SerialStream() {}
virtual ssize_t Write(const void *buf, size_t bytes) = 0;
virtual ssize_t Read (void *buf, size_t bytes) = 0;
virtual size_t TellP() const = 0;
virtual size_t TellG() const = 0;
virtual operator bool() const = 0;
enum InOut {
in, out
};
};
class SerialIn {
public:
DB *db_;
SerialStream &in_;
std::unique_ptr<SerialStream> in_owning_;
PkgInMap old_pkgref_;
ObjInMap old_objref_;
vec<Elf*> objref_;
vec<Package*> pkgref_;
// whether objref and pkgref are used
bool ver8_refs_ = false;
uint16_t version_ = 0;
private:
SerialIn(DB*, SerialStream*);
public:
static SerialIn* Open(DB *db, const string& file, bool gz);
};
class SerialOut {
public:
DB *db_;
SerialStream &out_;
std::unique_ptr<SerialStream> out_owning_;
std::map<const Elf*, size_t> objref_;
std::map<const Package*,size_t> pkgref_;
bool GetObjRef(const Elf*, size_t *out);
bool GetPkgRef(const Package*, size_t *out);
uint16_t version_ = 0;
private:
SerialOut(DB*, SerialStream*);
public:
static SerialOut* Open(DB *db, const string& file, bool gz);
};
template<typename T>
static inline SerialOut& operator<=(SerialOut &out, const T& r) {
out.out_.Write((const char*)&r, sizeof(r));
return out;
}
template<typename T>
static inline SerialIn& operator>=(SerialIn &in, T& r) {
in.in_.Read((char*)&r, sizeof(r));
return in;
}
// special for strings:
static inline SerialOut& operator<=(SerialOut &out, const string& r) {
auto len = static_cast<uint32_t>(r.length());
out.out_.Write((const char*)&len, sizeof(len));
out.out_.Write(r.c_str(), len);
return out;
}
static inline SerialIn& operator>=(SerialIn &in, string& r) {
uint32_t len;
in >= len;
r.resize(len);
in.in_.Read(&r[0], len);
return in;
}
// and for string tuples
static inline
SerialOut& operator<=(SerialOut &out, const tuple<string,string>& r) {
return (out <= std::get<0>(r)) <= std::get<1>(r);
}
static inline SerialIn& operator>=(SerialIn &in, tuple<string,string>& r) {
return (in >= std::get<0>(r)) >= std::get<1>(r);
}
bool write_objlist (SerialOut &out, const ObjectList &list);
bool read_objlist (SerialIn &in, ObjectList &list, const Config&);
bool write_objset (SerialOut &out, const ObjectSet &list);
bool read_objset (SerialIn &in, ObjectSet &list, const Config&);
bool write_stringlist(SerialOut &out, const vec<string> &list);
bool read_stringlist (SerialIn &in, vec<string> &list);
bool write_stringset (SerialOut &out, const StringSet &list);
bool read_stringset (SerialIn &in, StringSet &list);
bool write_dependlist(SerialOut &out, const vec<tuple<string,string>> &list);
bool read_dependlist (SerialIn &in, vec<tuple<string,string>> &list);
// backward compat
bool write_olddependlist(SerialOut &out, const vec<tuple<string,string>>&);
bool read_olddependlist (SerialIn &in, vec<tuple<string,string>>&);
} // ::pkgdepdb
#endif