chapter 5 c++ binding prof. hyoung-joo kim ([email protected]) oopsla lab. dept. of computer...
TRANSCRIPT
Chapter 5
C++ Binding
Prof. Hyoung-Joo Kim ([email protected])
OOPSLA Lab.Dept. of Computer Engineering
Seoul National Univ.
SNU OOPSLA Lab 2
Contents
• Instruction• C++ ODL• C++ OML• C++ OQL• Schema Access
SNU OOPSLA Lab 3
Introduction
• C++ binding: mapping the Object Model into C++
– C++ ODL: a library of classes and functions to implement the concepts defined in the ODMG Object Model
– C++ OML: Object Manipulation Language for retrieving objects from database and modifying them
– C++ OQL: a subset of OML which supports associative retrieval
SNU OOPSLA Lab 4
Introduction(con’t)
• Mapping the ODMG Object Model into C++– Object and Literal: depending on how a C++ class is instantiated
– Structure: C++ construct struct or class embedded in a class– Implementation: two parts
• interface(public part) and implementation(protected and private members and function definitions)
– Collection classes: a collection template classes in C++– Array: d_Varray C++ class– Relationship: instances of specific template classes in a class– Extents: d_Extent<T> class– Keys: not supported by C++– Names: set_object_name and rename_object methods in C++
SNU OOPSLA Lab 5
C++ ODL
attributes
relationships
operations
C++ ODLDeclarations
ODLPreprocessor
C++ HeaderFiles
data members
member functions
ODLDeclarations
Java ODLDeclarations
SNU OOPSLA Lab 6
C++ ODL(con’t)
extern const _professors[];extern const _advisor[];
class Professor : public d_Object { public: //properties
d_UShort age;d_UShort id_number;d_String name;d_Rel_Ref<Department, _professors> dept;d_Rel_Set<Student, _advisor> advisees;
//operationsvoid grant_tenure();void assign_course(Course &);
};
const _professors[] = “professors”;const _advisor[]=“advisor”;
e.g.
SNU OOPSLA Lab 7
C++ ODL(con’t)
How do we declare attributes, relationships, and operations which comprise C++ ODL?
properties-attributes
properties-relationships
behavior-operations
class declarationby C++ ODL
d_String, d_Interval, d_Date, d_Time, d_Timestamp
d_Rel_Ref<T, const char *>,d_Rel_Set<T, const char *>,d_Rel_List<T, const char *>
SNU OOPSLA Lab 8
C++ ODL: Attribute Declarations
• Standard C++ syntax and semantics for class definitions are supported.– But, compliant implementations need not support the
following data types within persistent classes:• unions• bit fields• references(&)
• Members within classes in C++ ODL– all primitive data types(e.g., d_String, d_char, etc.), except
those noted above
– structures and class objects(e.g., atomic objects, structured literal, structured object, etc.)
SNU OOPSLA Lab 9
C++ ODL: Attribute Declarations(con’t)
• Example
class Student : public d_Object { public:
d_String name;d_Date birth_date;Phone_Number dorm_phone;University_Address address;d_List<d_String> favorite_friends;
};
struct University_Address {d_Ushort PO_box;d_String university;d_String city;...
};
SNU OOPSLA Lab 10
C++ ODL: Attribute Declarations(con’t)
• Fixed-Length Types
– Unlike the C++ built-in types, the fixed-length types have
the same range and interpretation on all environments
Type Name Range Description
d_Short 16bit signed integer
d_Long 32bit signed integer
d_UShort 16bit unsigned integer
d_Boolean d_True ord_False
defines d_True(nonzero)and d_False(Zero)
SNU OOPSLA Lab 11
C++ ODL: Attribute Declarations(con’t)
• e.g., usage of fixed-length type
class Professor : public d_Object {
public: //properties
d_UShort age; ...};
short age;
int age;
Environment 1
Environment 2im
plementation16bit
16bit
16bit
SNU OOPSLA Lab 12
C++ ODL: Attribute Declarations(con’t)
• Refer to p130 - p137 of the text book, ODMG 2.0
– d_String
– d_Interval
– d_Date
– d_Time
SNU OOPSLA Lab 13
C++ ODL: Relationship Declarations
• A traversal path declaration is an attribute declaration and must be of type– d_Rel_Ref<T, const char *> for 1 : 1 relationship– d_Rel_Set<T, const char *> for 1 : m relationship– d_Rel_List<T, const char *> for 1 : m relationship + ordering
• Second template argument should be the name of the attribute in the other class for the inverse role in relationship
• Both classes in a relationship must have a member of one of the upper types
• The referential integrity of bidirectional relationships is automatically maintained
SNU OOPSLA Lab 14
C++ ODL: Relationship Declarations(con’t)
• Example
Processors Students
advisor
advisees
1 : m
class Professor : public d_Object { public: d_Rel_Set<Student, _advisor> advisees;};
extern const char _advisor[], _advisees[];
class Student : public d_Object { public: d_Rel_Ref<Professor, _advisees> advisor;};
extern const char _advisor[]=“advisor”;extern const char _advisees[]=“advisees”;
classes definition in C++ ODL
SNU OOPSLA Lab 15
C++ ODL: Operation Declarations
• Operation declarations are syntactically identical to member function declarations
SNU OOPSLA Lab 16
C++ OML
main() {d_Database *myDB;
d_Ref<Professor> prof = new(myDB, “Professor”) Professor;prof->age = 30;...prof->age = 40;prof->delete_object();...
}
Application by C++ binding
Persistent DB
• What is C++ OML?
C++ OML
SNU OOPSLA Lab 17
C++ OML(con’t)
• A guiding principle in the design of C++ OML
– The syntax used to create, delete, reference, and invoke operations on a persistent object should be, as far as possible, not different than used for transient objects
– In fact, ODMG 2.0 standard treats persistent and transient objects slightly differently: queries and transactions
SNU OOPSLA Lab 18
C++ OML(con’t)
• For what standard does C++ OML exist?– In application program
• Object Creation
• Object Deletion
• Object Modification
• Object References
SNU OOPSLA Lab 19
C++ OML: Object Creation
• Objects are created in C++ OML using the new operator
(1) creation of transient objects(2) creation of persistent objects and placement near clustering object(3) creation of persistent objects in specified database
(1) void* operator new(size_t size);(2) void* operator new(size_t size, const d_Ref_Any& clustering,
const *typename);(3) void* operator new(size_t size, d_Database *db, const char* typename);
SNU OOPSLA Lab 20
C++ OML: Object Creation(con’t)
• Example
d_Database *myDB, *yourDB;
(1) d_Ref<Schedule> sched = new Schedule;
(2) d_Ref<Professor> prof = new(yourDB, “Professor”) Professor;
(3) d_Ref<Student> stdnt1 = new(myDB, “Student”) Student;
(4) d_Ref<Student> stdnt2 = new(stdnt1, “Student”) Student;
(5) d_Ref<Student> temp_stdnt = new(d_Database::transient_memory, “Student”) Student;
SNU OOPSLA Lab 21
C++ OML: Object Deletion
• Objects can be deleted in C++ OML using the d_Ref::delete_obejct member function
• Object is removed from memory and , if it is a persistent object, from the database
• Example
d_Ref<anyType> objRef;
... //set objRef to refer to a persistent object
objRef.delete_object();
SNU OOPSLA Lab 22
C++ OML: Object Modification
• The state of an object is modified by updating its properties or by invoking operations on it
– Persistent objects which will be modified must communicate to the runtime ODMBS process the fact of “change”
– invoking the d_Object::mark_modified member function
– As a convenience, the programmer may omit mark_modified function, but for performance improvement this function calls are required
SNU OOPSLA Lab 23
C++ OML: Object Modification(con’t)
• Example
App1 App2
Pagebuffer
Pagebuffer
DB Server
“States will change” “You will be blocked”
//Application 1d_Ref<Professor> prof = new(myDB, “Professor”) Professor;prof->age = 30;...//Application 2d_Ref<Professor> prof
= myDB.lookup_object(...);prof->mark_modified();prof->age = 40;...
SNU OOPSLA Lab 24
C++ OML: Object Reference
• Object referencing– Objects, whether persistent or not, refer to other objects via o
bject references
– Object references are instances of template class d_Ref<T>
– All accesses to persistent objects are made via methods defined on classes d_Ref, d_Object and d_Database
e.g., dereference operator ->
– An object can always refer to another object of longer lifetime, but only refer to an object of shorter lifertime as long as the shorter-lived object exists
– initialization of values of persistent object’s pointers to transient objects & commitment
SNU OOPSLA Lab 25
C++ OML: Object Reference(con’t)
• Dereference operation App
Page buffer
DB Server
Page buffer Transient memorypoint swizzling
d_Database *myDB;...d_Ref<Professor> prof;...prof = myDB.lookup_object(“hjk”);...
SNU OOPSLA Lab 26
C++ OML: Object Names
• In a database application, other objects are generally accessed by “root” objects, so name facility of root objects needs
• A single, flat name scope per database, so all names in a particular database are unique
• Operations for name manipulating are defined in the d_Database class
• e.g., d_Ref<Professor> prof1 = new(myDB, “Professor”) Professor;myDB.set_object_name(prof1, “hjk”);....D_Ref<Professor> prof2 = myDB.lookup_object(“hjk”);...
SNU OOPSLA Lab 27
C++ OML: Attributes
• Accessing attributes of persistent objects– C++ OML uses standard C++ for access to attributes– mark_modified call before attribute is modified– Embedded objects(of persistent-capable class) are accessed as
attributes. If they be modified, what should be done?
• Exampled_Ref<Professor> prof;...prof.mark_modified();prof->TA.name=“kkk”; //TA is an embedded object... //class Professor : public d_Object
//{... // //not Ref<Student> TA// Student TA;// ...};
SNU OOPSLA Lab 28
C++ OML: Relationships
• Creating, traversing, and breaking relationships are defined in the C++ OML
• The integrity of relationships is maintained by the ODBMS
• Both to-one and to-many traversal paths are supported by the OML
• Template classes for relationships
template <class T, const char *m> class d_Rel_Ref : public d_Ref<T> {};template <class T, const char *m> class d_Rel_Set : public d_Set<d_Ref<T> > {};template <class T, const char *m> class d_Rel_List : public d_List<d_Ref<T> >{};
SNU OOPSLA Lab 29
C++ OML: Relationships(con’t)
• 1-1 relationship
extern const char _ra[], _rb[];
class A { d_Rel_Ref<B, _ra> rb;};
class B { d_Rel_Ref<A, _rb> ra;};
const char _ra[] = “ra”;const char _rb[] = “rb”;
a.rb = &b; //same effect as b.ra=&a;
a.rb.clear(); //same effect as b.ra.clear();a.rb = &bb;
rb
a
ra
b
rb
a
ra
b
rb
a
ra
bb
ra
b
SNU OOPSLA Lab 30
C++ OML: Relationships(con’t)
• 1-m relationship using d_Set
extern const char _ra[], _sb[];
class A { d_Rel_Set<B, _ra> sb;};
class B { d_Rel_Ref<A, _sb> ra;};
const char _ra[] = “ra”;const char _sb[] = “sb”;
a.sb.insert_element(&b);
b.ra = &aa;
sb
a
ra
b
sb
a
sb
aa
ra
b
SNU OOPSLA Lab 31
C++ OML: Relationships(con’t)
• 1-m relationship using d_List– same as 1-m relationship using d_Set except positional nature
extern const char _ra[], _listb[];
class A { d_Rel_List<B, _ra> listb;};
class B { d_Rel_List<A, _listb> ra;};
const char _ra[] = “ra”;const char _listb[] = “listb”;
SNU OOPSLA Lab 32
C++ OML: Relationships(con’t)
• m-m relationship
extern const char _la[], _lb[];
class A { d_Rel_List<B, _la> lb;};
class B { d_Rel_Ref<A, _lb> la;};
const char _la[] = “la”;const char _lb[] = “lb”;
a.sb.replace_element(&bb,2)
lb
b
a.sb.insert_element(&b);
lb
a
la
b
lb
a
la
bb
SNU OOPSLA Lab 33
C++ OML: Operations
• Operations(on transient and persistent objects) are defined in the OML as implemented in standard C++
– Overloading, dispatching, function call structure &
invocation, member function call structure & invocation,
argument passing & resolution, error handling, etc.
SNU OOPSLA Lab 34
C++ OML: d_Object Class
• Definition
• Class type definer: whether a class is persistent-capable
class My_Class: public d_Object {...};
class d_Object {
public://constructor, copy constructor, destructorvoid mark_modified();//operator newvoid operator delete(void*);virtual void d_activate();virtual void d_deactivate();
};
SNU OOPSLA Lab 35
C++ OML: d_Object Class(con’t)
• Misc– delete operator: remove from both application cache and dat
abase(same behavior as Ref<T>::delete_object)
– d_activate/d_deactivate: memory allocation/deallocation
– constructor: called when a object first created
– destructor: called when the object deleted from database (different from d_deactivate)
SNU OOPSLA Lab 36
C++ OML: Reference Classes
• Objects refer to other objects through reference– A d_Ref<T> is a reference to an instance of type T
– Also, d_Ref_Any class provides a generic reference to any type
– References guarantees integrity in reference to persistent objects
– One anomaly exists between d_Ref<T> and d_Ref_Any
e.g.
//X and Y are unrelatedd_Ref<X> x; //compilation without errord_Ref<Y> y(x); //run time error
SNU OOPSLA Lab 37
C++ OML: Reference Classes(con’t)
• Definition of d_Ref<T> and d_Ref_Any
template <class T> class d_Ref {
public://constructor, copy constructor, and destructor//assignment operatorT* operator->(); //dereference operatorvoid delete_object();d_Boolean operator!() const; //boolean predicates//friend functions for equal & not-equal (==, !=)
};
class d_Ref_Any {//almost same as d_Ref class
};
SNU OOPSLA Lab 38
C++ OML: Collection Classes
• Collection templates support the representation of a collection whose elements are of an arbitrary type
d_Collection
d_Set d_Dictionaryd_Varrayd_Listd_Bag
SNU OOPSLA Lab 39
C++ OML: Collection Classes(con’t)
• Copy semantics
//Professor class is persistent-capable
d_Set<Professor> s1, s2;s1=s2;
d_Set<d_Ref<Professor> > sr1, sr2;sr1 = sr2;
d_Ref<d_Set<Professor> > rs1, rs2;rs1 = rs2;
d_Ref<d_Set<d_Ref<Professor> > > rsr1, rsr2;rsr1 = rsr2;
How does copy semantics differs from each other?
SNU OOPSLA Lab 40
C++ OML: Collection Classes(con’t)
• Collections, Embedded and with d_Ref
S
LL
L
R T
STT
T
S RRR
T
T
T
d_Ref<T>
d_Set<d_Ref<T> >
d_Ref<d_Set<d_Ref<T> > >d_Set<T>
S RRR
T
T
TR
d_Set<Literal>
SNU OOPSLA Lab 41
C++ OML: Collection Classes(con’t)
• d_Collection class– as an abstract class in C++, cannot have instances
– definition
template<class T> class d_Collection: public d_Object {
public:
//constructor, copy constructor, and destructor
//equal & not-equal operator
unsigned long cardinality();
d_Boolean is_empty() const;
d_Boolean is_ordered() const;
//contain, insert, remove, iterator, select, and query operations};
SNU OOPSLA Lab 42
C++ OML: Collection Classes(con’t)
• d_Set class– an unordered collection with no duplicates
– definition
template<class T> class d_Set: public d_Collection {
public:
//constructor, copy constructor, and destructor
//union(+), intersection(*), difference(-) operations
//subset, superset operations, etc.};
SNU OOPSLA Lab 43
C++ OML: Collection Classes(con’t)
• Example (d_Set class)
//creation:d_Database db;d_Ref<Professor> hjk;d_Ref<d_Set<d_Ref<Professor> > > my_profs
= new(&db) d_Set<d_Ref<Professor> >; //insertion:
my_profs->insert_element(hjk);
//removal:my_profs->remove_element(hjk);
//deletion:my_profs.delete_object();
SNU OOPSLA Lab 44
C++ OML: Collection Classes(con’t)
• d_Bag class– an unordered collection with duplicates
– definition
template <class T> class d_Bag: public d_Collection {
public://constructor, copy constructor, and destructorunsigned long occurrences_of(const T &element) const;//almost same as d_Set class
};
SNU OOPSLA Lab 45
C++ OML: Collection Classes(con’t)
• d_List class– an ordered collection with duplicates
– definition
template <class T> class d_List: public d_Collection {
public://constructor, copy constructor, and destructorT retrieve/remove_first/last_element() const;d_Boolean find_element(element, position) const;T retrieve_element_at(position) const;void remove_element_at(position);void replace_element_at(element, position);//insert, concatenate operations, etc.
};
SNU OOPSLA Lab 46
C++ OML: Collection Classes(con’t)
• d_Varray & d_Dictionary class– refer to page 162 -163 of the text book, ODMG 2.0
SNU OOPSLA Lab 47
C++ OML: Collection Classes(con’t)
• d_Iterator class– a consistent protocol for sequentially returning each element
from the collection
– An iterator is initialized by the create_iterator method
– definitiontemplate <class T> class d_Iterator {
public://constructor, copy constructor, and destructor//assignment, equal, and not-equal operators//increment, decrement operatorsd_Boolean not_done() const;d_Boolean next(T &objRef);
};
SNU OOPSLA Lab 48
C++ OML: Collection Classes(con’t)
• Example(d_Iterator class)
d_Set<d_Ref<Professor> > profset;d_Iterator<d_Ref<Professor> > iter = profset.create_iterator();d_Ref<Professor> prof;
while(iter.next(prof)) { or for( ; iter.not_done(); ++iter) { ... prof = iter.get_element();} ...
}
SNU OOPSLA Lab 49
C++ OML: Transactions
• All access, creation, modification, and deletion of persistent objects must be done within a transaction– Transactions are implemented as objects of d_Transaction
– definition of class d_Transaction
class d_Transaction { public:
//constructor, destructorvoid begin(); void commit/abort();void checkpoint();void join/leave(); //thread operationsd_Boolean is_active() const;static d_Transaction* current();
};
SNU OOPSLA Lab 50
C++ OML: Transactions(con’t)
• Characteristics of transaction in ODMG– Transaction must be explicitly created and started
– begin/commit/abort/checkpoint operations
– Transient objects are not subject to transaction semantics
– “long transactions” is not defined in ODMG 2.0
– join/leave operations
– usage of threads with transactions(three ways)
thread
transaction
SNU OOPSLA Lab 51
C++ OML: d_Database Class
• All operations on database are included in (transient) database object of class d_Database– open/close operations
– name operations
d_Database *myDB;myDB->open(“myDB”);
d_Ref<Professor> prof = new(myDB, “Professor”) Professor;myDB->set_object_name(prof, “hjk”);...d_Ref<Professor> myprof = myDB->lookup_object(“hjk”);...myDB->close();
SNU OOPSLA Lab 52
C++ OML: d_Database Class(con’t)
• Definition
class d_Database { public:
static d_Database* const transient_memory;//constructorvoid open(dbname, access status);void close();void set_object_name(const d_Ref_Any &obj, const char* name);void rename_object(oldname, newname);void lookup_object(name) const;
private:...
};
SNU OOPSLA Lab 53
C++ OML: d_Extent<T> Class
• Class d_Extent<T> provides an interface to the extent of a persistence-capable– Specifier of database schema definition needs to determine
who maintains an extent for each persistent class• User as well as ODBMS maintains an extent of persistent class
(refer to Section 5.6)
– d_Extent is not persistent-capable
– d_Extent is semantically equivalent to d_Set
– d_Extent supports polymorphism
– Every d_Extent must be associated with a database
SNU OOPSLA Lab 54
C++ OML: d_Extent<T> Class(con’t)
• Definition
template <class T> class d_Extent {
public:d_Extent(const d_Database* base,
d_Boolean include_subclasses=d_True);virtual ~d_Extent();//nearly same as the d_Collection class
};
polymorphism
associated database
SNU OOPSLA Lab 55
C++ OML: Exceptions
• d_Error class defines state describing the cause of the error– refer to page 172 - page 173 of the text book, ODMG 2.0
SNU OOPSLA Lab 56
C++ OQL
• How are query executed in C++ binding?
1 Query method on class Collection(refer to class d_Collection)
int query(d_Collection<T> &result, const char* predicate) const;
Example:
d_Bag<d_Ref<Student> > mathematicians; Students->query(mathematicians,
“exist s in this.takes: s.section_of.name = \“math\” ”);
SNU OOPSLA Lab 57
C++ OQL(con’t)
2 d_oql_execute function– A query gets constructed via an object of type d_OQL_Query
template <class T> void d_oql_execute(d_OQL_Query &q, T &result);ortemplate <class T> void d_oql_execute(d_OQL_Query &q,
d_Iterator<T> &result);Example: d_Bag<d_Ref<Student> > mathematicians; d_Bag<d_Ref<Professor> > assisted_profs; double x = 50000.00; d_OQL_Query q1(“select t.assists.taught_by from t in TA
where t.salary > $1 and t in $2”); q1 << x << mathematicians; d_oql_execute(q1, assisted_profs);
SNU OOPSLA Lab 58
Schema Access
• The C++ schema definition– Refer to metadata in ODL described in Chapter 2
– Presently the read interface of the schema API are supported
– Application parts which take advantage of the schema API• Database tool development(e.g., class and object browsers, im
port/export utilities, Query by Example, etc.)
• CASE tools
• Schema management tools
• CORBA
• Access control and user authorization
SNU OOPSLA Lab 59
Schema Access(con’t)
• The ODMG schema access class hierarchy
d_Scope
d_Meta_Object
d_Classd_Ref_Type
D_Collection_Typed_Primitive_Typed_Structure_Type
d_Alias_Type
d_Relationshipd_Attribute
d_Module
d_Constantd_Parameterd_Exception
d_Operation d_Propertyd_Type
d_Inheritance
• Schema access interface– refer to p181 - p194 of the text book, ODMG 2.0