From: Charles Hixson Date: 2004-09-10T02:11:14+09:00 Subject: Re: Problem with ensuring consistency .. Finalization?? Robert Klemme wrote: >"Kaspar Schiess" schrieb im Newsbeitrag >news:chp75o$4gp$1@sea.gmane.org... > > >>-----BEGIN PGP SIGNED MESSAGE----- >>Hash: SHA1 >> >>Charles Hixson wrote: >> >>| I want to have a class which occasionally updates a file, but I want >> >> >to > > >>| ensure that it always flushes it's data to the file before the program >>| quits. >> >>Not quite sure if I really understand your problem, but I might offer >>some things to consider here: >> >>1. stream.sync= true for enabling autoflush. Is this not possible ? This >>seems to be the simplest solution. >> >>2. Use Ruby's block syntax to 'ensure' flush after update. >> >> > >Yeah, but it really depends on the usage pattern: I'm not sure whether a) >the file is overwritten on every access and b) how often these updates >take place. Charles, can you clarify? > > > >>3. At exit of ruby program, all Files should be flushed anyhow, correct >>me on this if I am mistaken. >> >> > >Hm, I would have guessed otherwise but apparently you are right: > >12:00:34 [source]: ruby -e 'io=File.open("x", "w");io.sync = >false;io.print "hello"' >12:00:53 [source]: cat x >hello12:00:55 [source]: > >Kind regards > > robert > What's being done here is updating a database (an Sqlite database actually). So what I want to do is accumulate a bunch of changes, and then periodically add them either when things would be idle or when the number of changes starts to use too much ram. But I don't want to loose them when the program terminates, and the class doesn't terminate itself (notice that the table is referred to, indirectly, via a class variable @@wtable). Now the data accumulation happens in a class separate from the class that manipulates the database table, etc. The stream.sync approach doesn't seem to apply here at all. (Note that I want to be flushing data in a Hash Table to the file...so I can't use any automatic file flushing.) The suggestion of the END block of the file is a plausible approach, which I had forgotten existed. What I really want is a class finalizer, but lacking that I should be able to make the END block work, with a bit of redesign. It will drastically decrease the portability of the class, but as each file can have it's own END block, it shouldn't decrease the portability of the file. Thanks for the help, Charles