© copyright 1999, objectivity, inc., all rights reserved objectivity/db for c++ developers - 1...

24
yright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

Upload: steven-gilbert

Post on 04-Jan-2016

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1

Associations

Library Container

Members Container

Database

Page 2: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 2

Objectives

In this section you will

Learn about the attributes of associations: Directionality Cardinality

Learn how to define an association

Learn how to add or delete associations

Page 3: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 3

What is an Association?

An association is a typed relationship between two objects

To allow an association between objects, you must define an association link in each object’s class definition

Associations are used to link objects together

Page 4: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 4

Associations in the Library Object Model

Book

Loan Patron

patron

loans[]

previousBook activeBook

activeLoanpreviousLoans[]

Page 5: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 5

Unidirectional Associations

A unidirectional association is one in which only one class defines an association link to another class

For example, there is only one Library to serve many Patrons

Library

PatronPatron Patron Patron

Page 6: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 6

Bidirectional Associations

Bidirectional associations are those in which each class instance has an association link to the other class

They guarantee referential integrity: prevents dangling references

They support two-way access paths For example, each patron has many loans and each loan

can be tracked back to a patron

Patron

Loan

Loan

Loan

Loan

Page 7: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 7

Cardinality in Associations

An association has two ends

Each end can connect to one or many objects, depending on how the association is defined in the database schema

Objectivity/DB associations support four categories of cardinality:

1:1 one-to-one 1:m one-to-many m:1 many-to-one n:m many-to-many

Page 8: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 8

Defining Association Links

A B

1 toB

one-to-one unidirectional

A B

1 mtoB[] toA

one-to-many bidirectional

A B

n mtoB[] toA[]

many-to-many bidirectional

class A : public ooObj { ooRef(B) toB;};class B : public ooObj {};

class A : public ooObj { ooRef(B) toB[] <-> toA;};class B : public ooObj { ooRef(A) toA <-> toB[];};

class A : public ooObj { ooRef(B) toB[] <-> toA[];};class B : public ooObj { ooRef(A) toA[] <-> toB[];};

Page 9: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 9

Using Associations

The DDL Processor generates several methods (member functions) for every association link declared in a class

You use these member functions to access the related objects and establish association links

Set as many associations to a basic object as possible at the same time so that storage is allocated contiguously

Page 10: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 10

Generated Member Functions: the to-one association

The DDL Processor automatically generates the member functions below for one-to-one and many-to-one associations

Association member functions are defined on an object, not on a handle

class Book : public ooObj {ooRef(Loan) activeLoan <-> activeBook;

};

ooStatus Book::set_activeLoan(ooHandle(Loan)& objH);ooStatus Book::del_activeLoan();ooStatus Book::activeLoan(ooHandle(Loan)& objH, ooMode mode);ooBoolean Book::exist_activeLoan

(const ooHandle(Loan)& objH);

DDL

Page 11: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 11

Generated Member Functions: the to-many association

The DDL Processor automatically generates the member functions below for one-to-many and many-to-many associations

class Book: public ooObj {

ooRef(Loan) previousLoans[] <-> previousBook;

};

ooStatus Book::add_previousLoans(ooHandle(Loan)& newObjH);ooStatus Book ::sub_ previousLoans(ooHandle(Loan)& objH);ooStatus Book ::del_previousLoans();ooStatus Book ::previousLoans(ooItr(Loan)&, ooMode);ooBoolean Book ::exist_previousLoans

(const ooHandle(Loan)& objH);

DDL

Page 12: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 12

The Unidirectional To-One Association

No methods are automatically generated for a simple ooRef data member. I.e.

This data member is assigned to other ooRef/ooHandle values using the assignment operator

Technically, it is not an “association” unless it has special propagate semantics (explained later)

class A : public ooObj { ooRef(B) toB;};class B : public ooObj {};

Page 13: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 13

Member Functions Examples// For the current book, set an active loan linkbookH -> set_activeLoan(loanH);

// For the current patron, delete all loanspatronH -> del_loans();

// Check if the current book has the active loanbookH -> exist_activeLoan(loanH);

// For the current book, add a previous loan linkbookH -> add_previousLoans(loanH);

// Delete the current loan from the patronpatronH -> sub_loans(loanH);

// Initialize the handle to the active loan objectbookH -> activeLoan(loanH);

orloanH = bookH->activeLoan();

Page 14: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 14

1. Define Associations in your Object Model (diagrams)

2. Enter the definition in the DDL files

3. Build your application

4. Run your application, whicha. Retrieves or creates two objects that are persistent

b. Calls the association member functions which connect the link:

Class::set_assocName() for a one-ended link

Class::add_assocName() for a many-ended link

Summary: Steps to Use Associations

Page 15: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 15

How Associations are Stored

class A : public ooObj {public:

ooRef(B) toB [];ooRef(C) toC [];

};

Object A1 of Class A

toC 1-17- 2- 3toC 1-17- 3-10toB 2- 6-14- 5... ...

Assoc.ID OID

System DefaultAssociation Array

(VArray)

4 bytes 8 bytes

C1C2B

To follow a specific association on an object, all entries in the system default association array are scanned until the desired association is found (i.e., linearly compare each entry in the VArray)

Page 16: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 16

Optimizing Associations

Associations can be optimized for size by using short OIDs

Associations can be optimized for speed and size by using inline associations

Improved traversal performance

Each inline to-many association is placed in a separate array (to-many) instead of the default association array

Inline association links are created by adding a property to your DDL file association definition

Page 17: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 17

Long and Short Inline Association Links

A long inline association link uses an Object Identifier (OID) to refer to the associated object

A short inline association link uses a short OID to refer to the associated object

A short inline association link is used in the same way as a long inline association link, but uses less storage space (only four bytes, rather than eight) to maintain the association, resulting in better runtime performance

When using short association links, objects being linked must be in the same container

Page 18: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 18

Defining & Storing Inline Associations

toB 1-17- 3- 1toC 1-17- 2- 3toC 1-17- 3-10... ...

Assoc.link name OID

4 bytes 8 bytes

B1C1C2

System DefaultAssociation Array

class A : public ooObj {public:

ooRef(B) toB <-> toA;ooRef(C) toC [];ooRef(D) toD;inline ooRef(E) toE [];inline ooShortRef(F) toF;inline ooShortRef(G) toG [];

...}

3- 5- 7- 96-12-10- 22-15- 5-14...

E1E2E3

8 bytesOID

6-156-18...

4 bytesShort OID

G1G2

Pointer to System Default

Association Array

Embedded Short

Inline Assocaition toF

6-12

...

System overhead

Pointer to Array for Inline

Association Line toE

Pointer to Array for Inline

Association Link toG

Embedded ooRef toD

5- 7-12-10

8 bytes

Object A1 of class A

Page 19: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 19

Accessing Inline Association Links

The member functions for accessing inline association links are generated by the DDL processor and placed in a C++ header file

The member functions are the same as those for accessing non-inline association links

Application programs must be relinked, but not recompiled, when the change is made

Page 20: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 20

Composite Objects & Propagation

Composite objects allow you to treat a group of associated objects as if they are a single object

Composite objects are built using Associations that have propagation semantics

Propagation causes each object in the composite object to be opened, operated on by the specified function, and then closed

Propagation is an atomic operation on a group of objects

Page 21: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 21

Effects of Propagation on Deleting Objects

Delete operations can be propagated

All objects to be deleted are first identified and then deleted as a group

All bidirectional associations are automatically adjusted to reflect deletions

BA

A2 C

If you delete object “B”, all objects pointed at by B withdelete propagate will becleaned up as well

Page 22: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 22

Propagation Syntax

When you define an association link, specify the operation to propagate to other objects using the assocLink behavior specifier:

assocLink : propOp(propagate {, propOp(propagate)})

Example:class Book: public ooObj {

public:

ooRef(Loan) previousLoans[] <-> previousBook:delete(propagate);

}

...

ooDelete(bookH); //deletes all related Loans

Page 23: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 23

Associations Summary

An association is a typed relationship between two objects

There are two types of associations: unidirectional and bidirectional

Associations can connect to one or many objects

Associations can be optimized by using long and short inline properties

Delete propagation semantics can be added to association links to affect composite objects

Page 24: © Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database

© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 24

Associations Lab

In this lab you will create associations between a Loan and a Book and a Patron.

You will: Add the new Loan class Define the association links Set/remove association links Build the LMS and populate

it with Books, Patrons, Loans Browse the federated

database and traverse theassociations Database: Lab5

Default ContainerBook objects

Library Container

Members Container

Patron objects

LOM

Loan objects