Skip to content

Commit 47700a6

Browse files
LaurentiuCristoforLaurentiu Cristofor
andauthored
Encapsulate the main database memory mapping operations (#538)
* Refactor main database server memory mapping operations and other cleanup * Improve implementation of is_initialized() * Remove redundant scope_guard * Remove inadvertently added json file * Some additional name cleanup and extension of the mapped_data_t functionality * Extend use of mapped_data_t to database client code * Rename variables, methods, and type related to mapped data * Remove state enum and rename is_closed back to is_initialized * Add a new helper, move helper implementations, and additional renaming * Update mapped log helper * Use mapped_log_t helper in client and server * Update servver log operation * Update log operations in client code * Add comments to the new classes Co-authored-by: Laurentiu Cristofor <[email protected]>
1 parent cb9073b commit 47700a6

15 files changed

+417
-313
lines changed

production/db/core/inc/db_client.hpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "gaia/db/db.hpp"
99

10+
#include "gaia_internal/common/mmap_helpers.hpp"
1011
#include "gaia_internal/common/retail_assert.hpp"
1112
#include "gaia_internal/common/system_table_types.hpp"
1213
#include "gaia_internal/db/triggers.hpp"
@@ -29,14 +30,14 @@ class client
2930
/**
3031
* @throws no_open_transaction if there is no active transaction.
3132
*/
32-
friend gaia::db::locators_t* gaia::db::get_shared_locators();
33+
friend gaia::db::locators_t* gaia::db::get_locators();
3334

3435
/**
3536
* @throws no_active_session if there is no active session.
3637
*/
37-
friend gaia::db::shared_counters_t* gaia::db::get_shared_counters();
38-
friend gaia::db::shared_data_t* gaia::db::get_shared_data();
39-
friend gaia::db::shared_id_index_t* gaia::db::get_shared_id_index();
38+
friend gaia::db::counters_t* gaia::db::get_counters();
39+
friend gaia::db::data_t* gaia::db::get_data();
40+
friend gaia::db::id_index_t* gaia::db::get_id_index();
4041

4142
friend gaia::db::memory_manager::address_offset_t gaia::db::allocate_object(
4243
gaia_locator_t locator,
@@ -45,7 +46,7 @@ class client
4546
public:
4647
static inline bool is_transaction_active()
4748
{
48-
return (s_locators != nullptr);
49+
return (s_private_locators.is_initialized());
4950
}
5051

5152
/**
@@ -77,16 +78,17 @@ class client
7778
private:
7879
// These fields have transaction lifetime.
7980
thread_local static inline gaia_txn_id_t s_txn_id = c_invalid_gaia_txn_id;
80-
thread_local static inline txn_log_t* s_log = nullptr;
81-
thread_local static inline int s_fd_log = -1;
81+
thread_local static inline mapped_log_t s_log{};
8282

83-
thread_local static inline locators_t* s_locators = nullptr;
83+
thread_local static inline mapped_data_t<locators_t> s_private_locators;
8484

8585
// These fields have session lifetime.
8686
thread_local static inline int s_fd_locators = -1;
87-
thread_local static inline shared_counters_t* s_counters = nullptr;
88-
thread_local static inline shared_data_t* s_data = nullptr;
89-
thread_local static inline shared_id_index_t* s_id_index = nullptr;
87+
88+
thread_local static inline mapped_data_t<counters_t> s_shared_counters;
89+
thread_local static inline mapped_data_t<data_t> s_shared_data;
90+
thread_local static inline mapped_data_t<id_index_t> s_shared_id_index;
91+
9092
thread_local static inline int s_session_socket = -1;
9193

9294
// s_events has transaction lifetime and is cleared after each transaction.
@@ -185,13 +187,13 @@ class client
185187
}
186188

187189
// We never allocate more than `c_max_log_records` records in the log.
188-
if (s_log->count == c_max_log_records)
190+
if (s_log.log()->count == c_max_log_records)
189191
{
190192
throw transaction_object_limit_exceeded();
191193
}
192194

193195
// Initialize the new record and increment the record count.
194-
txn_log_t::log_record_t* lr = s_log->log_records + s_log->count++;
196+
txn_log_t::log_record_t* lr = s_log.log()->log_records + s_log.log()->count++;
195197
lr->locator = locator;
196198
lr->old_offset = old_offset;
197199
lr->new_offset = new_offset;

production/db/core/inc/db_hash_map.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class db_hash_map
2121
public:
2222
static hash_node_t* insert(gaia::common::gaia_id_t id)
2323
{
24-
locators_t* locators = gaia::db::get_shared_locators();
25-
shared_id_index_t* id_index = gaia::db::get_shared_id_index();
24+
locators_t* locators = gaia::db::get_locators();
25+
id_index_t* id_index = gaia::db::get_id_index();
2626
if (locators == nullptr)
2727
{
2828
throw no_open_transaction();
@@ -76,8 +76,8 @@ class db_hash_map
7676

7777
static gaia_locator_t find(gaia::common::gaia_id_t id)
7878
{
79-
locators_t* locators = gaia::db::get_shared_locators();
80-
shared_id_index_t* id_index = gaia::db::get_shared_id_index();
79+
locators_t* locators = gaia::db::get_locators();
80+
id_index_t* id_index = gaia::db::get_id_index();
8181
if (locators == nullptr)
8282
{
8383
throw no_open_transaction();
@@ -109,7 +109,7 @@ class db_hash_map
109109

110110
static void remove(gaia::common::gaia_id_t id)
111111
{
112-
shared_id_index_t* id_index = gaia::db::get_shared_id_index();
112+
id_index_t* id_index = gaia::db::get_id_index();
113113
hash_node_t* node = id_index->hash_nodes + (id % c_hash_buckets);
114114

115115
while (node->id)

production/db/core/inc/db_helpers.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@ namespace db
2626

2727
inline gaia_id_t allocate_id()
2828
{
29-
shared_counters_t* counters = gaia::db::get_shared_counters();
29+
counters_t* counters = gaia::db::get_counters();
3030
gaia_id_t id = __sync_add_and_fetch(&counters->last_id, 1);
3131
return id;
3232
}
3333

3434
inline gaia_type_t allocate_type()
3535
{
36-
shared_counters_t* counters = gaia::db::get_shared_counters();
36+
counters_t* counters = gaia::db::get_counters();
3737
gaia_type_t type = __sync_add_and_fetch(&counters->last_type_id, 1);
3838
return type;
3939
}
4040

4141
inline gaia_txn_id_t allocate_txn_id()
4242
{
43-
shared_counters_t* counters = gaia::db::get_shared_counters();
43+
counters_t* counters = gaia::db::get_counters();
4444
gaia_txn_id_t txn_id = __sync_add_and_fetch(&counters->last_txn_id, 1);
4545
return txn_id;
4646
}
4747

4848
inline gaia_locator_t allocate_locator()
4949
{
50-
shared_counters_t* counters = gaia::db::get_shared_counters();
50+
counters_t* counters = gaia::db::get_counters();
5151

5252
// We need an acquire barrier before reading `last_locator`. We can
5353
// change this full barrier to an acquire barrier when we change to proper
@@ -81,7 +81,7 @@ inline void update_locator(
8181
gaia_locator_t locator,
8282
gaia::db::memory_manager::address_offset_t offset)
8383
{
84-
locators_t* locators = gaia::db::get_shared_locators();
84+
locators_t* locators = gaia::db::get_locators();
8585
if (!locators)
8686
{
8787
throw no_open_transaction();
@@ -92,8 +92,8 @@ inline void update_locator(
9292

9393
inline bool locator_exists(gaia_locator_t locator)
9494
{
95-
locators_t* locators = gaia::db::get_shared_locators();
96-
shared_counters_t* counters = gaia::db::get_shared_counters();
95+
locators_t* locators = gaia::db::get_locators();
96+
counters_t* counters = gaia::db::get_counters();
9797

9898
// We need an acquire barrier before reading `last_locator`. We can
9999
// change this full barrier to an acquire barrier when we change to proper
@@ -107,15 +107,15 @@ inline bool locator_exists(gaia_locator_t locator)
107107

108108
inline gaia_offset_t locator_to_offset(gaia_locator_t locator)
109109
{
110-
locators_t* locators = gaia::db::get_shared_locators();
110+
locators_t* locators = gaia::db::get_locators();
111111
return locator_exists(locator)
112112
? (*locators)[locator]
113113
: c_invalid_gaia_offset;
114114
}
115115

116116
inline db_object_t* offset_to_ptr(gaia_offset_t offset)
117117
{
118-
shared_data_t* data = gaia::db::get_shared_data();
118+
data_t* data = gaia::db::get_data();
119119
return (offset != c_invalid_gaia_offset)
120120
? reinterpret_cast<db_object_t*>(data->objects + offset)
121121
: nullptr;
@@ -130,7 +130,7 @@ inline db_object_t* locator_to_ptr(gaia_locator_t locator)
130130
// are no memory barriers.
131131
inline gaia_txn_id_t get_last_txn_id()
132132
{
133-
shared_counters_t* counters = gaia::db::get_shared_counters();
133+
counters_t* counters = gaia::db::get_counters();
134134
return counters->last_txn_id;
135135
}
136136

0 commit comments

Comments
 (0)