Best way to store and search in a key-value structure

I have an ESP8266 node mcu, and I connected a micro SD card and rfid mldule to it.

Let's consider I have a key-value array with RFID ID and user ID pairs like this:

0123456789:1
0213456789:3
0553434677:4
.......
6467744677:1000

Total 1000 keys

What is the best method to search if a user scans their RFID card?

Do I need to save this in a file on the SD card and then iterate through each line to check for a match, for example, for ID 6467744677?

Or do i need to copy all data from sd card to an array variable everytime when esp8266 power on ?

Which method is faster and better and what is ur suggestion ?

Put the data in an array, and if a simple linear search is fast enough for you, that is the best way.

Do I need to save this in a file

Or as binary data in any form of nonvolatile memory.

If you keep the numbers in your array continuously increasing (like it is now), you can make lookup way faster using better search algorithms.

i can make keys increasing but not by +1 so it can be 1,4,6,7,10...

so what is ur suggestion, to save on sd card and read from there every time?

or to store data from sd card in a volatile array

it will have 1000 lines (1000 keys)

Do you want to lookup the keys or the cardIDs?

You could store the data in the SPIFFs.
You would not need an sd.
It should easily fit in the stack memory of esp. So I would not read it repeatedly from sd.

Ah. The subject of much computer science. Probably a simple linear search is fine. It's not like you're going to get 100s of RFID card scans per second. Faster (MUCH faster) algorithms require that the table be structured, or parsed ahead of time to produce some sort of index structure.
For instance simply ensuring that your array is arranged in increasing key size ("sorted") would enable a binary search that would reduce the number of comparisons needed to find a match from max 1000 to max ~10.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.