From 3a5448480bb08ecd260a1281ef0f8a96dfbce337 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 5 Jun 2009 21:34:58 -0700 Subject: [PATCH] objclass: trivialmap --- src/Makefile.am | 3 ++ src/cls_trivialmap.cc | 91 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/cls_trivialmap.cc diff --git a/src/Makefile.am b/src/Makefile.am index 2d7caaa39fbbf..80c3492dd18db 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -134,6 +134,9 @@ libcls_crypto.so: cls_crypto.cc ${CXX} -fPIC -shared -g -o libcls_crypto.so -lcrypto cls_crypto.cc BUILT_SOURCES += libcls_crypto.so +libcls_trivialmap.so: cls_trivialmap.cc + ${CXX} -I. -fPIC -shared -g -o libcls_trivialmap.so cls_trivialmap.cc +BUILT_SOURCES += libcls_trivialmap.so ## libcephclient.so diff --git a/src/cls_trivialmap.cc b/src/cls_trivialmap.cc new file mode 100644 index 0000000000000..c02097ff3a684 --- /dev/null +++ b/src/cls_trivialmap.cc @@ -0,0 +1,91 @@ + +#include +#include + +#include "include/types.h" +#include "objclass/objclass.h" + + +CLS_VER(1,0) +CLS_NAME(trivialmap) + +cls_handle_t h_class; +cls_method_handle_t h_read_all; +cls_method_handle_t h_update; + +static int read_all(cls_method_context_t ctx, bufferlist *inbl, bufferlist *outbl) +{ + return cls_cxx_read(ctx, 0, 0, outbl); +} + +static int update(cls_method_context_t ctx, bufferlist *inbl, bufferlist *outbl) +{ + bufferlist::iterator ip = inbl->begin(); + + // read the whole object + bufferlist bl; + int r = cls_cxx_read(ctx, 0, 0, &bl); + if (r < 0) + return r; + + // parse + bufferlist header; + map m; + if (bl.length()) { + bufferlist::iterator p = bl.begin(); + ::decode(header, p); + ::decode(m, p); + assert(p.end()); + } + + // do the update(s) + while (!ip.end()) { + __u8 op; + nstring key; + ::decode(op, ip); + ::decode(key, ip); + + switch (op) { + case 1: // insert key + { + bufferlist data; + ::decode(data, ip); + m[key] = data; + } + break; + + case 2: // remove key + m.erase(key); + break; + + case 3: // update header + { + ::decode(header, ip); + } + break; + + default: + return -EINVAL; + } + } + + // reencode + bufferlist obl; + ::encode(header, obl); + ::encode(m, obl); + + // write it out + cls_cxx_replace(ctx, 0, obl.length(), &obl); + + return 0; +} + + +void class_init() +{ + cls_register("trivialmap", &h_class); + cls_register_cxx_method(h_class, "read_all", read_all, &h_read_all); + cls_register_cxx_method(h_class, "update", update, &h_update); + return; +} + -- 2.39.5