X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/2546a41ad36fdc9ef6471cb35a1d56930ae1b527..5a5c578e2e358a121cdb9234a6cb11c4ecfbf323:/log.cc?ds=sidebyside diff --git a/log.cc b/log.cc index 627b7ac..95c40e3 100644 --- a/log.cc +++ b/log.cc @@ -1,28 +1,25 @@ +#include "log.h" #include "paxos.h" -#include -#include -#include "threaded_log.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. -log::log(acceptor *_acc, std::string _me) : pxs (_acc) { +log::log(proposer_acceptor *_acc, string _me) : pxs (_acc) { name = "paxos-" + _me + ".log"; logread(); } void log::logread(void) { - std::ifstream from; - std::string type; + ifstream from(name); + string type; unsigned instance; - from.open(name.c_str()); LOG("logread"); while (from >> type) { if (type == "done") { - std::string v; + string v; from >> instance; from.get(); getline(from, v); @@ -34,13 +31,11 @@ void log::logread(void) { pxs->n_h.n = 0; pxs->n_a.n = 0; } else if (type == "propseen") { - from >> pxs->n_h.n; - from >> pxs->n_h.m; + from >> pxs->n_h.n >> 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; + string v; + from >> pxs->n_a.n >> pxs->n_a.m; from.get(); getline(from, v); pxs->v_a = v; @@ -53,28 +48,26 @@ void log::logread(void) { from.close(); } -std::string log::dump() { - std::ifstream from; - std::string res; - std::string v; - from.open(name.c_str()); +string log::dump() { + ifstream from(name); + string res; + string v; while (getline(from, v)) res += v + "\n"; from.close(); return res; } -void log::restore(std::string s) { - std::ofstream f; +void log::restore(string s) { LOG("restore: " << s); - f.open(name.c_str(), std::ios::trunc); + ofstream f(name, 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); +void log::loginstance(unsigned instance, string v) { + ofstream f(name, std::ios::app); f << "done " << instance << " " << v << "\n"; f.close(); } @@ -82,21 +75,15 @@ void log::loginstance(unsigned instance, std::string v) { // 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"; + ofstream f(name, std::ios::app); + f << "propseen " << n_h.n << " " << n_h.m << "\n"; f.close(); } // 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); +void log::logaccept(prop_t n, string v) { + ofstream f(name, std::ios::app); f << "accepted " << n.n << " " << n.m << " " << v << "\n"; f.close(); }