summaryrefslogtreecommitdiffstats
path: root/src/crimson/common/tmap_helpers.cc
blob: 9c14ebc450e7b3df2f2413bc0947303589240bec (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "crimson/common/tmap_helpers.h"

#include "include/buffer.h"
#include "include/encoding.h"
#include "include/rados.h"

namespace detail {

#define decode_or_return(v, bp) \
  try {				\
    ::decode(v, bp);		\
  } catch (...)	{		\
    return -EINVAL;		\
  }

class TMapContents {
  std::map<std::string, bufferlist> keys;
  bufferlist header;
public:
  TMapContents() = default;

  int decode(bufferlist::const_iterator &bliter) {
    keys.clear();
    header.clear();
    if (bliter.end()) {
      return 0;
    }
    decode_or_return(header, bliter);
    __u32 num_keys;
    decode_or_return(num_keys, bliter);
    for (; num_keys > 0; --num_keys) {
      std::string key;
      decode_or_return(key, bliter);
      decode_or_return(keys[key], bliter);
    }
    return 0;
  }

  bufferlist encode() {
    bufferlist bl;
    ::encode(header, bl);
    ::encode(static_cast<__u32>(keys.size()), bl);
    for (auto &[k, v]: keys) {
      ::encode(k, bl);
      ::encode(v, bl);
    }
    return bl;
  }

  int update(bufferlist::const_iterator in) {
    while (!in.end()) {
      __u8 op;
      decode_or_return(op, in);

      if (op == CEPH_OSD_TMAP_HDR) {
	decode_or_return(header, in);
	continue;
      }

      std::string key;
      decode_or_return(key, in);

      switch (op) {
      case CEPH_OSD_TMAP_SET: {
	decode_or_return(keys[key], in);
	break;
      }
      case CEPH_OSD_TMAP_CREATE: {
	if (keys.contains(key)) {
	  return -EEXIST;
	}
	decode_or_return(keys[key], in);
	break;
      }
      case CEPH_OSD_TMAP_RM: {
	auto kiter = keys.find(key);
	if (kiter == keys.end()) {
	  return -ENOENT;
	}
	keys.erase(kiter);
	break;
      }
      case CEPH_OSD_TMAP_RMSLOPPY: {
	keys.erase(key);
	break;
      }
      }
    }
    return 0;
  }

  int put(bufferlist::const_iterator in) {
    return 0;
  }
};

}

namespace crimson::common {

using do_tmap_up_ret = tl::expected<bufferlist, int>;
do_tmap_up_ret do_tmap_up(bufferlist::const_iterator in, bufferlist contents)
{
  detail::TMapContents tmap;
  auto bliter = contents.cbegin();
  int r = tmap.decode(bliter);
  if (r < 0) {
    return tl::unexpected(r);
  }
  r = tmap.update(in);
  if (r < 0) {
    return tl::unexpected(r);
  }
  return tmap.encode();
}

using do_tmap_up_ret = tl::expected<bufferlist, int>;
do_tmap_up_ret do_tmap_put(bufferlist::const_iterator in)
{
  detail::TMapContents tmap;
  int r = tmap.decode(in);
  if (r < 0) {
    return tl::unexpected(r);
  }
  return tmap.encode();
}

}