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
34 static int add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
38 bufferlist::iterator iter = in->begin();
41 ::decode(diff_str, iter);
42 } catch (const buffer::error &err) {
43 CLS_LOG(20, "add: invalid decode of input");
48 double difference = strtod(diff_str.c_str(), &end_ptr);
50 if (end_ptr && *end_ptr != '\0') {
51 CLS_ERR("add: invalid input value: %s", diff_str.c_str());
56 int ret = cls_cxx_map_get_val(hctx, key, &bl);
60 if (ret == -ENODATA || bl.length() == 0) {
64 CLS_ERR("add: error reading omap key %s: %d", key.c_str(), ret);
68 std::string stored_value(bl.c_str(), bl.length());
70 value = strtod(stored_value.c_str(), &end_ptr);
72 if (end_ptr && *end_ptr != '\0') {
73 CLS_ERR("add: invalid stored value: %s", stored_value.c_str());
80 std::stringstream stream;
81 stream << std::setprecision(DECIMAL_PRECISION) << value;
84 new_value.append(stream.str());
86 return cls_cxx_map_set_val(hctx, key, &new_value);
89 static int mul(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
93 bufferlist::iterator iter = in->begin();
96 ::decode(diff_str, iter);
97 } catch (const buffer::error &err) {
98 CLS_LOG(20, "add: invalid decode of input");
103 double difference = strtod(diff_str.c_str(), &end_ptr);
105 if (end_ptr && *end_ptr != '\0') {
106 CLS_ERR("add: invalid input value: %s", diff_str.c_str());
111 int ret = cls_cxx_map_get_val(hctx, key, &bl);
115 if (ret == -ENODATA || bl.length() == 0) {
117 } else if (ret < 0) {
118 if (ret != -ENOENT) {
119 CLS_ERR("add: error reading omap key %s: %d", key.c_str(), ret);
123 std::string stored_value(bl.c_str(), bl.length());
125 value = strtod(stored_value.c_str(), &end_ptr);
127 if (end_ptr && *end_ptr != '\0') {
128 CLS_ERR("add: invalid stored value: %s", stored_value.c_str());
135 std::stringstream stream;
136 stream << std::setprecision(DECIMAL_PRECISION) << value;
138 bufferlist new_value;
139 new_value.append(stream.str());
141 return cls_cxx_map_set_val(hctx, key, &new_value);
146 CLS_LOG(20, "loading cls_numops");
148 cls_handle_t h_class;
149 cls_method_handle_t h_add;
150 cls_method_handle_t h_mul;
152 cls_register("numops", &h_class);
154 cls_register_cxx_method(h_class, "add",
155 CLS_METHOD_RD | CLS_METHOD_WR,
158 cls_register_cxx_method(h_class, "mul",
159 CLS_METHOD_RD | CLS_METHOD_WR,