Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// -*- mode: c++; c-file-style: "k&r"; c-basic-offset: 4 -*-
// vim: set ts=4 sw=4:
/***********************************************************************
*
* common/versionedKVStore.h:
*
* Simple versioned key-value store that tracks the write timestamp
* and latest read timestamp for each version.
*
**********************************************************************/
#ifndef _VERSIONED_KV_STORE_H_
#define _VERSIONED_KV_STORE_H_
#include "paxos-lib/lib/assert.h"
#include "paxos-lib/lib/message.h"
#include "common/timestamp.h"
#include <set>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iostream>
class VersionedKVStore
{
public:
VersionedKVStore();
~VersionedKVStore();
bool get(const std::string &key, std::pair<Timestamp, std::string> &value);
bool get(const std::string &key, const Timestamp &t, std::pair<Timestamp, std::string> &value);
bool getRange(const std::string &key, const Timestamp &t, std::pair<Timestamp, Timestamp> &range);
bool getLastRead(const std::string &key, Timestamp &readTime);
bool getLastRead(const std::string &key, const Timestamp &t, Timestamp &readTime);
void put(const std::string &key, const std::string &value, const Timestamp &t);
void commitGet(const std::string &key, const Timestamp &readTime, const Timestamp &commit);
private:
struct VersionedValue {
Timestamp write;
std::string value;
VersionedValue(Timestamp commit) : write(commit), value("tmp") { };
VersionedValue(Timestamp commit, std::string val) : write(commit), value(val) { };
friend bool operator> (const VersionedValue &v1, const VersionedValue &v2) {
return v1.write > v2.write;
};
friend bool operator< (const VersionedValue &v1, const VersionedValue &v2) {
return v1.write < v2.write;
};
};
/* Global store which keeps key -> (timestamp, value) list. */
std::unordered_map< std::string, std::set<VersionedValue> > store;
std::unordered_map< std::string, std::map< Timestamp, Timestamp > > lastReads;
bool inStore(const std::string &key);
void getValue(const std::string &key, const Timestamp &t, std::set<VersionedValue>::iterator &it);
};
#endif /* _VERSIONED_KV_STORE_H_ */