-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
129 lines (114 loc) · 2.62 KB
/
Copy pathtable.cpp
File metadata and controls
129 lines (114 loc) · 2.62 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Implementation of the Table class.
* Represents an in-memory key/value store table.
*
* CSF Assignment 5
*
* Ifrah Attar - iattar1@jh.edu
* Morgan Huberty - mhubert3@jh.edu
*
*/
#include <cassert>
#include "table.h"
#include "guard.h"
/**
* @brief Constructs a new Table with a given name.
* Initializes the table's name and its mutex.
* @param name The name of the table.
*/
Table::Table( const std::string &name )
: m_name( name )
{
pthread_mutex_init(&m_lock, nullptr);
}
/**
* @brief Destroys the Table, cleaning up its mutex.
*/
Table::~Table()
{
pthread_mutex_destroy(&m_lock);
}
/**
* @brief Acquires the table's lock (blocking).
*/
void Table::lock()
{
pthread_mutex_lock(&m_lock);
}
/**
* @brief Releases the table's lock.
*/
void Table::unlock()
{
pthread_mutex_unlock(&m_lock);
}
/**
* @brief Tries to acquire the table's lock (non-blocking).
* @return True if the lock was acquired, false otherwise.
*/
bool Table::trylock()
{
return pthread_mutex_trylock(&m_lock) == 0;
}
/**
* @brief Proposes setting a key to a specific value.
* @param key The key to set.
* @param value The value to associate with key.
*/
void Table::set( const std::string &key, const std::string &value )
{
m_pending_data[key] = value;
m_pending_deletions.erase(key);
}
/**
* @brief Gets value associated with a key.
* @param key The key whose value to retrieve.
* @return The value associated with the key.
* @throw OperationException if key does not exist.
*/
std::string Table::get( const std::string &key )
{
if (m_pending_data.count(key)) {
return m_pending_data.at(key);
}
if(m_pending_deletions.count(key)) {
throw OperationException("Key not found");
}
if (m_data.count(key)) {
return m_data.at(key);
}
throw OperationException("Key not found");
}
/**
* @brief Checks if a key exists in table.
* @param key The key to check.
* @return True if the key exists, false otherwise.
*/
bool Table::has_key( const std::string &key )
{
if(m_pending_deletions.count(key)) {
return false;
}
return m_pending_data.count(key) || m_data.count(key);
}
/**
* @brief Commits all pending changes to the table.
*/
void Table::commit_changes()
{
for (const auto& pair : m_pending_data) {
m_data[pair.first] = pair.second;
}
for (const auto& key : m_pending_deletions) {
m_data.erase(key);
}
rollback_changes();
}
/**
* @brief Rolls back all pending changes.
*/
void Table::rollback_changes()
{
m_pending_data.clear();
m_pending_deletions.clear();
}