Download presentation
Presentation is loading. Please wait.
Published byAbraham Elvin Andrews Modified over 9 years ago
1
Chapter 5 C++ Binding Prof. Hyoung-Joo Kim (hjk@oopsla.snu.ac.kr) OOPSLA Lab. Dept. of Computer Engineering Seoul National Univ.
2
SNU OOPSLA Lab 2 Contents Instruction C++ ODL C++ OML C++ OQL Schema Access
3
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
4
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 class –Keys: not supported by C++ –Names: set_object_name and rename_object methods in C++
5
SNU OOPSLA Lab 5 C++ ODL attributes relationships operations C++ ODL Declarations ODL Preprocessor C++ Header Files data members member functions ODL Declarations Java ODL Declarations
6
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 dept; d_Rel_Set advisees; //operations void grant_tenure(); voidassign_course(Course &); }; const _professors[] = “professors”; const _advisor[]=“advisor”; e.g.
7
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 declaration by C++ ODL d_String, d_Interval, d_Date, d_Time, d_Timestamp d_Rel_Ref, d_Rel_Set, d_Rel_List
8
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.)
9
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 favorite_friends; }; struct University_Address { d_UshortPO_box; d_Stringuniversity; d_Stringcity;... };
10
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 or d_False defines d_True(nonzero) and d_False(Zero)
11
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 2 implementation 16bit
12
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
13
SNU OOPSLA Lab 13 C++ ODL: Relationship Declarations A traversal path declaration is an attribute declaration and must be of type –d_Rel_Ref for 1 : 1 relationship –d_Rel_Set for 1 : m relationship –d_Rel_List 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
14
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 advisees; }; extern const char _advisor[], _advisees[]; class Student : public d_Object { public: d_Rel_Ref advisor; }; extern const char _advisor[]=“advisor”; extern const char _advisees[]=“advisees”; classes definition in C++ ODL
15
SNU OOPSLA Lab 15 C++ ODL: Operation Declarations Operation declarations are syntactically identical to member function declarations
16
SNU OOPSLA Lab 16 C++ OML main() { d_Database *myDB; d_Ref 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
17
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
18
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
19
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);
20
SNU OOPSLA Lab 20 C++ OML: Object Creation(con’t) Example d_Database *myDB, *yourDB; (1) d_Ref sched = new Schedule; (2) d_Ref prof = new(yourDB, “Professor”) Professor; (3) d_Ref stdnt1 = new(myDB, “Student”) Student; (4) d_Ref stdnt2 = new(stdnt1, “Student”) Student; (5) d_Ref temp_stdnt = new(d_Database::transient_memory, “Student”) Student;
21
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 objRef;... //set objRef to refer to a persistent object objRef.delete_object();
22
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
23
SNU OOPSLA Lab 23 C++ OML: Object Modification(con’t) Example App1 App2 Page buffer Page buffer DB Server “States will change” “You will be blocked” //Application 1 d_Ref prof = new(myDB, “Professor”) Professor; prof->age = 30;... //Application 2 d_Ref prof = myDB.lookup_object(...); prof->mark_modified(); prof->age = 40;...
24
SNU OOPSLA Lab 24 C++ OML: Object Reference Object referencing –Objects, whether persistent or not, refer to other objects via object references –Object references are instances of template class d_Ref –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
25
SNU OOPSLA Lab 25 C++ OML: Object Reference(con’t) Dereference operation App Page buffer DB Server Page bufferTransient memory point swizzling d_Database *myDB;... d_Ref prof;... prof = myDB.lookup_object(“hjk”);...
26
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 prof1 = new(myDB, “Professor”) Professor; myDB.set_object_name(prof1, “hjk”);.... D_Ref prof2 = myDB.lookup_object(“hjk”);...
27
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? Example d_Ref prof;... prof.mark_modified(); prof->TA.name=“kkk”;//TA is an embedded object...//class Professor : public d_Object //{... // //not Ref TA // Student TA; //...};
28
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 d_Rel_Ref : public d_Ref {}; template class d_Rel_Set : public d_Set > {}; template class d_Rel_List : public d_List >{};
29
SNU OOPSLA Lab 29 C++ OML: Relationships(con’t) 1-1 relationship extern const char _ra[], _rb[]; class A { d_Rel_Ref rb; }; class B { d_Rel_Ref 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
30
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 sb; }; class B { d_Rel_Ref ra; }; const char _ra[] = “ra”; const char _sb[] = “sb”; a.sb.insert_element(&b); b.ra = &aa; sb a ra b sb a aa ra b
31
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 listb; }; class B { d_Rel_List ra; }; const char _ra[] = “ra”; const char _listb[] = “listb”;
32
SNU OOPSLA Lab 32 C++ OML: Relationships(con’t) m-m relationship extern const char _la[], _lb[]; class A { d_Rel_List lb; }; class B { d_Rel_Ref 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
33
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.
34
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, destructor voidmark_modified(); //operator new voidoperator delete(void*); virtual void d_activate(); virtual void d_deactivate(); };
35
SNU OOPSLA Lab 35 C++ OML: d_Object Class(con’t) Misc –delete operator: remove from both application cache and database(same behavior as Ref ::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)
36
SNU OOPSLA Lab 36 C++ OML: Reference Classes Objects refer to other objects through reference –A d_Ref 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 and d_Ref_Any e.g. //X and Y are unrelated d_Ref x;//compilation without error d_Ref y(x);//run time error
37
SNU OOPSLA Lab 37 C++ OML: Reference Classes(con’t) Definition of d_Ref and d_Ref_Any template class d_Ref { public: //constructor, copy constructor, and destructor //assignment operator T*operator->();//dereference operator void delete_object(); d_Booleanoperator!() const;//boolean predicates //friend functions for equal & not-equal (==, !=) }; class d_Ref_Any { //almost same as d_Ref class };
38
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_Dictionary d_Varray d_List d_Bag
39
SNU OOPSLA Lab 39 C++ OML: Collection Classes(con’t) Copy semantics //Professor class is persistent-capable d_Set s1, s2; s1=s2; d_Set > sr1, sr2; sr1 = sr2; d_Ref > rs1, rs2; rs1 = rs2; d_Ref > > rsr1, rsr2; rsr1 = rsr2; How does copy semantics differs from each other?
40
SNU OOPSLA Lab 40 C++ OML: Collection Classes(con’t) Collections, Embedded and with d_Ref S L L L R T S T T T S R R R T T T d_Ref d_Set > d_Ref > > d_Set S R R R T T T R
41
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 d_Collection: public d_Object { public: //constructor, copy constructor, and destructor //equal & not-equal operator unsigned long cardinality(); d_Booleanis_empty() const; d_Boolean is_ordered() const; //contain, insert, remove, iterator, select, and query operations };
42
SNU OOPSLA Lab 42 C++ OML: Collection Classes(con’t) d_Set class –an unordered collection with no duplicates –definition template class d_Set: public d_Collection { public: //constructor, copy constructor, and destructor //union(+), intersection(*), difference(-) operations //subset, superset operations, etc. };
43
SNU OOPSLA Lab 43 C++ OML: Collection Classes(con’t) Example (d_Set class) //creation: d_Database db; d_Ref hjk; d_Ref > > my_profs = new(&db) d_Set >; //insertion: my_profs->insert_element(hjk); //removal: my_profs->remove_element(hjk); //deletion: my_profs.delete_object();
44
SNU OOPSLA Lab 44 C++ OML: Collection Classes(con’t) d_Bag class –an unordered collection with duplicates –definition template class d_Bag: public d_Collection { public: //constructor, copy constructor, and destructor unsigned longoccurrences_of(const T &element) const; //almost same as d_Set class };
45
SNU OOPSLA Lab 45 C++ OML: Collection Classes(con’t) d_List class –an ordered collection with duplicates –definition template class d_List: public d_Collection { public: //constructor, copy constructor, and destructor Tretrieve/remove_first/last_element() const; d_Booleanfind_element(element, position) const; Tretrieve_element_at(position) const; voidremove_element_at(position); voidreplace_element_at(element, position); //insert, concatenate operations, etc. };
46
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
47
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 –definition template class d_Iterator { public: //constructor, copy constructor, and destructor //assignment, equal, and not-equal operators //increment, decrement operators d_Booleannot_done() const; d_Booleannext(T &objRef); };
48
SNU OOPSLA Lab 48 C++ OML: Collection Classes(con’t) Example(d_Iterator class) d_Set > profset; d_Iterator > iter = profset.create_iterator(); d_Ref prof; while(iter.next(prof)) { or for( ; iter.not_done(); ++iter) {... prof = iter.get_element(); }... }
49
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, destructor voidbegin(); voidcommit/abort(); voidcheckpoint(); voidjoin/leave();//thread operations d_Booleanis_active() const; static d_Transaction*current(); };
50
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
51
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 prof = new(myDB, “Professor”) Professor; myDB->set_object_name(prof, “hjk”);... d_Ref myprof = myDB->lookup_object(“hjk”);... myDB->close();
52
SNU OOPSLA Lab 52 C++ OML: d_Database Class(con’t) Definition class d_Database { public: static d_Database* const transient_memory; //constructor void 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:... };
53
SNU OOPSLA Lab 53 C++ OML: d_Extent Class Class d_Extent 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
54
SNU OOPSLA Lab 54 C++ OML: d_Extent Class(con’t) Definition template 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
55
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
56
SNU OOPSLA Lab 56 C++ OQL How are query executed in C++ binding? 1Query method on class Collection(refer to class d_Collection) int query(d_Collection &result, const char* predicate) const; Example: d_Bag > mathematicians; Students->query(mathematicians, “exist s in this.takes: s.section_of.name = \“math\” ”);
57
SNU OOPSLA Lab 57 C++ OQL(con’t) 2d_oql_execute function –A query gets constructed via an object of type d_OQL_Query template void d_oql_execute(d_OQL_Query &q, T &result); or template void d_oql_execute(d_OQL_Query &q, d_Iterator &result); Example: d_Bag > mathematicians; d_Bag > 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);
58
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, import/export utilities, Query by Example, etc.) CASE tools Schema management tools CORBA Access control and user authorization
59
SNU OOPSLA Lab 59 Schema Access(con’t) The ODMG schema access class hierarchy d_Scope d_Meta_Object d_Class d_Ref_Type D_Collection_Type d_Primitive_Type d_Structure_Type d_Alias_Type d_Relationship d_Attribute d_Module d_Constant d_Parameter d_Exception d_Operation d_Property d_Type d_Inheritance Schema access interface –refer to p181 - p194 of the text book, ODMG 2.0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.