FDS Write Update or both?

I need to store persistent data of ~50 configuration bytes which will be written on command and loaded automatically on power up. So I am using FDS and writing just the one file / record.

Obviously when the product first powers up the flash will be blank, so the record need to be created. Subsequently the record need to be updated.

I'm not sure the bahaviour of the create / update functions:

If I update a record whch doesn't exist, will it be created anyway?

I noticed during development that I run multiple fds_record_write with the same file ID  & record keys I get duplicated records (OK with different record IDs). Which one would be returned by the find function?

So my question is basically: Do I need to search for, then create or update my record? Or can I use fds_record_update for example to create then update which would make things a lot simpler.

Parents
  • Hi,

    Which one would be returned by the find function?

    It will find the first record with the same file and key, as ordered when it searched (this can change after a garbage collection so you should not pay any attention to the order). If you have multiple records call fds_record_find() / fds_record_find_by_key() / fds_record_find_in_file() again with the same parameters to continue searching. See the FDS API documentation for details.

    So my question is basically: Do I need to search for, then create or update my record? Or can I use fds_record_update for example to create then update which would make things a lot simpler.

    No, you need to search and update if it exists, or create if not. This does not have to be very complex, though. Here is a snippet doing that from the peer manager implementation:

    ...
        rec.file_id           = peer_id_to_file_id(peer_id);
        rec.key               = peer_data_id_to_record_key(p_peer_data->data_id);
        rec.data.p_data       = (void*)p_peer_data->p_all_data;
        rec.data.length_words = p_peer_data->length_words;
    
        ret = peer_data_find(peer_id, p_peer_data->data_id, &rec_desc);
    
        if (ret == NRF_ERROR_NOT_FOUND)
        {
            ret = fds_record_write(&rec_desc, &rec);
        }
        else // NRF_SUCCESS
        {
            // Update existing record.
            ret = fds_record_update(&rec_desc, &rec);
        }
    ...

Reply
  • Hi,

    Which one would be returned by the find function?

    It will find the first record with the same file and key, as ordered when it searched (this can change after a garbage collection so you should not pay any attention to the order). If you have multiple records call fds_record_find() / fds_record_find_by_key() / fds_record_find_in_file() again with the same parameters to continue searching. See the FDS API documentation for details.

    So my question is basically: Do I need to search for, then create or update my record? Or can I use fds_record_update for example to create then update which would make things a lot simpler.

    No, you need to search and update if it exists, or create if not. This does not have to be very complex, though. Here is a snippet doing that from the peer manager implementation:

    ...
        rec.file_id           = peer_id_to_file_id(peer_id);
        rec.key               = peer_data_id_to_record_key(p_peer_data->data_id);
        rec.data.p_data       = (void*)p_peer_data->p_all_data;
        rec.data.length_words = p_peer_data->length_words;
    
        ret = peer_data_find(peer_id, p_peer_data->data_id, &rec_desc);
    
        if (ret == NRF_ERROR_NOT_FOUND)
        {
            ret = fds_record_write(&rec_desc, &rec);
        }
        else // NRF_SUCCESS
        {
            // Update existing record.
            ret = fds_record_update(&rec_desc, &rec);
        }
    ...

Children
Related