]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Add new cls_numops class for numeric operations
authorJoaquim Rocha <joaquim.rocha@cern.ch>
Thu, 4 Jun 2015 13:22:12 +0000 (15:22 +0200)
committerJoaquim Rocha <joaquim.rocha@cern.ch>
Mon, 17 Aug 2015 20:23:30 +0000 (22:23 +0200)
Signed-off-by: Joaquim Rocha <joaquim.rocha@cern.ch>
src/cls/Makefile-client.am
src/cls/Makefile-server.am
src/cls/numops/cls_numops.cc [new file with mode: 0644]

index aa4a4e6054b6d4c640ebae467d0f6ba158ab4500..5a45a61e001e0de219ba6494e8011a4860bb8b25 100644 (file)
@@ -51,10 +51,15 @@ noinst_LIBRARIES += libcls_user_client.a
 libcls_cephfs_client_la_SOURCES = cls/cephfs/cls_cephfs_client.cc
 noinst_LTLIBRARIES += libcls_cephfs_client.la
 
+libcls_numops_client_la_SOURCES = cls/numops/cls_numops_client.cc
+noinst_LTLIBRARIES += libcls_numops_client.la
+DENCODER_DEPS += libcls_numops_client.la
+
 noinst_HEADERS += \
        cls/lock/cls_lock_types.h \
        cls/lock/cls_lock_ops.h \
        cls/lock/cls_lock_client.h \
+       cls/numops/cls_numops_client.h \
        cls/rbd/cls_rbd.h \
        cls/rbd/cls_rbd_client.h \
        cls/refcount/cls_refcount_ops.h \
index 7af69ba18dbc08a69a1a758e0b937c3a3b6186d3..5ac657352498fcfc0b48b561b235f1c16348e7df 100644 (file)
@@ -6,6 +6,10 @@ libcls_hello_la_LIBADD = $(PTHREAD_LIBS) $(EXTRALIBS)
 libcls_hello_la_LDFLAGS = ${AM_LDFLAGS} -module -avoid-version -shared -export-symbols-regex '.*__cls_.*'
 radoslib_LTLIBRARIES += libcls_hello.la
 
+libcls_numops_la_SOURCES = cls/numops/cls_numops.cc
+libcls_numops_la_LDFLAGS = ${AM_LDFLAGS} -module -avoid-version -shared -export-symbols-regex '.*__cls_.*'
+radoslib_LTLIBRARIES += libcls_numops.la
+
 libcls_rbd_la_SOURCES = cls/rbd/cls_rbd.cc
 libcls_rbd_la_LIBADD = $(PTHREAD_LIBS) $(EXTRALIBS)
 libcls_rbd_la_LDFLAGS = ${AM_LDFLAGS} -module -avoid-version -shared -export-symbols-regex '.*__cls_.*'
diff --git a/src/cls/numops/cls_numops.cc b/src/cls/numops/cls_numops.cc
new file mode 100644 (file)
index 0000000..3746459
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * 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 <asm-generic/errno.h>
+#include <iostream>
+#include <map>
+#include <string>
+#include <sstream>
+#include <cstdio>
+
+CLS_VER(1,0)
+CLS_NAME(numops)
+
+cls_handle_t h_class;
+cls_method_handle_t h_add;
+cls_method_handle_t h_mul;
+
+static int add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
+{
+  string key, diff_str;
+
+  bufferlist::iterator iter = in->begin();
+  try {
+    ::decode(key, iter);
+    ::decode(diff_str, iter);
+  } catch (const 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.str("");
+  stream << std::setprecision(10) << 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;
+
+  bufferlist::iterator iter = in->begin();
+  try {
+    ::decode(key, iter);
+    ::decode(diff_str, iter);
+  } catch (const 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.str("");
+  stream << std::setprecision(10) << value;
+
+  bufferlist new_value;
+  new_value.append(stream.str());
+
+  return cls_cxx_map_set_val(hctx, key, &new_value);
+}
+
+void __cls_init()
+{
+  CLS_LOG(20, "loading cls_numops");
+
+  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);
+}