wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S - Data Access Implementation question 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 "B&S - Data Access Implementation question" Watch "B&S - Data Access Implementation question" New topic
Author

B&S - Data Access Implementation question

Christiaan Thamm
Greenhorn

Joined: May 29, 2012
Posts: 19
Hi guys

Ive got a question, just want to make sure im not doing something really stupid.

Ive got the compulsory interface DB and the class Data that implements it. I decided to create two new interfaces for Data Access and Locking Management that the data class would use (instead of doing locking stuff and data access stuff in the same class).

In my Data Access Implementation I cache a key (created from the hashcodes the records' fields) and the position of the record in the file so that when the read method is called with the key my cache can tell you the location and then you can jump to that location in the file and pull the record. So when the impl is created i create the cache. When the read method is called i call the cache for the location, and use seek to jump to that location to do my businazz.

I havent seen anyone else follow this approach, am i trying to do something too complex? My class is getting a little big :/ . I didnt want to search through the entire file everytime i want to read a record, and i also didnt want to load the entire file into memory. The logical solution for me was to do something in the middle of the two approaches

Thoughts?

Thanks!


OCJA, OCJP, OCMJD, OCEJWCD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Why not simply calculate the position of the record based on the record number, the (fixed) length of a record and the offset (the position where the data section - containing all records - starts)?


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

Joined: May 29, 2012
Posts: 19
Roel De Nijs wrote:Why not simply calculate the position of the record based on the record number, the (fixed) length of a record and the offset (the position where the data section - containing all records - starts)?


Do you mean the recNo input is (as an example) 5 (meaning the 5th record in the file). Thats fine but how am i going to know what the 5th record in the file is, its not sorted and they are not numbered. So when the client (or whoever calls my method) calls for record 5, he doesnt know what he will get back? I might be missing something here . I did actually implement that at first just to check how to pull the records. But then i thought how is the requesting entity going to know what record 5 is, it can be anything

With my way each record has a unique identifier so you know what youre getting when you request a record back.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
Christiann,

In the file, you may have 70 bytes of meta data (location 0-69 in the file). Then, you may have 184 bytes for your first record, another 184 bytes for your next record .....
The first record starts at location 70. To read the second record, it starts at 70+184 and etc....
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

The record number is just an internal way to uniquely identify the records.

So a user will start with a search (or request all records). So searching for Paris may result in 5 records. On the screen the user sees the name, location, price,.. and the user just selects a record (to make a booking for example) and hits the book button. The user doesn't care if this record is the 5th, 50th or 500th in the database (file), he's just interested that it meets his needs (and wishes). You (as a developer) just have to make sure you can identify your records uniquely (otherwise a user who wants to book a contractor in Paris may book in Brussels). So you could give your records a number based on the order in the database file: first record is 0 (or 1), 2nd record is 1 (or 2),... But that's just 1 of the many possibilities, but it's definitely the most easy one. And another advantage: you can use the number to locate the record in the database file using a simple formula.
Christiaan Thamm
Greenhorn

Joined: May 29, 2012
Posts: 19
Roel De Nijs wrote:The record number is just an internal way to uniquely identify the records.

So a user will start with a search (or request all records). So searching for Paris may result in 5 records. On the screen the user sees the name, location, price,.. and the user just selects a record (to make a booking for example) and hits the book button. The user doesn't care if this record is the 5th, 50th or 500th in the database (file), he's just interested that it meets his needs (and wishes). You (as a developer) just have to make sure you can identify your records uniquely (otherwise a user who wants to book a contractor in Paris may book in Brussels). So you could give your records a number based on the order in the database file: first record is 0 (or 1), 2nd record is 1 (or 2),... But that's just 1 of the many possibilities, but it's definitely the most easy one. And another advantage: you can use the number to locate the record in the database file using a simple formula.


Okay Roel i get what youre saying. I think my confusing came in by me thinking that someone might want to (at some point) call the read method without the front-end process you mentioned above. I then thought how would that client know what hes calling. Theres no way of him knowing which Contractor will be returned. I then thought okay so maybe he needs to call with a specific key related to the contractor. But I guess thats not really a requirement here... As long as i can identify them the front-end doesnt need to care where it sits and the user doesnt need to know what is called

Thanks for your help, just wish I asked before coding all the stuff i did first!
 
 
subject: B&S - Data Access Implementation question
 
Similar Threads
Should lock methods be callable by the client
Public methods and automatic SUN testing
RandomAccessFile and synchronization issues.
why lost so many points on locking
record cache