blob: b19c1637a279c7f2329f914fa2241ac3427ba7e4 [file] [log] [blame]
Wyatt Heplerb7609542020-01-24 10:29:54 -08001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
Wyatt Heplerb7609542020-01-24 10:29:54 -080015#include "pw_kvs/key_value_store.h"
16
Wyatt Heplerbab0e202020-02-04 07:40:08 -080017#include <algorithm>
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -080018#include <cinttypes>
Wyatt Heplerb7609542020-01-24 10:29:54 -080019#include <cstring>
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080020#include <type_traits>
Wyatt Heplerb7609542020-01-24 10:29:54 -080021
Keir Mierle8c352dc2020-02-02 13:58:19 -080022#define PW_LOG_USE_ULTRA_SHORT_NAMES 1
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080023#include "pw_kvs_private/format.h"
24#include "pw_kvs_private/macros.h"
Keir Mierle8c352dc2020-02-02 13:58:19 -080025#include "pw_log/log.h"
Wyatt Heplerb7609542020-01-24 10:29:54 -080026
Wyatt Hepler2ad60672020-01-21 08:00:16 -080027namespace pw::kvs {
Wyatt Heplerb7609542020-01-24 10:29:54 -080028
Wyatt Hepleracaacf92020-01-24 10:58:30 -080029using std::byte;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080030using std::string_view;
Wyatt Hepleracaacf92020-01-24 10:58:30 -080031
Wyatt Heplerad0a7932020-02-06 08:20:38 -080032KeyValueStore::KeyValueStore(FlashPartition* partition,
33 const EntryHeaderFormat& format,
34 const Options& options)
35 : partition_(*partition),
36 entry_header_format_(format),
37 options_(options),
38 key_descriptor_list_{},
39 key_descriptor_list_size_(0),
40 sector_map_{},
41 sector_map_size_(partition_.sector_count()),
42 last_new_sector_(sector_map_.data()),
43 working_buffer_{} {}
44
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080045Status KeyValueStore::Init() {
Wyatt Heplerad0a7932020-02-06 08:20:38 -080046 if (kMaxUsableSectors < sector_map_size_) {
47 CRT("KeyValueStore::kMaxUsableSectors must be at least as large as the "
48 "number of sectors in the flash partition");
49 return Status::FAILED_PRECONDITION;
50 }
51
52 if (kMaxUsableSectors > sector_map_size_) {
53 DBG("KeyValueStore::kMaxUsableSectors is %zu sectors larger than needed",
54 kMaxUsableSectors - sector_map_size_);
55 }
56
Keir Mierle8c352dc2020-02-02 13:58:19 -080057 // Reset the number of occupied key descriptors; we will fill them later.
58 key_descriptor_list_size_ = 0;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080059
David Rogers8ce55cd2020-02-04 19:41:48 -080060 // TODO: init last_new_sector_ to a random sector. Since the on-flash stored
61 // information does not allow recovering the previous last_new_sector_ after
62 // clean start, random is a good second choice.
63
Keir Mierle8c352dc2020-02-02 13:58:19 -080064 const size_t sector_size_bytes = partition_.sector_size_bytes();
Keir Mierle8c352dc2020-02-02 13:58:19 -080065
David Rogersf0a35442020-02-04 12:16:38 -080066 if (working_buffer_.size() < sector_size_bytes) {
67 CRT("ERROR: working_buffer_ (%zu bytes) is smaller than sector "
68 "size (%zu bytes)",
69 working_buffer_.size(),
70 sector_size_bytes);
71 return Status::INVALID_ARGUMENT;
72 }
73
Keir Mierle8c352dc2020-02-02 13:58:19 -080074 DBG("First pass: Read all entries from all sectors");
Wyatt Heplerad0a7932020-02-06 08:20:38 -080075 for (size_t sector_id = 0; sector_id < sector_map_size_; ++sector_id) {
Keir Mierle8c352dc2020-02-02 13:58:19 -080076 // Track writable bytes in this sector. Updated after reading each entry.
77 sector_map_[sector_id].tail_free_bytes = sector_size_bytes;
78
79 const Address sector_address = sector_id * sector_size_bytes;
80 Address entry_address = sector_address;
81
82 for (int num_entries_in_sector = 0;; num_entries_in_sector++) {
83 DBG("Load entry: sector=%zu, entry#=%d, address=%zu",
84 sector_id,
85 num_entries_in_sector,
86 size_t(entry_address));
87
88 if (!AddressInSector(sector_map_[sector_id], entry_address)) {
89 DBG("Fell off end of sector; moving to the next sector");
90 break;
91 }
92
93 Address next_entry_address;
94 Status status = LoadEntry(entry_address, &next_entry_address);
95 if (status == Status::NOT_FOUND) {
96 DBG("Hit un-written data in sector; moving to the next sector");
97 break;
98 }
99 if (status == Status::DATA_LOSS) {
100 // It's not clear KVS can make a unilateral decision about what to do
101 // in corruption cases. It's an application decision, for which we
102 // should offer some configurability. For now, entirely bail out of
103 // loading and give up.
104 //
105 // Later, scan for remaining valid keys; since it's entirely possible
106 // that there is a duplicate of the key elsewhere and everything is
107 // fine. Later, we can wipe and maybe recover the sector.
108 //
109 // TODO: Implement rest-of-sector scanning for valid entries.
110 return Status::DATA_LOSS;
111 }
112 TRY(status);
113
114 // Entry loaded successfully; so get ready to load the next one.
115 entry_address = next_entry_address;
116
117 // Update of the number of writable bytes in this sector.
118 sector_map_[sector_id].tail_free_bytes =
119 sector_size_bytes - (entry_address - sector_address);
120 }
121 }
122
123 DBG("Second pass: Count valid bytes in each sector");
124 // Initialize the sector sizes.
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800125 for (size_t sector_id = 0; sector_id < sector_map_size_; ++sector_id) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800126 sector_map_[sector_id].valid_bytes = 0;
127 }
128 // For every valid key, increment the valid bytes for that sector.
129 for (size_t key_id = 0; key_id < key_descriptor_list_size_; ++key_id) {
130 uint32_t sector_id =
131 key_descriptor_list_[key_id].address / sector_size_bytes;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800132 EntryHeader header;
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800133 TRY(ReadEntryHeader(key_descriptor_list_[key_id].address, &header));
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800134 sector_map_[sector_id].valid_bytes += header.size();
Keir Mierle8c352dc2020-02-02 13:58:19 -0800135 }
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800136 initialized_ = true;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800137 return Status::OK;
138}
139
140Status KeyValueStore::LoadEntry(Address entry_address,
141 Address* next_entry_address) {
142 const size_t alignment_bytes = partition_.alignment_bytes();
143
Keir Mierle8c352dc2020-02-02 13:58:19 -0800144 EntryHeader header;
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800145 TRY(ReadEntryHeader(entry_address, &header));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800146 // TODO: Should likely add a "LogHeader" method or similar.
147 DBG("Header: ");
148 DBG(" Address = 0x%zx", size_t(entry_address));
149 DBG(" Magic = 0x%zx", size_t(header.magic()));
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800150 DBG(" Checksum = 0x%zx", size_t(header.checksum()));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800151 DBG(" Key length = 0x%zx", size_t(header.key_length()));
152 DBG(" Value length = 0x%zx", size_t(header.value_length()));
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800153 DBG(" Entry size = 0x%zx", size_t(header.size()));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800154 DBG(" Padded size = 0x%zx",
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800155 size_t(AlignUp(header.size(), alignment_bytes)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800156
157 if (HeaderLooksLikeUnwrittenData(header)) {
158 return Status::NOT_FOUND;
159 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800160
161 // TODO: Handle multiple magics for formats that have changed.
162 if (header.magic() != entry_header_format_.magic) {
163 // TODO: It may be cleaner to have some logging helpers for these cases.
164 CRT("Found corrupt magic: %zx; expecting %zx; at address %zx",
165 size_t(header.magic()),
166 size_t(entry_header_format_.magic),
167 size_t(entry_address));
168 return Status::DATA_LOSS;
169 }
170
171 // Read the key from flash & validate the entry (which reads the value).
172 KeyBuffer key_buffer;
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800173 TRY(ReadEntryKey(entry_address, header.key_length(), key_buffer.data()));
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800174 const string_view key(key_buffer.data(), header.key_length());
175
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800176 TRY(header.VerifyChecksumInFlash(
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800177 &partition_, entry_address, entry_header_format_.checksum));
178
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800179 KeyDescriptor key_descriptor(
180 key,
181 header.key_version(),
182 entry_address,
183 header.deleted() ? KeyDescriptor::kDeleted : KeyDescriptor::kValid);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800184
185 DBG("Key hash: %zx (%zu)",
186 size_t(key_descriptor.key_hash),
187 size_t(key_descriptor.key_hash));
188
189 TRY(AppendNewOrOverwriteStaleExistingDescriptor(key_descriptor));
190
191 // TODO: Extract this to something like "NextValidEntryAddress".
192 *next_entry_address =
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800193 AlignUp(key_descriptor.address + header.size(), alignment_bytes);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800194
195 return Status::OK;
196}
197
198// TODO: This method is the trigger of the O(valid_entries * all_entries) time
199// complexity for reading. At some cost to memory, this could be optimized by
200// using a hash table instead of scanning, but in practice this should be fine
201// for a small number of keys
202Status KeyValueStore::AppendNewOrOverwriteStaleExistingDescriptor(
203 const KeyDescriptor& key_descriptor) {
204 // With the new key descriptor, either add it to the descriptor table or
205 // overwrite an existing entry with an older version of the key.
206 KeyDescriptor* existing_descriptor = FindDescriptor(key_descriptor.key_hash);
207 if (existing_descriptor) {
208 if (existing_descriptor->key_version < key_descriptor.key_version) {
209 // Existing entry is old; replace the existing entry with the new one.
210 *existing_descriptor = key_descriptor;
211 } else {
212 // Otherwise, check for data integrity and leave the existing entry.
213 if (existing_descriptor->key_version == key_descriptor.key_version) {
214 ERR("Data loss: Duplicated old(=%zu) and new(=%zu) version",
215 size_t(existing_descriptor->key_version),
216 size_t(key_descriptor.key_version));
217 return Status::DATA_LOSS;
218 }
219 DBG("Found stale entry when appending; ignoring");
220 }
221 return Status::OK;
222 }
223 // Write new entry.
224 KeyDescriptor* newly_allocated_key_descriptor;
225 TRY(AppendEmptyDescriptor(&newly_allocated_key_descriptor));
226 *newly_allocated_key_descriptor = key_descriptor;
227 return Status::OK;
228}
229
230// TODO: Need a better name.
231Status KeyValueStore::AppendEmptyDescriptor(KeyDescriptor** new_descriptor) {
232 if (KeyListFull()) {
233 // TODO: Is this the right return code?
234 return Status::RESOURCE_EXHAUSTED;
235 }
236 *new_descriptor = &key_descriptor_list_[key_descriptor_list_size_++];
237 return Status::OK;
238}
239
240// TODO: Finish.
241bool KeyValueStore::HeaderLooksLikeUnwrittenData(
242 const EntryHeader& header) const {
243 // TODO: This is not correct; it should call through to flash memory.
244 return header.magic() == 0xffffffff;
245}
246
247KeyValueStore::KeyDescriptor* KeyValueStore::FindDescriptor(uint32_t hash) {
248 for (size_t key_id = 0; key_id < key_descriptor_list_size_; key_id++) {
249 if (key_descriptor_list_[key_id].key_hash == hash) {
250 return &(key_descriptor_list_[key_id]);
251 }
252 }
253 return nullptr;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800254}
255
256StatusWithSize KeyValueStore::Get(string_view key,
257 span<byte> value_buffer) const {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800258 TRY(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800259
David Rogers2761aeb2020-01-31 17:09:00 -0800260 const KeyDescriptor* key_descriptor;
261 TRY(FindKeyDescriptor(key, &key_descriptor));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800262
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800263 if (key_descriptor->deleted()) {
264 return Status::NOT_FOUND;
265 }
266
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800267 EntryHeader header;
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800268 TRY(ReadEntryHeader(key_descriptor->address, &header));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800269
Keir Mierle8c352dc2020-02-02 13:58:19 -0800270 StatusWithSize result = ReadEntryValue(*key_descriptor, header, value_buffer);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800271 if (result.ok() && options_.verify_on_read) {
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800272 return header.VerifyChecksum(entry_header_format_.checksum,
273 key,
274 value_buffer.subspan(0, result.size()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800275 }
276 return result;
277}
278
279Status KeyValueStore::Put(string_view key, span<const byte> value) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800280 DBG("Writing key/value; key length=%zu, value length=%zu",
281 key.size(),
282 value.size());
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800283
284 TRY(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800285
286 if (value.size() > (1 << 24)) {
287 // TODO: Reject sizes that are larger than the maximum?
288 }
289
David Rogers2761aeb2020-01-31 17:09:00 -0800290 KeyDescriptor* key_descriptor;
291 if (FindKeyDescriptor(key, &key_descriptor).ok()) {
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800292 DBG("Writing over existing entry for key 0x%08" PRIx32,
293 key_descriptor->key_hash);
294 return WriteEntryForExistingKey(
295 key_descriptor, KeyDescriptor::kValid, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800296 }
David Rogers2761aeb2020-01-31 17:09:00 -0800297
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800298 return WriteEntryForNewKey(key, value);
299}
300
301Status KeyValueStore::Delete(string_view key) {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800302 TRY(CheckOperation(key));
303
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800304 KeyDescriptor* key_descriptor;
305 TRY(FindKeyDescriptor(key, &key_descriptor));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800306
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800307 if (key_descriptor->deleted()) {
308 return Status::NOT_FOUND;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800309 }
310
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800311 DBG("Writing tombstone for key 0x%08" PRIx32, key_descriptor->key_hash);
312 return WriteEntryForExistingKey(
313 key_descriptor, KeyDescriptor::kDeleted, key, {});
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800314}
315
316KeyValueStore::iterator& KeyValueStore::iterator::operator++() {
317 // Skip to the next entry that is valid (not deleted).
318 while (++index_ < item_.kvs_.key_descriptor_list_size_ &&
319 descriptor().deleted()) {
320 }
321 return *this;
322}
323
324const KeyValueStore::Item& KeyValueStore::iterator::operator*() {
325 std::memset(item_.key_buffer_.data(), 0, item_.key_buffer_.size());
326
327 EntryHeader header;
328 if (item_.kvs_.ReadEntryHeader(descriptor().address, &header).ok()) {
329 item_.kvs_.ReadEntryKey(
330 descriptor().address, header.key_length(), item_.key_buffer_.data());
331 }
332
333 return item_;
334}
335
336KeyValueStore::iterator KeyValueStore::begin() const {
337 size_t i = 0;
338 // Skip over any deleted entries at the start of the descriptor list.
339 while (i < key_descriptor_list_size_ && key_descriptor_list_[i].deleted()) {
340 i += 1;
341 }
342 return iterator(*this, i);
343}
344
345// TODO(hepler): The valid entry count could be tracked in the KVS to avoid the
346// need for this for-loop.
347size_t KeyValueStore::size() const {
348 size_t valid_entries = 0;
349
350 for (size_t i = 0; i < key_descriptor_list_size_; ++i) {
351 if (!key_descriptor_list_[i].deleted()) {
352 valid_entries += 1;
353 }
354 }
355
356 return valid_entries;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800357}
358
Wyatt Heplered163b02020-02-03 17:49:32 -0800359StatusWithSize KeyValueStore::ValueSize(std::string_view key) const {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800360 TRY(CheckOperation(key));
Wyatt Heplered163b02020-02-03 17:49:32 -0800361
362 const KeyDescriptor* key_descriptor;
363 TRY(FindKeyDescriptor(key, &key_descriptor));
364
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800365 if (key_descriptor->deleted()) {
366 return Status::NOT_FOUND;
367 }
368
Wyatt Heplered163b02020-02-03 17:49:32 -0800369 EntryHeader header;
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800370 TRY(ReadEntryHeader(key_descriptor->address, &header));
Wyatt Heplered163b02020-02-03 17:49:32 -0800371
372 return StatusWithSize(header.value_length());
373}
374
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800375uint32_t KeyValueStore::HashKey(string_view string) {
376 uint32_t hash = 0;
377 uint32_t coefficient = 65599u;
378
379 for (char ch : string) {
380 hash += coefficient * unsigned(ch);
381 coefficient *= 65599u;
382 }
383
384 return hash;
385}
386
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800387Status KeyValueStore::FixedSizeGet(std::string_view key,
388 byte* value,
389 size_t size_bytes) const {
390 // Ensure that the size of the stored value matches the size of the type.
391 // Otherwise, report error. This check avoids potential memory corruption.
392 StatusWithSize result = ValueSize(key);
393 if (!result.ok()) {
394 return result.status();
Keir Mierle8c352dc2020-02-02 13:58:19 -0800395 }
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800396 if (result.size() != size_bytes) {
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800397 DBG("Requested %zu B read, but value is %zu B", size_bytes, result.size());
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800398 return Status::INVALID_ARGUMENT;
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800399 }
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800400 return Get(key, span(value, size_bytes)).status();
Keir Mierle8c352dc2020-02-02 13:58:19 -0800401}
402
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800403Status KeyValueStore::CheckOperation(string_view key) const {
Wyatt Hepleracaacf92020-01-24 10:58:30 -0800404 if (InvalidKey(key)) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800405 return Status::INVALID_ARGUMENT;
406 }
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800407 if (!initialized_) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800408 return Status::FAILED_PRECONDITION;
409 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800410 return Status::OK;
411}
412
David Rogers2761aeb2020-01-31 17:09:00 -0800413Status KeyValueStore::FindKeyDescriptor(string_view key,
414 const KeyDescriptor** result) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800415 char key_buffer[kMaxKeyLength];
416 const uint32_t hash = HashKey(key);
Wyatt Heplerb7609542020-01-24 10:29:54 -0800417
David Rogers2761aeb2020-01-31 17:09:00 -0800418 for (auto& descriptor : key_descriptors()) {
419 if (descriptor.key_hash == hash) {
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800420 TRY(ReadEntryKey(descriptor.address, key.size(), key_buffer));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800421
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800422 if (key == string_view(key_buffer, key.size())) {
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800423 DBG("Found match for key hash 0x%08" PRIx32, hash);
David Rogers2761aeb2020-01-31 17:09:00 -0800424 *result = &descriptor;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800425 return Status::OK;
426 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800427 }
428 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800429 return Status::NOT_FOUND;
430}
431
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800432Status KeyValueStore::ReadEntryHeader(Address address,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800433 EntryHeader* header) const {
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800434 return partition_.Read(address, sizeof(*header), header).status();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800435}
436
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800437Status KeyValueStore::ReadEntryKey(Address address,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800438 size_t key_length,
439 char* key) const {
440 // TODO: This check probably shouldn't be here; this is like
441 // checking that the Cortex M's RAM isn't corrupt. This should be
442 // done at boot time.
443 // ^^ This argument sometimes comes from EntryHeader::key_value_len,
444 // which is read directly from flash. If it's corrupted, we shouldn't try
445 // to read a bunch of extra data.
446 if (key_length == 0u || key_length > kMaxKeyLength) {
447 return Status::DATA_LOSS;
448 }
449 // The key is immediately after the entry header.
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800450 return partition_.Read(address + sizeof(EntryHeader), key_length, key)
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800451 .status();
452}
453
David Rogers2761aeb2020-01-31 17:09:00 -0800454StatusWithSize KeyValueStore::ReadEntryValue(
455 const KeyDescriptor& key_descriptor,
456 const EntryHeader& header,
457 span<byte> value) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800458 const size_t read_size = std::min(header.value_length(), value.size());
David Rogers2761aeb2020-01-31 17:09:00 -0800459 StatusWithSize result = partition_.Read(
460 key_descriptor.address + sizeof(header) + header.key_length(),
Keir Mierle8c352dc2020-02-02 13:58:19 -0800461 value.subspan(0, read_size));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800462 TRY(result);
463 if (read_size != header.value_length()) {
464 return StatusWithSize(Status::RESOURCE_EXHAUSTED, read_size);
465 }
466 return StatusWithSize(read_size);
467}
468
David Rogers2761aeb2020-01-31 17:09:00 -0800469Status KeyValueStore::WriteEntryForExistingKey(KeyDescriptor* key_descriptor,
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800470 KeyDescriptor::State new_state,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800471 string_view key,
472 span<const byte> value) {
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800473 // Find the original entry and sector to update the sector's valid_bytes.
474 EntryHeader original_entry;
475 TRY(ReadEntryHeader(key_descriptor->address, &original_entry));
476 SectorDescriptor& old_sector = SectorFromAddress(key_descriptor->address);
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800477
David Rogers2761aeb2020-01-31 17:09:00 -0800478 SectorDescriptor* sector;
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800479 TRY(FindOrRecoverSectorWithSpace(&sector, EntryHeader::size(key, value)));
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800480
David Rogers8ce55cd2020-02-04 19:41:48 -0800481 DBG("Writing existing entry; found sector: %zu", SectorIndex(sector));
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800482 TRY(AppendEntry(sector, key_descriptor, key, value, new_state));
483
484 old_sector.valid_bytes -= original_entry.size();
485 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800486}
487
488Status KeyValueStore::WriteEntryForNewKey(string_view key,
489 span<const byte> value) {
David Rogers2761aeb2020-01-31 17:09:00 -0800490 if (KeyListFull()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800491 WRN("KVS full: trying to store a new entry, but can't. Have %zu entries",
492 key_descriptor_list_size_);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800493 return Status::RESOURCE_EXHAUSTED;
494 }
495
David Rogers2761aeb2020-01-31 17:09:00 -0800496 // Modify the key descriptor at the end of the array, without bumping the map
497 // size so the key descriptor is prepared and written without committing
498 // first.
499 KeyDescriptor& key_descriptor =
500 key_descriptor_list_[key_descriptor_list_size_];
501 key_descriptor.key_hash = HashKey(key);
502 key_descriptor.key_version = 0; // will be incremented by AppendEntry()
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800503 key_descriptor.state = KeyDescriptor::kValid;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800504
David Rogers2761aeb2020-01-31 17:09:00 -0800505 SectorDescriptor* sector;
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800506 TRY(FindOrRecoverSectorWithSpace(&sector, EntryHeader::size(key, value)));
David Rogers8ce55cd2020-02-04 19:41:48 -0800507 DBG("Writing new entry; found sector: %zu", SectorIndex(sector));
David Rogers2761aeb2020-01-31 17:09:00 -0800508 TRY(AppendEntry(sector, &key_descriptor, key, value));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800509
Keir Mierle8c352dc2020-02-02 13:58:19 -0800510 // Only increment bump our size when we are certain the write succeeded.
David Rogers2761aeb2020-01-31 17:09:00 -0800511 key_descriptor_list_size_ += 1;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800512 return Status::OK;
513}
514
David Rogers2761aeb2020-01-31 17:09:00 -0800515Status KeyValueStore::RelocateEntry(KeyDescriptor& key_descriptor) {
David Rogersf0a35442020-02-04 12:16:38 -0800516 struct TempEntry {
517 std::array<char, kMaxKeyLength + 1> key;
518 std::array<char, sizeof(working_buffer_) - sizeof(key)> value;
519 };
520 TempEntry* entry = reinterpret_cast<TempEntry*>(working_buffer_.data());
521
522 // Read the entry to be relocated. Store the header in a local variable and
523 // store the key and value in the TempEntry stored in the static allocated
524 // working_buffer_.
525 EntryHeader header;
Wyatt Hepler4d78cd62020-02-05 13:05:58 -0800526 TRY(ReadEntryHeader(key_descriptor.address, &header));
527 TRY(ReadEntryKey(
528 key_descriptor.address, header.key_length(), entry->key.data()));
David Rogersf0a35442020-02-04 12:16:38 -0800529 string_view key = string_view(entry->key.data(), header.key_length());
530 StatusWithSize result = ReadEntryValue(
531 key_descriptor, header, as_writable_bytes(span(entry->value)));
532 if (!result.status().ok()) {
533 return Status::INTERNAL;
534 }
535
536 auto value = span(entry->value.data(), result.size());
537
538 TRY(header.VerifyChecksum(
539 entry_header_format_.checksum, key, as_bytes(value)));
540
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800541 SectorDescriptor& old_sector = SectorFromAddress(key_descriptor.address);
David Rogersf0a35442020-02-04 12:16:38 -0800542
543 // Find a new sector for the entry and write it to the new location.
David Rogers8ce55cd2020-02-04 19:41:48 -0800544 SectorDescriptor* new_sector;
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800545 TRY(FindSectorWithSpace(&new_sector, header.size(), &old_sector, true));
David Rogersf0a35442020-02-04 12:16:38 -0800546 return AppendEntry(new_sector, &key_descriptor, key, as_bytes(value));
David Rogersa12786b2020-01-31 16:02:33 -0800547}
548
David Rogers8db5a722020-02-03 18:28:34 -0800549// Find either an existing sector with enough space that is not the sector to
550// skip, or an empty sector. Maintains the invariant that there is always at
551// least 1 empty sector unless set to bypass the rule.
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800552Status KeyValueStore::FindSectorWithSpace(
553 SectorDescriptor** found_sector,
554 size_t size,
555 const SectorDescriptor* sector_to_skip,
556 bool bypass_empty_sector_rule) {
David Rogers8ce55cd2020-02-04 19:41:48 -0800557 // The last_new_sector_ is the sector that was last selected as the "new empty
558 // sector" to write to. This last new sector is used as the starting point for
559 // the next "find a new empty sector to write to" operation. By using the last
560 // new sector as the start point we will cycle which empty sector is selected
561 // next, spreading the wear across all the empty sectors and get a wear
562 // leveling benefit, rather than putting more wear on the lower number
563 // sectors.
564 //
565 // Locally use the sector index for ease of iterating through the sectors. For
566 // the persistent storage use SectorDescriptor* rather than sector index
567 // because SectorDescriptor* is the standard way to identify a sector.
568 size_t last_new_sector_index_ = SectorIndex(last_new_sector_);
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800569 size_t start = (last_new_sector_index_ + 1) % sector_map_size_;
David Rogers2761aeb2020-01-31 17:09:00 -0800570 SectorDescriptor* first_empty_sector = nullptr;
David Rogers8db5a722020-02-03 18:28:34 -0800571 bool at_least_two_empty_sectors = bypass_empty_sector_rule;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800572
David Rogers67f4b6c2020-02-06 16:17:09 -0800573 DBG("Find sector with %zu bytes available", size);
574 if (sector_to_skip != nullptr) {
575 DBG(" Skip sector %zu", SectorIndex(sector_to_skip));
576 }
577 if (bypass_empty_sector_rule) {
578 DBG(" Bypassing empty sector rule");
579 }
580
David Rogers8ce55cd2020-02-04 19:41:48 -0800581 // Look for a partial sector to use with enough space. Immediately use the
582 // first one of those that is found. While scanning for a partial sector, keep
583 // track of the first empty sector and if a second sector was seen.
584 for (size_t i = start; i != last_new_sector_index_;
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800585 i = (i + 1) % sector_map_size_) {
David Rogers2761aeb2020-01-31 17:09:00 -0800586 SectorDescriptor& sector = sector_map_[i];
Keir Mierle8c352dc2020-02-02 13:58:19 -0800587
David Rogers8db5a722020-02-03 18:28:34 -0800588 if (sector_to_skip == &sector) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800589 DBG(" Skipping the skip sector %zu", i);
David Rogers8db5a722020-02-03 18:28:34 -0800590 continue;
591 }
592
David Rogers67f4b6c2020-02-06 16:17:09 -0800593 DBG(" Examining sector %zu with %hu bytes available",
594 i,
595 sector.tail_free_bytes);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800596 if (!SectorEmpty(sector) && sector.HasSpace(size)) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800597 DBG(" Partially occupied sector %zu with enough space; done!", i);
David Rogers8ce55cd2020-02-04 19:41:48 -0800598 *found_sector = &sector;
599 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800600 }
601
602 if (SectorEmpty(sector)) {
603 if (first_empty_sector == nullptr) {
604 first_empty_sector = &sector;
605 } else {
606 at_least_two_empty_sectors = true;
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800607 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800608 }
609 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800610
David Rogers8ce55cd2020-02-04 19:41:48 -0800611 // If the scan for a partial sector does not find a suitable sector, use the
612 // first empty sector that was found. Normally it is required to keep 1 empty
613 // sector after the sector found here, but that rule can be bypassed in
614 // special circumstances (such as during garbage collection).
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800615 if (at_least_two_empty_sectors) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800616 DBG(" Found a usable empty sector; returning the first found (%zu)",
David Rogers8ce55cd2020-02-04 19:41:48 -0800617 SectorIndex(first_empty_sector));
618 last_new_sector_ = first_empty_sector;
619 *found_sector = first_empty_sector;
620 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800621 }
David Rogers8ce55cd2020-02-04 19:41:48 -0800622
623 // No sector was found.
David Rogers67f4b6c2020-02-06 16:17:09 -0800624 DBG(" Unable to find a usable sector");
David Rogers8ce55cd2020-02-04 19:41:48 -0800625 *found_sector = nullptr;
626 return Status::RESOURCE_EXHAUSTED;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800627}
628
David Rogers2761aeb2020-01-31 17:09:00 -0800629Status KeyValueStore::FindOrRecoverSectorWithSpace(SectorDescriptor** sector,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800630 size_t size) {
David Rogers8ce55cd2020-02-04 19:41:48 -0800631 Status result = FindSectorWithSpace(sector, size);
632 if (result.ok()) {
633 return result;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800634 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800635 if (options_.partial_gc_on_write) {
636 return GarbageCollectOneSector(sector);
637 }
David Rogers8ce55cd2020-02-04 19:41:48 -0800638 return result;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800639}
640
David Rogers2761aeb2020-01-31 17:09:00 -0800641KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect() {
642 SectorDescriptor* sector_candidate = nullptr;
David Rogersa12786b2020-01-31 16:02:33 -0800643 size_t candidate_bytes = 0;
644
645 // Step 1: Try to find a sectors with stale keys and no valid keys (no
646 // relocation needed). If any such sectors are found, use the sector with the
647 // most reclaimable bytes.
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800648 for (auto& sector : sectors()) {
David Rogersa12786b2020-01-31 16:02:33 -0800649 if ((sector.valid_bytes == 0) &&
650 (RecoverableBytes(sector) > candidate_bytes)) {
651 sector_candidate = &sector;
652 candidate_bytes = RecoverableBytes(sector);
653 }
654 }
655
656 // Step 2: If step 1 yields no sectors, just find the sector with the most
657 // reclaimable bytes.
658 if (sector_candidate == nullptr) {
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800659 for (auto& sector : sectors()) {
David Rogersa12786b2020-01-31 16:02:33 -0800660 if (RecoverableBytes(sector) > candidate_bytes) {
661 sector_candidate = &sector;
662 candidate_bytes = RecoverableBytes(sector);
663 }
664 }
665 }
666
David Rogers67f4b6c2020-02-06 16:17:09 -0800667 DBG("Found sector %zu to Garbage Collect, %zu recoverable bytes",
668 SectorIndex(sector_candidate),
669 RecoverableBytes(*sector_candidate));
David Rogersa12786b2020-01-31 16:02:33 -0800670 return sector_candidate;
671}
672
David Rogers2761aeb2020-01-31 17:09:00 -0800673Status KeyValueStore::GarbageCollectOneSector(SectorDescriptor** sector) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800674 DBG("Garbage Collect a single sector");
675
David Rogersa12786b2020-01-31 16:02:33 -0800676 // Step 1: Find the sector to garbage collect
David Rogers2761aeb2020-01-31 17:09:00 -0800677 SectorDescriptor* sector_to_gc = FindSectorToGarbageCollect();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800678
David Rogersa12786b2020-01-31 16:02:33 -0800679 if (sector_to_gc == nullptr) {
680 return Status::RESOURCE_EXHAUSTED;
681 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800682
David Rogersa12786b2020-01-31 16:02:33 -0800683 // Step 2: Move any valid entries in the GC sector to other sectors
684 if (sector_to_gc->valid_bytes != 0) {
David Rogers2761aeb2020-01-31 17:09:00 -0800685 for (auto& descriptor : key_descriptors()) {
686 if (AddressInSector(*sector_to_gc, descriptor.address)) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800687 DBG(" Relocate entry");
David Rogers2761aeb2020-01-31 17:09:00 -0800688 TRY(RelocateEntry(descriptor));
David Rogersa12786b2020-01-31 16:02:33 -0800689 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800690 }
691 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800692
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800693 if (sector_to_gc->valid_bytes != 0) {
David Rogers67f4b6c2020-02-06 16:17:09 -0800694 ERR(" Failed to relocate valid entries from sector being garbage "
695 "collected, %hu valid bytes remain",
696 sector_to_gc->valid_bytes);
Wyatt Heplerb7609542020-01-24 10:29:54 -0800697 return Status::INTERNAL;
698 }
699
David Rogersa12786b2020-01-31 16:02:33 -0800700 // Step 3: Reinitialize the sector
701 sector_to_gc->tail_free_bytes = 0;
702 TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1));
703 sector_to_gc->tail_free_bytes = partition_.sector_size_bytes();
Wyatt Heplerb7609542020-01-24 10:29:54 -0800704
David Rogers67f4b6c2020-02-06 16:17:09 -0800705 DBG(" Garbage Collect complete");
David Rogersa12786b2020-01-31 16:02:33 -0800706 *sector = sector_to_gc;
707 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800708}
709
David Rogers2761aeb2020-01-31 17:09:00 -0800710Status KeyValueStore::AppendEntry(SectorDescriptor* sector,
711 KeyDescriptor* key_descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800712 const string_view key,
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800713 span<const byte> value,
714 KeyDescriptor::State new_state) {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800715 // write header, key, and value
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800716 EntryHeader header;
717
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800718 if (new_state == KeyDescriptor::kDeleted) {
Wyatt Hepler6c24c062020-02-05 15:30:49 -0800719 header = EntryHeader::Tombstone(entry_header_format_.magic,
720 entry_header_format_.checksum,
721 key,
722 key_descriptor->key_version + 1);
723 } else {
724 header = EntryHeader::Valid(entry_header_format_.magic,
725 entry_header_format_.checksum,
726 key,
727 value,
728 key_descriptor->key_version + 1);
729 }
730
Keir Mierle8c352dc2020-02-02 13:58:19 -0800731 DBG("Appending entry with key version: %zx", size_t(header.key_version()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800732
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800733 Address address = NextWritableAddress(sector);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800734 DBG("Appending to address: %zx", size_t(address));
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800735
736 // Handles writing multiple concatenated buffers, while breaking up the writes
737 // into alignment-sized blocks.
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800738 TRY_ASSIGN(
739 size_t written,
740 partition_.Write(
741 address, {as_bytes(span(&header, 1)), as_bytes(span(key)), value}));
742
743 if (options_.verify_on_write) {
Wyatt Hepler0a223582020-02-04 17:47:40 -0800744 TRY(header.VerifyChecksumInFlash(
745 &partition_, address, entry_header_format_.checksum));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800746 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800747
David Rogers2761aeb2020-01-31 17:09:00 -0800748 key_descriptor->address = address;
749 key_descriptor->key_version = header.key_version();
Wyatt Hepler5a33d8c2020-02-06 09:32:58 -0800750 key_descriptor->state = new_state;
751
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800752 sector->valid_bytes += written;
753 sector->tail_free_bytes -= written;
754 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800755}
756
Keir Mierle8c352dc2020-02-02 13:58:19 -0800757void KeyValueStore::LogDebugInfo() {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800758 const size_t sector_size_bytes = partition_.sector_size_bytes();
759 DBG("====================== KEY VALUE STORE DUMP =========================");
760 DBG(" ");
761 DBG("Flash partition:");
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800762 DBG(" Sector count = %zu", partition_.sector_count());
763 DBG(" Sector max count = %zu", kMaxUsableSectors);
764 DBG(" Sectors in use = %zu", sector_map_size_);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800765 DBG(" Sector size = %zu", sector_size_bytes);
766 DBG(" Total size = %zu", partition_.size_bytes());
767 DBG(" Alignment = %zu", partition_.alignment_bytes());
768 DBG(" ");
769 DBG("Key descriptors:");
770 DBG(" Entry count = %zu", key_descriptor_list_size_);
771 DBG(" Max entry count = %zu", kMaxEntries);
772 DBG(" ");
773 DBG(" # hash version address address (hex)");
774 for (size_t i = 0; i < key_descriptor_list_size_; ++i) {
775 const KeyDescriptor& kd = key_descriptor_list_[i];
776 DBG(" |%3zu: | %8zx |%8zu | %8zu | %8zx",
777 i,
778 size_t(kd.key_hash),
779 size_t(kd.key_version),
780 size_t(kd.address),
781 size_t(kd.address));
782 }
783 DBG(" ");
784
785 DBG("Sector descriptors:");
786 DBG(" # tail free valid has_space");
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800787 for (size_t sector_id = 0; sector_id < sector_map_size_; ++sector_id) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800788 const SectorDescriptor& sd = sector_map_[sector_id];
789 DBG(" |%3zu: | %8zu |%8zu | %s",
790 sector_id,
791 size_t(sd.tail_free_bytes),
792 size_t(sd.valid_bytes),
793 sd.tail_free_bytes ? "YES" : "");
794 }
795 DBG(" ");
796
797 // TODO: This should stop logging after some threshold.
798 // size_t dumped_bytes = 0;
799 DBG("Sector raw data:");
Wyatt Heplerad0a7932020-02-06 08:20:38 -0800800 for (size_t sector_id = 0; sector_id < sector_map_size_; ++sector_id) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800801 // Read sector data. Yes, this will blow the stack on embedded.
802 std::array<byte, 500> raw_sector_data; // TODO
803 StatusWithSize sws =
804 partition_.Read(sector_id * sector_size_bytes, raw_sector_data);
805 DBG("Read: %zu bytes", sws.size());
806
807 DBG(" base addr offs 0 1 2 3 4 5 6 7");
808 for (size_t i = 0; i < sector_size_bytes; i += 8) {
809 DBG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x",
810 sector_id,
811 (sector_id * sector_size_bytes) + i,
812 i,
813 static_cast<unsigned int>(raw_sector_data[i + 0]),
814 static_cast<unsigned int>(raw_sector_data[i + 1]),
815 static_cast<unsigned int>(raw_sector_data[i + 2]),
816 static_cast<unsigned int>(raw_sector_data[i + 3]),
817 static_cast<unsigned int>(raw_sector_data[i + 4]),
818 static_cast<unsigned int>(raw_sector_data[i + 5]),
819 static_cast<unsigned int>(raw_sector_data[i + 6]),
820 static_cast<unsigned int>(raw_sector_data[i + 7]));
821
822 // TODO: Fix exit condition.
823 if (i > 128) {
824 break;
825 }
826 }
827 DBG(" ");
828 }
829
830 DBG("////////////////////// KEY VALUE STORE DUMP END /////////////////////");
831}
832
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800833} // namespace pw::kvs