]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #ifndef CEPH_OBJECT_H | |
16 | #define CEPH_OBJECT_H | |
17 | ||
9f95a23c TL |
18 | #include <cstdint> |
19 | #include <cstdio> | |
7c673cae | 20 | #include <iomanip> |
f67539c2 | 21 | #include <iosfwd> |
9f95a23c | 22 | #include <string> |
f67539c2 TL |
23 | #include <string> |
24 | #include <string_view> | |
7c673cae | 25 | |
11fdf7f2 | 26 | #include "include/rados.h" |
7c673cae FG |
27 | #include "include/unordered_map.h" |
28 | ||
29 | #include "hash.h" | |
30 | #include "encoding.h" | |
31 | #include "ceph_hash.h" | |
32 | #include "cmp.h" | |
33 | ||
11fdf7f2 TL |
34 | using namespace std; |
35 | ||
7c673cae | 36 | struct object_t { |
9f95a23c | 37 | std::string name; |
7c673cae FG |
38 | |
39 | object_t() {} | |
40 | // cppcheck-suppress noExplicitConstructor | |
41 | object_t(const char *s) : name(s) {} | |
42 | // cppcheck-suppress noExplicitConstructor | |
9f95a23c | 43 | object_t(const std::string& s) : name(s) {} |
f67539c2 TL |
44 | object_t(std::string&& s) : name(std::move(s)) {} |
45 | object_t(std::string_view s) : name(s) {} | |
7c673cae FG |
46 | |
47 | void swap(object_t& o) { | |
48 | name.swap(o.name); | |
49 | } | |
50 | void clear() { | |
51 | name.clear(); | |
52 | } | |
9f95a23c TL |
53 | |
54 | void encode(ceph::buffer::list &bl) const { | |
11fdf7f2 TL |
55 | using ceph::encode; |
56 | encode(name, bl); | |
7c673cae | 57 | } |
9f95a23c | 58 | void decode(ceph::buffer::list::const_iterator &bl) { |
11fdf7f2 TL |
59 | using ceph::decode; |
60 | decode(name, bl); | |
7c673cae FG |
61 | } |
62 | }; | |
63 | WRITE_CLASS_ENCODER(object_t) | |
64 | ||
65 | inline bool operator==(const object_t& l, const object_t& r) { | |
66 | return l.name == r.name; | |
67 | } | |
68 | inline bool operator!=(const object_t& l, const object_t& r) { | |
69 | return l.name != r.name; | |
70 | } | |
71 | inline bool operator>(const object_t& l, const object_t& r) { | |
72 | return l.name > r.name; | |
73 | } | |
74 | inline bool operator<(const object_t& l, const object_t& r) { | |
75 | return l.name < r.name; | |
76 | } | |
9f95a23c | 77 | inline bool operator>=(const object_t& l, const object_t& r) { |
7c673cae FG |
78 | return l.name >= r.name; |
79 | } | |
80 | inline bool operator<=(const object_t& l, const object_t& r) { | |
81 | return l.name <= r.name; | |
82 | } | |
9f95a23c | 83 | inline std::ostream& operator<<(std::ostream& out, const object_t& o) { |
7c673cae FG |
84 | return out << o.name; |
85 | } | |
86 | ||
87 | namespace std { | |
9f95a23c TL |
88 | template<> struct hash<object_t> { |
89 | size_t operator()(const object_t& r) const { | |
90 | //static hash<string> H; | |
91 | //return H(r.name); | |
92 | return ceph_str_hash_linux(r.name.c_str(), r.name.length()); | |
93 | } | |
94 | }; | |
7c673cae FG |
95 | } // namespace std |
96 | ||
97 | ||
98 | struct file_object_t { | |
99 | uint64_t ino, bno; | |
100 | mutable char buf[34]; | |
101 | ||
102 | file_object_t(uint64_t i=0, uint64_t b=0) : ino(i), bno(b) { | |
103 | buf[0] = 0; | |
104 | } | |
105 | ||
106 | const char *c_str() const { | |
107 | if (!buf[0]) | |
108 | snprintf(buf, sizeof(buf), "%llx.%08llx", (long long unsigned)ino, (long long unsigned)bno); | |
109 | return buf; | |
110 | } | |
111 | ||
112 | operator object_t() { | |
113 | return object_t(c_str()); | |
114 | } | |
115 | }; | |
116 | ||
117 | ||
118 | // --------------------------- | |
119 | // snaps | |
120 | ||
121 | struct snapid_t { | |
122 | uint64_t val; | |
123 | // cppcheck-suppress noExplicitConstructor | |
124 | snapid_t(uint64_t v=0) : val(v) {} | |
125 | snapid_t operator+=(snapid_t o) { val += o.val; return *this; } | |
126 | snapid_t operator++() { ++val; return *this; } | |
9f95a23c | 127 | operator uint64_t() const { return val; } |
7c673cae FG |
128 | }; |
129 | ||
9f95a23c TL |
130 | inline void encode(snapid_t i, ceph::buffer::list &bl) { |
131 | using ceph::encode; | |
132 | encode(i.val, bl); | |
133 | } | |
134 | inline void decode(snapid_t &i, ceph::buffer::list::const_iterator &p) { | |
135 | using ceph::decode; | |
136 | decode(i.val, p); | |
137 | } | |
7c673cae FG |
138 | |
139 | template<> | |
140 | struct denc_traits<snapid_t> { | |
141 | static constexpr bool supported = true; | |
142 | static constexpr bool featured = false; | |
143 | static constexpr bool bounded = true; | |
31f18b77 | 144 | static constexpr bool need_contiguous = true; |
7c673cae FG |
145 | static void bound_encode(const snapid_t& o, size_t& p) { |
146 | denc(o.val, p); | |
147 | } | |
9f95a23c | 148 | static void encode(const snapid_t &o, ceph::buffer::list::contiguous_appender& p) { |
7c673cae FG |
149 | denc(o.val, p); |
150 | } | |
9f95a23c | 151 | static void decode(snapid_t& o, ceph::buffer::ptr::const_iterator &p) { |
7c673cae FG |
152 | denc(o.val, p); |
153 | } | |
154 | }; | |
155 | ||
9f95a23c | 156 | inline std::ostream& operator<<(std::ostream& out, const snapid_t& s) { |
7c673cae FG |
157 | if (s == CEPH_NOSNAP) |
158 | return out << "head"; | |
159 | else if (s == CEPH_SNAPDIR) | |
160 | return out << "snapdir"; | |
161 | else | |
9f95a23c | 162 | return out << std::hex << s.val << std::dec; |
7c673cae FG |
163 | } |
164 | ||
165 | ||
166 | struct sobject_t { | |
167 | object_t oid; | |
168 | snapid_t snap; | |
169 | ||
170 | sobject_t() : snap(0) {} | |
171 | sobject_t(object_t o, snapid_t s) : oid(o), snap(s) {} | |
172 | ||
173 | void swap(sobject_t& o) { | |
174 | oid.swap(o.oid); | |
175 | snapid_t t = snap; | |
176 | snap = o.snap; | |
177 | o.snap = t; | |
178 | } | |
179 | ||
9f95a23c | 180 | void encode(ceph::buffer::list& bl) const { |
11fdf7f2 TL |
181 | using ceph::encode; |
182 | encode(oid, bl); | |
183 | encode(snap, bl); | |
7c673cae | 184 | } |
9f95a23c | 185 | void decode(ceph::buffer::list::const_iterator& bl) { |
11fdf7f2 TL |
186 | using ceph::decode; |
187 | decode(oid, bl); | |
188 | decode(snap, bl); | |
7c673cae FG |
189 | } |
190 | }; | |
191 | WRITE_CLASS_ENCODER(sobject_t) | |
192 | ||
193 | inline bool operator==(const sobject_t &l, const sobject_t &r) { | |
194 | return l.oid == r.oid && l.snap == r.snap; | |
195 | } | |
196 | inline bool operator!=(const sobject_t &l, const sobject_t &r) { | |
197 | return l.oid != r.oid || l.snap != r.snap; | |
198 | } | |
199 | inline bool operator>(const sobject_t &l, const sobject_t &r) { | |
200 | return l.oid > r.oid || (l.oid == r.oid && l.snap > r.snap); | |
201 | } | |
202 | inline bool operator<(const sobject_t &l, const sobject_t &r) { | |
203 | return l.oid < r.oid || (l.oid == r.oid && l.snap < r.snap); | |
204 | } | |
205 | inline bool operator>=(const sobject_t &l, const sobject_t &r) { | |
206 | return l.oid > r.oid || (l.oid == r.oid && l.snap >= r.snap); | |
207 | } | |
208 | inline bool operator<=(const sobject_t &l, const sobject_t &r) { | |
209 | return l.oid < r.oid || (l.oid == r.oid && l.snap <= r.snap); | |
210 | } | |
9f95a23c | 211 | inline std::ostream& operator<<(std::ostream& out, const sobject_t &o) { |
7c673cae FG |
212 | return out << o.oid << "/" << o.snap; |
213 | } | |
214 | namespace std { | |
9f95a23c TL |
215 | template<> struct hash<sobject_t> { |
216 | size_t operator()(const sobject_t &r) const { | |
217 | static hash<object_t> H; | |
218 | static rjhash<uint64_t> I; | |
219 | return H(r.oid) ^ I(r.snap); | |
220 | } | |
221 | }; | |
7c673cae FG |
222 | } // namespace std |
223 | ||
224 | #endif |