ch8 8
ch8 8
! Need for Complex Data Types ! Traditional database applications in data processing had
conceptually simple data types
! The Object-Oriented Data Model
! Relatively few data types, first normal form holds
! Object-Oriented Languages ! Complex data types have grown more important in recent years
! Persistent Programming Languages ! E.g. Addresses can be viewed as a
! Persistent C++ Systems " Single string, or
" Separate attributes for each part, or
" Composite attributes (which are not in first normal form)
! E.g. it is often convenient to store multivalued attributes as-is,
without creating a separate relation to store the values in first
normal form
! Applications
! computer-aided design, computer-aided software engineering
! multimedia and image databases, and document/hypertext
databases.
Database System Concepts 8.1 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.2 ©Silberschatz, Korth and Sudarshan
Object-
Object-Oriented Data Model Object Structure
Database System Concepts 8.3 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.4 ©Silberschatz, Korth and Sudarshan
! Methods are programs written in general-purpose language ! Similar objects are grouped into a class; each such object is
with the following features called an instance of its class
! only variables in the object itself may be referenced directly ! All objects in a class have the same
! data in other objects are referenced only by sending messages. ! Variables, with the same types
! Methods can be read-only or update methods ! message interface
! Read-only methods do not change the value of the object ! methods
! Strictly speaking, every attribute of an entity must be The may differ in the values assigned to variables
represented by a variable and two methods, one to read and ! Example: Group objects for people into a person class
the other to update the attribute
! Classes are analogous to entity sets in the E-R model
! e.g., the attribute address is represented by a variable address
and two messages get-address and set-address.
! For convenience, many object-oriented data models permit direct
access to variables of other objects.
Database System Concepts 8.5 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.6 ©Silberschatz, Korth and Sudarshan
! Methods are defined separately ! variables/messages specific to employees associated with class
employee; similarly for customer
! E.g. int employment-length() { return today() – start-date;}
int set-address(string new-address) { address = new-address;}
Database System Concepts 8.7 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.8 ©Silberschatz, Korth and Sudarshan
Inheritance (Cont.) Class Hierarchy Definition
class person{
string name;
! Place classes into a specialization/IS-A hierarchy string address:
! variables/messages belonging to class person are };
inherited by class employee as well as customer class customer isa person {
! Result is a class hierarchy int credit-rating;
};
class employee isa person {
date start-date;
int salary;
};
class officer isa employee {
int office-number,
int expense-account-number,
Note analogy with ISA Hierarchy in the E-R model };
..
.
Database System Concepts 8.9 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.10 ©Silberschatz, Korth and Sudarshan
Database System Concepts 8.11 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.12 ©Silberschatz, Korth and Sudarshan
Database System Concepts 8.13 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.14 ©Silberschatz, Korth and Sudarshan
! An object retains its identity even if some or all of the values ! Object identifiers used to uniquely identify objects
of variables or definitions of methods change over time. ! Object identifiers are unique:
! Object identity is a stronger notion of identity than in " no two objects have the same identifier
programming languages or data models not based on object " each object has only one object identifier
orientation. ! E.g., the spouse field of a person object may be an identifier of
! Value – data value; e.g. primary key value used in relational another person object.
systems. ! can be stored as a field of an object, to refer to another object.
! Name – supplied by user; used for variables in procedures. ! Can be
! Built-in – identity built into data model or programming " system generated (created by database) or
language.
" external (such as social-security number)
" no user-supplied identifier is required.
! System generated identifiers:
" Is the form of identity used in object-oriented systems.
" Are easier to use, but cannot be used across database systems
" May be redundant if unique identifier already exists
Database System Concepts 8.15 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.16 ©Silberschatz, Korth and Sudarshan
Object Containment Object-
Object-Oriented Languages
Database System Concepts 8.17 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.18 ©Silberschatz, Korth and Sudarshan
! Persistent Programming languages allow objects to be created ! Drawbacks of persistent programming languages
and stored in a database, and used directly from a programming
language ! Due to power of most programming languages, it is easy to make
programming errors that damage the database.
! allow data to be manipulated directly from the programming language
! Complexity of languages makes automatic high-level optimization
" No need to go through SQL.
more difficult.
! No need for explicit format (type) changes
! Do not support declarative querying as well as relational databases
" format changes are carried out transparently by system
" Without a persistent programming language, format changes
becomes a burden on the programmer
– More code to be written
– More chance of bugs
! allow objects to be manipulated in-memory
" no need to explicitly load from or store to the database
– Saved code, and saved overhead of loading/storing large
amounts of data
Database System Concepts 8.19 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.20 ©Silberschatz, Korth and Sudarshan
! Approaches to make transient objects persistent include ! A persistent object is assigned a persistent object identifier.
establishing ! Degrees of permanence of identity:
! Persistence by Class – declare all objects of a class to be ! Intraprocedure – identity persists only during the executions of a
persistent; simple but inflexible. single procedure
! Persistence by Creation – extend the syntax for creating objects to ! Intraprogram – identity persists only during execution of a single
specify that that an object is persistent. program or query.
! Persistence by Marking – an object that is to persist beyond ! Interprogram – identity persists from one program execution to
program execution is marked as persistent before program another, but may change if the storage organization is changed
termination.
! Persistent – identity persists throughout program executions and
! Persistence by Reachability - declare (root) persistent objects; structural reorganizations of data; required for object-oriented
objects are persistent if they are referred to (directly or indirectly) systems.
from a root object.
" Easier for programmer, but more overhead for database system
" Similar to garbage collection used e.g. in Java, which
also performs reachability tests
Database System Concepts 8.21 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.22 ©Silberschatz, Korth and Sudarshan
Object Identity and Pointers (Cont.) Storage and Access of Persistent Objects
How to find objects in the database:
! In O-O languages such as C++, an object identifier is
! Name objects (as you would name files)
actually an in-memory pointer.
! Cannot scale to large number of objects.
! Persistent pointer – persists beyond program execution
! Typically given only to class extents and other collections of
! can be thought of as a pointer into the database objects, but not objects.
" E.g. specify file identifier and offset into the file
! Expose object identifiers or persistent pointers to the objects
! Problems due to database reorganization have to be dealt
! Can be stored externally.
with by keeping forwarding pointers
! All objects have object identifiers.
! Store collections of objects, and allow programs to iterate
over the collections to find required objects
! Model collections of objects as collection types
! Class extent - the collection of all objects belonging to the
class; usually maintained for all classes that can have persistent
objects.
Database System Concepts 8.23 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.24 ©Silberschatz, Korth and Sudarshan
Persistent C++ Systems ODMG C++ Object Definition Language
! C++ language allows support for persistence to be added without ! The Object Database Management Group is an industry
changing the language consortium aimed at standardizing object-oriented databases
! Declare a class called Persistent_Object with attributes and methods ! in particular persistent programming languages
to support persistence
! Includes standards for C++, Smalltalk and Java
! Overloading – ability to redefine standard function names and
operators (i.e., +, –, the pointer deference operator –>) when applied ! ODMG-93
to new types ! ODMG-2.0 and 3.0 (which is 2.0 plus extensions to Java)
! Template classes help to build a type-safe type system supporting
" Our description based on ODMG-2.0
collections and persistent types.
! Providing persistence without extending the C++ language is ! ODMG C++ standard avoids changes to the C++ language
! relatively easy to implement ! provides functionality via template classes and class libraries
! but more difficult to use
! Persistent C++ systems that add features to the C++ language
have been built, as also systems that avoid changing the
language
Database System Concepts 8.25 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.26 ©Silberschatz, Korth and Sudarshan
Database System Concepts 8.27 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.28 ©Silberschatz, Korth and Sudarshan
Database System Concepts 8.29 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.30 ©Silberschatz, Korth and Sudarshan
Database System Concepts 8.31 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.32 ©Silberschatz, Korth and Sudarshan
ODMG C++OML: Database and Object ODMG C++ OML: Example
Functions
! Class d_Database provides methods to
int create_account_owner(String name, String Address){
! open a database: open(databasename) Database bank_db.obj;
Database * bank_db= & bank_db.obj;
! give names to objects: set_object_name(object, name)
bank_db =>open(“Bank-DB”);
! look up objects by name: lookup_object(name) d.Transaction Trans;
! rename objects: rename_object(oldname, newname) Trans.begin();
! close a database (close());
d_Ref<Account> account = new(bank_db) Account;
! Class d_Object is inherited by all persistent classes.
d_Ref<Customer> cust = new(bank_db) Customer;
! provides methods to allocate and delete objects cust->name - name;
! method mark_modified() must be called before an object is cust->address = address;
updated. cust->accounts.insert_element(account);
" Is automatically called when object is created ... Code to initialize other fields
Trans.commit();
}
Database System Concepts 8.33 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.34 ©Silberschatz, Korth and Sudarshan
ODMG C++ OML: Example (Cont.) ODMG C++ OML: Example of Iterators
! Collections (sets, lists etc.) also provide create_iterator() method. while{iter.next (p))
print_cust (p); // Function assumed to be defined elsewhere
Trans.commit();
}
Database System Concepts 8.35 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.36 ©Silberschatz, Korth and Sudarshan
Database System Concepts 8.37 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.38 ©Silberschatz, Korth and Sudarshan
! ODMG-3.0 defines extensions to Java for persistence ! Transaction must start accessing database from one of the root
! Java does not support templates, so language extensions are object (looked up by name)
required ! finds other objects by following pointers from the root objects
! Model for persistence: persistence by reachability ! Objects referred to from a fetched object are allocated space in
! Matches Java’s garbage collection model memory, but not necessarily fetched
! Garbage collection needed on the database also ! Fetching can be done lazily
! Only one pointer type for transient and persistent pointers ! An object with space allocated but not yet fetched is called a hollow
object
! Class is made persistence capable by running a post-processor
on object code generated by the Java compiler ! When a hollow object is accessed, its data is fetched from disk.
! Contrast with pre-processor used in C++
! Post-processor adds mark_modified() automatically
! Defines collection types DSet, DBag, DList, etc.
! Uses Java iterators, no need for new iterator class
Database System Concepts 8.39 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.40 ©Silberschatz, Korth and Sudarshan
Specialization Hierarchy for the Bank Example
End of Chapter
Class Hierarchy Corresponding to Figure 8.2 Class DAG for the Bank Example
Database System Concepts 8.43 ©Silberschatz, Korth and Sudarshan Database System Concepts 8.44 ©Silberschatz, Korth and Sudarshan