So many changes. Broken.
[invirt/third/libt4.git] / log.cc
diff --git a/log.cc b/log.cc
index 627b7ac..36f10a3 100644 (file)
--- a/log.cc
+++ b/log.cc
-#include "paxos.h"
-#include <fstream>
-#include <iostream>
-#include "threaded_log.h"
+#include "include/log.h"
+#include "include/paxos.h"
 
-// Paxos must maintain some durable state (i.e., that survives power
-// failures) to run Paxos correct.  This module implements a log with
-// all durable state to run Paxos.  Since the values chosen correspond
-// to views, the log contains all views since the beginning of time.
+// Maintains durable state (i.e. surviving power failures) needed for correct
+// operation of Paxos as a log.
 
-log::log(acceptor *_acc, std::string _me) : pxs (_acc) {
-    name = "paxos-" + _me + ".log";
-    logread();
-}
+log::log(string _me) : name("paxos-" + _me + ".log") {}
 
-void log::logread(void) {
-    std::ifstream from;
-    std::string type;
-    unsigned instance;
+void log::replay() {
+    auto from = unmarshall(read());
+    string type;
 
-    from.open(name.c_str());
-    LOG("logread");
+    DEBUG_LOG << "Replaying paxos log from disk";
     while (from >> type) {
-        if (type == "done") {
-            std::string v;
-            from >> instance;
-            from.get();
-            getline(from, v);
-            pxs->values[instance] = v;
-            pxs->instance_h = instance;
-            LOG("logread: instance: " << instance << " w. v = " <<
-                    pxs->values[instance]);
-            pxs->v_a.clear();
-            pxs->n_h.n = 0;
-            pxs->n_a.n = 0;
-        } else if (type == "propseen") {
-            from >> pxs->n_h.n;
-            from >> pxs->n_h.m;
-            LOG("logread: high update: " << pxs->n_h.n << "(" << pxs->n_h.m << ")");
-        } else if (type == "accepted") {
-            std::string v;
-            from >> pxs->n_a.n;
-            from >> pxs->n_a.m;
-            from.get();
-            getline(from, v);
-            pxs->v_a = v;
-            LOG("logread: prop update " << pxs->n_a.n << "(" << pxs->n_a.m << ") with v = " << pxs->v_a);
+        if (handlers.count(type)) {
+            handlers[type](from);
         } else {
-            LOG("logread: unknown log record");
+            DEBUG_LOG << "unknown log record";
             VERIFY(0);
         }
-    } 
-    from.close();
-}
-
-std::string log::dump() {
-    std::ifstream from;
-    std::string res;
-    std::string v;
-    from.open(name.c_str());
-    while (getline(from, v))
-        res += v + "\n";
-    from.close();
-    return res;
-}
-
-void log::restore(std::string s) {
-    std::ofstream f;
-    LOG("restore: " << s);
-    f.open(name.c_str(), std::ios::trunc);
-    f << s;
-    f.close();
-}
-
-// XXX should be an atomic operation
-void log::loginstance(unsigned instance, std::string v) {
-    std::ofstream f(name, std::ios::app);
-    f << "done " << instance << " " << v << "\n";
-    f.close();
+    }
 }
 
-// an acceptor should call logprop(n_h) when it
-// receives a prepare to which it responds prepare_ok().
-void log::logprop(prop_t n_h) {
-    std::ofstream f;
-    f.open(name.c_str(), std::ios::app);
-    f << "propseen";
-    f << " ";
-    f << n_h.n;
-    f << " ";
-    f << n_h.m;
-    f << "\n";
-    f.close();
+string log::read() {
+    return (std::stringstream() << std::ifstream(name).rdbuf()).str();
 }
 
-// an acceptor should call logaccept(n_a, v_a) when it
-// receives an accept RPC to which it replies accept_ok().
-void log::logaccept(prop_t n, std::string v) {
-    std::ofstream f(name, std::ios::app);
-    f << "accepted " << n.n << " " << n.m << " " << v << "\n";
-    f.close();
+void log::write(string s) {
+    DEBUG_LOG << s;
+    std::ofstream(name, std::ios::trunc) << s;
 }