Data Management Design

streakconvertingΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

66 εμφανίσεις


© 2010 Bennett, McRobb and Farmer


1

Data Management Design

Based on Chapter 18

Bennett, McRobb and Farmer

Object Oriented Systems Analysis
and Design Using UML

4
th

Edition, McGraw Hill, 2010


2


© 2010 Bennett, McRobb and Farmer

In This Lecture You Will Learn:


The different ways of storing persistent
objects


The differences between object and
relational databases


How to design data management
objects


How to extend sequence diagrams to
include data management objects


3


© 2010 Bennett, McRobb and Farmer

Persistence


Some objects are transient, exist in
memory and are discarded when an
application terminates


Some objects must exist from one
execution of an application to another or
be shared among different instances of
applications. Such objects are
persistent
objects


4


© 2010 Bennett, McRobb and Farmer

Persistence Mechanisms


Files hold data, typically on magnetic media
such as disks and tapes


Database management systems (DBMS) hold
tables of data (relational DBMS) or objects
(object DBMS)


DBMS use files to store data or objects, but they
hide the physical processes for storing data
beneath a layer of abstraction


Objects can also be
serialized

directly to files


5


© 2010 Bennett, McRobb and Farmer

Persistence Architecture


The choice of the architecture for persistence is
a system design issue


The design of storage for specific classes and
associations within the framework of that
architecture is a class design issue


The overall design may be constrained by
having to operate with existing systems or using
existing DBMS


6


© 2010 Bennett, McRobb and Farmer

Persistence Design Questions


Can files be used for some storage?


Is it truly an O
-
O system or just an
interface to a relational database using
Java or C++?


Will the system use an existing DBMS?


Will it use a relational DBMS?


Will it use an object DBMS?


7


© 2010 Bennett, McRobb and Farmer

Persistence Design Questions


What is the logical layering of the system?


What is the physical layering of the
system?


Is the system distributed? Does this
include distributed data storage?


What protocols will be used to
communicate within the system?


8


© 2010 Bennett, McRobb and Farmer

File Systems

Files and record structures


Fixed length (padded)


Variable length (delimited)


Header and detail


Tagged data (XML)

12345678901234567890123456789012345

Simon Bennett Leice

ster GB 21322012002

”Simon”,”Bennett”,”Leicester”,”GB”,
213,”22
-
01
-
2002”

1,”Simon”,”Bennett”

2,1,”0077098641”,2002

2,2,”0077096738”,2001

<author>


<forename>Simon</forename>


<surname>Bennett</surname>

</author>


9


© 2010 Bennett, McRobb and Farmer

File Systems

File organization


Serial

new records appended


Sequential

records ordered in file,
usually according to a numeric key


Random

uses an algorithm to convert a
key to an address in the file


10


© 2010 Bennett, McRobb and Farmer

File Systems

File access methods


Serial

to read serial and sequential files


Index
-
sequential

using indexes to find records
in a sequential file and improve access time


Direct

using
relative

or
hashed

addressing to
move directly to the required record in the file


11


© 2010 Bennett, McRobb and Farmer

Index
-
sequential Access

Searching for Hamer using index
-
sequential file access


12


© 2010 Bennett, McRobb and Farmer

Direct Access




13


© 2010 Bennett, McRobb and Farmer

Improving Access


Creating a linked list in random files to
make it possible to read records in
sequential order


Adding a secondary index keyed on a field
that is not the key on which the main
access method is based


Indexes can be inverted files or use other
techniques such as B
-
trees


14


© 2010 Bennett, McRobb and Farmer

File Types


Master files


Transaction files


Index files


Temporary file or work files


Backup files


Parameter files


15


© 2010 Bennett, McRobb and Farmer

File Example


Using files in Java to handle localization of
prompts and messages


Use the java.util.Locale class to hold information
about the current locale


language_country_variant


fr_FR_EURO


fr_CA


en_UK


en_AU


16


© 2010 Bennett, McRobb and Farmer

File Example


The Java class java.util.ResourceBundle
uses the locale to load a file with locale
-
specific values e.g.
UIResources_fr_FR_EURO


Java code to use this:

resources = ResourceBundle.getBundle(”UIResources”,
currentLocale);

Button cancelButton = new Button(resources.getString
(”Cancel”);


rather than

Button cancelButton = new Button(”Cancel”);


17


© 2010 Bennett, McRobb and Farmer

Resource File


File for French is
UIResources_fr_FR_EURO


Contains

Cancel = Annuler

OK = OK

File = Fichier




18


© 2010 Bennett, McRobb and Farmer

Database Management

Systems (DBMS)


Problems with files:


redundancy

number of files grows with applications,
and data is duplicated


inconsistency

data is updated in one application’s
files, but not in another’s


maintenance problems

changes to data structures
mean changes to many programs


difficulty combining data

business needs may mean
users want data from different applications


19


© 2010 Bennett, McRobb and Farmer

DBMS


Corporate database consolidates data for
different applications


Each application then has its own view of
a subset of the data

Database









Application 1









Application 2










20


© 2010 Bennett, McRobb and Farmer

DBMS Schema


Ultimately data in databases is stored in
files, but their structure is hidden from
developers

Conceptual Schema

External Schema

Internal Schema

The view on data used by



application programs.



The logical model of data that is



separate from how it is used.



The physical storage of data in



files and indexes.




21


© 2010 Bennett, McRobb and Farmer

DBMS Features


Data Definition Language (DDL)


Data Manipulation Language (DML)


Integrity Constraints


Transaction Management


Concurrency


Security


Tuning of Storage


22


© 2010 Bennett, McRobb and Farmer

Advantages of DBMS


Eliminate unnecessary duplication of data


Enforce data integrity through constraints


Changes to conceptual schema need not affect
external schema


Changes to internal schema need not affect the
conceptual schema


Many tools are available to manage the
database


23


© 2010 Bennett, McRobb and Farmer

Disadvantages of DBMS


Cost of investing in the DBMS


Running cost, including staff (Database
Administrators) to manage the DBMS


Processing overhead in converting data to
format required by programs


24


© 2010 Bennett, McRobb and Farmer

Types of DBMS


Relational

represent data in tables


tables consist of rows of data organized in
columns


25


© 2010 Bennett, McRobb and Farmer

Types of DBMS


Object

store objects as objects


designed to handle complex nested objects
for graphical and multimedia applications


Object
-
relational

hybrid databases that
can store data in tables but can also store
objects in tables


26


© 2010 Bennett, McRobb and Farmer

Relational DBMS


To store objects in a relational database,
the objects have to be ‘flattened’ into
tables


Complex objects have to be taken apart
and the parts stored in different tables


When retrieved from the database, the
object has to be reassembled from the
parts in different tables


27


© 2010 Bennett, McRobb and Farmer

Normalization


Data from complex structures is ‘flattened’
into tables


Typically normalization is carried out as far
as ‘Third Normal Form’


In an object
-
oriented system, we may use
normalization to convert classes to table
schemas


28


© 2010 Bennett, McRobb and Farmer

Normalization Example


ic1:InternationalCampaign



campaignCode =



SMGL



campaignTitle =





Soong Motors











Helion Launch





locationsList:LocationList








locationCode

=





HK




locationName =





Hong Kong




locationMgr =





Vincent Sieuw




locationMgrTel = ext. 456




locationCode =





NY




locationName =





N

ew York




locationMgr =





Martina Duarte




locationMgrTel =




ext. 312






locationCode =





TO




locationName =





Toronto




locationMgr =





Pie

rre Dubois




locationMgrTel =




ext. 37





ic2:InternationalCampaign





campaignCode =



YPSC



campaignTitle =





Yellow Partridge













Summer Collection














locationCode =





HK




locationNa

me =





Hong Kong




locationMgr =





Jenny Lee




locationMgrTel =


ext. 413






locationCode =





NY




locationName =





New York




locationMgr =





Martina Duarte




locationMgrTel =




ext. 312



locationCount = 3

locationCount = 2

locationsList[0]:Location

locationsList[1]:Location

locationsList[2]:Location

locationsList:LocationList

locationsList[0]:Location

locationsList[1]:Location


29


© 2010 Bennett, McRobb and Farmer

Objects as a Table


To get to First Normal Form, break out the
repeating groups


30


© 2010 Bennett, McRobb and Farmer

First Normal Form


31


© 2010 Bennett, McRobb and Farmer

Second Normal Form


32


© 2010 Bennett, McRobb and Farmer

Third Normal Form


33


© 2010 Bennett, McRobb and Farmer

Alternative Approach


Classes with simple data structure become
tables


Object IDs become primary keys


Where classes contain another class as an
attribute create a table for the embedded class


For collections create two tables, one for the
objects in the collection, the other to hold Object
IDs of the containing objects and the contained
objects


34


© 2010 Bennett, McRobb and Farmer

Alternative Approach


One
-
to
-
many associations can be treated like
collections


Many
-
to
-
many associations become two
separate tables for the objects and a table to
hold pairs of Object IDs


One
-
to
-
one associations are implemented as
foreign
-
key attributes

each class gains an extra
attribute for the Object ID of the other


35


© 2010 Bennett, McRobb and Farmer

Alternative Approach


To implement inheritance


only implement the superclass as a table
including all subclass attributes


only implement the subclasses as tables,
duplicating superclass attributes in each


implement superclass and subclasses as
tables with shared primary keys


Each approach has disadvantages


36


© 2010 Bennett, McRobb and Farmer

Object DBMS


ODBMS have the advantage that objects can be
stored directly


Object Data Management Group (ODMG)
standard


Not all object databases conform to the standard


Object databases are closely linked to
programming languages with ways of navigating
through the database


37


© 2010 Bennett, McRobb and Farmer

Sample Java

db4o Storage Operation

public static void createClients(ObjectContainer db) {


StaffMember staff1 = new StaffMember(1,"Amarjeet Grewal");


Address address1 = new Address("Holborn House", "", "High

Street", "", "Holborn", "London", "", "SW1 8YH", "United

Kingdom");


Client client1 = new Client(45, "Holborn Motors", "Dave

Richards", "company@holborn.co.uk", "02079996780",

"02079996790" ,staff1, address1);


db.store(client1);


System.out.println("Stored
\
n" + client1);

}



38


© 2010 Bennett, McRobb and Farmer

Sample Java

db4o Retrieval Operation

retrieveClientsByContact(db, 1, "Amarjeet Grewal");

...


public static void retrieveClientsByContact




(ObjectContainer db, int staffNo, String staffName) {


StaffMember staffProto = new StaffMember(staffNo, staffName);


Client clientProto = new Client();


clientProto.setStaffContact(staffProto);


ObjectSet result = db.queryByExample(clientProto);


listResult(result);

}


39


© 2010 Bennett, McRobb and Farmer

Object DBMS


Some will transparently ‘materialize’
objects from the database when they are
referred to


Update transactions need to be bracketed
with start and finish transaction methods


Operations are still implemented in object
-
oriented languages


40


© 2010 Bennett, McRobb and Farmer

Designing Data

Management Classes


Alternatives (two in bold are covered here):


add save and retrieve operations to classes


make save and retrieve class
-
scope methods


allow all persistent objects to inherit from a
PersistentObject superclass


use collection classes to manage persistence


use broker classes to manage persistence


use a parameterized class to handle persistence for
different classes


41


© 2010 Bennett, McRobb and Farmer

PersistentObject



Create an
abstract
superclass and
make all
persistent
classes inherit
from it

PersistentObject
{Abstract}
-
objid
:
int
-
iterator
:
RandomAccessFile
+
getObject
(
int
): Object
+ store( )
+ delete( )
+ update( )
+ iterate( ): Object
+ write( ) {Abstract}
+ read( ) {Abstract}
Location
-
locationCode
: String
-
locationName
: String
-
intCampaignList
:
IntCampaign
[ *]
+
findByLocationCode
( String ):
Loca
tion
+
iterateLocation
( ): Location
+
iterateIntCampaign
( ):
IntCampaign
+
addIntCampaign
(
IntCampaign
)
+
removeIntCampaign
( St
ring )
+
numberOfCampaigns
( ):
int
+ write( )
+ read( )
PersistentObject
{Abstract}
-
objid
:
int
-
iterator
:
RandomAccessFile
+
getObject
(
int
): Object
+ store( )
+ delete( )
+ update( )
+ iterate( ): Object
+ write( ) {Abstract}
+ read( ) {Abstract}
Location
-
locationCode
: String
-
locationName
: String
-
intCampaignList
:
IntCampaign
[ *]
+
findByLocationCode
( String ):
Loca
tion
+
iterateLocation
( ): Location
+
iterateIntCampaign
( ):
IntCampaign
+
addIntCampaign
(
IntCampaign
)
+
removeIntCampaign
( St
ring )
+
numberOfCampaigns
( ):
int
+ write( )
+ read( )
Location
-
locationCode
: String
-
locationName
: String
-
intCampaignList
:
IntCampaign
[ *]
+
findByLocationCode
( String ):
Loca
tion
+
iterateLocation
( ): Location
+
iterateIntCampaign
( ):
IntCampaign
+
addIntCampaign
(
IntCampaign
)
+
removeIntCampaign
( St
ring )
+
numberOfCampaigns
( ):
int
+ write( )
+ read( )

42


© 2010 Bennett, McRobb and Farmer

PersistentObject

Materialization as Class Method


Sequence diagram

Location(



locationCode,

locationName,

intCampaignList[ ])



getNumberOf



Campaigns





:ListCampaigns

Dialog

Location



findByLocation

Code

(locCode)





numberOfCampaigns







:Location





sd

Get number of campaigns for location


43


© 2010 Bennett, McRobb and Farmer

Database Broker


Use a broker class responsible for
materializing instances of each class from
the database

Location



LocationBroker





-


instance: LocationBroker





-


LocationBroker( )



+ instance( ): LocationBroker



+ findByLocationCode( String ): Location

+ iterateLocation( ): Location





materializes






44


© 2010 Bennett, McRobb and Farmer

Database Broker

Materializes Instances


Sequence diagram

Location(



locationCode,

locationName,

intCampaignList[ ])



GetNumberOf



Campaigns





:ListCampaigns



Dialog



:LocationBroker





findByLocation

Code

(locCode)





numberOfCampaigns







:Location





sd

Get number of campaigns for location


45


© 2010 Bennett, McRobb and Farmer

Inheritance Hierarchy of
Database Brokers

DatabaseBroker


RelationalBroker


FileBroker


IntCampaignBroker


LocationBroker


IntCampaign


Location


materializes



materializes




46


© 2010 Bennett, McRobb and Farmer

RelationalBroker and Other
Classes

util::brokers
::
RelationalBroker
com::agate::brokers
::
LocationBroker
java::sql::Connection
com::agate::domain
::
entity::Location
materializes
java::sql::ResultSet
java::sql::Statement
oracle::jdbc::driver::Oracle
Driver
util::brokers
::
RelationalBroker
com::agate::brokers
::
LocationBroker
java::sql::Connection
com::agate::domain
::
entity::Location
materializes
java::sql::ResultSet
java::sql::Statement
oracle::jdbc::driver::Oracle
Driver

47


© 2010 Bennett, McRobb and Farmer

Package Diagram

com
«
import
»
«
import
»
agate
java
jdbc
driver
sql
util
brokers
oracle
brokers
com
«
import
»
«
import
»
agate
java
jdbc
driver
sql
util
brokers
oracle
brokers

48


© 2010 Bennett, McRobb and Farmer

Proxy Pattern


Proxy objects act as placeholders for the real
objects, e.g. IntCampaigns in Locations


The IntCampaignProxy has the same interface
as IntCampaign, but no data


When a Location requires data about one of its
IntCampaigns, it sends a message to the Proxy


The Proxy requests the Broker to materialize the
IntCampaign and passes the message on


49


© 2010 Bennett, McRobb and Farmer

Proxy Pattern


The Proxy can then replace the reference to
itself in the Location with a reference to the real
materialized object


This approach can be combined with caching of
objects


The caches can be used by the Broker to check
whether an object is already in memory and
save materializing it from the database if it is


50


© 2010 Bennett, McRobb and Farmer

Adding Caches


Six caches


new clean cache


new dirty cache


new deleted cache


old clean cache


old dirty cache


old deleted cache


51


© 2010 Bennett, McRobb and Farmer

Transaction Commit

Cache Actions


Six caches


new clean cache


new dirty cache


new deleted cache


old clean cache


old dirty cache


old deleted cache


Cache actions


write to database


write to database


delete from cache


delete from cache


write to database


delete from database


52


© 2010 Bennett, McRobb and Farmer

Class Diagram with

Caches and Proxies







DatabaseBroker









RelationalBroker









IntCampaignBroker









IntCampaign





materializes









Cache







1





6





«

Interface

»



IntCampaign

Interface





«

Proxy

»



IntCampaignProxy









Location





runs in









«

realize»



«

realize»






53


© 2010 Bennett, McRobb and Farmer

Collaboration Diagram

:IntCampaignBroker

:IntCampaign



:Cache



:IntCampaignProxy









:Location







1: printList( )





1.1: getTitle( )



1.2: inMemory( )





1.3 [not in memory]:

getIntCampaign



( objectid )





1.4*: inCache



( objectid )







1.5 [not in cache]:

retrieveIntCampaign

( objectid )



1.6: getTitle( )





sd

Retrieve an InternationalCampaign


54


© 2010 Bennett, McRobb and Farmer

Using a Framework


Why develop a framework when you
can use an existing one?


Object
-
table mappings


JDO


Hibernate


Standard APIs to map attributes of
classes to columns in a relational
database table using XML persistence
descriptors


55


© 2010 Bennett, McRobb and Farmer

Using a Framework


Java Persistence API


Replaces Container
-
Managed Persistence
(CMP) for Enterprise JavaBeans (EJBs) used in
J2EE


Can use XML or Java annotations

import

javax
.
persistence
.
*
;


...

@Entity

public

class

Client

{

@Id

private

int

clientNo
;


56


© 2010 Bennett, McRobb and Farmer

Summary

In this lecture you have learned about:


The different ways of storing persistent objects


The differences between object and relational
databases


How to design data management objects


How to extend sequence diagrams to include
data management objects


57


© 2010 Bennett, McRobb and Farmer

References


Silberschatz, Korth and Sudarshan (2005)



Howe (2001)


Roos (2003)


Connolly and Begg (2010)



(For full bibliographic details, see Bennett,
McRobb and Farmer)