diff --git a/pw_kvs/BUILD.bazel b/pw_kvs/BUILD.bazel index bc2da8ef08..5e67a7108d 100644 --- a/pw_kvs/BUILD.bazel +++ b/pw_kvs/BUILD.bazel @@ -48,7 +48,6 @@ cc_library( "public/pw_kvs/flash_memory.h", "public/pw_kvs/format.h", "public/pw_kvs/io.h", - "public/pw_kvs/key.h", "public/pw_kvs/key_value_store.h", ], includes = ["public"], @@ -563,18 +562,6 @@ pw_cc_test( ], ) -pw_cc_test( - name = "key_test", - srcs = [ - "key_test.cc", - ], - deps = [ - ":pw_kvs", - "//pw_status", - "//pw_unit_test", - ], -) - pw_cc_test( name = "key_value_store_test", srcs = ["key_value_store_test.cc"], diff --git a/pw_kvs/BUILD.gn b/pw_kvs/BUILD.gn index 428fbca3e7..b04dd0767e 100644 --- a/pw_kvs/BUILD.gn +++ b/pw_kvs/BUILD.gn @@ -42,7 +42,6 @@ pw_source_set("pw_kvs") { "public/pw_kvs/flash_test_partition.h", "public/pw_kvs/format.h", "public/pw_kvs/io.h", - "public/pw_kvs/key.h", "public/pw_kvs/key_value_store.h", ] sources = [ @@ -379,7 +378,6 @@ pw_test_group("tests") { ":alignment_test", ":checksum_test", ":converts_to_span_test", - ":key_test", ] if (defined(pw_toolchain_SCOPE.is_host_toolchain) && @@ -651,11 +649,6 @@ pw_test("sectors_test") { sources = [ "sectors_test.cc" ] } -pw_test("key_test") { - deps = [ ":pw_kvs" ] - sources = [ "key_test.cc" ] -} - pw_test("key_value_store_wear_test") { deps = [ ":fake_flash", diff --git a/pw_kvs/CMakeLists.txt b/pw_kvs/CMakeLists.txt index fa13b11c7f..2709fd3bf8 100644 --- a/pw_kvs/CMakeLists.txt +++ b/pw_kvs/CMakeLists.txt @@ -31,7 +31,6 @@ pw_add_library(pw_kvs STATIC public/pw_kvs/flash_test_partition.h public/pw_kvs/format.h public/pw_kvs/io.h - public/pw_kvs/key.h public/pw_kvs/key_value_store.h public/pw_kvs/internal/entry.h public/pw_kvs/internal/entry_cache.h @@ -663,16 +662,6 @@ pw_add_test(pw_kvs.sectors_test pw_kvs ) -pw_add_test(pw_kvs.key_test - SOURCES - key_test.cc - PRIVATE_DEPS - pw_kvs - GROUPS - modules - pw_kvs -) - pw_add_test(pw_kvs.key_value_store_wear_test SOURCES key_value_store_wear_test.cc diff --git a/pw_kvs/entry.cc b/pw_kvs/entry.cc index 6e6fb0f4e5..a7bf4f146f 100644 --- a/pw_kvs/entry.cc +++ b/pw_kvs/entry.cc @@ -76,7 +76,7 @@ Status Entry::ReadKey(FlashPartition& partition, Entry::Entry(FlashPartition& partition, Address address, const EntryFormat& format, - Key key, + std::string_view key, span value, uint16_t value_size_bytes, uint32_t transaction_id) @@ -98,7 +98,8 @@ Entry::Entry(FlashPartition& partition, } } -StatusWithSize Entry::Write(Key key, span value) const { +StatusWithSize Entry::Write(std::string_view key, + span value) const { FlashPartition::Output flash(partition(), address_); return AlignedWrite( flash, @@ -184,7 +185,8 @@ Status Entry::ValueMatches(span value) const { return OkStatus(); } -Status Entry::VerifyChecksum(Key key, span value) const { +Status Entry::VerifyChecksum(std::string_view key, + span value) const { if (checksum_algo_ == nullptr) { return header_.checksum == 0 ? OkStatus() : Status::DataLoss(); } @@ -255,7 +257,7 @@ void Entry::DebugLog() const { PW_LOG_DEBUG(" Alignment = 0x%x", unsigned(alignment_bytes())); } -span Entry::CalculateChecksum(const Key key, +span Entry::CalculateChecksum(const std::string_view key, span value) const { checksum_algo_->Reset(); diff --git a/pw_kvs/entry_cache.cc b/pw_kvs/entry_cache.cc index d066a18985..eae685a9a7 100644 --- a/pw_kvs/entry_cache.cc +++ b/pw_kvs/entry_cache.cc @@ -63,7 +63,7 @@ void EntryMetadata::Reset(const KeyDescriptor& descriptor, Address address) { StatusWithSize EntryCache::Find(FlashPartition& partition, const Sectors& sectors, const EntryFormats& formats, - Key key, + std::string_view key, EntryMetadata* metadata) const { const uint32_t hash = internal::Hash(key); Entry::KeyBuffer key_buffer; @@ -72,13 +72,13 @@ StatusWithSize EntryCache::Find(FlashPartition& partition, for (size_t i = 0; i < descriptors_.size(); ++i) { if (descriptors_[i].key_hash == hash) { bool key_found = false; - Key read_key; + std::string_view read_key; for (Address address : addresses(i)) { Status read_result = Entry::ReadKey(partition, address, key.size(), key_buffer.data()); - read_key = Key(key_buffer.data(), key.size()); + read_key = std::string_view(key_buffer.data(), key.size()); if (read_result.ok() && hash == internal::Hash(read_key)) { key_found = true; diff --git a/pw_kvs/flash_memory.cc b/pw_kvs/flash_memory.cc index a28d910fb3..ea4fb83c4d 100644 --- a/pw_kvs/flash_memory.cc +++ b/pw_kvs/flash_memory.cc @@ -31,8 +31,6 @@ namespace pw::kvs { using std::byte; -#if PW_CXX_STANDARD_IS_SUPPORTED(17) - Status FlashPartition::Writer::DoWrite(ConstByteSpan data) { if (partition_.size_bytes() <= position_) { return Status::OutOfRange(); @@ -66,8 +64,6 @@ StatusWithSize FlashPartition::Reader::DoRead(ByteSpan data) { return sws; } -#endif // PW_CXX_STANDARD_IS_SUPPORTED(17) - StatusWithSize FlashPartition::Output::DoWrite(span data) { PW_TRY_WITH_SIZE(flash_.Write(address_, data)); address_ += data.size(); diff --git a/pw_kvs/flash_partition_stream_test.cc b/pw_kvs/flash_partition_stream_test.cc index 9917a60842..3111344e90 100644 --- a/pw_kvs/flash_partition_stream_test.cc +++ b/pw_kvs/flash_partition_stream_test.cc @@ -26,8 +26,6 @@ #include "pw_span/span.h" #include "pw_unit_test/framework.h" -#if PW_CXX_STANDARD_IS_SUPPORTED(17) - #ifndef PW_FLASH_TEST_ALIGNMENT #define PW_FLASH_TEST_ALIGNMENT 1 #endif @@ -505,5 +503,3 @@ TEST_F(FlashStreamTest, Invald_Ops) { } // namespace } // namespace pw::kvs - -#endif // PW_CXX_STANDARD_IS_SUPPORTED(17) diff --git a/pw_kvs/key_test.cc b/pw_kvs/key_test.cc deleted file mode 100644 index ad91d30975..0000000000 --- a/pw_kvs/key_test.cc +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright 2020 The Pigweed Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not -// use this file except in compliance with the License. You may obtain a copy of -// the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -// License for the specific language governing permissions and limitations under -// the License. - -#include "pw_kvs/key.h" - -#include -#include - -#include "pw_unit_test/framework.h" - -namespace pw::kvs { - -namespace { - -constexpr const char kTestString[] = "test_string"; -constexpr std::string_view kTestStringView{kTestString}; - -// kTestString2 starts with same string as kTestString. -constexpr const char kTestString2[] = "test_string2"; - -} // namespace - -TEST(Key, ConstructorEmpty) { - Key key; - EXPECT_EQ(key.size(), 0u); - EXPECT_TRUE(key.empty()); - EXPECT_EQ(key.data(), nullptr); - EXPECT_EQ(key.begin(), key.end()); -} - -TEST(Key, ConstructorString) { - std::string str{kTestStringView}; - Key key{str}; - EXPECT_EQ(key.size(), kTestStringView.size()); - EXPECT_FALSE(key.empty()); - EXPECT_EQ(key.data(), str.data()); - EXPECT_EQ(key.front(), kTestStringView.front()); - EXPECT_EQ(key.back(), kTestStringView.back()); -} - -TEST(Key, ConstructorStringView) { - Key key{kTestStringView}; - EXPECT_EQ(key.size(), kTestStringView.size()); - EXPECT_FALSE(key.empty()); - EXPECT_EQ(key.data(), kTestStringView.data()); - EXPECT_EQ(key.front(), kTestStringView.front()); - EXPECT_EQ(key.back(), kTestStringView.back()); -} - -TEST(Key, ConstructorNullTermString) { - Key key{kTestString}; - EXPECT_EQ(key.size(), kTestStringView.size()); - EXPECT_FALSE(key.empty()); - EXPECT_EQ(key.data(), kTestString); - EXPECT_EQ(key.front(), kTestStringView.front()); - EXPECT_EQ(key.back(), kTestStringView.back()); -} - -TEST(Key, ConstructorCharPtrLength) { - Key key{kTestString, kTestStringView.size() + 1}; // include null terminator - EXPECT_EQ(key.size(), kTestStringView.size() + 1); - EXPECT_FALSE(key.empty()); - EXPECT_EQ(key.data(), kTestStringView.data()); - EXPECT_EQ(key.front(), kTestStringView.front()); - EXPECT_EQ(key.back(), '\0'); -} - -TEST(Key, ConstructorCopy) { - Key key1{kTestString}; - Key key{key1}; - EXPECT_EQ(key.size(), kTestStringView.size()); - EXPECT_FALSE(key.empty()); - EXPECT_EQ(key.data(), kTestStringView.data()); - EXPECT_EQ(key.front(), kTestStringView.front()); - EXPECT_EQ(key.back(), kTestStringView.back()); -} - -TEST(Key, Access) { - Key key{kTestStringView}; - for (size_t i = 0; i < key.size(); i++) { - EXPECT_EQ(key[i], kTestStringView[i]); - EXPECT_EQ(key.at(i), kTestStringView.at(i)); - } -} - -TEST(Key, Iterator) { - size_t i = 0; - for (auto c : Key{kTestString}) { - EXPECT_EQ(c, kTestString[i++]); - } -} - -TEST(Key, Same) { - // Since start of two test strings are the same, verify those are equal. - EXPECT_TRUE((Key{kTestString} == Key{kTestString2, kTestStringView.size()})); - EXPECT_FALSE((Key{kTestString} != Key{kTestString2, kTestStringView.size()})); -} - -TEST(Key, Different) { - EXPECT_FALSE(Key{kTestString} == Key{kTestString2}); - EXPECT_TRUE(Key{kTestString} != Key{kTestString2}); -} - -TEST(Key, DifferentWithSameLength) { - // Start second test string offset by one. - EXPECT_FALSE( - (Key{kTestString} == Key{kTestString2 + 1, kTestStringView.size()})); - EXPECT_TRUE( - (Key{kTestString} != Key{kTestString2 + 1, kTestStringView.size()})); -} - -TEST(Key, ConvertToStringView) { - std::string_view view = Key{kTestString}; - EXPECT_TRUE(view == kTestStringView); -} - -} // namespace pw::kvs diff --git a/pw_kvs/key_value_store.cc b/pw_kvs/key_value_store.cc index 130c9be54a..f6d36decf3 100644 --- a/pw_kvs/key_value_store.cc +++ b/pw_kvs/key_value_store.cc @@ -32,7 +32,7 @@ namespace { using std::byte; -constexpr bool InvalidKey(Key key) { +constexpr bool InvalidKey(std::string_view key) { return key.empty() || (key.size() > internal::Entry::kMaxKeyLength); } @@ -239,7 +239,7 @@ Status KeyValueStore::InitializeMetadata() { // initializing last_new_sector_. for (EntryMetadata& metadata : entry_cache_) { if (metadata.addresses().size() < redundancy()) { - PW_LOG_DEBUG("Key 0x%08x missing copies, has %u, needs %u", + PW_LOG_DEBUG("std::string_view 0x%08x missing copies, has %u, needs %u", unsigned(metadata.hash()), unsigned(metadata.addresses().size()), unsigned(redundancy())); @@ -368,7 +368,7 @@ Status KeyValueStore::LoadEntry(Address entry_address, // Read the key from flash & validate the entry (which reads the value). Entry::KeyBuffer key_buffer; PW_TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer)); - const Key key(key_buffer.data(), key_length); + const std::string_view key(key_buffer.data(), key_length); PW_TRY(entry.VerifyChecksumInFlash()); @@ -444,7 +444,7 @@ Status KeyValueStore::RemoveDeletedKeyEntries() { #endif // PW_KVS_REMOVE_DELETED_KEYS_IN_HEAVY_MAINTENANCE -StatusWithSize KeyValueStore::Get(Key key, +StatusWithSize KeyValueStore::Get(std::string_view key, span value_buffer, size_t offset_bytes) const { PW_TRY_WITH_SIZE(CheckReadOperation(key)); @@ -455,7 +455,7 @@ StatusWithSize KeyValueStore::Get(Key key, return Get(key, metadata, value_buffer, offset_bytes); } -Status KeyValueStore::PutBytes(Key key, span value) { +Status KeyValueStore::PutBytes(std::string_view key, span value) { PW_TRY(CheckWriteOperation(key)); PW_LOG_DEBUG("Writing key/value; key length=%u, value length=%u", unsigned(key.size()), @@ -487,7 +487,7 @@ Status KeyValueStore::PutBytes(Key key, span value) { return status; } -Status KeyValueStore::Delete(Key key) { +Status KeyValueStore::Delete(std::string_view key) { PW_TRY(CheckWriteOperation(key)); EntryMetadata metadata; @@ -529,7 +529,7 @@ KeyValueStore::iterator KeyValueStore::begin() const { return iterator(*this, cache_iterator); } -StatusWithSize KeyValueStore::ValueSize(Key key) const { +StatusWithSize KeyValueStore::ValueSize(std::string_view key) const { PW_TRY_WITH_SIZE(CheckReadOperation(key)); EntryMetadata metadata; @@ -557,7 +557,8 @@ Status KeyValueStore::ReadEntry(const EntryMetadata& metadata, return read_result; } -Status KeyValueStore::FindEntry(Key key, EntryMetadata* metadata_out) const { +Status KeyValueStore::FindEntry(std::string_view key, + EntryMetadata* metadata_out) const { StatusWithSize find_result = entry_cache_.Find(partition_, sectors_, formats_, key, metadata_out); @@ -567,7 +568,8 @@ Status KeyValueStore::FindEntry(Key key, EntryMetadata* metadata_out) const { return find_result.status(); } -Status KeyValueStore::FindExisting(Key key, EntryMetadata* metadata_out) const { +Status KeyValueStore::FindExisting(std::string_view key, + EntryMetadata* metadata_out) const { Status status = FindEntry(key, metadata_out); // If the key's hash collides with an existing key or if the key is deleted, @@ -579,7 +581,7 @@ Status KeyValueStore::FindExisting(Key key, EntryMetadata* metadata_out) const { return status; } -StatusWithSize KeyValueStore::Get(Key key, +StatusWithSize KeyValueStore::Get(std::string_view key, const EntryMetadata& metadata, span value_buffer, size_t offset_bytes) const { @@ -601,7 +603,7 @@ StatusWithSize KeyValueStore::Get(Key key, return result; } -Status KeyValueStore::FixedSizeGet(Key key, +Status KeyValueStore::FixedSizeGet(std::string_view key, void* value, size_t size_bytes) const { PW_TRY(CheckWriteOperation(key)); @@ -612,7 +614,7 @@ Status KeyValueStore::FixedSizeGet(Key key, return FixedSizeGet(key, metadata, value, size_bytes); } -Status KeyValueStore::FixedSizeGet(Key key, +Status KeyValueStore::FixedSizeGet(std::string_view key, const EntryMetadata& metadata, void* value, size_t size_bytes) const { @@ -640,7 +642,7 @@ StatusWithSize KeyValueStore::ValueSize(const EntryMetadata& metadata) const { return StatusWithSize(entry.value_size()); } -Status KeyValueStore::CheckWriteOperation(Key key) const { +Status KeyValueStore::CheckWriteOperation(std::string_view key) const { if (InvalidKey(key)) { return Status::InvalidArgument(); } @@ -652,7 +654,7 @@ Status KeyValueStore::CheckWriteOperation(Key key) const { return OkStatus(); } -Status KeyValueStore::CheckReadOperation(Key key) const { +Status KeyValueStore::CheckReadOperation(std::string_view key) const { if (InvalidKey(key)) { return Status::InvalidArgument(); } @@ -667,7 +669,7 @@ Status KeyValueStore::CheckReadOperation(Key key) const { Status KeyValueStore::WriteEntryForExistingKey(EntryMetadata& metadata, EntryState new_state, - Key key, + std::string_view key, span value) { // Read the original entry to get the size for sector accounting purposes. Entry entry; @@ -676,7 +678,8 @@ Status KeyValueStore::WriteEntryForExistingKey(EntryMetadata& metadata, return WriteEntry(key, value, new_state, &metadata, &entry); } -Status KeyValueStore::WriteEntryForNewKey(Key key, span value) { +Status KeyValueStore::WriteEntryForNewKey(std::string_view key, + span value) { // If there is no room in the cache for a new entry, it is possible some cache // entries could be freed by removing deleted keys. If deleted key removal is // enabled and the KVS is configured to make all possible writes succeed, @@ -703,7 +706,7 @@ Status KeyValueStore::WriteEntryForNewKey(Key key, span value) { return WriteEntry(key, value, EntryState::kValid); } -Status KeyValueStore::WriteEntry(Key key, +Status KeyValueStore::WriteEntry(std::string_view key, span value, EntryState new_state, EntryMetadata* prior_metadata, @@ -751,7 +754,7 @@ Status KeyValueStore::WriteEntry(Key key, KeyValueStore::EntryMetadata KeyValueStore::CreateOrUpdateKeyDescriptor( const Entry& entry, - Key key, + std::string_view key, EntryMetadata* prior_metadata, size_t prior_size) { // If there is no prior descriptor, create a new one. @@ -853,7 +856,7 @@ Status KeyValueStore::MarkSectorCorruptIfNotOk(Status status, } Status KeyValueStore::AppendEntry(const Entry& entry, - Key key, + std::string_view key, span value) { const StatusWithSize result = entry.Write(key, value); @@ -1055,7 +1058,8 @@ Status KeyValueStore::RelocateKeyAddressesInSector( span reserved_addresses) { for (FlashPartition::Address& address : metadata.addresses()) { if (sectors_.AddressInSector(sector_to_gc, address)) { - PW_LOG_DEBUG(" Relocate entry for Key 0x%08" PRIx32 ", sector %u", + PW_LOG_DEBUG(" Relocate entry for std::string_view 0x%08" PRIx32 + ", sector %u", metadata.hash(), sectors_.Index(sectors_.FromAddress(address))); PW_TRY(RelocateEntry(metadata, address, reserved_addresses)); @@ -1245,13 +1249,14 @@ Status KeyValueStore::EnsureEntryRedundancy() { continue; } - PW_LOG_DEBUG(" Key with %u of %u copies found, adding missing copies", - unsigned(metadata.addresses().size()), - unsigned(redundancy())); + PW_LOG_DEBUG( + " std::string_view with %u of %u copies found, adding missing copies", + unsigned(metadata.addresses().size()), + unsigned(redundancy())); Status fill_status = AddRedundantEntries(metadata); if (fill_status.ok()) { internal_stats_.missing_redundant_entries_recovered += 1; - PW_LOG_DEBUG(" Key missing copies added"); + PW_LOG_DEBUG(" std::string_view missing copies added"); } else { PW_LOG_DEBUG(" Failed to add key missing copies"); if (repair_status.ok()) { @@ -1305,7 +1310,7 @@ Status KeyValueStore::Repair() { } KeyValueStore::Entry KeyValueStore::CreateEntry(Address address, - Key key, + std::string_view key, span value, EntryState state) { // Always bump the transaction ID when creating a new entry. @@ -1348,7 +1353,7 @@ void KeyValueStore::LogDebugInfo() const { PW_LOG_DEBUG(" Alignment = %u", unsigned(partition_.alignment_bytes())); PW_LOG_DEBUG(" "); - PW_LOG_DEBUG("Key descriptors:"); + PW_LOG_DEBUG("std::string_view descriptors:"); PW_LOG_DEBUG(" Entry count = %u", unsigned(entry_cache_.total_entries())); PW_LOG_DEBUG(" Max entry count = %u", unsigned(entry_cache_.max_entries())); @@ -1426,14 +1431,16 @@ void KeyValueStore::LogSectors() const { } void KeyValueStore::LogKeyDescriptor() const { - PW_LOG_DEBUG("Key descriptors: count %u", + PW_LOG_DEBUG("std::string_view descriptors: count %u", unsigned(entry_cache_.total_entries())); for (const EntryMetadata& metadata : entry_cache_) { - PW_LOG_DEBUG(" - Key: %s, hash %#x, transaction ID %u, first address %#x", - metadata.state() == EntryState::kDeleted ? "Deleted" : "Valid", - unsigned(metadata.hash()), - unsigned(metadata.transaction_id()), - unsigned(metadata.first_address())); + PW_LOG_DEBUG( + " - std::string_view: %s, hash %#x, transaction ID %u, first address " + "%#x", + metadata.state() == EntryState::kDeleted ? "Deleted" : "Valid", + unsigned(metadata.hash()), + unsigned(metadata.transaction_id()), + unsigned(metadata.first_address())); } } diff --git a/pw_kvs/public/pw_kvs/flash_memory.h b/pw_kvs/public/pw_kvs/flash_memory.h index 1ec16cbf13..88983b9246 100644 --- a/pw_kvs/public/pw_kvs/flash_memory.h +++ b/pw_kvs/public/pw_kvs/flash_memory.h @@ -24,11 +24,8 @@ #include "pw_span/span.h" #include "pw_status/status.h" #include "pw_status/status_with_size.h" - -#if PW_CXX_STANDARD_IS_SUPPORTED(17) // Requires C++17 for pw::Result #include "pw_stream/seek.h" #include "pw_stream/stream.h" -#endif // PW_CXX_STANDARD_IS_SUPPORTED(17) namespace pw { namespace kvs { @@ -146,7 +143,6 @@ class FlashPartition { // The flash address is in the range of: 0 to PartitionSize. using Address = uint32_t; -#if PW_CXX_STANDARD_IS_SUPPORTED(17) // Requires C++17 for pw::Result class Writer final : public stream::NonSeekableWriter { public: constexpr Writer(kvs::FlashPartition& partition) @@ -206,7 +202,6 @@ class FlashPartition { size_t read_limit_; size_t position_; }; -#endif // PW_CXX_STANDARD_IS_SUPPORTED(17) // Implement Output for the Write method. class Output final : public pw::Output { diff --git a/pw_kvs/public/pw_kvs/internal/entry.h b/pw_kvs/public/pw_kvs/internal/entry.h index cbf0ceabe9..4cfc31c2b8 100644 --- a/pw_kvs/public/pw_kvs/internal/entry.h +++ b/pw_kvs/public/pw_kvs/internal/entry.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "pw_kvs/alignment.h" #include "pw_kvs/checksum.h" @@ -26,7 +27,6 @@ #include "pw_kvs/format.h" #include "pw_kvs/internal/hash.h" #include "pw_kvs/internal/key_descriptor.h" -#include "pw_kvs/key.h" #include "pw_span/span.h" namespace pw { @@ -65,7 +65,7 @@ class Entry { static Entry Valid(FlashPartition& partition, Address address, const EntryFormat& format, - Key key, + std::string_view key, span value, uint32_t transaction_id) { return Entry( @@ -76,7 +76,7 @@ class Entry { static Entry Tombstone(FlashPartition& partition, Address address, const EntryFormat& format, - Key key, + std::string_view key, uint32_t transaction_id) { return Entry(partition, address, @@ -89,7 +89,9 @@ class Entry { Entry() = default; - KeyDescriptor descriptor(Key key) const { return descriptor(Hash(key)); } + KeyDescriptor descriptor(std::string_view key) const { + return descriptor(Hash(key)); + } KeyDescriptor descriptor(uint32_t key_hash) const { return KeyDescriptor{key_hash, @@ -97,7 +99,7 @@ class Entry { deleted() ? EntryState::kDeleted : EntryState::kValid}; } - StatusWithSize Write(Key key, span value) const; + StatusWithSize Write(std::string_view key, span value) const; // Changes the format and transcation ID for this entry. In order to calculate // the new checksum, the entire entry is read into a small stack-allocated @@ -124,13 +126,14 @@ class Entry { Status ValueMatches(span value) const; - Status VerifyChecksum(Key key, span value) const; + Status VerifyChecksum(std::string_view key, + span value) const; Status VerifyChecksumInFlash() const; // Calculates the total size of an entry, including padding. static size_t size(const FlashPartition& partition, - Key key, + std::string_view key, span value) { return AlignUp(sizeof(EntryHeader) + key.size() + value.size(), std::max(partition.alignment_bytes(), kMinAlignmentBytes)); @@ -176,7 +179,7 @@ class Entry { Entry(FlashPartition& partition, Address address, const EntryFormat& format, - Key key, + std::string_view key, span value, uint16_t value_size_bytes, uint32_t transaction_id); @@ -203,7 +206,7 @@ class Entry { return as_bytes(span(&header_.checksum, 1)); } - span CalculateChecksum(Key key, + span CalculateChecksum(std::string_view key, span value) const; Status CalculateChecksumFromFlash(); diff --git a/pw_kvs/public/pw_kvs/internal/entry_cache.h b/pw_kvs/public/pw_kvs/internal/entry_cache.h index 179a7ccd4d..8a3324dbb8 100644 --- a/pw_kvs/public/pw_kvs/internal/entry_cache.h +++ b/pw_kvs/public/pw_kvs/internal/entry_cache.h @@ -15,6 +15,7 @@ #include #include +#include #include #include "pw_containers/vector.h" @@ -22,7 +23,6 @@ #include "pw_kvs/format.h" #include "pw_kvs/internal/key_descriptor.h" #include "pw_kvs/internal/sectors.h" -#include "pw_kvs/key.h" #include "pw_span/span.h" namespace pw { @@ -177,7 +177,7 @@ class EntryCache { StatusWithSize Find(FlashPartition& partition, const Sectors& sectors, const EntryFormats& formats, - Key key, + std::string_view key, EntryMetadata* metadata) const; // Adds a new descriptor to the descriptor list. The entry MUST be unique and diff --git a/pw_kvs/public/pw_kvs/internal/hash.h b/pw_kvs/public/pw_kvs/internal/hash.h index e56fe880f9..5616e58f54 100644 --- a/pw_kvs/public/pw_kvs/internal/hash.h +++ b/pw_kvs/public/pw_kvs/internal/hash.h @@ -14,15 +14,14 @@ #pragma once #include - -#include "pw_kvs/key.h" +#include namespace pw { namespace kvs { namespace internal { // The hash function used to hash keys. -constexpr uint32_t Hash(Key string) { +constexpr uint32_t Hash(std::string_view string) { uint32_t hash = 0; uint32_t coefficient = 65599u; diff --git a/pw_kvs/public/pw_kvs/key.h b/pw_kvs/public/pw_kvs/key.h deleted file mode 100644 index a5ca69f09b..0000000000 --- a/pw_kvs/public/pw_kvs/key.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2020 The Pigweed Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not -// use this file except in compliance with the License. You may obtain a copy of -// the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -// License for the specific language governing permissions and limitations under -// the License. -#pragma once - -#include -#include -#include - -#if __cplusplus >= 201703L -#include -#endif // __cplusplus >= 201703L - -namespace pw { -namespace kvs { - -// Key was a simplified string_view used for KVS pre-C++17. -// It is now a simple alias since Pigweed requires C++17. -using Key = std::string_view; - -} // namespace kvs -} // namespace pw diff --git a/pw_kvs/public/pw_kvs/key_value_store.h b/pw_kvs/public/pw_kvs/key_value_store.h index 3f37f81a93..5a355658c2 100644 --- a/pw_kvs/public/pw_kvs/key_value_store.h +++ b/pw_kvs/public/pw_kvs/key_value_store.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "pw_containers/vector.h" @@ -27,7 +28,6 @@ #include "pw_kvs/internal/key_descriptor.h" #include "pw_kvs/internal/sectors.h" #include "pw_kvs/internal/span_traits.h" -#include "pw_kvs/key.h" #include "pw_span/span.h" #include "pw_status/status.h" #include "pw_status/status_with_size.h" @@ -158,7 +158,7 @@ class KeyValueStore { /// is too large. /// /// @endrst - StatusWithSize Get(Key key, + StatusWithSize Get(std::string_view key, span value, size_t offset_bytes = 0) const; @@ -170,7 +170,7 @@ class KeyValueStore { /// instead of the array itself. template ::value>> - Status Get(const Key& key, const Pointer& pointer) const { + Status Get(const std::string_view& key, const Pointer& pointer) const { using T = std::remove_reference_t>; CheckThatObjectCanBePutOrGet(); return FixedSizeGet(key, pointer, sizeof(T)); @@ -208,13 +208,13 @@ class KeyValueStore { /// @endrst template ::value>* = nullptr> - Status Put(const Key& key, const T& value) { + Status Put(const std::string_view& key, const T& value) { return PutBytes(key, as_bytes(internal::make_span(value))); } template ::value>* = nullptr> - Status Put(const Key& key, const T& value) { + Status Put(const std::string_view& key, const T& value) { CheckThatObjectCanBePutOrGet(); return PutBytes(key, as_bytes(span(&value, 1))); } @@ -242,7 +242,7 @@ class KeyValueStore { /// INVALID_ARGUMENT: ``key`` is empty or too long. /// /// @endrst - Status Delete(Key key); + Status Delete(std::string_view key); /// Returns the size of the value corresponding to the key. /// @@ -264,7 +264,7 @@ class KeyValueStore { /// INVALID_ARGUMENT: ``key`` is empty or too long. /// /// @endrst - StatusWithSize ValueSize(Key key) const; + StatusWithSize ValueSize(std::string_view key) const; /// Performs all maintenance possible, including all needed repairing of /// corruption and garbage collection of reclaimable space in the KVS. When @@ -505,7 +505,7 @@ class KeyValueStore { // interruption. Status RemoveDeletedKeyEntries(); - Status PutBytes(Key key, span value); + Status PutBytes(std::string_view key, span value); StatusWithSize ValueSize(const EntryMetadata& metadata) const; @@ -523,7 +523,7 @@ class KeyValueStore { // key's hash collides with the hash for an existing // descriptor // - Status FindEntry(Key key, EntryMetadata* metadata_out) const; + Status FindEntry(std::string_view key, EntryMetadata* metadata_out) const; // Searches for a KeyDescriptor that matches this key and sets *metadata_out // to point to it if one is found. @@ -531,38 +531,40 @@ class KeyValueStore { // OK: there is a matching descriptor and *metadata_out is set // NOT_FOUND: there is no descriptor that matches this key // - Status FindExisting(Key key, EntryMetadata* metadata_out) const; + Status FindExisting(std::string_view key, EntryMetadata* metadata_out) const; - StatusWithSize Get(Key key, + StatusWithSize Get(std::string_view key, const EntryMetadata& metadata, span value_buffer, size_t offset_bytes) const; - Status FixedSizeGet(Key key, void* value, size_t size_bytes) const; + Status FixedSizeGet(std::string_view key, + void* value, + size_t size_bytes) const; - Status FixedSizeGet(Key key, + Status FixedSizeGet(std::string_view key, const EntryMetadata& metadata, void* value, size_t size_bytes) const; - Status CheckWriteOperation(Key key) const; - Status CheckReadOperation(Key key) const; + Status CheckWriteOperation(std::string_view key) const; + Status CheckReadOperation(std::string_view key) const; Status WriteEntryForExistingKey(EntryMetadata& metadata, EntryState new_state, - Key key, + std::string_view key, span value); - Status WriteEntryForNewKey(Key key, span value); + Status WriteEntryForNewKey(std::string_view key, span value); - Status WriteEntry(Key key, + Status WriteEntry(std::string_view key, span value, EntryState new_state, EntryMetadata* prior_metadata = nullptr, const internal::Entry* prior_entry = nullptr); EntryMetadata CreateOrUpdateKeyDescriptor(const Entry& new_entry, - Key key, + std::string_view key, EntryMetadata* prior_metadata, size_t prior_size); @@ -579,7 +581,9 @@ class KeyValueStore { Status MarkSectorCorruptIfNotOk(Status status, SectorDescriptor* sector); - Status AppendEntry(const Entry& entry, Key key, span value); + Status AppendEntry(const Entry& entry, + std::string_view key, + span value); StatusWithSize CopyEntryToSector(Entry& entry, SectorDescriptor* new_sector, @@ -634,7 +638,7 @@ class KeyValueStore { Status Repair(); internal::Entry CreateEntry(Address address, - Key key, + std::string_view key, span value, EntryState state);