Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading DB records

 
Laura Williamson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I can read all the data now.
Is it best to create a list of all records from the flatfile and store this? Or is it best just to leave the flatfile and seek out DB records as you need to access/update them?
The problem I see with keeping a list stored is need to updata data in two places if a chance occurs (in the list and in the flatfile)
Thanks - all advice appreciated.
 
Denis Spirin
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Laura

The problem I see with keeping a list stored is need to updata data in two places if a chance occurs (in the list and in the flatfile)

The problem with seeking db file is bad performance.
When you search db, you have to check all records. This happens more often than update; while when you update db all you need is seek() position in file and write new record.
Keeping copy of database leads to better performance and thats what I'd prefer.
[ April 30, 2004: Message edited by: Denis Spirin ]
 
Laura Williamson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK will go with that
thanks again
LW
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura,
Denis is absolutely right! Two things, though:
  • performance is not one of the assignments objectives.
  • if you use RandomAccessFile (which you should, I suggest) you get a little caching for free as the RAF uses an internal buffer i.e. it's very fast. I've tested this with several dozen parallel threads accessing the same RAF instance that reference a file with 3000 records.
  • I'd recommend you go for the simple solution first and leave the cache aside. You can still add it in the end and wrap it around your existing data class using delegation.
    Regards,
    Marcel
     
    Willy Leung
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    I am also debating whether to cache all records. Here're my thoughts.
    I am making the assumption that two specific fields (name + location) in a record make up a primary key. However, the DB interface deals solely with record numbers. So I need to keep a mapping between record numbers and primary keys. If I am cahcing names and locations already, why not the rest of the record and the location of the record in the database? IMO, the cache would make implementing all the methods a lot simpler.
    Any comments?
    Willy
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 12007
    215
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Willy,
    Welcome to JavaRanch and this forum.
    I agree that with the way you are proposing, it will make sense to cache the entire record.
    You do need to check that your proposed primary key is logically reasonable given the types of data that can go in them for your assignment (that is, that there is no chance that someone may have a valid reason for wanting to add another record with the same name / location values).
    Regards, Andrew
     
    Willy Leung
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Andrew,
    Thanks for the warm welcome and your reply.
    After looking at the data in the database file, I do think it is logical to assume that name and location together make up a unique key. I plan to document this in my choices.txt and to go with the caching approach.
    Thanks again.
    Willy
     
    Hanna Habashy
    Ranch Hand
    Posts: 532
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi guys:
    I am also debating between caching the data or using RandomAccessFile stream. My initial thought on caching the record from the database is its size. In real life project, you never know the size of you database, and never know how big the database can grow to. If the database file grow too big, the jvm will run out of memory.
    It is the same case dealing with RDB. One can't bring the whole database to the jvm, that is why we have to access the database every time we need to perform a query. I know the project scope is much smaller than real life project, but there are 100 points in general consideration. If the examiner try to test the project with larg size database file, say 150 Mb, and the JVM heap running at 128 mb (defualt), he will get an exception.
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Hanna,
    I am also debating between caching the data or using RandomAccessFile stream. My initial thought on caching the record from the database is its size. In real life project, you never know the size of you database, and never know how big the database can grow to. If the database file grow too big, the jvm will run out of memory.
    It is the same case dealing with RDB. One can't bring the whole database to the jvm, that is why we have to access the database every time we need to perform a query. I know the project scope is much smaller than real life project, but there are 100 points in general consideration. If the examiner try to test the project with larg size database file, say 150 Mb, and the JVM heap running at 128 mb (defualt), he will get an exception.

    There is a very simple way to handle that issue. It's like a magic, but it works very well: simply wrap your cached records in SoftReferences.
    It is guaranteed that the JVM will reclaim any memory used by softly-reachable objects before it may throw an OutOfMemoryError.
    Regards,
    Phil.
     
    Hanna Habashy
    Ranch Hand
    Posts: 532
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Philippe:
    thanks for the tip. I have never used java.lang.ref package before. I read the api, but I don't have a clear understanding of how to apply this technique to the issue.
    If I can store reference to objects, where the object has to live?
    for example: if I am reading the db file, and ecapsulate each record in a value object, then referenced this object. The object itself has to be created and stored on the heap, hence using the memory.
    Also, consider mutiple users who are accessing different database files. No one needs to reference to the other preson database objects. Each database file will have its corosponding value objects.
    clear my understading if I am wrong.
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Hanna,
    thanks for the tip. I have never used java.lang.ref package before. I read the api, but I don't have a clear understanding of how to apply this technique to the issue.

    You're lucky enough: among the three types of references, the soft one is the easiest to understand and use.
    If I can store reference to objects, where the object has to live?
    for example: if I am reading the db file, and ecapsulate each record in a value object, then referenced this object. The object itself has to be created and stored on the heap, hence using the memory.

    The object referenced through a SoftReference lives on the heap as any Java object. The trick resides in what the garbage collector *may* do with the object in case there is no other reference to your object than "soft" ones. If it feels short of memory, it may - and *must* before throwing an OutOfMemoryError - clear your soft reference(s) and reclaim the memory used by your object (garbage-collect it).
    Practically, lets say that your cache is implemented through a HashMap mapping recNos to records (String[]):
    Your Cache class basically needs two simple methods:

    And any class using your cache will use it this way:

    Regards,
    Phil.
    [ May 04, 2004: Message edited by: Philippe Maquet ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic