aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird:1.2.1   Accessing the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird:1.2.1   Accessing the database" Watch "URLyBird:1.2.1   Accessing the database" New topic
Author

URLyBird:1.2.1 Accessing the database

Michael Fisherman
Greenhorn

Joined: Oct 11, 2007
Posts: 18
when first looking at this assignment it seemed to be straight forward but the further I analyze this project the more questions come up, and the more my pen gets gnarled at with all the bite marks. But from other responses I here, I am glad I am not alone ...

When I read the URLyBird database upon first connection I read through the
entire database and collect the meta data info of the database: i.e the header information , schema info and the record status of all the records (the 1, 0 flag if a record has been deleted or not). I do not read the actual data in, but leave the file open and my RandomAccessFile pointer ready available to jump to a record in the file. So when a client calls on one of the database access functions, my file pointer will jump to the record in the file and retrieve the data for the user. The update and create functions will actually write to the file instead of saving it in a buffer.

Is this a reasonable way ? I have noticed that the other option is to read the entire datafile and input it in a data structure and upon exiting then save the data structure back to the file.

I think the former is better as far as saving storage space but of course access time will slow down. And I felt that the implementation better represented the DBAccess readRecord(recNo) description:

// Reads a record from the file. Returns an array where each element is
// a record value.

(did they mean file in the literal since ? )

Any comments ?
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
Hi Michael

I do the same thing. read update delete etc everything is done directly from and to the file. I dont have any cache yet. I thought it is easier to maintain than with using a cache(the data structure that you mentioned in your post)

cache will be good when searches are lot more than update and delete operations.

This is my understanding. and there are lots of posts on whether to use cache or not. Havent gone through them thoroughly yet.

I dont think that read from file literally mean read directly from file only. It guess its our choice on how to implement the read.
Thanks


SCJP 5.0<br />SCJD working on it
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Hi Michael,

I am also intended not to cache my records but just access file when necessary, because with caching the records you have a lot of issues that makes your code more complex and less clear:
- what to do if the saving on exiting fails? what if there is some power break (maybe a whole day of reserving rooms may be disappeared? ...
- if you choose to update file and cache each time, you have other issues like i described in this thread

so that's why my intention is not too implement a cache, but work with direct file access

i have also a question for you: at first connection you read complete db-info (header info, schema info, record status,...), how did you implement locking with this one? do you have 1 method doing it all where you lock at start and unlock at the end, or do you use small pieces of code between a synchronized block? because you will need locking on different objects (e.g. you need to lock db and lock the object containing schema-info when reading in schema-info), so that may turn in a dead-lock situation.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Michael Fisherman
Greenhorn

Joined: Oct 11, 2007
Posts: 18
Hi Roel

Thanks Mary and Roel for your input. This is a cool community here !

i have also a question for you: at first connection you read complete db-info (header info, schema info, record status,...), how did you implement locking with this one? do you have 1 method doing it all where you lock at start and unlock at the end, or do you use small pieces of code between a synchronized block? because you will need locking on different objects (e.g. you need to lock db and lock the object containing schema-info when reading in schema-info), so that may turn in a dead-lock situation.



What I assume is that the Server is up before any client connects. My Data class's constructor function is implemented when the Server starts. Through my meta class I read all the db-info without locks, since there is no client there anyway. My meta-data structure created at the start just contains the list of meta like records , where each record just contains a delete flag and a lock cookie. Then when a client connects and calls a update, create or delete function my Data class uses my meta class as a proxy which will handle the locking before and after accessing the class that loads and manages the file.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: URLyBird:1.2.1 Accessing the database