OODB(gozde-ozahi)
Download
Report
Transcript OODB(gozde-ozahi)
OBJECT DATABASES and
An Advent Open Source :
DB4O
Gözde Özahi
CEng553 / Fall 2008
Introduction to ODBMS
Short History
İn existence now for nearly 2 decades
started with accomodating OO languages like Smalltalk,
C++ and Java
with commercial products shipping the mid 90’s
today used in large scale applications including
telecommunication, banking, manufacturing, insurance,
shipping.
for applications having complex data
good at storing complex data
Introduction to ODBMS
Important milestones of history
Early 1980s – Orion Research Project at MCC
Late 1980s – First wave of commercial products
1991 – ODMG (with 5 major OODMS vendors)
1995 – OODMS Manifesto (Malcolm Atkinson)
2001 – Final ODMG 3.0 standards released
2004 – Advent of Open Source (db4o released as
free)
What is ODBMS?
Referred as object-oriented DBMS
Modeling and creation of data as objects
Support for classes of objects
Inheritance of class properities and methods by
subclasses
ODBMS must satisfy two criteria (The
OODBMS Manifesto)
should be a DBMS
should be an OO system
What is ODBMS?
no standard for what consituted ODBMS
no standard query language (as SQL in
RDBMS)
originally thought to replace RDBMS (for
better fit with OO languages).
high switching cost directed to ORDBMS
now established as a complement, not a
replacement to RDBMS
ODBMS vs. RDBMS
An object database (ODBMS) stores
objects directly
Reational Model Weaknesses
Data Model
A single bulk data type (relation)
No direct support
Hierarchies (part-of or is-a).
Advanced data types (spatial, multimedia...)
Programming
impedance mismatches due to query language
emphasis
Challenging to make existing application data
persist.
Object Databases
Motivation:to overcome RDB Weaknesses
Richer data models
Closer integration with programming languages
Kinds of ODBs
Object relational (Oracle, DB2, PostgreSQL).
Semantic data model(Jasmine).
Programming language centred (Objectivity,
FastObjects, Versant, ObjectStore).
Object Database Standards
SQL-99:
Object data management group (ODMG):
Mainstream SQL standard.
Oracle, DB2, Informix.
Outgoing mainstream object database standard.
Objectivity, FastObjects, Versant.
Java data objects (JDO):
Java-specific object-based database access.
Poet, Versant, ObjectStore, SolarMetric...
Object Query Language (OQL)
Declarative query language
Not computationally complete
Syntax based on SQL (select, from, where)
Additional flexibility (queries with user defined
operators and types)
Example of OQL query
The following is a sample query
“what are the names of the black colored
product?”
Select distinct p.name
From products p
Where p.color = “black”
Valid in both SQL and OQL, but results are
different.
Result of the query (SQL)
Original table
Product no
Name
Color
P1
Ford Mustang
Black
P2
Toyota Celica
Green
P3
Mercedes SLK
Black
Result
Name
Ford Mustang
Mercedes SLK
The statement queries a relational
database.
=> Returns a table with rows.
Result of the query (OQL)
Original table
Product no
Name
Color
P1
Ford Mustang
Black
P2
Toyota Celica
Green
P3
Mercedes SLK
Black
Result
String
String
Ford Mustang
Mercedes SLK
- The statement
queries an objectoriented database
=> Returns a
collection of objects.
Comparison
Queries look very similar in SQL and OQL,
sometimes they are the same
In fact, the results they give are very different
Query returns:
OQL
SQL
Object
Tuple
Collection of objects Table
ODBMS for RDBMS Users
RDBMS vendors (IBM, Informix, Oracle..
Adding ORDBMS functionality)
OODM architecture and user expectations
get into mix
While RDB architectures are very similar,
OODB architectures vary considerably.
Consider your application’s characteristics
and find which OODB architecture is best
suited
Structured Data Types
SQL:1999 allow uses to define new data
types, in additions to built-in data types (e.g.
integers)
SQL:1999 also introduced two type
constructors
Types defined by using type constructors are
structured data types
ROW
base ARRAY
Operations On Structured Data
Creating a “row type”
Example:
create row type AddressType(
street
char(50),
city
char(20));
create row type StarType(
name
char(30),
address
AddressType);
Operations On Structured Data
Creating “Table”
create table Address of type AddressType;
create table MovieStar of type StarType;
Instances of Row types are tuples in tables
Operations On Structured Data(sample
query )
Find the names and street addresses of
those MovieStars who stay in the city
“Ankara”:
select MovieStar.name,
MovieStar.address.street
from MovieStar
where MovieStar.address.city = “Ankara”;
When to use an ODBMS (some example
conditions)
Embedded DBMS Applications
Complex Data (object) Relationships
Deep object Structures
Changing Data (object) Structures
Usage of agile techniques in development
team
Programming in an OO Language
Used objects in developemtn is including
collections
Advent of Open Source:
DB4O
What is db4o?
Open source object database
1,000,000 downloads,
20,000 registered community members
200 customers
smoothly integrates into an OO system
cutting down on development time by
skipping the costly object-relational mapping
stays highly reliable and performant
DB4O Concepts
Object Container
Querying
Transaction
Object Identity
Indexing
Inheritance
Object Container
simple and straightforward interface to object
persistence – ObjectContainer
ObjectContainer is your db4o database
Java: ObjectContainer container =
Db4o.openFile(filename)
open an ObjectContainer when the application starts and
close it, when the session is finished
all the basic functionality to work with persistent
objects
save a new or updated object of any class using
ObjectContainer#set(object)
Deletion is done with => Java: container.delete(object)
Object Container - Storing Objects
public static void storePilot()
{
new File(YAPFILENAME).delete();
ObjectContainer db=Db4o.openFile(YAPFILENAME);
try
{
Pilot pilot=new Pilot("Michael Schumacher",0);
db.set(pilot);
System.out.println("Stored "+pilot);
// change pilot and resave updated
pilot.addPoints(10);
db.set(pilot);
System.out.println("Stored "+pilot);
}
finally
{
db.close();
}
}
Querying
db4o supplies three querying systems
Query-By-Example (QBE),
Native Queries (NQ), and
the SODA Query API.
Querying - QBE
you provide db4o with a template object.
db4o will return all of the objects which match
all non-default field values
public static void retrievePilotByName(ObjectContainer db)
{
Pilot proto=new Pilot("Michael Schumacher",0);
ObjectSet result=db.get(proto);
listResult(result);
}
Querying - QBE
QBE has some obvious limitations:
db4o must reflect all members of your example object.
You cannot perform advanced query expressions. (AND,
OR, NOT, etc.)
You cannot constrain on values like 0 (integers), "" (empty
strings), or nulls (reference types) because they would be
interpreted as unconstrained.
You need to be able to create objects without initialized
fields. That means you can not initialize fields where they
are declared. You can not enforce contracts that objects of
a class are only allowed in a well-defined initialized state.
You need a constructor to create objects without initialized
fields.
Querying - NQ
main db4o query interface
recommended way to query databases from
your application
simply use the semantics of your programming
language
perfectly standardized
safe choice for the future
your provide the ability to run one or more
lines of code against all instances of a class
Querying - NQ
db4o will attempt to optimize native query
expressions and run them against indexes
public static void primitiveQuery(ObjectContainer db)
{
List <Pilot> pilots = db.query(new Predicate<Pilot>()
{
public boolean match(Pilot pilot)
{
return pilot.getPoints() == 100;
}
});
}
Querying – SODA Query API
db4o's low level querying API
allowing direct access to nodes of query
graphs
SODA uses strings to identify fields
Not perfectly typesafe
compile-time not checked
quite verbose to write
For most applications Native Queries will be
the better querying interface
Querying – SODA Query API
how our familiar QBE queries are expressed with SODA
public static void retrieveAllPilots()
{
ObjectContainer db=Db4o.openFile(YAPFILENAME);
try
{
Query query=db.query();
query.constrain(Pilot.class);
ObjectSet result=query.execute();
listResult(result);
}
finally
{
db.close();
}
}
Transaction
All work within db4o ObjectContainer is transactional
implicitly started when you open a container
the current transaction is implicitly committed when
you close it again.
ACID transaction model
Data transaction journaling
zero data loss in case of system failure
automatic data recovery after system failure
db4o core is thread-safe for simultaneous
operations
Transaction – Commit
choose to make a commit explicit or you may
leave it for the #close() call
public static void storeCarCommit(ObjectContainer db)
{
Pilot pilot=new Pilot("Rubens Barrichello",99);
Car car=new Car("BMW");
car.setPilot(pilot);
db.set(car);
db.commit();
}
Transaction – Rollback
do not want to save changes to the database, you
can call rollback
resetting the state of our database to the last
commit point
public static void storeCarRollback(ObjectContainer db)
{
Pilot pilot=new Pilot("Michael Schumacher",100);
Car car=new Car("Ferrari");
car.setPilot(pilot);
db.set(car);
db.rollback();
}
Object Identity
Db4o keeps references to all persistent
objects whether they were retrieved, created
or activated in this session
The main role of the reference system
to provide access to the required data with the
best speed
lowest memory consumption
Performance and usability of the reference
system depend much on how the system
manages objects identities
Indexing
Allows to index fields
To request an index to be created by API method called
in configuration file
// assuming
class Foo
{
String bar;
}
Db4o.configure().objectClass(Foo.class).objectField("bar").indexed(true);
Once created will remain in database
Inheritance
Subclasses and Inheritance with an example
public class SensorReadout
{
private Date time;
private Car car;
private String description;
/*
* Suppose constructors and methods are here
*/
}
public class TemratureSensorReadout extends SensorReadout
{
....
}
public class PressureSensorReadout extends SensorReadout
{
....
}
Inheritance
Suppose sensors are added to the database (two times)
history.add(new TemperatureSensorReadout(new Date(), this, "oil",
pollOilTemperature()));
history.add(new TemperatureSensorReadout(new Date(), this, "water",
pollWaterTemperature()));
history.add(new PressureSensorReadout(new Date(), this, "oil",
pollOilPressure()));
retrieveTemperatureReadoutsQBE
SensorReadout proto= new
TemperatureSensorReadout(null,null,null,0.0);
ObjectSet result=db.get(proto);
listResult(result);
Inheritance
Output:
4
BMW[Rubens Barrichello/99]/6 : Tue Jan 23 23:32:01 CET 2007 : oil
temp : 0.0
BMW[Rubens Barrichello/99]/6 : Tue Jan 23 23:32:01 CET 2007 : water
temp : 0.2
BMW[Rubens Barrichello/99]/6 : Tue Jan 23 23:32:01 CET 2007 : oil
temp : 0.30000000000000004
BMW[Rubens Barrichello/99]/6 : Tue Jan 23 23:32:01 CET 2007 : water
temp : 0.8
References
Malcolm P. Atkinson, François Bancilhon,
David J. DeWitt, Klaus R. Dittrich, David Maier,
Stanley B. Zdonik: The Object-Oriented
Database System Manifesto.
Moira Norrie, Object Oriented Databases Complete and up to date lecture series, ETH
Zürich
www.db4o.com
db4o-6.0-tutorial.pdf
Db4o Reference Guide