]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
beginnings of monitorstore thinger
authorsageweil <sageweil@29311d96-e01e-0410-9327-a35deaab8ce9>
Thu, 8 Feb 2007 03:41:56 +0000 (03:41 +0000)
committersageweil <sageweil@29311d96-e01e-0410-9327-a35deaab8ce9>
Thu, 8 Feb 2007 03:41:56 +0000 (03:41 +0000)
git-svn-id: https://ceph.svn.sf.net/svnroot/ceph@1085 29311d96-e01e-0410-9327-a35deaab8ce9

branches/riccardo/monitor2/mon/MonitorStore.cc [new file with mode: 0644]
branches/riccardo/monitor2/mon/MonitorStore.h [new file with mode: 0644]

diff --git a/branches/riccardo/monitor2/mon/MonitorStore.cc b/branches/riccardo/monitor2/mon/MonitorStore.cc
new file mode 100644 (file)
index 0000000..12d0ad1
--- /dev/null
@@ -0,0 +1,79 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
+/*
+ * 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 "MonitorStore.h"
+#include "common/Clock.h"
+
+#include "config.h"
+#undef dout
+#define  dout(l) if (l<=g_conf.debug || l<=g_conf.debug_mon) cout << g_clock.now() << " store(" << dir <<") "
+#define  derr(l) if (l<=g_conf.debug || l<=g_conf.debug_mon) cerr << g_clock.now() << " store(" << dir <<") "
+
+#include <stdio.h>
+
+
+void MonitorStore::init()
+{
+  // verify dir exists
+  DIR *d = ::opendir(dir);
+  if (!d) {
+       derr(1) << "basedir " << dir << " dne" << endl;
+       assert(0);
+  }
+  ::closedir(d);
+}
+
+
+version_t MonitorStore::get_int(const char *a, const char *b)
+{
+  char fn[200];
+  if (b)
+       sprintf(fn, "%s/%s/%s", dir, a, b);
+  else
+       sprintf(fn, "%s/%s", dir, a);
+
+  FILE *f = ::fopen(fn, "r");
+  if (!f) 
+       return 0;
+
+  char buf[20];
+  ::fgets(buf, 20, f);
+  ::fclose(f);
+
+  version_t val = atoi(buf);
+
+  if (b) {
+       dout(10) << "get_int " << a << "/" << b << " = " << val << endl;
+  } else {
+       dout(10) << "get_int " << a << " = " << val << endl;
+  }
+  return val;
+}
+
+
+void MonitorStore::set_int(version_t val, const char *a, const char *b)
+{
+  char fn[200];
+  if (b) {
+       dout(10) << "set_int " << a << "/" << b << " = " << val << endl;
+       sprintf(fn, "%s/%s/%s", dir, a, b);
+  } else {
+       dout(10) << "set_int " << a << " = " << val << endl;
+       sprintf(fn, "%s/%s", dir, a);
+  }
+  
+  FILE *f = ::fopen(fn, "w");
+  assert(f);
+  ::fprintf(f, "%lld\n", val);
+  ::fclose(f);
+}
diff --git a/branches/riccardo/monitor2/mon/MonitorStore.h b/branches/riccardo/monitor2/mon/MonitorStore.h
new file mode 100644 (file)
index 0000000..ed78cbf
--- /dev/null
@@ -0,0 +1,45 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
+/*
+ * 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.
+ * 
+ */
+
+#ifndef __MON_MONITORSTORE_H
+#define __MON_MONITORSTORE_H
+
+#include "include/types.h"
+#include "include/buffer.h"
+
+class MonitorStore {
+  const char *dir;
+
+  version_t get_int(const char *a, const char *b=0);
+  void set_int(version_t v, const char *a, const char *b=0);
+
+  int get_bl(const char *nm, bufferlist& bl);
+  int put_bl(const char *nm, bufferlist& bl);
+
+  void init();
+
+public:
+  MonitorStore(const char *d) :
+    dir(d) {
+    init();
+  }
+
+  version_t get_incarnation() { return get_int("incarnation"); }
+  void set_incarnation(version_t i) { set_int(i, "incarnation"); }
+  
+  version_t get_last_proposal() { return get_int("last_proposal"); }
+  void set_last_proposal(version_t i) { set_int(i, "last_proposal"); }
+};
+
+
+#endif