tweedegolf / sequential-storage

A crate for storing data in flash memory with minimal need for erasing pages
Apache License 2.0
87 stars 8 forks source link

Get number of saved items in map #33

Closed HaoboGu closed 4 months ago

HaoboGu commented 4 months ago

Hello! is there any way to get the number of saved items right now? I want to have some control about the number of saved item, ensuring the flash is always have a slot for new item.

diondokter commented 4 months ago

Hey, no there's not. And it'd be a pain to implement, because the system doesn't know about the keys really. It only knows how to compare two keys to know if they're the same. You can implement it yourself though. Just ask for every key you know and count how many times you receive Some.

You can also calculate how many items fit on a page. How much data does each item contain? Round that up to word alignment. For every item, there's also an item header of 8 bytes, rounded up to word alignment. Two words of a page are used for bookkeeping.

So say you've got items of 20 bytes, an alignment of 4 and your page is 1024 bytes long, then you can fit (1024 - 4*2) / (8 + 20) = 36.5. If you have items of many different sizes this calculation gets a bit more difficult, but I'm sure you can figure it out with this info.

And remember, with the Map, an old item will eventually be overridden. Only the most recent item with a given key will be kept around.

HaoboGu commented 4 months ago

And it'd be a pain to implement, because the system doesn't know about the keys really.

yeah, that's exactly what I'm struggling with. I want to iterate through the map and check all entries, just like what heapless::IndexMap::iter() does. Is it possible to add this?

You can implement it yourself though. Just ask for every key you know and count how many times you receive Some.

This is what I'm doing now, sort of inefficient but does the work :D

diondokter commented 4 months ago

It would be possible, but there'd be so many downsides. The library really doesn't know anything about the keys in its storage. It only knows how to compare them.

You as the user do know your keys, so creating an iterator out of it is 100x easier and without many downsides.

HaoboGu commented 4 months ago

gotcha! thanks!