]>
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 | ||
34 | struct object_t { | |
9f95a23c | 35 | std::string name; |
7c673cae FG |
36 | |
37 | object_t() {} | |
38 | // cppcheck-suppress noExplicitConstructor | |
39 | object_t(const char *s) : name(s) {} | |
40 | // cppcheck-suppress noExplicitConstructor | |
9f95a23c | 41 | object_t(const std::string& s) : name(s) {} |
f67539c2 TL |
42 | object_t(std::string&& s) : name(std::move(s)) {} |
43 | object_t(std::string_view s) : name(s) {} | |
7c673cae FG |
44 | |
45 | void swap(object_t& o) { | |
46 | name.swap(o.name); | |
47 | } | |
48 | void clear() { | |
49 | name.clear(); | |
50 | } | |
9f95a23c TL |
51 | |
52 | void encode(ceph::buffer::list &bl) const { | |
11fdf7f2 TL |
53 | using ceph::encode; |
54 | encode(name, bl); | |
7c673cae | 55 | } |
9f95a23c | 56 | void decode(ceph::buffer::list::const_iterator &bl) { |
11fdf7f2 TL |
57 | using ceph::decode; |
58 | decode(name, bl); | |
7c673cae FG |
59 | } |
60 | }; | |
61 | WRITE_CLASS_ENCODER(object_t) | |
62 | ||
63 | inline bool operator==(const object_t& l, const object_t& r) { | |
64 | return l.name == r.name; | |
65 | } | |
66 | inline bool operator!=(const object_t& l, const object_t& r) { | |
67 | return l.name != r.name; | |
68 | } | |
69 | inline bool operator>(const object_t& l, const object_t& r) { | |
70 | return l.name > r.name; | |
71 | } | |
72 | inline bool operator<(const object_t& l, const object_t& r) { | |
73 | return l.name < r.name; | |
74 | } | |
9f95a23c | 75 | inline bool operator>=(const object_t& l, const object_t& r) { |
7c673cae FG |
76 | return l.name >= r.name; |
77 | } | |
78 | inline bool operator<=(const object_t& l, const object_t& r) { | |
79 | return l.name <= r.name; | |
80 | } | |
9f95a23c | 81 | inline std::ostream& operator<<(std::ostream& out, const object_t& o) { |
7c673cae FG |
82 | return out << o.name; |
83 | } | |
84 | ||
85 | namespace std { | |
9f95a23c TL |
86 | template<> struct hash<object_t> { |
87 | size_t operator()(const object_t& r) const { | |
88 | //static hash<string> H; | |
89 | //return H(r.name); | |
90 | return ceph_str_hash_linux(r.name.c_str(), r.name.length()); | |
91 | } | |
92 | }; | |
7c673cae FG |
93 | } // namespace std |
94 | ||
95 | ||
96 | struct file_object_t { | |
97 | uint64_t ino, bno; | |
98 | mutable char buf[34]; | |
99 | ||
100 | file_object_t(uint64_t i=0, uint64_t b=0) : ino(i), bno(b) { | |
101 | buf[0] = 0; | |
102 | } | |
103 | ||
104 | const char *c_str() const { | |
105 | if (!buf[0]) | |
106 | snprintf(buf, sizeof(buf), "%llx.%08llx", (long long unsigned)ino, (long long unsigned)bno); | |
107 | return buf; | |
108 | } | |
109 | ||
110 | operator object_t() { | |
111 | return object_t(c_str()); | |
112 | } | |
113 | }; | |
114 | ||
115 | ||
116 | // --------------------------- | |
117 | // snaps | |
118 | ||
119 | struct snapid_t { | |
120 | uint64_t val; | |
121 | // cppcheck-suppress noExplicitConstructor | |
122 | snapid_t(uint64_t v=0) : val(v) {} | |
123 | snapid_t operator+=(snapid_t o) { val += o.val; return *this; } | |
124 | snapid_t operator++() { ++val; return *this; } | |
9f95a23c | 125 | operator uint64_t() const { return val; } |
7c673cae FG |
126 | }; |
127 | ||
9f95a23c TL |
128 | inline void encode(snapid_t i, ceph::buffer::list &bl) { |
129 | using ceph::encode; | |
130 | encode(i.val, bl); | |
131 | } | |
132 | inline void decode(snapid_t &i, ceph::buffer::list::const_iterator &p) { | |
133 | using ceph::decode; | |
134 | decode(i.val, p); | |
135 | } | |
7c673cae FG |
136 | |
137 | template<> | |
138 | struct denc_traits<snapid_t> { | |
139 | static constexpr bool supported = true; | |
140 | static constexpr bool featured = false; | |
141 | static constexpr bool bounded = true; | |
31f18b77 | 142 | static constexpr bool need_contiguous = true; |
7c673cae FG |
143 | static void bound_encode(const snapid_t& o, size_t& p) { |
144 | denc(o.val, p); | |
145 | } | |
9f95a23c | 146 | static void encode(const snapid_t &o, ceph::buffer::list::contiguous_appender& p) { |
7c673cae FG |
147 | denc(o.val, p); |
148 | } | |
9f95a23c | 149 | static void decode(snapid_t& o, ceph::buffer::ptr::const_iterator &p) { |
7c673cae FG |
150 | denc(o.val, p); |
151 | } | |
152 | }; | |
153 | ||
9f95a23c | 154 | inline std::ostream& operator<<(std::ostream& out, const snapid_t& s) { |
7c673cae FG |
155 | if (s == CEPH_NOSNAP) |
156 | return out << "head"; | |
157 | else if (s == CEPH_SNAPDIR) | |
158 | return out << "snapdir"; | |
159 | else | |
9f95a23c | 160 | return out << std::hex << s.val << std::dec; |
7c673cae FG |
161 | } |
162 | ||
163 | ||
164 | struct sobject_t { | |
165 | object_t oid; | |
166 | snapid_t snap; | |
167 | ||
168 | sobject_t() : snap(0) {} | |
169 | sobject_t(object_t o, snapid_t s) : oid(o), snap(s) {} | |
170 | ||
171 | void swap(sobject_t& o) { | |
172 | oid.swap(o.oid); | |
173 | snapid_t t = snap; | |
174 | snap = o.snap; | |
175 | o.snap = t; | |
176 | } | |
177 | ||
9f95a23c | 178 | void encode(ceph::buffer::list& bl) const { |
11fdf7f2 TL |
179 | using ceph::encode; |
180 | encode(oid, bl); | |
181 | encode(snap, bl); | |
7c673cae | 182 | } |
9f95a23c | 183 | void decode(ceph::buffer::list::const_iterator& bl) { |
11fdf7f2 TL |
184 | using ceph::decode; |
185 | decode(oid, bl); | |
186 | decode(snap, bl); | |
7c673cae FG |
187 | } |
188 | }; | |
189 | WRITE_CLASS_ENCODER(sobject_t) | |
190 | ||
191 | inline bool operator==(const sobject_t &l, const sobject_t &r) { | |
192 | return l.oid == r.oid && l.snap == r.snap; | |
193 | } | |
194 | inline bool operator!=(const sobject_t &l, const sobject_t &r) { | |
195 | return l.oid != r.oid || l.snap != r.snap; | |
196 | } | |
197 | inline bool operator>(const sobject_t &l, const sobject_t &r) { | |
198 | return l.oid > r.oid || (l.oid == r.oid && l.snap > r.snap); | |
199 | } | |
200 | inline bool operator<(const sobject_t &l, const sobject_t &r) { | |
201 | return l.oid < r.oid || (l.oid == r.oid && l.snap < r.snap); | |
202 | } | |
203 | inline bool operator>=(const sobject_t &l, const sobject_t &r) { | |
204 | return l.oid > r.oid || (l.oid == r.oid && l.snap >= r.snap); | |
205 | } | |
206 | inline bool operator<=(const sobject_t &l, const sobject_t &r) { | |
207 | return l.oid < r.oid || (l.oid == r.oid && l.snap <= r.snap); | |
208 | } | |
9f95a23c | 209 | inline std::ostream& operator<<(std::ostream& out, const sobject_t &o) { |
7c673cae FG |
210 | return out << o.oid << "/" << o.snap; |
211 | } | |
212 | namespace std { | |
9f95a23c TL |
213 | template<> struct hash<sobject_t> { |
214 | size_t operator()(const sobject_t &r) const { | |
215 | static hash<object_t> H; | |
216 | static rjhash<uint64_t> I; | |
217 | return H(r.oid) ^ I(r.snap); | |
218 | } | |
219 | }; | |
7c673cae FG |
220 | } // namespace std |
221 | ||
222 | #endif |