NVS set the same value as stored in nvs partition

Sweaps
Posts: 5
Joined: Fri May 29, 2020 6:44 am

NVS set the same value as stored in nvs partition

Postby Sweaps » Thu Sep 10, 2020 1:51 pm

Hello,

I'm working on ESP-IDF v4.0.1

I would like to know what happens if I call nvs_set_* and I already have got the same value stored in my nvs partition ?
In my code, I don't know if I need to check the previous value stored before storing a new one and I don't find a good information into the docs.

I found the followings information in https://docs.espressif.com/projects/esp ... flash.html:
Keys are required to be unique. Assigning a new value to an existing key works as follows:

- if the new value is of the same type as the old one, value is updated
- if the new value has a different data type, an error is returned

and https://docs.espressif.com/projects/esp ... alue-pairs :
Log of key-value pairs
NVS stores key-value pairs sequentially, with new key-value pairs being added at the end. When a value of any given key has to be updated, a new key-value pair is added at the end of the log and the old key-value pair is marked as erased.

but I still don't know the answer after, can someone help me ? :D

ESP_Sprite
Posts: 3937
Joined: Thu Nov 26, 2015 4:08 am

Re: NVS set the same value as stored in nvs partition

Postby ESP_Sprite » Fri Sep 11, 2020 8:31 am

You can normally just store the data and it'll overwrite the old value if it's there. Only exception is if the existing value is of a different type: for instance, you do a nvs_set_i8(handle, "myKey", 1) followed by a nvs_set_str(handle, "myKey", "hello world");. As long as you don't do that, you can happily write away your values without checking if there's one already first.

Sweaps
Posts: 5
Joined: Fri May 29, 2020 6:44 am

Re: NVS set the same value as stored in nvs partition

Postby Sweaps » Fri Sep 11, 2020 1:58 pm

Hello ESP_Sprite,

Sorry, maybe my question was unclear.

What happens if I do this ?

Code: Select all

nvs_open(handle);
nvs_set_i8(handle, "myKey", 1) ;
nvs_commit(handle);
nvs_close(handle);

nvs_open(handle);
nvs_set_i8(handle, "myKey", 1) ;
nvs_commit(handle);
nvs_close(handle);
I save the same value which is 1 on "myKey".
Does the ESP32 overwrite the value in the NVS and resave it ? Or ESP32 understand it is the same value and does not write into the NVS ?

ESP_Sprite
Posts: 3937
Joined: Thu Nov 26, 2015 4:08 am

Re: NVS set the same value as stored in nvs partition

Postby ESP_Sprite » Sun Sep 13, 2020 7:47 am

Looks like it doesn't write the value if it's the same as is already in flash.

Who is online

Users browsing this forum: No registered users and 40 guests