1 / 34

A Simple Java Relational Database

This article provides an overview of objects, their attributes, and persistence in Java, with a proposed intermediate solution for simple and complex persistence options. Includes code review and a demo.

sarthur
Download Presentation

A Simple Java Relational Database

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Simple Java Relational Database Thomas A. Bullinger March 20, 2001 tomb@emrt.com

  2. Overview • Objects • Object Attributes • Persistence • Simple • Complex • Options in Java • Proposed Intermediate Solution • Code Review • Demo • Questions www.emrt.com

  3. Properties of Objects • What is an object? • State • Data associated with the object • Behavior • Functions to operate on the data • Identity • The distinguishing characteristic of objects www.emrt.com

  4. Object State • Can be classified in multiple ways • Stack-based (typically local, not object scope) • Heap-based (local or object scope) • Static • Object based (automatic) • Persistent www.emrt.com

  5. Stack-based • Code Example … int countThings(Vector things) { int thingCount; … } … www.emrt.com

  6. Heap-based • Code Example … Vector myCollection = new Vector(25); … www.emrt.com

  7. Instance-Based • Code Example Root=C:\Project1 DataFilename=datasetOne.dat Class MyClass { public int thingCounter = 25; … } www.emrt.com

  8. Class-Based • Code Example Class MyClass { public static int THING_COUNTER = 25; … } www.emrt.com

  9. Persistent • Requirements for persistence • Should be transparent to client code • Should be type-safe • Should support object relationships • Inheritance • Composition • Should support dynamic queries www.emrt.com

  10. Simple Persistence • java.util.Properties • A simple persistence mechanism • Based on Key/ Value pairs (one-to-one relationship) • Subclass of java.util.Hashtable • Data represented by Strings • Strings are polymorphic • Methods of interest in Properties: • Object setProperty(String key, String value) • String getProperty(String value) • void store(OutputStream out, String header) • void load(InputStrream input) • Enumeration propertyNames( ) www.emrt.com

  11. PropertyFile • A simple subclass of java.util.Properties • Methods of interest in PropertyFile • Constructors • PropertyFile(String filename) • PropertyFile(PropertyFile props) • Persistence (overloaded methods) • store(String filename, String header) • store( ) • Accessors • String getFilename( ) • (plus all methods of java.util.Properties) • Source Code: PropertyFile.java www.emrt.com

  12. PropertyFile Contents Contents of “Config.ini”: Root=C:\Project1 DataFilename=datasetOne.dat www.emrt.com

  13. PropertyFile Example import PropertyFile; … PropertyFile myConfig = new PropertyFile(“Config.ini”); String rootDirectory = myConfig.getProperty(“Root”); String dataFilename = myConfig.getProperty(“DataFilename”); PropertyFile dataFile = new PropertyFile(rootDirectory + “\” + dataFilename); … www.emrt.com

  14. Strings are Polymorphic int count = Integer.parseInt(myConfig.getProperty(“TotalFiles”); double grade = Double.toDouble(myConfig.getProperty(“AverageGrade”); String myName = myConfig.getProperty(“LastName”) + “, “ + myConfig.getProperty(“FirstName”) + myConfig.getProperty(“MiddleInitial”); www.emrt.com

  15. One-to-Many Relationships • Properties map one key to one value • What if there are multiple values? • StringList is a collection of comma-separated strings • Implements Enumeration interface to walk the list • Methods of interest: • StringList(String list) // constructor • int length( ) • String toString( ) • boolean hasMoreElements( ) • Object nextElement( ) • Source Code: StringList.java www.emrt.com

  16. StringList Usage String myNames =myConfig.getProperty(“NameList”); StringList myNameList = new StringList(myNames); while(myNameList.hasMoreElements() == true) { System.out.println(“Name: “ + myNameList.nextElement()); } www.emrt.com

  17. What about Objects? • Serializable as a solution • Used for RMI, can be leveraged for persistence • Requires assorted support methods • writeObject • readObject • writeReplace • readResolve • Binary data representation • Does not support dynamic queries www.emrt.com

  18. Complex Persistence • JDBC • Layered on a relational database • Specifies an interface only • Requires a database server • Oracle • Microsoft SQL Server • Microsoft Access • Others • Requires administration • Requires Database architecture • Impedance Mismatch (object / relational) • Suitable for large applications www.emrt.com

  19. Intermediate Solution • Map an object onto a PropertyFile • PropertyFile contains data elements of object • Persistent Class provides methods and conversions • Persistence mechanism hooked into constructor / destructor • One subdirectory for each class • Subdirectory contains a PropertyFile for each instance • PropertyFile contains each attribute and value • Persistent base class provides persistence mechanism and attribute accessors www.emrt.com

  20. Persistence Participants • PropertyFile • DataStore • Storable • PropertyFileCache • Application-Specific classes www.emrt.com

  21. DataStore • Singleton factory for persistent objects • Based on directories of PropertyFiles • Provides support for dynamic queries • Provides support to serialize / deserialize attributes • Provides support for object deletion • Can be pointed to different sets of objects • Source Code: DataStore.java www.emrt.com

  22. Storable • The base class for storable objects • Provides interface to DataStore for serialization / deserialization • Provides version capability • Provides generic data accessors (protected) • getValue • setValue www.emrt.com

  23. Storable (continued) • Provides other accessors • isPersistent • isTransient • isDirty • getVersion • getType • isNew • Source Code: Storable.java www.emrt.com

  24. PropertyFileCache • A dynamic collection of PropertyFiles • Keeps a file in memory for the next access • A performance optimization for queries • Can get you in serious trouble! www.emrt.com

  25. MapFile • A mapping of instance name to Directory & PropertyFile • Example: mapfile.dat www.emrt.com

  26. Storable Subclasses • Inherits from Storable • Provides hard-coded type & version • Provides accessors and conversions for each data element (if required) • Provides class-specific behavior • Example: Video.java, MovieStudio.java www.emrt.com

  27. Relationships • Implemented via instance name references • StringList provides one-to-many relationships • Many-to-many implemented via intermediate classes (like any other RDB) • Implementing class required to perform lookups as needed • Example: Video.java www.emrt.com

  28. Queries • Client provides a Properties instance for query • Query behavior examines each potential instance and matches candidates to contents of query set • Matches are returned as a Vector of “hits” • Client must traverse “hit list” www.emrt.com

  29. Query Example • Source Code: RJugDemo.java www.emrt.com

  30. Pros • Quick & Easy • Data represented as text in ASCII files • Support for simple queries • Limited versioning • Easy extensibility • Support for OO constructs • Composition (via explicit relational lookup) • Inheritance (via traditional mechanism) www.emrt.com

  31. Cons • Slow (especially for complex queries) • Doesn’t scale well • Requires explicit accessors • Or client knowledge of type mapping • Requires mapping to/from Strings • Practical versioning may be problematic • Automatic updates? www.emrt.com

  32. In Action! • Source Code: RJugDemo.java • Execution: • Database Test Tool: www.emrt.com

  33. Other Issues: • Programmatic Creation • Transient Objects • Caching www.emrt.com

  34. Questions? www.emrt.com

More Related