• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this good enough?  RSS feed

 
Maki Jav
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have written a simple class for reading/writing to a Flat File Database ie a file to store objects in a
collection. Whenever I want to store my objects, I may use a vector, an arraylist etc and store in it myObjects. At retrieving time, I get a collection, which I convert back to vector or an arraylist and get myObject back again through casting the objects stored in that Collection.

Question is... Is this class good enough? Do I need to add more functionality? Tell me please!



Thanx in Advance

Maki Jav
[ January 25, 2005: Message edited by: Maki Jav ]
 
Joe Ess
Bartender
Posts: 9436
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good enough for what is the question. What are your requirements?
Why does the comment for public void save(Object val) indicate that it "loads a stored Collection"?
Where is the method to load an object?
What happens if I invoke save(Object) several times (supposing there is a method to load Objects)? How do I get all my Object's back?
 
Maki Jav
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

1) The load()method first loads a Collection from a file (if it has one ) and adds object to that and then saves that Collection back to file; be it a vector, an ArrayList or whatever...

2) There is no method to load a single object as this is not required. It is a collection/array of objects that I intend to get.

3) Invoking save(Object) several times will just add objects to the Collection Object contained in a specified file...

4) How do I get all my Object's back? Well! to this I can say that you will get a Collection of
all your objects back. You may use iteration then.


Thank you for your critical analysis. I need to make it a good class so that others developers here find it useful too.

Maki Jav
[ January 25, 2005: Message edited by: Maki Jav ]
 
Maki Jav
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Well! I think that there is no one to give me any idea for improvement.
My class is almost decided upon then. So I should use it the way it is?!

Maki Jav
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd throw Joe's question back at you: "Good enough for what?" Would I use this class to build an ecommerce website expecting 10,000 visitors a day? Probably not. Would I use it in a simple command-line tool that needed exactly this behavior? Sure.

You can always expand it later and add more features. You might consider breaking out an interface so you can swap out the implementation in the future, but again it depends on how it will be used.

If you have specific questions or problems in using it, you'll probably get more feedback. People are volunteering here to help others solve problems. While many are happy to provide design advice, I suspect it's a lower priority. I've been extremely busy myself the past couple weeks, so I know I've been having to be more picky about the topics to which I reply.
 
Maki Jav
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It is intended to provide for small to medium size data storage. It can be used in, say, a desktop application for a small firm which needs to save its one or two types of records per day.
Actually, I am writing a utility for ease of programming, which will contain database access and connection pooling, jtable values handling, Numeric JTextFields (for use with jdk1.2 -1.3) etc. I wanted to have this class in that utility too.

I have persisted a vector with 100,000 objects , yet the file size was 4kb. So I thought that such a startegy would solve atleast some of my problems...
Like, I would not have to use MS Access for my small projects, etc etc

Thank you,

Maki Jav
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maki Jav:
I have persisted a vector with 100,000 objects , yet the file size was 4kb.
You might want to double-check the file and then your code, for that doesn't make sense.

4kb = 4,096 bytes for 100,000 objects > 24 objects per byte

Unless you've found a way to represent 3 objects in a single bit (0 or 1), there must be a problem. If not, apply for a patent right now -- you're gonna be rich!
 
Maki Jav
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Read this post

You may turn jsp into a java class and use it.

Check for yourself...

Thank you,


Maki Jav
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you happen to notice in that post that in fact only 4 objects are being stored in the file: one ArrayList and three Badarians? Okay, the ArrayList holds an Object[] which is a fifth object and each Badarian contains a String (the other private instance Objects are null), but that still only pushes it to 8 objects in total.

Also, your comment states "401 bytes file generated." I ran this same code (outside of a servlet) on my machine (WinXP) and it generated a file of 500,401 bytes -- slightly less than 500kb, off by a factor of 1,000.

The reason is that Serialization will store each instance only once, even if it is referenced eight billion times. If you had 100,000 unique instances, the file size would be much bigger. Looking at the generated file shows that it was able to store each duplicated reference as 5 bytes. This makes since given 100,000 references comes out to 500,401 bytes, leaving 401 bytes for the 3 Badarians and the ArrayList.

Another note, if you are going to create an ArrayList of a known size, you can presize it ahead of time. While the difference was slight on my machine (Athlon XP 2800+, ran in about 550ms vs. 650ms, too short for a reliable benchmark), it may be more pronounced if you're seeing 8 second runtimes. Just pass in a parameter to the constructor specifying how many slots it should have to start: new ArrayList(100002). Otherwise, every time it fills up it needs to reallocate and copy the internal array.

Try having your code create 100,000 random Badarian instances to get a better feel for performance and disk space needed. Also, don't include creating the random instances in the time since you won't normally be doing that.
[ February 14, 2005: Message edited by: David Harkness ]
 
Maki Jav
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

That was informative. Gee! I am saved from going for a patent!

Jokes apart, I really appreciate your reply...

Thanks,

Maki Jav
[ February 18, 2005: Message edited by: Maki Jav ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!