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