PDA

View Full Version : Removing old data from a stringlist



ianatwork
03-07-2008, 03:42 PM
Hello,

I'm using the Delphi StringList as a buffer to hold data. This data is sent out of a socket when the link is available else the data is stored in the list. As I don't want the list to grow out of control I'm allowing the user to define a maximum size for the list and a life for each entry. The life of an entry is in the range of 1 minute to 24 hours in minute increments, this is a global setting so all items have this limit. I had planned to look at the oldest item on the list every minute and remove it when it exceeds the defined time. My problem is how to best to define when an item is too old and should be removed. My initial thought was that when I add an item to the list I use the current time + the timeout. When I check the first item (which will be the oldest), I check to see if that time is met and remove it. I then check the next item and if that's ok I go back to checking every minute etc. This method isn't good as it will go wrong if the user changes the pc time or the clock changes for daylight saving. What about if I have an internal counter that starts at 0 and is incremented every minute. I could use the above method but use the counter rather than the pc time. Any other ideas ?

arthurprs
03-07-2008, 04:34 PM
just deleting the entry does not free the data?

I think Modern Pascal (FP,Delphi) handles string data automatically

VilleK
03-07-2008, 05:45 PM
Daylight saving or user changes time are very rare occasions :)

I would just do this:


while (first_item_in_list is too old) OR (list.count >= maximum user defined size)
delete first item

chronozphere
03-07-2008, 05:57 PM
I would use a TQueue for that, because the situation you describe asks for a FIFO datastructure (first-in-first-out). A list could work here, but a Queue is more appropriate IMHO. :)

Writing your own timer is a good sollution, but i don't see many reasons why users should change the system time (At least i don't change it, except for the summer-time to winter-time switch). You would also ask yourself whether it is a bad bug or not. In some cases it would totally disorganize the system, but in most cases it doesn't matter IMHO. If it's a real big chunk of data, i suggest you write it to your HDD, instead of keeping it in RAM. When using the great ammount of space usually available on a harddrive, it might be unnecessary to use lifetime for your data. ;)

Brainer
03-07-2008, 07:42 PM
What about if I have an internal counter that starts at 0 and is incremented every minute. I could use the above method but use the counter rather than the pc time.
That's probably the best solution. Also, you could use another thread that would work as the counter and maybe a yet another one for deleting the data if it's too old. That would make your application multithreaded. But be careful when sharing data between many threads - I suggest you use critical sections - they are the best solution IMHO. ;)