Type cleanups
[invirt/third/libt4.git] / log.cc
1 #include "paxos.h"
2 #include <fstream>
3 #include <iostream>
4 #include "tprintf.h"
5
6 // Paxos must maintain some durable state (i.e., that survives power
7 // failures) to run Paxos correct.  This module implements a log with
8 // all durable state to run Paxos.  Since the values chosen correspond
9 // to views, the log contains all views since the beginning of time.
10
11 log::log(acceptor *_acc, std::string _me)
12   : pxs (_acc)
13 {
14   name = "paxos-" + _me + ".log";
15   logread();
16 }
17
18 void
19 log::logread(void)
20 {
21   std::ifstream from;
22   std::string type;
23   unsigned instance;
24
25   from.open(name.c_str());
26   LOG("logread");
27   while (from >> type) {
28     if (type == "done") {
29       std::string v;
30       from >> instance;
31       from.get();
32       getline(from, v);
33       pxs->values[instance] = v;
34       pxs->instance_h = instance;
35       LOG("logread: instance: " << instance << " w. v = " <<
36               pxs->values[instance]);
37       pxs->v_a.clear();
38       pxs->n_h.n = 0;
39       pxs->n_a.n = 0;
40     } else if (type == "propseen") {
41       from >> pxs->n_h.n;
42       from >> pxs->n_h.m;
43       LOG("logread: high update: " << pxs->n_h.n << "(" << pxs->n_h.m << ")");
44     } else if (type == "accepted") {
45       std::string v;
46       from >> pxs->n_a.n;
47       from >> pxs->n_a.m;
48       from.get();
49       getline(from, v);
50       pxs->v_a = v;
51       LOG("logread: prop update " << pxs->n_a.n << "(" << pxs->n_a.m << ") with v = " << pxs->v_a);
52     } else {
53       LOG("logread: unknown log record");
54       VERIFY(0);
55     }
56   } 
57   from.close();
58 }
59
60 std::string 
61 log::dump()
62 {
63   std::ifstream from;
64   std::string res;
65   std::string v;
66   from.open(name.c_str());
67   while (getline(from, v)) {
68     res = res + v + "\n";
69   }
70   from.close();
71   return res;
72 }
73
74 void
75 log::restore(std::string s)
76 {
77   std::ofstream f;
78   LOG("restore: " << s);
79   f.open(name.c_str(), std::ios::trunc);
80   f << s;
81   f.close();
82 }
83
84 // XXX should be an atomic operation
85 void
86 log::loginstance(unsigned instance, std::string v)
87 {
88   std::ofstream f;
89   f.open(name.c_str(), std::ios::app);
90   f << "done";
91   f << " ";
92   f << instance;
93   f << " ";
94   f << v;
95   f << "\n";
96   f.close();
97 }
98
99 // an acceptor should call logprop(n_h) when it
100 // receives a prepare to which it responds prepare_ok().
101 void
102 log::logprop(prop_t n_h)
103 {
104   std::ofstream f;
105   f.open(name.c_str(), std::ios::app);
106   f << "propseen";
107   f << " ";
108   f << n_h.n;
109   f << " ";
110   f << n_h.m;
111   f << "\n";
112   f.close();
113 }
114
115 // an acceptor should call logaccept(n_a, v_a) when it
116 // receives an accept RPC to which it replies accept_ok().
117 void
118 log::logaccept(prop_t n, std::string v)
119 {
120   std::ofstream f;
121   f.open(name.c_str(), std::ios::app);
122   f << "accepted";
123   f << " ";
124   f << n.n;
125   f << " ";
126   f << n.m;
127   f << " ";
128   f << v;
129   f << "\n";
130   f.close();
131 }
132