Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / cls / numops / cls_numops_client.cc
1 /*
2  * Ceph - scalable distributed file system
3  *
4  * Copyright (C) 2015 CERN
5  *
6  * Author: Joaquim Rocha <joaquim.rocha@cern.ch>
7  *
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.
12  *
13  */
14
15 #include "cls/numops/cls_numops_client.h"
16 #include "include/encoding.h"
17 #include "include/rados/librados.hpp"
18
19 #include <errno.h>
20 #include <sstream>
21
22 namespace rados {
23   namespace cls {
24     namespace numops {
25
26       int add(librados::IoCtx *ioctx,
27               const std::string& oid,
28               const std::string& key,
29               double value_to_add)
30       {
31         bufferlist in, out;
32         ::encode(key, in);
33
34         std::stringstream stream;
35         stream << value_to_add;
36
37         ::encode(stream.str(), in);
38
39         return ioctx->exec(oid, "numops", "add", in, out);
40       }
41
42       int sub(librados::IoCtx *ioctx,
43               const std::string& oid,
44               const std::string& key,
45               double value_to_subtract)
46       {
47         return add(ioctx, oid, key, -value_to_subtract);
48       }
49
50       int mul(librados::IoCtx *ioctx,
51               const std::string& oid,
52               const std::string& key,
53               double value_to_multiply)
54       {
55         bufferlist in, out;
56         ::encode(key, in);
57
58         std::stringstream stream;
59         stream << value_to_multiply;
60
61         ::encode(stream.str(), in);
62
63         return ioctx->exec(oid, "numops", "mul", in, out);
64       }
65
66       int div(librados::IoCtx *ioctx,
67               const std::string& oid,
68               const std::string& key,
69               double value_to_divide)
70       {
71         if (value_to_divide == 0)
72           return -EINVAL;
73
74         return mul(ioctx, oid, key, 1 / value_to_divide);
75       }
76
77     } // namespace numops
78   } // namespace cls
79 } // namespace rados