Skip to content

Commit

Permalink
Encapsulate the main database memory mapping operations (#538)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
LaurentiuCristofor and Laurentiu Cristofor authored Feb 10, 2021
1 parent cb9073b commit 47700a6
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 313 deletions.
28 changes: 15 additions & 13 deletions production/db/core/inc/db_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "gaia/db/db.hpp"

#include "gaia_internal/common/mmap_helpers.hpp"
#include "gaia_internal/common/retail_assert.hpp"
#include "gaia_internal/common/system_table_types.hpp"
#include "gaia_internal/db/triggers.hpp"
Expand All @@ -29,14 +30,14 @@ class client
/**
* @throws no_open_transaction if there is no active transaction.
*/
friend gaia::db::locators_t* gaia::db::get_shared_locators();
friend gaia::db::locators_t* gaia::db::get_locators();

/**
* @throws no_active_session if there is no active session.
*/
friend gaia::db::shared_counters_t* gaia::db::get_shared_counters();
friend gaia::db::shared_data_t* gaia::db::get_shared_data();
friend gaia::db::shared_id_index_t* gaia::db::get_shared_id_index();
friend gaia::db::counters_t* gaia::db::get_counters();
friend gaia::db::data_t* gaia::db::get_data();
friend gaia::db::id_index_t* gaia::db::get_id_index();

friend gaia::db::memory_manager::address_offset_t gaia::db::allocate_object(
gaia_locator_t locator,
Expand All @@ -45,7 +46,7 @@ class client
public:
static inline bool is_transaction_active()
{
return (s_locators != nullptr);
return (s_private_locators.is_initialized());
}

/**
Expand Down Expand Up @@ -77,16 +78,17 @@ class client
private:
// These fields have transaction lifetime.
thread_local static inline gaia_txn_id_t s_txn_id = c_invalid_gaia_txn_id;
thread_local static inline txn_log_t* s_log = nullptr;
thread_local static inline int s_fd_log = -1;
thread_local static inline mapped_log_t s_log{};

thread_local static inline locators_t* s_locators = nullptr;
thread_local static inline mapped_data_t<locators_t> s_private_locators;

// These fields have session lifetime.
thread_local static inline int s_fd_locators = -1;
thread_local static inline shared_counters_t* s_counters = nullptr;
thread_local static inline shared_data_t* s_data = nullptr;
thread_local static inline shared_id_index_t* s_id_index = nullptr;

thread_local static inline mapped_data_t<counters_t> s_shared_counters;
thread_local static inline mapped_data_t<data_t> s_shared_data;
thread_local static inline mapped_data_t<id_index_t> s_shared_id_index;

thread_local static inline int s_session_socket = -1;

// s_events has transaction lifetime and is cleared after each transaction.
Expand Down Expand Up @@ -185,13 +187,13 @@ class client
}

// We never allocate more than `c_max_log_records` records in the log.
if (s_log->count == c_max_log_records)
if (s_log.log()->count == c_max_log_records)
{
throw transaction_object_limit_exceeded();
}

// Initialize the new record and increment the record count.
txn_log_t::log_record_t* lr = s_log->log_records + s_log->count++;
txn_log_t::log_record_t* lr = s_log.log()->log_records + s_log.log()->count++;
lr->locator = locator;
lr->old_offset = old_offset;
lr->new_offset = new_offset;
Expand Down
10 changes: 5 additions & 5 deletions production/db/core/inc/db_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class db_hash_map
public:
static hash_node_t* insert(gaia::common::gaia_id_t id)
{
locators_t* locators = gaia::db::get_shared_locators();
shared_id_index_t* id_index = gaia::db::get_shared_id_index();
locators_t* locators = gaia::db::get_locators();
id_index_t* id_index = gaia::db::get_id_index();
if (locators == nullptr)
{
throw no_open_transaction();
Expand Down Expand Up @@ -76,8 +76,8 @@ class db_hash_map

static gaia_locator_t find(gaia::common::gaia_id_t id)
{
locators_t* locators = gaia::db::get_shared_locators();
shared_id_index_t* id_index = gaia::db::get_shared_id_index();
locators_t* locators = gaia::db::get_locators();
id_index_t* id_index = gaia::db::get_id_index();
if (locators == nullptr)
{
throw no_open_transaction();
Expand Down Expand Up @@ -109,7 +109,7 @@ class db_hash_map

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

while (node->id)
Expand Down
20 changes: 10 additions & 10 deletions production/db/core/inc/db_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ namespace db

inline gaia_id_t allocate_id()
{
shared_counters_t* counters = gaia::db::get_shared_counters();
counters_t* counters = gaia::db::get_counters();
gaia_id_t id = __sync_add_and_fetch(&counters->last_id, 1);
return id;
}

inline gaia_type_t allocate_type()
{
shared_counters_t* counters = gaia::db::get_shared_counters();
counters_t* counters = gaia::db::get_counters();
gaia_type_t type = __sync_add_and_fetch(&counters->last_type_id, 1);
return type;
}

inline gaia_txn_id_t allocate_txn_id()
{
shared_counters_t* counters = gaia::db::get_shared_counters();
counters_t* counters = gaia::db::get_counters();
gaia_txn_id_t txn_id = __sync_add_and_fetch(&counters->last_txn_id, 1);
return txn_id;
}

inline gaia_locator_t allocate_locator()
{
shared_counters_t* counters = gaia::db::get_shared_counters();
counters_t* counters = gaia::db::get_counters();

// We need an acquire barrier before reading `last_locator`. We can
// change this full barrier to an acquire barrier when we change to proper
Expand Down Expand Up @@ -81,7 +81,7 @@ inline void update_locator(
gaia_locator_t locator,
gaia::db::memory_manager::address_offset_t offset)
{
locators_t* locators = gaia::db::get_shared_locators();
locators_t* locators = gaia::db::get_locators();
if (!locators)
{
throw no_open_transaction();
Expand All @@ -92,8 +92,8 @@ inline void update_locator(

inline bool locator_exists(gaia_locator_t locator)
{
locators_t* locators = gaia::db::get_shared_locators();
shared_counters_t* counters = gaia::db::get_shared_counters();
locators_t* locators = gaia::db::get_locators();
counters_t* counters = gaia::db::get_counters();

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

inline gaia_offset_t locator_to_offset(gaia_locator_t locator)
{
locators_t* locators = gaia::db::get_shared_locators();
locators_t* locators = gaia::db::get_locators();
return locator_exists(locator)
? (*locators)[locator]
: c_invalid_gaia_offset;
}

inline db_object_t* offset_to_ptr(gaia_offset_t offset)
{
shared_data_t* data = gaia::db::get_shared_data();
data_t* data = gaia::db::get_data();
return (offset != c_invalid_gaia_offset)
? reinterpret_cast<db_object_t*>(data->objects + offset)
: nullptr;
Expand All @@ -130,7 +130,7 @@ inline db_object_t* locator_to_ptr(gaia_locator_t locator)
// are no memory barriers.
inline gaia_txn_id_t get_last_txn_id()
{
shared_counters_t* counters = gaia::db::get_shared_counters();
counters_t* counters = gaia::db::get_counters();
return counters->last_txn_id;
}

Expand Down
Loading

0 comments on commit 47700a6

Please sign in to comment.