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"
29 #include <include/compat.h>
31 #define DECIMAL_PRECISION 10
36 static int add(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
40 bufferlist::iterator iter
= in
->begin();
43 ::decode(diff_str
, iter
);
44 } catch (const buffer::error
&err
) {
45 CLS_LOG(20, "add: invalid decode of input");
50 double difference
= strtod(diff_str
.c_str(), &end_ptr
);
52 if (end_ptr
&& *end_ptr
!= '\0') {
53 CLS_ERR("add: invalid input value: %s", diff_str
.c_str());
58 int ret
= cls_cxx_map_get_val(hctx
, key
, &bl
);
62 if (ret
== -ENODATA
|| bl
.length() == 0) {
66 CLS_ERR("add: error reading omap key %s: %d", key
.c_str(), ret
);
70 std::string
stored_value(bl
.c_str(), bl
.length());
72 value
= strtod(stored_value
.c_str(), &end_ptr
);
74 if (end_ptr
&& *end_ptr
!= '\0') {
75 CLS_ERR("add: invalid stored value: %s", stored_value
.c_str());
82 std::stringstream stream
;
83 stream
<< std::setprecision(DECIMAL_PRECISION
) << value
;
86 new_value
.append(stream
.str());
88 return cls_cxx_map_set_val(hctx
, key
, &new_value
);
91 static int mul(cls_method_context_t hctx
, bufferlist
*in
, bufferlist
*out
)
95 bufferlist::iterator iter
= in
->begin();
98 ::decode(diff_str
, iter
);
99 } catch (const buffer::error
&err
) {
100 CLS_LOG(20, "add: invalid decode of input");
105 double difference
= strtod(diff_str
.c_str(), &end_ptr
);
107 if (end_ptr
&& *end_ptr
!= '\0') {
108 CLS_ERR("add: invalid input value: %s", diff_str
.c_str());
113 int ret
= cls_cxx_map_get_val(hctx
, key
, &bl
);
117 if (ret
== -ENODATA
|| bl
.length() == 0) {
119 } else if (ret
< 0) {
120 if (ret
!= -ENOENT
) {
121 CLS_ERR("add: error reading omap key %s: %d", key
.c_str(), ret
);
125 std::string
stored_value(bl
.c_str(), bl
.length());
127 value
= strtod(stored_value
.c_str(), &end_ptr
);
129 if (end_ptr
&& *end_ptr
!= '\0') {
130 CLS_ERR("add: invalid stored value: %s", stored_value
.c_str());
137 std::stringstream stream
;
138 stream
<< std::setprecision(DECIMAL_PRECISION
) << value
;
140 bufferlist new_value
;
141 new_value
.append(stream
.str());
143 return cls_cxx_map_set_val(hctx
, key
, &new_value
);
148 CLS_LOG(20, "loading cls_numops");
150 cls_handle_t h_class
;
151 cls_method_handle_t h_add
;
152 cls_method_handle_t h_mul
;
154 cls_register("numops", &h_class
);
156 cls_register_cxx_method(h_class
, "add",
157 CLS_METHOD_RD
| CLS_METHOD_WR
,
160 cls_register_cxx_method(h_class
, "mul",
161 CLS_METHOD_RD
| CLS_METHOD_WR
,