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