pw_kvs: Add KVS method to get max size key-value
Add method to get the maximum size allowed for a key-value pair.
Change-Id: I09f83aeecdc2ea46ea7ae0d7891c89dc7c21eb13
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/14180
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: David Rogers <davidrogers@google.com>
diff --git a/pw_kvs/key_value_store_test.cc b/pw_kvs/key_value_store_test.cc
index f08f436..4479c56 100644
--- a/pw_kvs/key_value_store_test.cc
+++ b/pw_kvs/key_value_store_test.cc
@@ -430,8 +430,14 @@
default_format);
ASSERT_OK(kvs.Init());
+ size_t max_key_value_size = kvs.max_key_value_size_bytes();
+ EXPECT_EQ(max_key_value_size,
+ KeyValueStore::max_key_value_size_bytes(
+ flash.partition.sector_size_bytes()));
+
size_t max_value_size =
flash.partition.sector_size_bytes() - sizeof(EntryHeader) - 1;
+ EXPECT_EQ(max_key_value_size, (max_value_size + 1));
// Use the large_test_flash as a big chunk of data for the Put statement.
ASSERT_GT(sizeof(large_test_flash), max_value_size + 2 * sizeof(EntryHeader));
diff --git a/pw_kvs/public/pw_kvs/internal/entry.h b/pw_kvs/public/pw_kvs/internal/entry.h
index 5305ab6..31b0acd 100644
--- a/pw_kvs/public/pw_kvs/internal/entry.h
+++ b/pw_kvs/public/pw_kvs/internal/entry.h
@@ -138,6 +138,10 @@
std::max(partition.alignment_bytes(), kMinAlignmentBytes));
}
+ // Byte size of overhead (not-key, not-value) in an entry. Does not include
+ // any paddding used to get proper size alignment.
+ static constexpr size_t entry_overhead() { return sizeof(EntryHeader); }
+
Address address() const { return address_; }
void set_address(Address address) { address_ = address; }
diff --git a/pw_kvs/public/pw_kvs/key_value_store.h b/pw_kvs/public/pw_kvs/key_value_store.h
index 01b055d..6988d6f 100644
--- a/pw_kvs/public/pw_kvs/key_value_store.h
+++ b/pw_kvs/public/pw_kvs/key_value_store.h
@@ -303,6 +303,18 @@
bool error_detected() const { return error_detected_; }
+ // Maximum number of bytes allowed for a key-value combination.
+ size_t max_key_value_size_bytes() const {
+ return max_key_value_size_bytes(partition_.sector_size_bytes());
+ }
+
+ // Maximum number of bytes allowed for a given sector size for a key-value
+ // combination.
+ static constexpr size_t max_key_value_size_bytes(
+ size_t partition_sector_size_bytes) {
+ return partition_sector_size_bytes - Entry::entry_overhead();
+ }
+
// Check KVS for any error conditions. Primarily intended for test and
// internal use.
bool CheckForErrors();