Re: Tips on Saving Data?
Re: Tips on Saving Data?
- Subject: Re: Tips on Saving Data?
- From: Matthew Cox <email@hidden>
- Date: Thu, 11 Dec 2003 15:47:37 -0500
On Thursday, December 11, 2003, at 11:31 AM, Matt Walters wrote:
Hey,
I am creating an application that would highly benefit from this
journaling type of scheme. Searching google gives me mostly brief
descriptions about what journaling is, very little on actually
implementing it.
Implementing this type of journalling is very easy. Lets say you have
your main datafile, File X. And you have your journal, File Y.
Presumably, X is somewhat intensive to save to (searching for records,
appending changes, etc.)
The structure of Y is something like this, but change it to suit your
needs. You could implement this in XML, but its overkill for a file
that's only meant to be used by the application behind the scenes.
BYTE 0-3 (long)Magic Number // This is a special value that makes
sure its really a journal file, and not say an MP3. It can also be used
to check for endianess of data in the file if you want to be fancy.
BYTE 4 (char)FLAGS // The flag field basically says that the journal
file contains data, but you can add other information if you need to
keep up with it.
BYTE 5-9 (long)Number Of Records // This says how many records the
journal contains.
BYTE 10 START OF RECORDS
A record is basically just four bytes saying how long the real data is,
and then the data. Kind of like:
struct data_record {
unsigned long size;
char data[size]; // Obviously this is illegal, but its just a
conceptual example
}
Interacting with the journal has two main tasks:
1) Writing Data
The first thing to do is go where the last record ends. If you are
saving objective-C objects, then you can use NSCoding to push them into
an NSData object. Write first the size of that NSData object, then the
NSData object itself. Finally, increment the number of records in the
file.
If you don't know where the file ends, the way to get there is simple.
Start at the beginning of the file. Now's a good time to verify the
magic number. Then, read the flags to see if there's data in the file.
If there is, read the number of records. Then, go to byte ten in the
file. Read the size of that record, skip forward that many bytes, read
the next size, skip forward, etc. Each time you read a record size,
decrement the number of records remaining, and when you get to zero,
you're at the end of the data.
2) Saving Data to main file.
What to do is open the file Y, verify the magic number, and then see if
there are changes in the file (the flag field reflects this.) Then,
read the number of entries, and then you can simply start reading
records. Read in the size of the record, then read that many bytes into
an NSData object, and use your friend NSCoding to decode it into your
Objective-C object. Continue reading all this into the file. Finally,
take your objects/data and write them into the main database. Then you
can just delete the file.
That's it. Its pretty simple in concept, and gives you some serious
power. Because you don't update the number of entries until after
they've been written, if you application crashes/power goes out/act of
god happens, you always have consistent data in the journal, and no
partially written records (at least not ones you'll read mistakenly).
If you don't feel like implementing something like this, have a look at
sqlite (www.sqlite.org) There are projects to wrap this in objective-c
objects to use it for object archiving.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.