summaryrefslogtreecommitdiffstats
path: root/src/cls/numops/cls_numops.cc
blob: 331eb7dca516c239f225ea23db27177d633764fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2015 CERN
 *
 * Author: Joaquim Rocha <joaquim.rocha@cern.ch>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 */

/** \file
 *
 * This is an OSD class that implements methods for object numeric options on
 * its omap values.
 *
 */

#include "objclass/objclass.h"
#include <errno.h>
#include <string>
#include <sstream>
#include <cstdio>
#include <include/compat.h>

#define DECIMAL_PRECISION 10

using ceph::bufferlist;
using std::string;
using ceph::decode;
using ceph::encode;

CLS_VER(1,0)
CLS_NAME(numops)

static int add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
  string key, diff_str;

  auto iter = in->cbegin();
  try {
    decode(key, iter);
    decode(diff_str, iter);
  } catch (const ceph::buffer::error &err) {
    CLS_LOG(20, "add: invalid decode of input");
    return -EINVAL;
  }

  char *end_ptr = 0;
  double difference = strtod(diff_str.c_str(), &end_ptr);

  if (end_ptr && *end_ptr != '\0') {
    CLS_ERR("add: invalid input value: %s", diff_str.c_str());
    return -EINVAL;
  }

  bufferlist bl;
  int ret = cls_cxx_map_get_val(hctx, key, &bl);

  double value;

  if (ret == -ENODATA || bl.length() == 0) {
    value = 0;
  } else if (ret < 0) {
    if (ret != -ENOENT) {
      CLS_ERR("add: error reading omap key %s: %d", key.c_str(), ret);
    }
    return ret;
  } else {
    std::string stored_value(bl.c_str(), bl.length());
    end_ptr = 0;
    value = strtod(stored_value.c_str(), &end_ptr);

    if (end_ptr && *end_ptr != '\0') {
      CLS_ERR("add: invalid stored value: %s", stored_value.c_str());
      return -EBADMSG;
    }
  }

  value += difference;

  std::stringstream stream;
  stream << std::setprecision(DECIMAL_PRECISION) << value;

  bufferlist new_value;
  new_value.append(stream.str());

  return cls_cxx_map_set_val(hctx, key, &new_value);
}

static int mul(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
  string key, diff_str;

  auto iter = in->cbegin();
  try {
    decode(key, iter);
    decode(diff_str, iter);
  } catch (const ceph::buffer::error &err) {
    CLS_LOG(20, "mul: invalid decode of input");
    return -EINVAL;
  }

  char *end_ptr = 0;
  double difference = strtod(diff_str.c_str(), &end_ptr);

  if (end_ptr && *end_ptr != '\0') {
    CLS_ERR("mul: invalid input value: %s", diff_str.c_str());
    return -EINVAL;
  }

  bufferlist bl;
  int ret = cls_cxx_map_get_val(hctx, key, &bl);

  double value;

  if (ret == -ENODATA || bl.length() == 0) {
    value = 0;
  } else if (ret < 0) {
    if (ret != -ENOENT) {
      CLS_ERR("mul: error reading omap key %s: %d", key.c_str(), ret);
    }
    return ret;
  } else {
    std::string stored_value(bl.c_str(), bl.length());
    end_ptr = 0;
    value = strtod(stored_value.c_str(), &end_ptr);

    if (end_ptr && *end_ptr != '\0') {
      CLS_ERR("mul: invalid stored value: %s", stored_value.c_str());
      return -EBADMSG;
    }
  }

  value *= difference;

  std::stringstream stream;
  stream << std::setprecision(DECIMAL_PRECISION) << value;

  bufferlist new_value;
  new_value.append(stream.str());

  return cls_cxx_map_set_val(hctx, key, &new_value);
}

CLS_INIT(numops)
{
  CLS_LOG(20, "loading cls_numops");

  cls_handle_t h_class;
  cls_method_handle_t h_add;
  cls_method_handle_t h_mul;

  cls_register("numops", &h_class);

  cls_register_cxx_method(h_class, "add",
                          CLS_METHOD_RD | CLS_METHOD_WR,
                          add, &h_add);

  cls_register_cxx_method(h_class, "mul",
                          CLS_METHOD_RD | CLS_METHOD_WR,
                          mul, &h_mul);
}