Object Databases - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Object Databases

Description:

Need to save state of an object (i.e. the values of its fields) for future use ... Old-Style Object DB (ObjectStore, FastObjects) Strict adherence to principles ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 25
Provided by: simonm3
Category:

less

Transcript and Presenter's Notes

Title: Object Databases


1
Object Databases
  • CC292
  • Simon M. Lucas

2
Overview
  • Object Graphs
  • Persistence Mechanisms
  • Object DBs brief history
  • Lightweight Object DB db4o
  • Working with db4o
  • Storage
  • Querying (retrieval)
  • Deletion
  • Transactions will be covered later

3
Object Graph ExampleNode Class
  • class Node
  • Node x

4
Object Graph (of Nodes)
  • Node a new Node()
  • Node b new Node()
  • Node c new Node()
  • Node d new Node()
  • a.x b
  • b.x d
  • d.x a
  • c.x d

5
Exercises
  • Sketch graph of objects from previous slide
  • Indicate which nodes are reachable from
  • a
  • b
  • c
  • d
  • (Also indicate the shortest path distance)

6
Persistence Mechanisms
  • Need to save state of an object (i.e. the values
    of its fields) for future use
  • Within web application programming
  • Object state is lost when the web application
    server (Tomcat) is restarted
  • Therefore need some persistence mechanism
  • Some approaches
  • File System (either manually written code or WOX)
  • Relational Database (via JDBC)
  • Object Database
  • Native XML Database

7
File System
  • Simple to begin with for simple objects
  • Can be very tedious
  • Need to synchronise changes to read / write code
    and object fields (not version resilient)
  • Becomes hard to manage
  • Hard to query efficiently
  • Still has its uses (e.g. log files)
  • Use of auto XML writers such as WOX solves some
    of these problems (which ones?)

8
Relational Database
  • Large installed base, well understood
  • Not a good fit for objects
  • Some object relational mapping needs to be done
    (tools such as Hibernate will do some of this)
  • Often leads to code with string queries in
  • Select from student where nameSimon
  • But what if there is no matching DB field
  • Errors not picked up at compile time!
  • Bad news!
  • Dreaded Web App Error SQL ERROR

9
Object Databases
  • Potentially ideal solution
  • Store objects directly in Object DB
  • No special mapping process
  • Save large object graphs (see below) with single
    line of code
  • In practice, some interesting issues
  • Referential integrity
  • Transactions
  • Starting to be used commercially
  • Will ObjectDBs one day overtake Relational DBs?

10
Old-Style Object DB(ObjectStore, FastObjects)
  • Strict adherence to principles
  • Enforced referential integrity
  • An object could not be deleted while other
    objects still referred to it
  • Enforced transactions
  • Objects could only be accessed within a
    transaction
  • This effect would be cascaded (so that objects
    referenced by root object would be inaccessible
    while root was opened for access)
  • Hard to work with Java (classes had to be
    modified)

11
Lightweight Object DBdb4o http//www.db4o.com
  • Abandons all above principles!
  • Programmer is given more responsibility
  • Con possible to get things badly wrong
  • Pro DB is simple to use
  • Pro DB is MUCH easier to implement
  • An interesting idea lets see how it works

12
Main ideas in db4o
  • ObjectContainer db
  • Used to save OR update Object o
  • db.set(o)
  • Retrieve all objects of a Given class c
  • List objects db.get(c)
  • List objects db.get(Product.class)
  • Query by Example(QBE)
  • List objects db.get(new MProduct(Sony))
  • Run Queries given Predicate p
  • List objects db.query(p)
  • Delete an Object o
  • db.delete(o)

13
Object Graphs
  • db.set(o) will save all the objects reachable
    from o.
  • But if one of the reachable objects changes,
    then calling db.set(o) again will not save the
    change!
  • The algorithm to save an object graph is
  • For each reachable object from root (o)
  • Save if not already saved

14
Product Example
  • A Product has a name, and a price
  • A Manufactured Product also has a Manufacturer
  • A manufacturer has a name and a URL
  • In this example well see how to save objects of
    these types
  • And how to write queries for them

15
The Data Classes
  • public class Product
  • String name
  • double price
  • // constructor and toString() omitted
  • public class MProduct extends Product
  • Manufacturer manufacturer
  • public class Manufacturer
  • String name
  • String url

16
Saving Some Objects
  • public static void main(String args) throws
    Exception
  • ObjectContainer db Db4o.openFile(dbName)
  • db.set(new Product("USB Splash 1 Gb",
    29.99))
  • db.set(new Product("USB Flash 256 Mb",
    19.99))
  • Manufacturer sony new
    Manufacturer("Sony", "www.sony.com")
  • MProduct mp new MProduct("USB
    Flash 256 Mb", 22.99, sony)
  • db.set(mp)
  • sony.url "http//apple.com"
  • db.set(new MProduct( "USB Flash
    Drive 8 Mb", 2.99, sony))
  • System.out.println("Set the objects")
  • db.close()

17
Running Queries
  • There are several ways to run queries
  • (Query By Example (QBE), SODA, and Predicates)
  • Here well just cover the Predicate method
  • Main concept
  • Define a Predicate that takes objects of a
    certain class as an argument
  • The Predicate should return true if the object
    should be returned, false otherwise

18
Example Predicates
  • Two examples
  • Our Manufacturer predicate always returns true
  • Hence all the Manufacturers in the DB are
    returned
  • Our Product predicate selects all products whose
    name contains a given substring

19
All Manufacturers Query(how could you simplify
this?)
  • public static void manufacturers(ObjectContainer
    db)
  • ListltManufacturergt set db.query(new
    PredicateltManufacturergt()
  • public boolean match(Manufacturer m)
  • return true
  • )
  • // now do something with the result
  • for (Manufacturer m set)
  • System.out.println(m)

20
Like Products Query
  • public static void likeProductQuery(ObjectContain
    er db, final String sub)
  • ListltProductgt set db.query(new
  • PredicateltProductgt()
  • public boolean match(Product p) //
    this tests of sub is a // sub-string of
    p.name
  • return p.name.indexOf(sub) ! -1
  • )
  • for (Product p set)
  • System.out.println(p)

21
Running The Queries
  • public static void main(String args)
  • ObjectContainer db
  • Db4o.openFile(SaveProducts.dbName)
  • // print all products with Flash in title
  • likeProductQuery(db, "Flash")
  • System.out.println("")
  • // print all manufacturers
  • manufacturers(db)
  • db.close()

22
Sample Output
  • Exercise Fill in what the output would be!

23
Retrieval Depth
  • An important concept
  • Given an object graph, compute the shortest path
    to every reachable object
  • When retrieving an Object
  • only those objects within depth distance from the
    root object are retrieved
  • All other references set to null
  • The root object in this case, is the argument
    to the predicate
  • Default depth in db4o is 5
  • Exercise underline the root object in each of
    the previous queries.

24
Summary
  • So far weve seen how easy a lightweight Object
    DB such as db4o can be to use
  • Some unresolved issues
  • Query efficiency in our example the predicate
    had to be tested against every instance of an
    appropriate class!
  • Transactions to be covered later
  • Future prospects very exciting
  • Will use db4o in lab and for the assignments
Write a Comment
User Comments (0)
About PowerShow.com