1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
25 #include "include/unordered_map.h"
29 #include "ceph_hash.h"
36 // cppcheck-suppress noExplicitConstructor
37 object_t(const char *s) : name(s) {}
38 // cppcheck-suppress noExplicitConstructor
39 object_t(const string& s) : name(s) {}
41 void swap(object_t& o) {
48 void encode(bufferlist &bl) const {
51 void decode(bufferlist::iterator &bl) {
55 WRITE_CLASS_ENCODER(object_t)
57 inline bool operator==(const object_t& l, const object_t& r) {
58 return l.name == r.name;
60 inline bool operator!=(const object_t& l, const object_t& r) {
61 return l.name != r.name;
63 inline bool operator>(const object_t& l, const object_t& r) {
64 return l.name > r.name;
66 inline bool operator<(const object_t& l, const object_t& r) {
67 return l.name < r.name;
69 inline bool operator>=(const object_t& l, const object_t& r) {
70 return l.name >= r.name;
72 inline bool operator<=(const object_t& l, const object_t& r) {
73 return l.name <= r.name;
75 inline ostream& operator<<(ostream& out, const object_t& o) {
80 template<> struct hash<object_t> {
81 size_t operator()(const object_t& r) const {
82 //static hash<string> H;
84 return ceph_str_hash_linux(r.name.c_str(), r.name.length());
90 struct file_object_t {
94 file_object_t(uint64_t i=0, uint64_t b=0) : ino(i), bno(b) {
98 const char *c_str() const {
100 snprintf(buf, sizeof(buf), "%llx.%08llx", (long long unsigned)ino, (long long unsigned)bno);
104 operator object_t() {
105 return object_t(c_str());
110 // ---------------------------
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; }
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); }
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) {
134 static void encode(const snapid_t &o, buffer::list::contiguous_appender& p) {
137 static void decode(snapid_t& o, buffer::ptr::iterator &p) {
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";
148 return out << hex << s.val << dec;
156 sobject_t() : snap(0) {}
157 sobject_t(object_t o, snapid_t s) : oid(o), snap(s) {}
159 void swap(sobject_t& o) {
166 void encode(bufferlist& bl) const {
170 void decode(bufferlist::iterator& bl) {
175 WRITE_CLASS_ENCODER(sobject_t)
177 inline bool operator==(const sobject_t &l, const sobject_t &r) {
178 return l.oid == r.oid && l.snap == r.snap;
180 inline bool operator!=(const sobject_t &l, const sobject_t &r) {
181 return l.oid != r.oid || l.snap != r.snap;
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);
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);
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);
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);
195 inline ostream& operator<<(ostream& out, const sobject_t &o) {
196 return out << o.oid << "/" << o.snap;
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);