Download presentation
Presentation is loading. Please wait.
Published byTyrone Fleming Modified over 8 years ago
1
A Simple Java Relational Database Thomas A. Bullinger March 20, 2001 tomb@emrt.com
2
www.emrt.com Overview Objects Object Attributes Persistence – Simple – Complex – Options in Java – Proposed Intermediate Solution Code Review Demo Questions
3
www.emrt.com 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
4
www.emrt.com 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
5
www.emrt.com Stack-based Code Example … int countThings(Vector things) { int thingCount; … } … int countThings(Vector things) { int thingCount; … } …
6
www.emrt.com Heap-based Code Example … Vector myCollection = new Vector(25); … Vector myCollection = new Vector(25); …
7
www.emrt.com Instance-Based Code Example Root=C:\Project1 DataFilename=datasetOne.dat Root=C:\Project1 DataFilename=datasetOne.dat Class MyClass { public int thingCounter = 25; … } Class MyClass { public int thingCounter = 25; … }
8
www.emrt.com Class-Based Code Example Class MyClass { public static int THING_COUNTER = 25; … } Class MyClass { public static int THING_COUNTER = 25; … }
9
www.emrt.com Persistent Requirements for persistence – Should be transparent to client code – Should be type-safe – Should support object relationships Inheritance Composition – Should support dynamic queries
10
www.emrt.com 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( )
11
www.emrt.com 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
12
www.emrt.com PropertyFile Contents Contents of “Config.ini”: Root=C:\Project1 DataFilename=datasetOne.dat Root=C:\Project1 DataFilename=datasetOne.dat
13
www.emrt.com 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); … import PropertyFile; … PropertyFile myConfig = new PropertyFile(“Config.ini”); String rootDirectory = myConfig.getProperty(“Root”); String dataFilename = myConfig.getProperty(“DataFilename”); PropertyFile dataFile = new PropertyFile(rootDirectory + “\” + dataFilename); …
14
www.emrt.com 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”); 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”);
15
www.emrt.com 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
16
www.emrt.com StringList Usage String myNames =myConfig.getProperty(“NameList”); StringList myNameList = new StringList(myNames); while(myNameList.hasMoreElements() == true) { System.out.println(“Name: “ + myNameList.nextElement()); } String myNames =myConfig.getProperty(“NameList”); StringList myNameList = new StringList(myNames); while(myNameList.hasMoreElements() == true) { System.out.println(“Name: “ + myNameList.nextElement()); }
17
www.emrt.com 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
18
www.emrt.com 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
19
www.emrt.com 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
20
www.emrt.com Persistence Participants PropertyFile DataStore Storable PropertyFileCache Application-Specific classes
21
www.emrt.com 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
22
www.emrt.com Storable The base class for storable objects Provides interface to DataStore for serialization / deserialization Provides version capability Provides generic data accessors (protected) – getValue – setValue
23
www.emrt.com Storable (continued) Provides other accessors – isPersistent – isTransient – isDirty – getVersion – getType – isNew Source Code: Storable.java
24
www.emrt.com 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!
25
www.emrt.com MapFile A mapping of instance name to Directory & PropertyFile Example: mapfile.dat
26
www.emrt.com 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
27
www.emrt.com 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
28
www.emrt.com 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”
29
www.emrt.com Query Example Source Code: RJugDemo.java
30
www.emrt.com 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)
31
www.emrt.com 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?
32
www.emrt.com In Action! Source Code: RJugDemo.java Execution: Database Test Tool:
33
www.emrt.com Other Issues: Programmatic Creation Transient Objects Caching
34
www.emrt.com Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.