9-Object Oriented Model
9-Object Oriented Model
Dr. M. Brindha
Assistant Professor
Department of CSE
NIT, Trichy-15
Need for Complex Data Types
• Traditional
database applications in data processing had
conceptually simple data types
• Relatively few data types, first normal form holds
• Strictly
speaking, every attribute of an entity must be
represented by a variable and two methods, one to
read and the other to update the attribute
• 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.
Object Classes
• Similar objects are grouped into a class; each such
object is called an instance of its class
• All objects in a class have the same
• Variables, with the same types
• message interface
• methods
The may differ in the values assigned to variables
• Example: Group objects for people into a person class
• Classes are analogous to entity sets in the E-R model
Class Definition Example
class employee {
/*Variables */
string name;
string address;
date start-date;
int salary;
/* Messages */
int annual-salary();
string get-name();
string get-address();
int set-address(string new-address);
int employment-length();
};
• Methods to read and set the other variables are also
needed with strict encapsulation
• Methods are defined separately
• E.g. int employment-length() { return today() – start-date;}
int set-address(string new-address) { address = new-address;}
Inheritance
• E.g.,
class of bank customers is similar to class of bank
employees, although there are differences
• both share some variables and messages, e.g., name and address.
• But there are variables and messages specific to each class e.g.,
salary for employees and credit-rating for customers.
• Every employee is a person; thus employee is a
specialization of person
• Similarly, customer is a specialization of person.
• Create classes person, employee and customer
• variables/messages applicable to all persons associated with class
person.
• variables/messages specific to employees associated with class
employee; similarly for customer
Inheritance (Cont.)
• Place classes into a specialization/IS-A
hierarchy
• variables/messages belonging to class person are
inherited by class employee as well as customer
• Result is a class hierarchy
• Similarly,
insert_element() and delete_element() methods of
d_Rel_Set use type definition to find and update the inverse
link automatically
Implementing Relationships
• E.g.
extern const char _owners[ ], _accounts[ ];
class Account : public d.Object {
….
d_Rel_Set <Customer, _accounts> owners;
}
// .. Since strings can’t be used in templates …
const char _owners= “owners”;
const char _accounts= “accounts”;
ODMG C++ Object Manipulation Language
• Uses persistent versions of C++ operators such as new(db)
Trans.commit();
}
ODMG C++ OML: Example (Cont.)
• Class extents maintained automatically in the database.
• To access a class extent:
d_Extent<Customer>
customerExtent(bank_db);
• Class d_Extent provides method
d_Iterator<T> create_iterator()
to create an iterator on the class extent
• Also provides select(pred) method to return iterator on
objects that satisfy selection predicate pred.
• Iterators help step through objects in a collection or class
extent.
• Collections (sets, lists etc.) also provide create_iterator()
method.
ODMG C++ OML: Example of Iterators
int print_customers() {
Database bank_db_obj;
Database * bank_db = &bank_db_obj;
bank_db->open (“Bank-DB”);
d_Transaction Trans; Trans.begin ();
d_Extent<Customer> all_customers(bank_db);
d_Iterator<d_Ref<Customer>> iter;
iter = all_customers–>create_iterator();
d_Ref <Customer> p;
while{iter.next (p))
print_cust (p); // Function assumed to
be defined elsewhere
Trans.commit();
}
Specialization Hierarchy for the Bank Example
Class Hierarchy Corresponding to Figure 8.2
Class DAG for the Bank Example
Containment Hierarchy for Bicycle-Design Database
Multiversion Schemes
• Multiversion schemes keep old versions of data item to increase
concurrency.
•Multiversion Timestamp Ordering
•Multiversion Two-Phase Locking
• Each successful write results in the creation of a new version of the
data item written.
• Use timestamps to label versions.
• When a read(Q) operation is issued, select an appropriate version
of Q based on the timestamp of the transaction, and return the
value of the selected version.
• reads never have to wait as an appropriate version is returned
immediately.
Multiversion Timestamp
Ordering
• Each data item Q has a sequence of versions <Q1, Q2,...., Qm>. Each
version Qk contains three data fields:
•Content -- the value of version Qk.
•W-timestamp(Qk) -- timestamp of the
transaction that created (wrote) version Qk
•R-timestamp(Qk) -- largest timestamp of a
transaction that successfully read version Qk
• when a transaction Ti creates a new version Qk of Q, Qk's W-
timestamp and R-timestamp are initialized to TS(Ti).
• R-timestamp of Qk is updated whenever a transaction Tj reads Qk,
and TS(Tj) > R-timestamp(Qk).
•
Multiversion Timestamp
The multiversion timestamp scheme presented next ensures serializability.
•
Ordering
Suppose (Cont)
that transaction T issues a read(Q) or write(Q) operation. Let Q
i k
denote the version of Q whose write timestamp is the largest write
timestamp less than or equal to TS(Ti).
1. If transaction Ti issues a read(Q), then the value returned is the
content of version Qk.
2. If transaction Ti issues a write(Q), and if TS(Ti) < R-
timestamp(Qk), then transaction Ti is rolled
back. Otherwise, if TS(Ti) = W-timestamp(Qk), the contents of Qk
are overwritten, otherwise a new version of Q is created.
• Reads always succeed; a write by Ti is rejected if some other transaction Tj
that (in the serialization order defined by the timestamp values) should
read Ti's write, has already read a version created by a transaction older
than Ti.
Multiversion Two-Phase
• Differentiates between
read-only Locking
transactions and update transactions
• Update transactions acquire read and write locks, and hold all locks up
to the end of the transaction. That is, update transactions follow
rigorous two-phase locking.
•Each successful write results in the creation of a
new version of the data item written.
•each version of a data item has a single
timestamp whose value is obtained from a
counter ts-counter that is incremented during
commit processing.
• Read-only transactions are assigned a timestamp by reading the current
value of ts-counter before they start execution; they follow the
multiversion timestamp-ordering protocol for performing reads.
Multiversion Two-Phase Locking
• When an update transaction wants to read a data item,
it obtains a
(Cont.)
shared lock on it, and reads the latest version.
• When it wants to writean item, it obtains X lock on; it then creates a
new version of the item and sets this version's timestamp to .
• When update transaction Ti completes, commit processing occurs:
•Ti sets timestamp on the versions it has created to
ts-counter + 1
•Ti increments ts-counter by 1
• Read-only transactions that start after Ti increments ts-counter will see
the values updated by Ti.
• Read-only transactions that start before Ti increments the
ts-counter will see the value before the updates by Ti.
• Only serializable schedules are produced.
Thank You!!!