]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ObjectStore: Remove code duplication when dumping transactions.
authorJoao Eduardo Luis <jecluis@gmail.com>
Tue, 10 Apr 2012 11:28:43 +0000 (12:28 +0100)
committerSage Weil <sage@newdream.net>
Thu, 12 Apr 2012 17:59:23 +0000 (10:59 -0700)
By using OStreamFormatter, we can have a single function responsible for
dumping a transaction. We keep the same old functions for outputting
directly to a Formatter and to an ostream, but these are only wrappers
for the function that will really handle the dumping.

The "real" dump() function will now take only a Formatter as an argument,
to which we will output. We keep the 'dump(std::ostream& out)' function,
although now it simply creates an OStreamFormatter and passes it to the
dump() function.

Signed-off-by: Joao Eduardo Luis <jecluis@gmail.com>
src/os/ObjectStore.cc
src/os/ObjectStore.h

index 6b4ad67f28dab6327736b6532b8432b3d4cf21f9..1a2af8e4972305da8c5e7e425cb4b6b2323a7919 100644 (file)
@@ -1,4 +1,17 @@
-
+// -*- 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) 2004-2006 Sage Weil <sage@newdream.net>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software 
+ * Foundation.  See file COPYING.
+ * 
+ */
+#include <sstream>
 #include "ObjectStore.h"
 #include "common/Formatter.h"
 
@@ -12,21 +25,23 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
   f->open_array_section("ops");
   iterator i = begin();
   int op_num = 0;
-  while (i.have_op()) {
+  bool stop_looping = false;
+  while (i.have_op() && !stop_looping) {
     int op = i.get_op();
+    f->open_object_section("op");
+    f->dump_int("num", op_num);
+
     switch (op) {
     case Transaction::OP_NOP:
-      f->open_object_section("nop");
-      f->close_section();
+      f->dump_string("type", "nop");
       break;
     case Transaction::OP_TOUCH:
       {
        coll_t cid = i.get_cid();
        hobject_t oid = i.get_oid();
-       f->open_object_section("touch");
+       f->dump_string("type", "touch");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->close_section();
       }
       break;
       
@@ -38,12 +53,12 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        uint64_t len = i.get_length();
        bufferlist bl;
        i.get_bl(bl);
-       f->open_object_section("write");
+       f->dump_string("type", "write");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->dump_unsigned("offset", off);
-       f->dump_unsigned("length", len);
-       f->close_section();
+        f->dump_unsigned("length", len);
+        f->dump_unsigned("offset", off);
+        f->dump_unsigned("bufferlist length", bl.length());
       }
       break;
       
@@ -53,12 +68,11 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        hobject_t oid = i.get_oid();
        uint64_t off = i.get_length();
        uint64_t len = i.get_length();
-       f->open_object_section("zero");
+       f->dump_string("type", "zero");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->dump_unsigned("offset", off);
+        f->dump_unsigned("offset", off);
        f->dump_unsigned("length", len);
-       f->close_section();
       }
       break;
       
@@ -68,12 +82,11 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        hobject_t oid = i.get_oid();
        uint64_t off = i.get_length();
        uint64_t len = i.get_length();
-       f->open_object_section("trim_cache");
+       f->dump_string("type", "trim_cache");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
        f->dump_unsigned("offset", off);
        f->dump_unsigned("length", len);
-       f->close_section();
       }
       break;
       
@@ -82,11 +95,10 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        coll_t cid = i.get_cid();
        hobject_t oid = i.get_oid();
        uint64_t off = i.get_length();
-       f->open_object_section("truncate");
+       f->dump_string("type", "truncate");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
        f->dump_unsigned("offset", off);
-       f->close_section();
       }
       break;
       
@@ -94,10 +106,9 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
       {
        coll_t cid = i.get_cid();
        hobject_t oid = i.get_oid();
-       f->open_object_section("remove");
+       f->dump_string("type", "remove");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->close_section();
       }
       break;
       
@@ -108,12 +119,11 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        string name = i.get_attrname();
        bufferlist bl;
        i.get_bl(bl);
-       f->open_object_section("setattr");
+       f->dump_string("type", "setattr");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
        f->dump_string("name", name);
        f->dump_unsigned("length", bl.length());
-       f->close_section();
       }
       break;
       
@@ -123,13 +133,14 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        hobject_t oid = i.get_oid();
        map<string, bufferptr> aset;
        i.get_attrset(aset);
-       f->open_object_section("setattrs");
+       f->dump_string("type", "setattrs");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
        f->open_object_section("attr_lens");
-       for (map<string,bufferptr>::iterator p = aset.begin(); p != aset.end(); ++p)
+       for (map<string,bufferptr>::iterator p = aset.begin();
+           p != aset.end(); ++p) {
          f->dump_unsigned(p->first.c_str(), p->second.length());
-       f->close_section();
+       }
        f->close_section();
       }
       break;
@@ -139,11 +150,10 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        coll_t cid = i.get_cid();
        hobject_t oid = i.get_oid();
        string name = i.get_attrname();
-       f->open_object_section("rmattr");
+       f->dump_string("type", "rmattr");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
        f->dump_string("name", name);
-       f->close_section();
       }
       break;
 
@@ -151,10 +161,9 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
       {
        coll_t cid = i.get_cid();
        hobject_t oid = i.get_oid();
-       f->open_object_section("rmattrs");
+       f->dump_string("type", "rmattrs");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->close_section();
       }
       break;
       
@@ -163,11 +172,10 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        coll_t cid = i.get_cid();
        hobject_t oid = i.get_oid();
        hobject_t noid = i.get_oid();
-       f->open_object_section("clone");
+       f->dump_string("type", "clone");
        f->dump_stream("collection") << cid;
        f->dump_stream("src_oid") << oid;
        f->dump_stream("dst_oid") << noid;
-       f->close_section();
       }
       break;
 
@@ -178,13 +186,12 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        hobject_t noid = i.get_oid();
        uint64_t off = i.get_length();
        uint64_t len = i.get_length();
-       f->open_object_section("clonerange");
+       f->dump_string("type", "clonerange");
        f->dump_stream("collection") << cid;
        f->dump_stream("src_oid") << oid;
        f->dump_stream("dst_oid") << noid;
        f->dump_unsigned("offset", off);
        f->dump_unsigned("len", len);
-       f->close_section();
       }
       break;
 
@@ -196,32 +203,29 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        uint64_t srcoff = i.get_length();
        uint64_t len = i.get_length();
        uint64_t dstoff = i.get_length();
-       f->open_object_section("clonerange2");
+       f->dump_string("type", "clonerange2");
        f->dump_stream("collection") << cid;
        f->dump_stream("src_oid") << oid;
        f->dump_stream("dst_oid") << noid;
        f->dump_unsigned("src_offset", srcoff);
        f->dump_unsigned("len", len);
        f->dump_unsigned("dst_offset", dstoff);
-       f->close_section();
       }
       break;
 
     case Transaction::OP_MKCOLL:
       {
        coll_t cid = i.get_cid();
-       f->open_object_section("mkcoll");
+       f->dump_string("type", "mkcoll");
        f->dump_stream("collection") << cid;
-       f->close_section();
       }
       break;
 
     case Transaction::OP_RMCOLL:
       {
        coll_t cid = i.get_cid();
-       f->open_object_section("rmcoll");
+       f->dump_string("type", "rmcoll");
        f->dump_stream("collection") << cid;
-       f->close_section();
       }
       break;
 
@@ -230,11 +234,10 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        coll_t ocid = i.get_cid();
        coll_t ncid = i.get_cid();
        hobject_t oid = i.get_oid();
-       f->open_object_section("collection_add");
+       f->dump_string("type", "collection_add");
        f->dump_stream("src_collection") << ocid;
        f->dump_stream("dst_collection") << ncid;
        f->dump_stream("oid") << oid;
-       f->close_section();
       }
       break;
 
@@ -242,10 +245,9 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        {
        coll_t cid = i.get_cid();
        hobject_t oid = i.get_oid();
-       f->open_object_section("collection_remove");
+       f->dump_string("type", "collection_remove");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->close_section();
        }
       break;
 
@@ -269,11 +271,10 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        string name = i.get_attrname();
        bufferlist bl;
        i.get_bl(bl);
-       f->open_object_section("collection_setattr");
+       f->dump_string("type", "collection_setattr");
        f->dump_stream("collection") << cid;
        f->dump_string("name", name);
        f->dump_unsigned("length", bl.length());
-       f->close_section();
       }
       break;
 
@@ -281,26 +282,23 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
       {
        coll_t cid = i.get_cid();
        string name = i.get_attrname();
-       f->open_object_section("collection_rmattr");
+       f->dump_string("type", "collection_rmattr");
        f->dump_stream("collection") << cid;
        f->dump_string("name", name);
-       f->close_section();
       }
       break;
 
     case Transaction::OP_STARTSYNC:
-      f->open_object_section("startsync");
-      f->close_section();
+      f->dump_string("type", "startsync");
       break;
 
     case Transaction::OP_COLL_RENAME:
       {
        coll_t cid(i.get_cid());
        coll_t ncid(i.get_cid());
-       f->open_object_section("collection_rename");
+       f->dump_string("type", "collection_rename");
        f->dump_stream("src_collection") << cid;
        f->dump_stream("dst_collection") << ncid;
-       f->close_section();
       }
       break;
 
@@ -308,10 +306,9 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
       {
        coll_t cid(i.get_cid());
        hobject_t oid = i.get_oid();
-       f->open_object_section("omap_clear");
+       f->dump_string("type", "omap_clear");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->close_section();
       }
       break;
 
@@ -321,15 +318,14 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        hobject_t oid = i.get_oid();
        map<string, bufferlist> aset;
        i.get_attrset(aset);
-       f->open_object_section("omap_setkeys");
+       f->dump_string("type", "omap_setkeys");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
        f->open_object_section("attr_lens");
        for (map<string, bufferlist>::iterator p = aset.begin();
-            p != aset.end();
-            ++p)
+           p != aset.end(); ++p) {
          f->dump_unsigned(p->first.c_str(), p->second.length());
-       f->close_section();
+       }
        f->close_section();
       }
       break;
@@ -340,10 +336,9 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        hobject_t oid = i.get_oid();
        set<string> keys;
        i.get_keyset(keys);
-       f->open_object_section("omap_rmkeys");
+       f->dump_string("type", "omap_rmkeys");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
-       f->close_section();
       }
       break;
 
@@ -353,271 +348,23 @@ void ObjectStore::Transaction::dump(ceph::Formatter *f)
        hobject_t oid = i.get_oid();
        bufferlist bl;
        i.get_bl(bl);
-       f->open_object_section("omap_setheader");
+       f->dump_string("type", "omap_setheader");
        f->dump_stream("collection") << cid;
        f->dump_stream("oid") << oid;
        f->dump_stream("header_length") << bl.length();
-       f->close_section();
       }
       break;
 
     default:
-      f->open_object_section("unknown");
+      f->dump_string("type", "unknown");
       f->dump_unsigned("opcode", op);
-      f->close_section();
-      return;
-    }
-    op_num++;
-  }
-  f->close_section();
-}
-
-void ObjectStore::Transaction::dump(ostream& out)
-{
-  iterator i = begin();
-  int op_num = 0;
-  while (i.have_op()) {
-    int op = i.get_op();
-    switch (op) {
-    case Transaction::OP_NOP:
-      break;
-    case Transaction::OP_TOUCH:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       out << op_num << ": touch " << cid << " " << oid << "\n";
-      }
-      break;
-      
-    case Transaction::OP_WRITE:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       uint64_t off = i.get_length();
-       uint64_t len = i.get_length();
-       bufferlist bl;
-       i.get_bl(bl);
-       out << op_num << ": write " << cid << " " << oid << " " << off << "~" << len << " (" << bl.length() << ")\n";
-      }
-      break;
-      
-    case Transaction::OP_ZERO:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       uint64_t off = i.get_length();
-       uint64_t len = i.get_length();
-       out << op_num << ": zero " << cid << " " << oid << " " << off << "~" << len << "\n";
-      }
-      break;
-      
-    case Transaction::OP_TRIMCACHE:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       uint64_t off = i.get_length();
-       uint64_t len = i.get_length();
-       out << op_num << ": trim_from_cache " << cid << " " << oid << " " << off << "~" << len << "\n";
-      }
-      break;
-      
-    case Transaction::OP_TRUNCATE:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       uint64_t off = i.get_length();
-       out << op_num << ": truncate " << cid << " " << oid << " " << off << "\n";
-      }
-      break;
-      
-    case Transaction::OP_REMOVE:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       out << op_num << ": remove " << cid << " " << oid << "\n";
-      }
-      break;
-      
-    case Transaction::OP_SETATTR:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       string name = i.get_attrname();
-       bufferlist bl;
-       i.get_bl(bl);
-       out << op_num << ": setattr " << cid << " " << oid << " " << name << " (" << bl.length() << ")\n";
-      }
-      break;
-      
-    case Transaction::OP_SETATTRS:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       map<string, bufferptr> aset;
-       i.get_attrset(aset);
-       out << op_num << ": setattrs " << cid << " " << oid << " " << aset << "\n";
-      }
-      break;
-
-    case Transaction::OP_RMATTR:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       string name = i.get_attrname();
-       out << op_num << ": rmattr " << cid << " " << oid << " " << name << "\n";
-      }
-      break;
-
-    case Transaction::OP_RMATTRS:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       out << op_num << ": rmattrs " << cid << " " << oid << "\n";
-      }
+      stop_looping = true;
       break;
-      
-    case Transaction::OP_CLONE:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       hobject_t noid = i.get_oid();
-       out << op_num << ": clone " << cid << " " << oid << " -> " << noid << "\n";
-      }
-      break;
-
-    case Transaction::OP_CLONERANGE:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       hobject_t noid = i.get_oid();
-       uint64_t off = i.get_length();
-       uint64_t len = i.get_length();
-       out << op_num << ": clone_range " << cid << " " << oid << " -> " << noid << " " << off << "~" << len << " -> " << off << "\n";
-      }
-      break;
-
-    case Transaction::OP_CLONERANGE2:
-      {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       hobject_t noid = i.get_oid();
-       uint64_t srcoff = i.get_length();
-       uint64_t len = i.get_length();
-       uint64_t dstoff = i.get_length();
-       out << op_num << ": clone_range " << cid << " " << oid << " -> " << noid << " " << srcoff << "~" << len << " -> " << dstoff << "\n";
-      }
-      break;
-
-    case Transaction::OP_MKCOLL:
-      {
-       coll_t cid = i.get_cid();
-       out << op_num << ": mkcoll " << cid << "\n";
-      }
-      break;
-
-    case Transaction::OP_RMCOLL:
-      {
-       coll_t cid = i.get_cid();
-       out << op_num << ": rmcoll " << cid << "\n";
-      }
-      break;
-
-    case Transaction::OP_COLL_ADD:
-      {
-       coll_t ocid = i.get_cid();
-       coll_t ncid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       out << op_num << ": coll_add " << ocid << " " << ncid << " " << oid << "\n";
-      }
-      break;
-
-    case Transaction::OP_COLL_REMOVE:
-       {
-       coll_t cid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       out << op_num << ": coll_remove " << cid << " " << oid << "\n";
-       }
-      break;
-
-    case Transaction::OP_COLL_MOVE:
-       {
-       coll_t ocid = i.get_cid();
-       coll_t ncid = i.get_cid();
-       hobject_t oid = i.get_oid();
-       out << op_num << ": coll_move " << ocid << " " << ncid << " " << oid << "\n";
-       }
-      break;
-
-    case Transaction::OP_COLL_SETATTR:
-      {
-       coll_t cid = i.get_cid();
-       string name = i.get_attrname();
-       bufferlist bl;
-       i.get_bl(bl);
-       out << op_num << ": coll_setattr " << cid << " " << name << " (" << bl.length() << ")\n";
-      }
-      break;
-
-    case Transaction::OP_COLL_RMATTR:
-      {
-       coll_t cid = i.get_cid();
-       string name = i.get_attrname();
-       out << op_num << ": coll_rmattr " << cid << " " << name << "\n";
-      }
-      break;
-
-    case Transaction::OP_STARTSYNC:
-      out << op_num << ": startsync\n";
-      break;
-
-    case Transaction::OP_COLL_RENAME:
-      {
-       coll_t cid(i.get_cid());
-       coll_t ncid(i.get_cid());
-       out << op_num << ": coll_rename " << cid << " -> " << ncid << "\n";
-      }
-      break;
-    case Transaction::OP_OMAP_CLEAR:
-      {
-       coll_t cid(i.get_cid());
-       hobject_t oid = i.get_oid();
-       out << op_num << ": tmap_clear " << cid << "   " << oid << "\n";
-      }
-      break;
-    case Transaction::OP_OMAP_SETKEYS:
-      {
-       coll_t cid(i.get_cid());
-       hobject_t oid = i.get_oid();
-       map<string, bufferlist> aset;
-       i.get_attrset(aset);
-       out << op_num << ": tmap_setkeys " << cid << "   " << oid << "\n";
-      }
-      break;
-    case Transaction::OP_OMAP_RMKEYS:
-      {
-       coll_t cid(i.get_cid());
-       hobject_t oid = i.get_oid();
-       set<string> keys;
-       i.get_keyset(keys);
-       out << op_num << ": tmap_rmkeys " << cid << "   " << oid << "\n";
-      }
-      break;
-    case Transaction::OP_OMAP_SETHEADER:
-      {
-       coll_t cid(i.get_cid());
-       hobject_t oid = i.get_oid();
-       bufferlist bl;
-       i.get_bl(bl);
-       out << op_num << ": tmap_setheader" << cid << "   " << oid << "\n";
-      }
-      break;
-
-    default:
-      out << op_num << ": unknown op code " << op << "\n";
-      return;
     }
+    f->close_section();
     op_num++;
   }
+  f->close_section();
 }
 
 void ObjectStore::Transaction::generate_test_instances(list<ObjectStore::Transaction*>& o)
@@ -667,3 +414,4 @@ void ObjectStore::Transaction::generate_test_instances(list<ObjectStore::Transac
   t->collection_rename(c, c2);
   o.push_back(t);  
 }
+
index 9e4496a82f165f30f305c907f1570b50063b7fe1..f3070a973daf00273c24076dbb418b251cbfa85f 100644 (file)
@@ -11,8 +11,6 @@
  * Foundation.  See file COPYING.
  * 
  */
-
-
 #ifndef CEPH_OBJECTSTORE_H
 #define CEPH_OBJECTSTORE_H
 
@@ -577,7 +575,6 @@ public:
       DECODE_FINISH(bl);
     }
 
-    void dump(ostream& out);
     void dump(ceph::Formatter *f);
     static void generate_test_instances(list<Transaction*>& o);
   };