1.1 各种Object

1. object_t

这个object_t实际上就是只有一个name在起作用。

struct object_t {
string name;
object_t() {}
// cppcheck-suppress noExplicitConstructor
object_t(const char *s) : name(s) {}
// cppcheck-suppress noExplicitConstructor
object_t(const string& s) : name(s) {}
void swap(object_t& o) {
name.swap(o.name);
}
void clear() {
name.clear();
}
void encode(bufferlist &bl) const {
using ceph::encode;
encode(name, bl);
}
void decode(bufferlist::const_iterator &bl) {
using ceph::decode;
decode(name, bl);
}
};
WRITE_CLASS_ENCODER(object_t)

尾部的这个宏,实际上没有什么特别的。这里可以把这个宏展开。

WRITE_CLASS_ENCODER

#define WRITE_CLASS_ENCODER(cl) \
inline void encode(const cl &c, ::ceph::bufferlist &bl, uint64_t features=0) { \
ENCODE_DUMP_PRE(); c.encode(bl); ENCODE_DUMP_POST(cl); } \
inline void decode(cl &c, ::ceph::bufferlist::const_iterator &p) { c.decode(p); }

展开之后的代码就是

inline void encode(const cl &c, ::ceph::bufferlist &bl, uint64_t features=0) {
ENCODE_DUMP_PRE(); // -> 这两个是没有什么用的
c.encode(bl);
ENCODE_DUMP_POST(cl); // 这个宏的作用就是说想把dump出来的object打印到/tmp目录。
}
inline void decode(cl &c, ::ceph::bufferlist::const_iterator &p) {
c.decode(p);
}

所以这两个宏的作用就是直接调用类的encode和decode函数。

问题

object_t只提供了name并没有提供其他任何存储空间,那么一个object的内容在哪里呢?

重载运算符函数

针对于object_tCeph重载了很多运算符,看起来是比较繁锁的,就是不知道有没有更加简便的方法。

inline bool operator==(const object_t& l, const object_t& r) {
return l.name == r.name;
}
inline bool operator!=(const object_t& l, const object_t& r) {
return l.name != r.name;
}
inline bool operator>(const object_t& l, const object_t& r) {
return l.name > r.name;
}
inline bool operator<(const object_t& l, const object_t& r) {
return l.name < r.name;
}
inline bool operator>=(const object_t& l, const object_t& r) {
return l.name >= r.name;
}
inline bool operator<=(const object_t& l, const object_t& r) {
return l.name <= r.name;
}
inline ostream& operator<<(ostream& out, const object_t& o) {
return out << o.name;
}

如是只是为了支持hash函数,那么只需要==被重载就可以了。

如何自定义hash支持

namespace std {
template<> struct hash<object_t> {
size_t operator()(const object_t& r) const {
//static hash<string> H;
//return H(r.name);
return ceph_str_hash_linux(r.name.c_str(), r.name.length());
}
};
} // namespace std