]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
objclass: trivialmap
authorSage Weil <sage@newdream.net>
Sat, 6 Jun 2009 04:34:58 +0000 (21:34 -0700)
committerSage Weil <sage@newdream.net>
Sat, 6 Jun 2009 04:34:58 +0000 (21:34 -0700)
src/Makefile.am
src/cls_trivialmap.cc [new file with mode: 0644]

index 2d7caaa39fbbf4445f8a388a31153324a88627e9..80c3492dd18db5be9dba02836a8877dffc3389ab 100644 (file)
@@ -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 (file)
index 0000000..c02097f
--- /dev/null
@@ -0,0 +1,91 @@
+
+#include <iostream>
+#include <errno.h>
+
+#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<nstring, bufferlist> 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;
+}
+