"I'm not back." - Bill Harding, Twister
There is NO record number in the header of file in my assignement. /QUOTE]
Sorry.
Ok, I what Jim've said is exactly what I was afraid of.
Ok. Do you think it is Ok that to do the following in constructor of Data(Singletone):
1. Open File (File file = new File("...");
2. Create fileChannel for writing):
3. Calculate size of the file and then number of records (I need it to loop records till the end to cach them later)
4. Create DataInputStream read and cache all record and close DataInputStream.
Do you think it is acceptable?
Tx,
Vlad
[ September 13, 2003: Message edited by: Vlad Rabkin ]
I have forgot to write "no" in my text. So, I wanted to say:
4. Create DataInputStream read and cache all record and close DataInputStream.
Calculate size of the file and then number of records...
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
That is what I do and therefore I don't have to deal with EOF marker or exception
I use RandomAccessFile directly to read the header (no need for DataInputStream), then I get the FileChannel to read the records.
Andrew said it is not a problem to read whole record instead of reading first a flag, since performance for read 1 byte and 156 bytes is almost the same.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Vlad:
Phil, I hate it, I am lazy and find always execuces not to do something, and you come with your ideas, which make me refactor some things !
Andrew:
Calculating number of records based on file size gives you an additional bonus as well: you have an extra check that the file is reasonable (not corrupt).
Phil (first post):
I have not such a number of records in the header of my file. But if I had one, I think that I would throw what I called an InvalidDataFileException is that number is incompatible with the normal file size as computed.
I would do that for consistency with my own current design : when opening the database, such an InvalidDataFileException is thrown if one of those cases occurs :
- the file signature is invalid;
- the record length as computed from individual fields lenghts read from the header is different from the record length mentioned in the header too;
- the record length is incompatible with the current Charset.
Vlad:
1) I have first to initialize array to read record. If the record is marked as deleted I still have to read whole record first, or I can first initialize array with one element just to read a flag and then if it is not deleted initialize second array to read the rest. DataInputStream/RAF allows me to read a flag without initializing an array. By the way: what would you preffer? I don't remember, but I guess Andrew said it is not a problem to read whole record instead of reading first a flag, since performance for read 1 byte and 156 bytes is almost the same.
Vlad:Well, I used DataInputStream for Header to avoid problem with requirements, and DataInputStream for record jus not to use another stream or RAF. I know you have wrote some time before that Sun just said that header is in DataInputStream format, nobody restricts to use RAF for reading.
All numeric values are stored in the header information use the formats of the DataInputStream and DataOutputStream classes.
Vlad:
As Max gave idea to use nio I use FileChannel for writing records (at least because it is faster, but I was to lazy to rewrite code for reading, moreover, there is one small advantage to use DataInputStream/RAF instead of FileChannel for reading recods:
Is mixing nio and standard io a problem. I myself use old io to read thhe header and nio for the records.
when your program reads one byte, the operating system actually reads one block.
First, I agree with what Andrew just wrote on the subject. But I would add this : if you read the deleted flag separately from the record, you are sure to be slower because your two reads
So RAF is compatible with both mentioned classes.
I allocate only one ByteBuffer, reused all over again for each read record.
FileChannels represent a two-way connection to a file, allowing you to read and write to the file using that single connection".
So what? DataInputStream not, but RAF makes it also.
Phil, you can't say that you use only nio, since you read file header with io (RAF)...
Ok, I agree on all this issues and will refactor my Data class.
Thanx for all your suggestions!
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
And you've both got Data-level sync which prevents multiple threads from using thins concurrently, correct?
For comparison, with Record-level sync I need to allocate() each ByteBuffer as new.
Which still seems to be pretty darn quick, I think because the system is good at caching these behind the scenes.
"I'm not back." - Bill Harding, Twister
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|