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