Skip to content

Commit

Permalink
SQUASHME: add cls module for CMPXATTR-like functionality in omap
Browse files Browse the repository at this point in the history
  • Loading branch information
cbodley committed Apr 8, 2020
1 parent 97c5ecb commit 3fe8f98
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 384 deletions.
31 changes: 22 additions & 9 deletions src/cls/cmpomap/client.cc
Expand Up @@ -18,41 +18,54 @@

namespace cls::cmpomap {

int cmp_vals(librados::ObjectReadOperation& op, ComparisonMap comparisons)
int cmp_vals(librados::ObjectReadOperation& op,
Mode mode, Op comparison, ComparisonMap values,
std::optional<ceph::bufferlist> default_value)
{
if (comparisons.size() > max_keys) {
if (values.size() > max_keys) {
return -E2BIG;
}
cmp_vals_op call;
call.entries = std::move(comparisons);
call.mode = mode;
call.comparison = comparison;
call.values = std::move(values);
call.default_value = std::move(default_value);

bufferlist in;
encode(call, in);
op.exec("cmpomap", "cmp_vals", in);
return 0;
}

int cmp_set_vals(librados::ObjectWriteOperation& op, ComparisonMap comparisons)
int cmp_set_vals(librados::ObjectWriteOperation& op,
Mode mode, Op comparison, ComparisonMap values,
std::optional<ceph::bufferlist> default_value)
{
if (comparisons.size() > max_keys) {
if (values.size() > max_keys) {
return -E2BIG;
}
cmp_set_vals_op call;
call.entries = std::move(comparisons);
call.mode = mode;
call.comparison = comparison;
call.values = std::move(values);
call.default_value = std::move(default_value);

bufferlist in;
encode(call, in);
op.exec("cmpomap", "cmp_set_vals", in);
return 0;
}

int cmp_rm_keys(librados::ObjectWriteOperation& op, ComparisonMap comparisons)
int cmp_rm_keys(librados::ObjectWriteOperation& op,
Mode mode, Op comparison, ComparisonMap values)
{
if (comparisons.size() > max_keys) {
if (values.size() > max_keys) {
return -E2BIG;
}
cmp_rm_keys_op call;
call.entries = std::move(comparisons);
call.mode = mode;
call.comparison = comparison;
call.values = std::move(values);

bufferlist in;
encode(call, in);
Expand Down
68 changes: 8 additions & 60 deletions src/cls/cmpomap/client.h
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include <optional>
#include "include/rados/librados_fwd.hpp"
#include "types.h"

Expand All @@ -27,24 +28,26 @@ static constexpr uint32_t max_keys = 1000;
/// comparisons with Mode::U64, failure to decode an input value is reported
/// as -EINVAL, while failure to decode a stored value is reported as -EIO
[[nodiscard]] int cmp_vals(librados::ObjectReadOperation& op,
ComparisonMap comparisons);
Mode mode, Op comparison, ComparisonMap values,
std::optional<ceph::bufferlist> default_value);

/// process each of the omap value comparisons according to the same rules as
/// cmpxattr(). any key/value pairs that compare successfully are overwritten
/// with the corresponding input value. for comparisons with Mode::U64, failure
/// to decode an input value is reported as -EINVAL. decode failure of a stored
/// value is treated as an unsuccessful comparison and is not reported as an
/// error
[[nodiscard]] int cmp_set_vals(librados::ObjectWriteOperation& op,
ComparisonMap comparisons);
[[nodiscard]] int cmp_set_vals(librados::ObjectWriteOperation& writeop,
Mode mode, Op comparison, ComparisonMap values,
std::optional<ceph::bufferlist> default_value);

/// process each of the omap value comparisons according to the same rules as
/// cmpxattr(). any key/value pairs that compare successfully are removed. for
/// comparisons with Mode::U64, failure to decode an input value is reported as
/// -EINVAL. decode failure of a stored value is treated as an unsuccessful
/// comparison and is not reported as an error
[[nodiscard]] int cmp_rm_keys(librados::ObjectWriteOperation& op,
ComparisonMap comparisons);
[[nodiscard]] int cmp_rm_keys(librados::ObjectWriteOperation& writeop,
Mode mode, Op comparison, ComparisonMap values);


// bufferlist factories for comparison values
Expand All @@ -60,59 +63,4 @@ inline ceph::bufferlist u64_buffer(uint64_t value) {
return bl;
}

// string comparison factory functions
inline Comparison string_eq(ceph::bufferlist v) {
return { Mode::String, Op::EQ, std::move(v) };
}
inline Comparison string_ne(ceph::bufferlist v) {
return { Mode::String, Op::NE, std::move(v) };
}
inline Comparison string_gt(ceph::bufferlist v) {
return { Mode::String, Op::GT, std::move(v) };
}
inline Comparison string_gte(ceph::bufferlist v) {
return { Mode::String, Op::GTE, std::move(v) };
}
inline Comparison string_lt(ceph::bufferlist v) {
return { Mode::String, Op::LT, std::move(v) };
}
inline Comparison string_lte(ceph::bufferlist v) {
return { Mode::String, Op::LTE, std::move(v) };
}

// u64 comparison factory functions
inline Comparison u64_eq(ceph::bufferlist v) {
return { Mode::U64, Op::EQ, std::move(v) };
}
inline Comparison u64_ne(ceph::bufferlist v) {
return { Mode::U64, Op::NE, std::move(v) };
}
inline Comparison u64_gt(ceph::bufferlist v) {
return { Mode::U64, Op::GT, std::move(v) };
}
inline Comparison u64_gte(ceph::bufferlist v) {
return { Mode::U64, Op::GTE, std::move(v) };
}
inline Comparison u64_lt(ceph::bufferlist v) {
return { Mode::U64, Op::LT, std::move(v) };
}
inline Comparison u64_lte(ceph::bufferlist v) {
return { Mode::U64, Op::LTE, std::move(v) };
}

// generic comparison factory functions
inline Comparison eq(std::string_view v) { return string_eq(string_buffer(v)); }
inline Comparison ne(std::string_view v) { return string_ne(string_buffer(v)); }
inline Comparison gt(std::string_view v) { return string_gt(string_buffer(v)); }
inline Comparison gte(std::string_view v) { return string_gte(string_buffer(v)); }
inline Comparison lt(std::string_view v) { return string_lt(string_buffer(v)); }
inline Comparison lte(std::string_view v) { return string_lte(string_buffer(v)); }

inline Comparison eq(uint64_t v) { return u64_eq(u64_buffer(v)); }
inline Comparison ne(uint64_t v) { return u64_ne(u64_buffer(v)); }
inline Comparison gt(uint64_t v) { return u64_gt(u64_buffer(v)); }
inline Comparison gte(uint64_t v) { return u64_gte(u64_buffer(v)); }
inline Comparison lt(uint64_t v) { return u64_lt(u64_buffer(v)); }
inline Comparison lte(uint64_t v) { return u64_lte(u64_buffer(v)); }

} // namespace cls::cmpomap
42 changes: 33 additions & 9 deletions src/cls/cmpomap/ops.h
Expand Up @@ -20,56 +20,80 @@
namespace cls::cmpomap {

struct cmp_vals_op {
ComparisonMap entries;
Mode mode;
Op comparison;
ComparisonMap values;
std::optional<ceph::bufferlist> default_value;
};

inline void encode(const cmp_vals_op& o, ceph::bufferlist& bl, uint64_t f=0)
{
ENCODE_START(1, 1, bl);
encode(o.entries, bl);
encode(o.mode, bl);
encode(o.comparison, bl);
encode(o.values, bl);
encode(o.default_value, bl);
ENCODE_FINISH(bl);
}

inline void decode(cmp_vals_op& o, ceph::bufferlist::const_iterator& bl)
{
DECODE_START(1, bl);
decode(o.entries, bl);
decode(o.mode, bl);
decode(o.comparison, bl);
decode(o.values, bl);
decode(o.default_value, bl);
DECODE_FINISH(bl);
}

struct cmp_set_vals_op {
ComparisonMap entries;
Mode mode;
Op comparison;
ComparisonMap values;
std::optional<ceph::bufferlist> default_value;
};

inline void encode(const cmp_set_vals_op& o, ceph::bufferlist& bl, uint64_t f=0)
{
ENCODE_START(1, 1, bl);
encode(o.entries, bl);
encode(o.mode, bl);
encode(o.comparison, bl);
encode(o.values, bl);
encode(o.default_value, bl);
ENCODE_FINISH(bl);
}

inline void decode(cmp_set_vals_op& o, ceph::bufferlist::const_iterator& bl)
{
DECODE_START(1, bl);
decode(o.entries, bl);
decode(o.mode, bl);
decode(o.comparison, bl);
decode(o.values, bl);
decode(o.default_value, bl);
DECODE_FINISH(bl);
}

struct cmp_rm_keys_op {
ComparisonMap entries;
Mode mode;
Op comparison;
ComparisonMap values;
};

inline void encode(const cmp_rm_keys_op& o, ceph::bufferlist& bl, uint64_t f=0)
{
ENCODE_START(1, 1, bl);
encode(o.entries, bl);
encode(o.mode, bl);
encode(o.comparison, bl);
encode(o.values, bl);
ENCODE_FINISH(bl);
}

inline void decode(cmp_rm_keys_op& o, ceph::bufferlist::const_iterator& bl)
{
DECODE_START(1, bl);
decode(o.entries, bl);
decode(o.mode, bl);
decode(o.comparison, bl);
decode(o.values, bl);
DECODE_FINISH(bl);
}

Expand Down

0 comments on commit 3fe8f98

Please sign in to comment.