]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_json: Support en/decoding Boost's flat containers
authorAdam C. Emerson <aemerson@redhat.com>
Tue, 10 Sep 2019 22:53:58 +0000 (18:53 -0400)
committerAdam C. Emerson <aemerson@redhat.com>
Wed, 9 Sep 2020 02:09:40 +0000 (22:09 -0400)
Specifically boost::container::flat_map and boost::container::flat_set

Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
src/common/ceph_json.h

index 041f52455b8bc35f1985110cf61b3977445ce29d..185d0c364435877a6ea70e7fbccf1294a1ae1a5b 100644 (file)
@@ -5,6 +5,7 @@
 #include <typeindex>
 #include <include/types.h>
 #include <boost/container/flat_map.hpp>
+#include <boost/container/flat_set.hpp>
 #include <include/ceph_fs.h>
 #include "common/ceph_time.h"
 
@@ -219,6 +220,21 @@ void decode_json_obj(std::set<T>& l, JSONObj *obj)
   }
 }
 
+template<class T>
+void decode_json_obj(boost::container::flat_set<T>& l, JSONObj *obj)
+{
+  l.clear();
+
+  JSONObjIter iter = obj->find_first();
+
+  for (; !iter.end(); ++iter) {
+    T val;
+    JSONObj *o = *iter;
+    decode_json_obj(val, o);
+    l.insert(val);
+  }
+}
+
 template<class T>
 void decode_json_obj(std::vector<T>& l, JSONObj *obj)
 {
@@ -251,6 +267,23 @@ void decode_json_obj(std::map<K, V, C>& m, JSONObj *obj)
   }
 }
 
+template<class K, class V, class C = std::less<K> >
+void decode_json_obj(boost::container::flat_map<K, V, C>& m, JSONObj *obj)
+{
+  m.clear();
+
+  JSONObjIter iter = obj->find_first();
+
+  for (; !iter.end(); ++iter) {
+    K key;
+    V val;
+    JSONObj *o = *iter;
+    JSONDecoder::decode_json("key", key, o);
+    JSONDecoder::decode_json("val", val, o);
+    m[key] = val;
+  }
+}
+
 template<class K, class V>
 void decode_json_obj(std::multimap<K, V>& m, JSONObj *obj)
 {
@@ -524,6 +557,18 @@ static void encode_json(const char *name, const std::set<T, Compare>& l, ceph::F
   f->close_section();
 }
 
+template<class T, class Compare = std::less<T> >
+static void encode_json(const char *name,
+                        const boost::container::flat_set<T, Compare>& l,
+                        ceph::Formatter *f)
+{
+  f->open_array_section(name);
+  for (auto iter = l.cbegin(); iter != l.cend(); ++iter) {
+    encode_json("obj", *iter, f);
+  }
+  f->close_section();
+}
+
 template<class T>
 static void encode_json(const char *name, const std::vector<T>& l, ceph::Formatter *f)
 {
@@ -547,6 +592,19 @@ static void encode_json(const char *name, const std::map<K, V, C>& m, ceph::Form
   f->close_section();
 }
 
+template<class K, class V, class C = std::less<K> >
+static void encode_json(const char *name, const boost::container::flat_map<K, V, C>& m, ceph::Formatter *f)
+{
+  f->open_array_section(name);
+  for (auto i = m.cbegin(); i != m.cend(); ++i) {
+    f->open_object_section("entry");
+    encode_json("key", i->first, f);
+    encode_json("val", i->second, f);
+    f->close_section();
+  }
+  f->close_section();
+}
+
 template<class K, class V>
 static void encode_json(const char *name, const std::multimap<K, V>& m, ceph::Formatter *f)
 {
@@ -642,6 +700,66 @@ static void encode_json(const char *name, const std::optional<T>& o, ceph::Forma
 }
 
 
+template<class K, class V>
+void encode_json_map(const char *name, const boost::container::flat_map<K, V>& m, ceph::Formatter *f)
+{
+  f->open_array_section(name);
+  for (auto iter = m.cbegin(); iter != m.cend(); ++iter) {
+    encode_json("obj", iter->second, f);
+  }
+  f->close_section();
+}
+
+
+template<class K, class V>
+void encode_json_map(const char *name, const char *index_name,
+                     const char *object_name, const char *value_name,
+                     void (*cb)(const char *, const V&, ceph::Formatter *, void *), void *parent,
+                     const boost::container::flat_map<K, V>& m, ceph::Formatter *f)
+{
+  f->open_array_section(name);
+  for (auto iter = m.cbegin(); iter != m.cend(); ++iter) {
+    if (index_name) {
+      f->open_object_section("key_value");
+      f->dump_string(index_name, iter->first);
+    }
+
+    if (object_name) {
+      f->open_object_section(object_name);
+    }
+
+    if (cb) {
+      cb(value_name, iter->second, f, parent);
+    } else {
+      encode_json(value_name, iter->second, f);
+    }
+
+    if (object_name) {
+      f->close_section();
+    }
+    if (index_name) {
+      f->close_section();
+    }
+  }
+  f->close_section(); 
+}
+
+template<class K, class V>
+void encode_json_map(const char *name, const char *index_name,
+                     const char *object_name, const char *value_name,
+                     const boost::container::flat_map<K, V>& m, ceph::Formatter *f)
+{
+  encode_json_map<K, V>(name, index_name, object_name, value_name, NULL, NULL, m, f);
+}
+
+template<class K, class V>
+void encode_json_map(const char *name, const char *index_name, const char *value_name,
+                     const boost::container::flat_map<K, V>& m, ceph::Formatter *f)
+{
+  encode_json_map<K, V>(name, index_name, NULL, value_name, NULL, NULL, m, f);
+}
+
+
 class JSONFormattable : public ceph::JSONFormatter {
   JSONObj::data_val value;
   std::vector<JSONFormattable> arr;