mirror of
https://fuchsia.googlesource.com/third_party/pigweed.googlesource.com/pigweed/pigweed
synced 2024-09-21 14:16:26 +00:00
f3884eb9d4
Fix garbage collection when using redudant entries. - Improve "find sector to write entry to" and "find sector to GC" algorithms so they work better with redundant entries. - Add support in GC to be aware of partially written keys, where the first entry is written to flash, but GC is needed to free space for the redundant entries to have space to be written. Tested with kEntryRedundancy up to 2. Change-Id: Iff88d970714fa1ece634af926a1838a5f7d5d7e7
64 lines
2.0 KiB
C++
64 lines
2.0 KiB
C++
// 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 "gtest/gtest.h"
|
|
#include "pw_kvs/crc16_checksum.h"
|
|
#include "pw_kvs/in_memory_fake_flash.h"
|
|
#include "pw_kvs/key_value_store.h"
|
|
|
|
namespace pw::kvs {
|
|
namespace {
|
|
|
|
using std::byte;
|
|
|
|
constexpr size_t kMaxEntries = 256;
|
|
constexpr size_t kMaxUsableSectors = 256;
|
|
|
|
// 4 x 4k sectors, 16 byte alignment
|
|
FakeFlashBuffer<4 * 1024, 6> test_flash(16);
|
|
FlashPartition test_partition(&test_flash, 0, test_flash.sector_count());
|
|
|
|
ChecksumCrc16 checksum;
|
|
|
|
class EmptyInitializedKvs : public ::testing::Test {
|
|
protected:
|
|
EmptyInitializedKvs()
|
|
: kvs_(&test_partition, {.magic = 0xBAD'C0D3, .checksum = &checksum}) {
|
|
test_partition.Erase(0, test_partition.sector_count());
|
|
ASSERT_EQ(Status::OK, kvs_.Init());
|
|
}
|
|
|
|
KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs_;
|
|
};
|
|
|
|
TEST_F(EmptyInitializedKvs, Put_VaryingKeysAndValues) {
|
|
char value[] =
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
|
|
"34567890123"; // 64 (with final \0);
|
|
static_assert(sizeof(value) == 64);
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
for (unsigned key_size = 1; key_size < sizeof(value); ++key_size) {
|
|
for (unsigned value_size = 0; value_size < sizeof(value); ++value_size) {
|
|
ASSERT_EQ(Status::OK,
|
|
kvs_.Put(std::string_view(value, key_size),
|
|
as_bytes(span(value, value_size))));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace pw::kvs
|