Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on record numbers (Bogitt & Scarper)

 
Joshua Hutchings
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This like seem like a bit of a stupid question to you all but the issue of record numbers has me somewhat confused. The API in DBMain.java uses record numbers to do all sorts of things but the schema in the instructions doesn't have a record number field and nor does looking at the schema defined in the db file itself provide one. Even the data doesn't have record numbers (as far as I can tell). So where do these mysterious numbers live? Is it based on the order in which they appear in the file itself?

Any help on this matter would be greatly appreciated,

Cheers,
Hutch
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua,

Welcome to JavaRanch and this forum!

So where do these mysterious numbers live? Is it based on the order in which they appear in the file itself?


Yes, record numbers represent the order of records in the file. And as the records have a fixed length, they can be used to compute the offset (position) of any record in the file with a formula like this one:

offset = dataOffset + (recNo-1) * recordLength

Regards,

Phil.
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Said another way, you pretty much get to define the record record numbers. But pretty much, you only have two reasonable options, and that is whether the first record in the database is considered record #0 or record #1.

Based upon the sample database I received, the first record (Buonnarotti & Company) is record 0. The next one is record 1, and so forth.

Here's a function I wrote which I found very useful:

[code]
protected int getRecordCount() throws IOException
{
long dataSpace = _file.length() - _offset;
return (int)(dataSpace / (_metadata.getRecordSize() + 1));
}
[code]

Where _offset is the start of record 0 (which, it HAPPENS to turn out, is byte #72), and _metadata.getRecordSize() is the length of a record (not including the delete indicator - that's what the + 1 is for at the end)

Is this helpful, or confusing?
 
Eben Hewitt
Author
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created an object representing each record as I read in the data file initially. Then I could store as a member variable of my record object where in the file that record starts. That allowed me to make a record number that the interface methods could refer to that is like an auto-incrementing primary key in an RDBMS.

When I needed to write to the file, the record object already knew where to go to do the write. That way I didn't have to keep the file open or even have any reference to the file other than a quick method local open, write, close deal.

I think there is a lot of debate about whether or not to create an in-memory representation of the database or just hit the file directly for eveything. That's just how I did it, because it's closer to what I'm used to doing regularly, say, with a DAO.
 
Jan Rotthaus
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua,

I got the same assignment, and I agree with Robert: I think the recNo represents the position of the record in the file. As in Java positions (e.g. in arrays) usually start with 0, I decided to start with recNo = 0 for the first record.

But because it is not specified, I think you can choose any interpretation as long as it's consistent.

Jan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic