[util] Switch CDB to use util::const_buffer

CDBs can only ever be read-only, so just using const_buffer makes sense.
This commit is contained in:
Justin C. Miller
2023-01-29 22:43:16 -08:00
parent 8b29680850
commit 7771584a18
2 changed files with 8 additions and 8 deletions

View File

@@ -57,21 +57,21 @@ inline uint32_t strlen(const char *s) {
} // anon namespace } // anon namespace
cdb::cdb(buffer data) : cdb::cdb(const_buffer data) :
m_data(data) m_data(data)
{ {
if (data.count < min_length) if (data.count < min_length)
m_data = {0, 0}; m_data = {0, 0};
} }
const buffer const_buffer
cdb::retrieve(const char *key) const cdb::retrieve(const char *key) const
{ {
uint32_t len = strlen(key); uint32_t len = strlen(key);
return retrieve(reinterpret_cast<const uint8_t *>(key), len); return retrieve(reinterpret_cast<const uint8_t *>(key), len);
} }
const buffer const_buffer
cdb::retrieve(const uint8_t *key, uint32_t len) const cdb::retrieve(const uint8_t *key, uint32_t len) const
{ {
if (!m_data.pointer || !m_data.count) if (!m_data.pointer || !m_data.count)
@@ -95,7 +95,7 @@ cdb::retrieve(const uint8_t *key, uint32_t len) const
if (s->hash == h) { if (s->hash == h) {
record const *r = at<record>(s->position); record const *r = at<record>(s->position);
if (equal(key, len, &r->data[0], r->keylen)) if (equal(key, len, &r->data[0], r->keylen))
return buffer::from_const( &r->data[r->keylen], r->vallen ); return const_buffer::from( &r->data[r->keylen], r->vallen );
} }
i = (i + 1) % p->length; i = (i + 1) % p->length;

View File

@@ -10,20 +10,20 @@ namespace util {
class cdb class cdb
{ {
public: public:
cdb(buffer data); cdb(const_buffer data);
/// Retrieve a value from the database for the given key. /// Retrieve a value from the database for the given key.
/// \arg key A null-terminated string key /// \arg key A null-terminated string key
/// \returns A const util::buffer pointing to the data in memory. /// \returns A const util::buffer pointing to the data in memory.
/// The buffer will be {0, 0} if the key is not found. /// The buffer will be {0, 0} if the key is not found.
const buffer retrieve(const char *key) const; const_buffer retrieve(const char *key) const;
/// Retrieve a value from the database for the given key. /// Retrieve a value from the database for the given key.
/// \arg key Pointer to a key as an array of bytes /// \arg key Pointer to a key as an array of bytes
/// \arg len Length of the key /// \arg len Length of the key
/// \returns A const util::buffer pointing to the data in memory. /// \returns A const util::buffer pointing to the data in memory.
/// The buffer will be {0, 0} if the key is not found. /// The buffer will be {0, 0} if the key is not found.
const buffer retrieve(const uint8_t *key, uint32_t len) const; const_buffer retrieve(const uint8_t *key, uint32_t len) const;
private: private:
template <typename T> template <typename T>
@@ -31,7 +31,7 @@ private:
return reinterpret_cast<T const*>(util::offset_pointer<const void>(m_data.pointer, offset)); return reinterpret_cast<T const*>(util::offset_pointer<const void>(m_data.pointer, offset));
} }
buffer m_data; const_buffer m_data;
}; };
} // namespace } // namespace