added configurable NuDB block size support in xahaud

This commit is contained in:
Valon Mamudi
2025-06-09 02:19:53 +02:00
committed by Nicholas Dudfield
parent 9378f1a0ad
commit 8d377199ce
2 changed files with 80 additions and 1 deletions

View File

@@ -1127,6 +1127,40 @@
# it must be defined with the same value in both
# sections.
#
# Optional keys for NuDB only:
#
# nudb_block_size Block size in bytes for NuDB storage.
# Must be a power of 2 between 4096 and 32768.
# Default is 4096.
#
# This parameter controls the fundamental storage unit
# size for NuDB's internal data structures. The choice
# of block size can significantly impact performance
# depending on your storage hardware and filesystem:
#
# - 4096 bytes: Optimal for most standard SSDs and
# traditional filesystems (ext4, NTFS, HFS+).
# Provides good balance of performance and storage
# efficiency. Recommended for most deployments.
#
# - 8192-16384 bytes: May improve performance on
# high-end NVMe SSDs and copy-on-write filesystems
# like ZFS or Btrfs that benefit from larger block
# alignment. Can reduce metadata overhead for large
# databases.
#
# - 32768 bytes (32K): Maximum supported block size
# for high-performance scenarios with very fast
# storage. May increase memory usage and reduce
# efficiency for smaller databases.
#
# Note: This setting cannot be changed after database
# creation without rebuilding the entire database.
# Choose carefully based on your hardware and expected
# database size.
#
# Example: nudb_block_size=4096
#
# These keys modify the behavior of online_delete, and thus are only
# relevant if online_delete is defined and non-zero:

View File

@@ -48,6 +48,7 @@ public:
size_t const keyBytes_;
std::size_t const burstSize_;
std::string const name_;
std::size_t const blockSize_;
nudb::store db_;
std::atomic<bool> deletePath_;
Scheduler& scheduler_;
@@ -62,6 +63,7 @@ public:
, keyBytes_(keyBytes)
, burstSize_(burstSize)
, name_(get(keyValues, "path"))
, blockSize_(parseBlockSize(keyValues, journal))
, deletePath_(false)
, scheduler_(scheduler)
{
@@ -81,6 +83,7 @@ public:
, keyBytes_(keyBytes)
, burstSize_(burstSize)
, name_(get(keyValues, "path"))
, blockSize_(parseBlockSize(keyValues, journal))
, db_(context)
, deletePath_(false)
, scheduler_(scheduler)
@@ -137,7 +140,7 @@ public:
uid,
salt,
keyBytes_,
nudb::block_size(kp),
blockSize_,
0.50,
ec);
if (ec == nudb::errc::file_exists)
@@ -362,6 +365,48 @@ public:
{
return 3;
}
private:
static std::size_t
parseBlockSize(Section const& keyValues, beast::Journal journal)
{
std::size_t blockSize = 4096; // Default 4K
std::string blockSizeStr;
if (!get_if_exists(keyValues, "nudb_block_size", blockSizeStr))
{
return blockSize; // Early return with default
}
try
{
std::size_t const parsedBlockSize =
beast::lexicalCastThrow<std::size_t>(blockSizeStr);
// Validate: must be power of 2 between 4K and 32K
if (parsedBlockSize < 4096 || parsedBlockSize > 32768 ||
(parsedBlockSize & (parsedBlockSize - 1)) != 0)
{
JLOG(journal.warn())
<< "Invalid nudb_block_size: " << parsedBlockSize
<< ". Must be power of 2 between 4096 and 32768. Using "
"default 4096.";
return 4096;
}
JLOG(journal.info())
<< "Using custom NuDB block size: " << parsedBlockSize
<< " bytes";
return parsedBlockSize;
}
catch (std::exception const& e)
{
JLOG(journal.warn())
<< "Invalid nudb_block_size value: " << blockSizeStr
<< ". Using default 4096. Error: " << e.what();
return 4096;
}
}
};
//------------------------------------------------------------------------------