From f2faf8cdd9efd3ff044d3f67d5cae62addfec679 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Thu, 7 Jan 2016 13:23:12 +0000 Subject: [PATCH] Use MDB_APPEND mode where possible When keys are contiguous and monotonically increasing, this gets denser page utilization (doesn't leave padding in page splits). Can't be used for keys that are inserted in random order (e.g. hashes) In total this only saves around 1.5% of space compared to original DB code. The previous patch accounted for 0.8% savings on its own; the blocks tables just aren't that big. --- src/blockchain_db/lmdb/db_lmdb.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 2017ccb43..805b56ef4 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -707,7 +707,7 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou MDB_val_copy k(m_num_outputs); MDB_val_copy v(tx_hash); - result = mdb_cursor_put(m_cur_output_txs, &k, &v, 0); + result = mdb_cursor_put(m_cur_output_txs, &k, &v, MDB_APPEND); if (result) throw0(DB_ERROR(std::string("Failed to add output tx hash to db transaction: ").append(mdb_strerror(result)).c_str())); result = mdb_cursor_put(m_cur_tx_outputs, &v, &k, 0); @@ -715,7 +715,7 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou throw0(DB_ERROR(std::string("Failed to add to db transaction: ").append(mdb_strerror(result)).c_str())); MDB_val_copy val_local_index(local_index); - result = mdb_cursor_put(m_cur_output_indices, &k, &val_local_index, 0); + result = mdb_cursor_put(m_cur_output_indices, &k, &val_local_index, MDB_APPEND); if (result) throw0(DB_ERROR(std::string("Failed to add tx output index to db transaction: ").append(mdb_strerror(result)).c_str())); @@ -733,7 +733,7 @@ void BlockchainLMDB::add_output(const crypto::hash& tx_hash, const tx_out& tx_ou MDB_val_copy data(od); //MDB_val_copy val_pubkey(boost::get(tx_output.target).key); - if (mdb_cursor_put(m_cur_output_keys, &k, &data, 0)) + if (mdb_cursor_put(m_cur_output_keys, &k, &data, MDB_APPEND)) throw0(DB_ERROR("Failed to add output pubkey to db transaction")); } else @@ -2615,7 +2615,7 @@ void BlockchainLMDB::set_hard_fork_starting_height(uint8_t version, uint64_t hei MDB_val_copy val_key(version); MDB_val_copy val_value(height); - if (auto result = mdb_put(*txn_ptr, m_hf_starting_heights, &val_key, &val_value, 0)) + if (auto result = mdb_put(*txn_ptr, m_hf_starting_heights, &val_key, &val_value, MDB_APPEND)) throw1(DB_ERROR(std::string("Error adding hard fork starting height to db transaction: ").append(mdb_strerror(result)).c_str())); TXN_BLOCK_POSTFIX_SUCCESS(); @@ -2650,7 +2650,11 @@ void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version) MDB_val_copy val_key(height); MDB_val_copy val_value(version); - if (auto result = mdb_put(*txn_ptr, m_hf_versions, &val_key, &val_value, 0)) + int result; + result = mdb_put(*txn_ptr, m_hf_versions, &val_key, &val_value, MDB_APPEND); + if (result == MDB_KEYEXIST) + result = mdb_put(*txn_ptr, m_hf_versions, &val_key, &val_value, 0); + if (result) throw1(DB_ERROR(std::string("Error adding hard fork version to db transaction: ").append(mdb_strerror(result)).c_str())); TXN_BLOCK_POSTFIX_SUCCESS();