In order to simplify the file reading / writing process, can I use a data cache Map <recordID, string>, and read all the data from the file into this Map in the beginning of the application, and write them back to the file in the end. All the operations in the middle are only performed in the cache. Is this OK?
If this is OK, we probably need to include the 1 Byte flag value in each String whenever needed, correct?
I used also a record cache approach. If you use search engine, you'll find tons of threads about this subject.
Do not include the "deleted" flag in your String. It doesn't make sense at all, I guarantee it will completely mess up your code,... More reasons why not to include it in String (or pojo) can be found here. You can simply store null in your map when the record is deleted.
The only reason that I want to have deleted flag in String is to enable reusing deleted record when creating a new record, so that I can compare those deleted ones and see if I can reuse any of them. If we store null, how can we compare the records? Or we can store the flag info into a different structure, e.g., another Map <recNo, delelted flag>, right?
There is no need to store the deleted flag, you just compare the stored value for an specific key
if it's equal to null or not.
If there are no null values once you iterate through all the keys, just create a new record.
When you write the cache to the file and you find a null value, write the deleted flag data record to the file.
Joined: Nov 06, 2006
Thanks Carlos. So, reusing deleted record only means reusing the deleted space, it is NOT necessary to have the same record content before reusing, correct? If that is the case, I am clear now.
You just reuse the space of a deleted record when creating a new one, not the actual data of the deleted record. Should be obvious, because the create method has a String as parameter (which is the data for the new record). How would you reuse the same record content if the application used a RDBMS (instead of flat fil)e and delete from room where id = 1 actually deletes the record, no possibility to recover the actual field values of the deleted record.
Joined: Nov 06, 2006
I see. Thanks Roel for my beginner question Now I am very clear about everything.