2 * Ceph - scalable distributed file system
4 * Copyright (C) 2015 CERN
6 * Author: Joaquim Rocha <joaquim.rocha@cern.ch>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
17 * This is an OSD class that implements methods for object numeric options on
22 #include "objclass/objclass.h"
27 #include <include/compat.h>
29 #define DECIMAL_PRECISION 10
31 using ceph::bufferlist
;
39 static int add(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
43 auto iter
= in
->cbegin();
46 decode(diff_str
, iter
);
47 } catch (const buffer::error
&err
) {
48 CLS_LOG(20, "add: invalid decode of input");
53 double difference
= strtod(diff_str
.c_str(), &end_ptr
);
55 if (end_ptr
&& *end_ptr
!= '\0') {
56 CLS_ERR("add: invalid input value: %s", diff_str
.c_str());
61 int ret
= cls_cxx_map_get_val(hctx
, key
, &bl
);
65 if (ret
== -ENODATA
|| bl
.length() == 0) {
69 CLS_ERR("add: error reading omap key %s: %d", key
.c_str(), ret
);
73 std::string
stored_value(bl
.c_str(), bl
.length());
75 value
= strtod(stored_value
.c_str(), &end_ptr
);
77 if (end_ptr
&& *end_ptr
!= '\0') {
78 CLS_ERR("add: invalid stored value: %s", stored_value
.c_str());
85 std::stringstream stream
;
86 stream
<< std::setprecision(DECIMAL_PRECISION
) << value
;
89 new_value
.append(stream
.str());
91 return cls_cxx_map_set_val(hctx
, key
, &new_value
);
94 static int mul(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
98 auto iter
= in
->cbegin();
101 decode(diff_str
, iter
);
102 } catch (const buffer::error
&err
) {
103 CLS_LOG(20, "mul: invalid decode of input");
108 double difference
= strtod(diff_str
.c_str(), &end_ptr
);
110 if (end_ptr
&& *end_ptr
!= '\0') {
111 CLS_ERR("mul: invalid input value: %s", diff_str
.c_str());
116 int ret
= cls_cxx_map_get_val(hctx
, key
, &bl
);
120 if (ret
== -ENODATA
|| bl
.length() == 0) {
122 } else if (ret
< 0) {
123 if (ret
!= -ENOENT
) {
124 CLS_ERR("mul: error reading omap key %s: %d", key
.c_str(), ret
);
128 std::string
stored_value(bl
.c_str(), bl
.length());
130 value
= strtod(stored_value
.c_str(), &end_ptr
);
132 if (end_ptr
&& *end_ptr
!= '\0') {
133 CLS_ERR("mul: invalid stored value: %s", stored_value
.c_str());
140 std::stringstream stream
;
141 stream
<< std::setprecision(DECIMAL_PRECISION
) << value
;
143 bufferlist new_value
;
144 new_value
.append(stream
.str());
146 return cls_cxx_map_set_val(hctx
, key
, &new_value
);
151 CLS_LOG(20, "loading cls_numops");
153 cls_handle_t h_class
;
154 cls_method_handle_t h_add
;
155 cls_method_handle_t h_mul
;
157 cls_register("numops", &h_class
);
159 cls_register_cxx_method(h_class
, "add",
160 CLS_METHOD_RD
| CLS_METHOD_WR
,
163 cls_register_cxx_method(h_class
, "mul",
164 CLS_METHOD_RD
| CLS_METHOD_WR
,