summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_map/Types.cc
blob: 47de9c3cf590bf2348da407b9f09fccfc1f71e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "Types.h"
#include "include/ceph_assert.h"
#include "include/stringify.h"
#include "common/Formatter.h"
#include <iostream>

namespace rbd {
namespace mirror {
namespace image_map {

const std::string UNMAPPED_INSTANCE_ID("");

namespace {

template <typename E>
class GetTypeVisitor : public boost::static_visitor<E> {
public:
  template <typename T>
  inline E operator()(const T&) const {
    return T::TYPE;
  }
};

class EncodeVisitor : public boost::static_visitor<void> {
public:
  explicit EncodeVisitor(bufferlist &bl) : m_bl(bl) {
  }

  template <typename T>
  inline void operator()(const T& t) const {
    using ceph::encode;
    encode(static_cast<uint32_t>(T::TYPE), m_bl);
    t.encode(m_bl);
  }
private:
  bufferlist &m_bl;
};

class DecodeVisitor : public boost::static_visitor<void> {
public:
  DecodeVisitor(__u8 version, bufferlist::const_iterator &iter)
    : m_version(version), m_iter(iter) {
  }

  template <typename T>
  inline void operator()(T& t) const {
    t.decode(m_version, m_iter);
  }
private:
  __u8 m_version;
  bufferlist::const_iterator &m_iter;
};

class DumpVisitor : public boost::static_visitor<void> {
public:
  explicit DumpVisitor(Formatter *formatter, const std::string &key)
    : m_formatter(formatter), m_key(key) {}

  template <typename T>
  inline void operator()(const T& t) const {
    auto type = T::TYPE;
    m_formatter->dump_string(m_key.c_str(), stringify(type));
    t.dump(m_formatter);
  }
private:
  ceph::Formatter *m_formatter;
  std::string m_key;
};

} // anonymous namespace

PolicyMetaType PolicyData::get_policy_meta_type() const {
  return boost::apply_visitor(GetTypeVisitor<PolicyMetaType>(), policy_meta);
}

void PolicyData::encode(bufferlist& bl) const {
  ENCODE_START(1, 1, bl);
  boost::apply_visitor(EncodeVisitor(bl), policy_meta);
  ENCODE_FINISH(bl);
}

void PolicyData::decode(bufferlist::const_iterator& it) {
  DECODE_START(1, it);

  uint32_t policy_meta_type;
  decode(policy_meta_type, it);

  switch (policy_meta_type) {
  case POLICY_META_TYPE_NONE:
    policy_meta = PolicyMetaNone();
    break;
  default:
    policy_meta = PolicyMetaUnknown();
    break;
  }

  boost::apply_visitor(DecodeVisitor(struct_v, it), policy_meta);
  DECODE_FINISH(it);
}

void PolicyData::dump(Formatter *f) const {
  boost::apply_visitor(DumpVisitor(f, "policy_meta_type"), policy_meta);
}

void PolicyData::generate_test_instances(std::list<PolicyData *> &o) {
  o.push_back(new PolicyData(PolicyMetaNone()));
}

std::ostream &operator<<(std::ostream &os, const ActionType& action_type) {
  switch (action_type) {
  case ACTION_TYPE_NONE:
    os << "NONE";
    break;
  case ACTION_TYPE_MAP_UPDATE:
    os << "MAP_UPDATE";
    break;
  case ACTION_TYPE_MAP_REMOVE:
    os << "MAP_REMOVE";
    break;
  case ACTION_TYPE_ACQUIRE:
    os << "ACQUIRE";
    break;
  case ACTION_TYPE_RELEASE:
    os << "RELEASE";
    break;
  default:
    os << "UNKNOWN (" << static_cast<uint32_t>(action_type) << ")";
    break;
  }
  return os;
}

} // namespace image_map
} // namespace mirror
} // namespace rbd