This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Flash Data storage index will not initialized after delete?

Say I have 3 records, then I delete them and collect the garbage. If I try to save a new record again, it will be saved with a record ID 4 , but it should be back to 1 after the delete.

EDIT: The indexes are actually going crazy, if you start your program by erasing memory and then saving a new record after it, and you run this multiple times, your writing record ID for every save would go :

ID=1
ID=3
ID=5
ID=7

instead of staying at 1 all the time , because you delete and collect every run before you save.

delete :

        #define FILE_ID     0x1111
	#define REC_KEY     0x2222
	fds_record_desc_t   record_desc;
	fds_find_token_t    ftok ={0};

 

	ftok.page=0;
	ftok.p_addr=NULL;
	// Loop and find records with same ID and rec key and mark them as deleted. 
	while (fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
	{
		fds_record_delete(&record_desc);
                
                 
	}
	 
         fds_gc();

I save again only after the handler result :

static void fds_evt_handler(fds_evt_t const * const p_fds_evt) {
...
...
 case FDS_EVT_GC:
                 if (p_fds_evt->result == FDS_SUCCESS)  
                  { (*flashDelegate)("garbage collected !"); }   // ** this will happen and here i save

case FDS_EVT_DEL_FILE  //this will not happen but it will delete the file
...

So what happens is that when I save after the delete, he start saving from record ID >1 .

Save with this :

        #define FILE_ID     0x1111
	#define REC_KEY     0x2222

 

	fds_record_t        record;
	fds_record_desc_t   record_desc;
	fds_record_chunk_t  record_chunk;
	// Set up data.
	record_chunk.p_data         = &toSave;
	record_chunk.length_words   = (sizeof(toSave)+3)/4;
	// Set up record.
	record.file_id              = FILE_ID;
	record.key              		= REC_KEY;
	record.data.p_chunks       = &record_chunk;
	record.data.num_chunks   = 1;
			
	ret_code_t ret = fds_record_write(&record_desc, &record);

I even tried to init it before write with this :

  record_desc.record_id = 1 ;

but it's still different from 1 even after the delete and the gc.

Parents
  • FormerMember
    0 FormerMember

    As written in the FDS doucmentation, deleting a record doesn't remove the record:

    Deleting a record does not actually delete the record data and clear the used flash space, but it invalidates the record. After a record has been deleted, it cannot be opened, read, or located anymore.

    However, the flash space that is used by the record is not freed right away. To free the space that is used by invalidated records, you must run garbage collection.

    The record ID is an incrementing number, and it is not being initialized upon a garbage collection (at least not as far as I can see).

    A new record ID is created from record_id_new() in fds.c

  • I see, it's a strange behavior in software but what do I know :) thanks a lot.

Reply Children
No Data
Related