-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.h
90 lines (71 loc) · 2.77 KB
/
Database.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Simple key/value database for parallel analysis demo
// Ole Hansen, 17-Dec-20
#ifndef PPODD_DATABASE_H
#define PPODD_DATABASE_H
#include <string>
#include <vector>
#include <optional>
#include <iostream>
class Database {
public:
// Exception that can be thrown during reading
class bad_db_syntax : public std::runtime_error {
public:
explicit bad_db_syntax( const std::string& what_arg )
: std::runtime_error(what_arg) {}
};
// Construct empty database
Database() = default;
// Construct new database and open and parse filename
explicit Database( const std::string& filename );
// Open and parse filename. Deletes any previous values read.
// Returns number of keys found, a negative number on error.
int Open( const std::string& filename ) {
Clear();
return Append(filename);
}
// Open and parse filename. Append to existing values read.
// Returns number of keys found, or a negative number on error.
int Append( const std::string& filename );
// Get value for key and module. The module is essentially a key namespace.
// If search != 0, retry with module="" if not found (global fallback).
[[nodiscard]] std::optional<double> Get( const std::string& key,
const std::string& module,
int search = 0 ) const;
// Get value for global key
[[nodiscard]] std::optional<double> Get( const std::string& key ) const {
return Get(key, std::string(), 0);
}
// Clear database. Deletes all contents.
void Clear();
// Remove given key. Returns the key's value if it existed
std::optional<double> Erase( const std::string& key,
const std::string& module );
// Set given key to given value. Returns true if a new key was created.
bool Set(const std::string& key, const std::string& module, double val );
[[nodiscard]] bool IsReady() const { return m_is_ready; }
[[nodiscard]] size_t GetSize() const { return m_items.size(); }
void Print( std::ostream& os = std::cout ) const;
private:
// A key/value pair read from the database
struct Item {
Item() = default;
Item( std::string _key, std::string _module, double _value )
: key{std::move(_key)}
, module{std::move(_module)}
, value{_value}
{}
void print( std::ostream& os = std::cout ) const;
std::string key;
std::string module;
double value{};
} __attribute__((aligned(64)));
// Collection of key/value pairs read from the database.
std::vector<Item> m_items;
bool m_is_ready{false}; // True if database successfully read
static int ParseDBkey( const std::string& line, Item& item );
void ParseDBline( const std::string& line );
bool Set( Item& item );
};
extern Database database;
#endif //PPODD_DATABASE_H