• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading Record by Rec Number

 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have been able to write a test class to read in schema and records and print them out. I've noticed that the first method in my DB interface is this:

// Reads a record from the file. Returns an array where each
// element is a record value.
public String[] read(int recNo) throws RecordNotFoundException;



I understand how it works. It takes a specific record number and then is returned as a String array with elements in the String being (in my case): name, location, specialties, etc. But how do I assign a record number to each record in my database? My one thought would be that in my Data class, the constructor executes all of the code to read in everything from my db file, and then stores all of the records in an ArrayList. I know then by the order they were inserted each will have a unique index for each record. However, that seems like a lot of overhead when having to update, delete, etc. I'm sure there has to be a better way of doing this? Any help or suggestions is greatly appreciated! Thanks!
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can assign a record number to each record in order of appearance in the file, so the 1st record is #0, and the next is #1, etc. You can then compute the offset of any record using this line:


Matt
[ November 18, 2004: Message edited by: Matt Sheehan. ]
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. First, what does the zeroOffset variable mean? And second, where would I place this line of code? Thanks!
 
Matt Sheehan.
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
zeroOffset is the offset to the start of record zero. It could also be viewed as the length, in bytes, of the schema description data.

In my implementation I use a RandomAccessFile. When I need to read or write to a record I must first call 'file.seek(offset)', using the offset computation I mentioned, to get the file pointer to the correct place in the file.

Matt
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matt Sheehan.:
zeroOffset is the offset to the start of record zero. It could also be viewed as the length, in bytes, of the schema description data.

In my implementation I use a RandomAccessFile. When I need to read or write to a record I must first call 'file.seek(offset)', using the offset computation I mentioned, to get the file pointer to the correct place in the file.

Matt



Okay, that makes sense. I'm just a bit confused about how I implement this. Here again is that method in my interface:

// Reads a record from the file. Returns an array where each
// element is a record value.
public String[] read(int recNo) throws RecordNotFoundException;

Maybe I am just a little confused about how I use this method, if that makes sense. The parameter just takes a record number, which looking at it, just seems like some arbitrary number. Maybe it's late and i'm not thinking clearly. What exactly do I use this method for, besides the stated obvious of the comments? I can see reading the record, returning the string array, editing it, and calling the update method on the record; that makes sense. But how would I know what record I wanted if I only have a set of arbitray record numbers from 0 to whatever the last record number is? I hope my question makes sense. Thanks!
[ November 18, 2004: Message edited by: Daniel Simpson ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel,

Just a quick note: My assignment is B&S contractors, so my records are a representation of a contractor.

The parameter just takes a record number, which looking at it, just seems like some arbitrary number. Maybe it's late and i'm not thinking clearly. What exactly do I use this method for, besides the stated obvious of the comments? I can see reading the record, returning the string array, editing it, and calling the update method on the record; that makes sense. But how would I know what record I wanted if I only have a set of arbitray record numbers from 0 to whatever the last record number is?



I'm assuming that, like me, you have this method or one similar:


A search involves first calling this find method to get the record numbers of records that match the search criteria, and then going through and actually getting the records by calling read(recNos[n]) for each record number n in the integer array returned by the find method.

Another time where read(recNo) is used is when the user wants to book a contractor. Say the user selects a contractor and presses the "book" button. I get the record number of this contractor, call read(recNo), make sure the contractor hasn't been booked since the last time I updated my view, and, if not, I then book the contractor (by updating the owner field of the record).

So to sum things up, the record number isn't something that's stored in the database file itself: it's simply the location of a record relative to other records. If you're using a RandomAccessFile and you want to move your file pointer to the 5th file, then you would use Matt's code as follows:

where zeroOffset is the start of the first file (end of the schema), recordLength is the length in bytes of a record as determined in the schema, and flagLength is the 2 byte flag telling whether the record is valid or not. To ensure that recNo isn't just an arbitrary number, you need to keep a record and it's corresponding record number together ONCE YOU TAKE IT OUT OF THE DATABASE FILE!!!
Therefore, you need to find a way to transport a record (String[]) and a record number (int) together as one unit back to your display. I did this by creating a class called Contractor, which has the following constructor:

It is instances of this Contractor class, NOT instances of String[], that I put in my TableModel.

Maybe it's late and i'm not thinking clearly.


Me too, so let me know if I can clarify anything.
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jared, thanks for the reply. I have two questions. I believe that we have the same assignment version. First, what is the purpose of the create and delete methods? They obviously aren't accessible to the end user, so I'm guessing they are only for us to use as a means of testing records by creating and deleting? Also, are you using your Data class to do all of the implemented methods of DB? Where are you putting your code for reading in the schema, records, etc. of the database file? Thanks!
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Data class doesn't give a $@#% about B&S contractors. It's only job is to provide basic database functionality for a file that follows the provided format. So, for example, if an ice cream company creates a database file using this format, my Data class would work with their database file. Again, my Data class has nothing to do with contractors, and it doesn't know what functionality the client side program will have. So therefore it is justifiable to implement all database functionality.

When I get to the parts of my program where I implement program-specific stuff, I never end up using create or delete, so you are correct, they are never accessible to the end user. But lets say B&S were to say "oh, by the way, we need you to write a small maintenance program to create, modify, and delete records on the server." If this happened, you could simply reuse your Data class for this program (assuming you implemented create and delete).

Perhaps an easier way of looking at it is that Sun requires you to implement the supplied interface, the little extra code of delete and create is painless, so just do it.

So to answer, I implemented the entire interface in Data, and I also read in and stored the schema in my Data class.
[ November 19, 2004: Message edited by: Jared Chapman ]
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jared Chapman:
My Data class doesn't give a $@#% about B&S contractors. It's only job is to provide basic database functionality for a file that follows the provided format. So, for example, if an ice cream company creates a database file using this format, my Data class would work with their database file. Again, my Data class has nothing to do with contractors, and it doesn't know what functionality the client side program will have. So therefore it is justifiable to implement all database functionality.

When I get to the parts of my program where I implement program-specific stuff, I never end up using create or delete, so you are correct, they are never accessible to the end user. But lets say B&S were to say "oh, by the way, we need you to write a small maintenance program to create, modify, and delete records on the server." If this happened, you could simply reuse your Data class for this program (assuming you implemented create and delete).

Perhaps an easier way of looking at it is that Sun requires you to implement the supplied interface, the little extra code of delete and create is painless, so just do it.

So to answer, I implemented the entire interface in Data, and I also read in and stored the schema in my Data class.

[ November 19, 2004: Message edited by: Jared Chapman ]



I agree, I actually have a Maint program in my test package that allows me to add, delete and arbitrarily modify the database and lock and unlock records. This makes it much easier to put the database in a state where testing is possible. The Maint program shares all the code with the Client except for the View and Controller. The Model, Network, and Server are all the same.
 
reply
    Bookmark Topic Watch Topic
  • New Topic