• Nenhum resultado encontrado

Object-Oriented Databases

N/A
N/A
Protected

Academic year: 2022

Share "Object-Oriented Databases"

Copied!
36
0
0

Texto

(1)

Obj t O i t d D t b

Object-Oriented Databases

db4o: Part 2

Configuration and Tuning, Distribution and Replication

Schema Evolution: Refactoring, Inheritance Evolution

Callbacks and Translators

(2)

Summary: db4o Part 1 y

ƒ Managing databases with an object container

ƒ Retrieving objects

ƒ query by exampleq y y p

ƒ native queries

ƒ SODA queries

ƒ Updating and deleting simple and complex objects

ƒ configuration of update, delete and activation depth

ƒ inconsistencies between in-memory and stored objects

ƒ Transactions

ƒ commit and rollback

ƒ concurrent transactions, collision detection and avoidance

(3)

Configuration and Tuning g g

ƒ Configuration interface

ƒ global configuration set through Db4o.configure()

ƒ current global settings are cloned when object container or object server opened

ƒ further changes of global configuration not propagated to already existing object containers and object servers

existing object containers and object servers

ƒ External tools

performance tuning

ƒ performance tuning

ƒ database diagnostics

ƒ Indexes

ƒ Indexes

ƒ optimise query evaluation

(4)

Configuration Interface g

ƒ Represented by com.db4o.config.Configuration

ƒ Methods rather than properties files

ƒ Configuration setting groups Configuration setting groups

ƒ object-related methods

ƒ file-related methodsfile related methods

ƒ reflection-related methods

ƒ communication-related methods

ƒ logging-related methods

ƒ miscellaneous configuration methods

ƒ Configuring an existing object container or object server

ƒ access settings with ExtObjectContainer#configure() or

(5)

External Tools

ƒ Defragment

ƒ removes unused fields and management information

ƒ compacts database file and provides faster access

ƒ initiated from command line or from within application

ƒ Statistics

ƒ computes and outputs statics about a database file

ƒ executed from command line or programmatically

ƒ Logger

ƒ logs all objects in a database file

ƒ logs all objects of a given class

ƒ run from command line

(6)

Indexes

ƒ Trade-off between increased query performance and decreased storage, update and delete performance

ƒ Support for B-Tree indexes on single object fields Support for B Tree indexes on single object fields

ƒ enabled or disabled using configuration interface

ƒ internal field i indexed_ is set to true or false for indexed field

ƒ index created or removed automatically when object container or object server is opened

ƒ Example

// create an index

Db4o.configure().objectClass(...).objectField(...).indexed(true);

// remove an index

Db4o.configure().objectClass(...).objectField(...).indexed(false);

(7)

Tuning for Speed g p

ƒ Heuristics to improve performance of db4o

ƒ Object loading

ƒ use appropriate activation depthpp p p

ƒ use multiple object containers

ƒ disable weak references if not required (no updates performed)

ƒ Database tests

ƒ disable detection of schema changes

ƒ disable instantiation tests of persistent classes at start-up

ƒ Query evaluation

ƒ set field indexes on most used objects to improve searches

ƒ optimise native queries

(8)

Distribution and Replication p

ƒ Local mode

ƒ standalone database

ƒ database file opened and accessed directly

ƒ one user, one process or one thread at a time

ƒ Client/Server mode

ƒ multiple clients interact with one central server

ƒ server listens for and accepts connections

ƒ clients connect to server to perform database tasks

ƒ Replication

ƒ multiple server manage redundant copies of a database

ƒ changes are replicated from master to client servers

li t d d t b d t b k t i t t

ƒ replicated databases need to be kept consistent

(9)

Client/Server Modes: Networking Mode g

Virtual Machine

db4o Client

Database Operations

Database File db4o Server

db4o Client

db4o Client

Database File

db4o Client

ƒ Client opens TCP/IP connection to server p

ƒ method Db4o.openServer(filename, port)

ƒ method Db4o.openClient(host, port, user, pass)

ƒ Client sends query, insert, update and delete instructions

to server and receives data from the server

(10)

Client/Server Modes: Embedded Mode

Virtual Machine

Database File db4o Server

Embedded Client Database Operations

Database File

ƒ Not distributed across a network

ƒ client and server run in the same virtual machine

ƒ better performance for multi-threaded applications

ƒ Server is started on port 0

ƒ Client is opened using Client is opened using ObjectServer#openClient() ObjectServer#openClient()

(11)

Client/Server Modes: Out-of-Band Signalling g g

Server Machine Client Machine

MessageRecipient processMessage MessageSender

sendMessage

Database File ObjectServer

Database Operations and Commands ObjectContainer

ƒ Basic client/server mode cannot transmit command

ƒ operations are limited to methods of ObjectContainer

ƒ Out-of-band signalling g g

ƒ interface MessageSender

ƒ interface MessageRecipient

(12)

Replication p

ƒ Database managed by redundant servers

ƒ data changes on masters or publishers

ƒ changes replicated to clients of subscribers

ƒ Several forms of replication supported

ƒ snapshot replication

ƒ transactional replication

ƒ merge replication

ƒ Replication in db4o has to be coded into application and

cannot be configured on an administrative level

(13)

Replication Modes: Snapshot Replication p p p

M t

Client Client

Master

Client Client

ƒ Snapshots of the master database replicated to client p p

ƒ state-based

ƒ periodical schedule

ƒ Support in db4o

ƒ special SODA query to detect all new and updated objectsp q y p j

(14)

Replication Modes: Transactional Replication p p

M t

Client Client

Master

Client Client

ƒ Changes are synchronised after transaction g y

ƒ operation based

ƒ changes are replicated immediately

ƒ Support in db4o

ƒ single object replication with ReplicationProcess

(15)

Replication Modes: Merge Replication p g p

M t

Client Client

Master

Client Client

ƒ Changes from client are merged to central server g g

ƒ Other clients are updated to reflect changes

ƒ Can be done either transactionally or on a periodic basis

ƒ Can be done either transactionally or on a periodic basis

ƒ Typically occurs if subscribers are occasionally offline

(16)

Core Replication p

ƒ Transfers data between peer object containers

ƒ local database files

ƒ object servers in networking or embedded mode

ƒ Requires three steps

ƒ generating unique IDs and version numbers

ƒ creating a ReplicationProcess object

ƒ replicating objects

ƒ Replication mode is dependent on implementation

ƒ Replication is bidirectional by default

ƒ replication can be configured to be unidirectional using method ReplicationProcess#setDirection()

(17)

Core Replication p

// configuration

Db4o.configure().generateUUIDs(Integer.MAX VALUE);g () g ( g _ );

Db4o.configure().generateVersionNumbers(true);

// replication process

ReplicationProcess replication = db1.ext().replicationBegin(db2, new ReplicationConflictHandler() {

public Object resolveConflict(ReplicationProcess p, Object a, Object b) {

return a;

return a;

} } );

replication.setDirection(db1, db2);

// update database and replicate (transactional replication) Author alex = new Author("Alexandre de Spindler");

db1 t( l ) db1.set(alex);

replication.replicate(alex);

replication.commit();

(18)

Core Replication p

// query for changed objects Query query = db1.query();

Q y q y q y();

query.constrain(Publication.class);

replication.whereModified(query);

ObjectSet<Publication> result = query.execute();

// replicate objects (snapshot replication) for (Publication publication: result) {

replication replicate(publication);

replication.replicate(publication);

}

replication.commit();

(19)

db4o Replication System p y

ƒ Introduced in db4o version 5.1

ƒ Replication solution separated from db4o core

ƒ bridges divide between db4o and relational databasesg

ƒ uni- or bidirectional replication

ƒ replication of relational databases based on Hibernate

ƒ Supported replication providers

ƒ db4o to db4o

ƒ db4o to Hibernate, Hibernate to db4o

ƒ Hibernate to Hibernate

ƒ Concepts similar to core replication

ƒ ReplicationSession instead of ReplicationProcess

(20)

Schema Evolution

ƒ Class definitions and inheritance structure can change

ƒ additional application requirements

ƒ software refactoring

ƒ Class definitions and hierarchy are database schema

ƒ In object-oriented database schema evolution is simpler j p as in object-relational mappings as only one data model

C C C C C C

C C C C C C

T T

T

OODBMS RDBMS

OODBMS RDBMS

(21)

Refactoring Scenarios g

ƒ Changes to interface implemented by class

ƒ supported as db4o only stores data and not implementations

ƒ Removing a field g

ƒ new objects stored in new format

ƒ additional field ignored in objects stored in old format

ƒ Adding a field

ƒ new objects stored in new format

ƒ additional field set to null in objects stored in old format

ƒ Changing the type of a field

ƒ simply stored as a new field

ƒ manual migration if old and new type incompatible

(22)

Refactoring Scenarios g

ƒ Renaming a field

ƒ old field is deleted and a new inserted

ƒ data migration through configuration interface

ƒ Renaming a class

Db4o.configure().objectClass(...).objectField(...).rename(...);

g

ƒ managed through configuration interface

Db4o.configure().objectClass(...).rename(...);

ƒ Merging fields

ƒ Splitting fields manual using a helper program

g () j ( ) ( )

ƒ Splitting fields manual using a helper program

ƒ Moving fields

(23)

Inheritance Evolution

ƒ Refactoring of inheritance structure

ƒ deleting classes from inheritance hierarchy

ƒ inserting classes into inheritance hierarchy

ƒ swap classes in inheritance hierarchy

ƒ Tools for inheritance evolution are being developed

ƒ create a type-less transfer database

ƒ switch classpath manually

(24)

Callbacks

ƒ Set of methods called in response to events (triggers)

ƒ db4o events

ƒ activate and deactivate

ƒ new, update and delete

ƒ Methods called before and after event

ƒ methods starting with can called before event

ƒ methods starting with on called after event

ƒ Methods defined by interface ObjectCallbacks

ƒ interface does not have to be implemented explicitly by p p y y persistent class to use its functionality

ƒ any number of methods can be implemented by persistent class

(25)

Callbacks

package com.db4o.ext;

public interface ObjectCallbacks { public interface ObjectCallbacks {

public boolean objectCanActivate(ObjectContainer c);

public boolean objectCanDeactivate(ObjectContainer c);

public boolean objectCanDelete(ObjectContainer c);

public boolean objectCanDelete(ObjectContainer c);

public boolean objectCanNew(ObjectContainer c);

public boolean objectCanUpdate(ObjectContainer c);

public void objectOnActivate(ObjectContainer c);

p j ( j )

public void objectOnDeactivate(ObjectContainer c);

public void objectOnDelete(ObjectContainer c);

public void objectOnNew(ObjectContainer c);

public void objectOnUpdate(ObjectContainer c);

public void objectOnUpdate(ObjectContainer c);

}

(26)

Use Cases for Callbacks

ƒ Recording or preventing updates

ƒ methods canUpdate() and onUpdate()

ƒ Setting default values after refactoring g g

ƒ get values before update using method canNew()

ƒ Checking object integrity before storing objects g j g y g j

ƒ check field values using methods canNew() and canUpdate()

ƒ Setting transient fields Setting transient fields

ƒ Restoring connected state when objects activated

ƒ display graphical elements or restore network connections

ƒ display graphical elements or restore network connections

ƒ Creating special indexes

ƒ detect if a field is queried often and create index automatically

ƒ detect if a field is queried often and create index automatically

(27)

Controlling Object Instantiation g j

ƒ No convention imposed for persistent classes by db4o

ƒ Objects are instantiated using one of three techniques

ƒ using a constructorg

ƒ bypassing the constructor

ƒ using a translator

ƒ For certain classes it is important which of these methods is used to retrieve objects j

ƒ if available, bypassing the constructor is default setting

ƒ behaviour can be configured globally or per class

ƒ for debugging, db4o can be configured to throw an exception if the objects of a class cannot be stored

(28)

Using Constructors g

ƒ db4o can use a constructor to instantiate objects

ƒ if no default public constructor is present, all available constructors are tested to create instances of a class

ƒ null or default values passed to all constructor arguments

ƒ first successfully tested constructor is used throughout session if instance of a class cannot be created the object is not stored

ƒ if instance of a class cannot be created, the object is not stored

ƒ by default, execution will continue without any message or error

ƒ Settings adjusted through configuration interface

ƒ Settings adjusted through configuration interface

// global setting

Db4o.configure().callConstructors(true) // per class setting

Db4o.configure().objectClass(...).callConstructors(true) // exceptions for debugging

// p gg g

(29)

Using Constructors g

public class Person { Date birthdate;

transient Calendar today;

bli P (D t bi thd t ) { public Person(Date birthdate) {

this.birthdate = birthdate;

// get today's date and store it in a transient field this.today = Calendar.getInstance();

}

public int getAge() {

Calendar birth = Calendar getInstance();

Calendar birth = Calendar.getInstance();

birth.setTime(this.birthdate);

// NullPointerException in the next line if constructor not called!

int years = this.today.get(Calendar.YEAR) – birth.get(Calendar.YEAR);

int diff = birth.add(Calendar.YEAR, age);

return (today.before(birth)) ? age-- : age;

}

(30)

Bypassing Constructors yp g

ƒ Constructors that cannot handle null or default values must be bypassed

ƒ db4o uses platform-specific mechanisms to bypass db4o uses platform specific mechanisms to bypass constructors

ƒ Not all environments support this feature

ƒ Not all environments support this feature

ƒ Sun Java Virtual Machine (only JRE 1.4 and above)

ƒ Microsoft NET Framework (except Compact Framework)

ƒ Microsoft .NET Framework (except Compact Framework)

ƒ Default setting if supported by current environment

ƒ Breaks classes that rely on constructors being executed

ƒ Breaks classes that rely on constructors being executed

(31)

Bypassing Constructors yp g

public class Person { Calendar birthdate;

int age;

bli P (C l d bi thd t ) { public Person(Calendar birthdate) {

this.birthdate = birthdate;

// calculate age

Calendar today = Calendar.getInstance();

// NullPointerException in next line if called with null value!

int years = today.get(Calendar.YEAR) – this.birthdate.get(Calendar.YEAR);

int diff = birth add(Calendar YEAR age);

int diff = birth.add(Calendar.YEAR, age);

this.age = (today.before(birth)) ? age-- : age;

} ...

}

(32)

Translators

ƒ Some classes cannot be cleanly reinstantiated by db4o using either method

ƒ constructor needed to populate transient membersp p

ƒ constructor fails if called with null or default values

ƒ Translators control loading and storing of such objects g g j

ƒ Interface ObjectTranslator

public Object onStore(ObjectContainer c, Object appObject);

public void onActivate(

ObjectContainer c, Object appObject, Object storedObject);

public Class storedClass();

ƒ Interface ObjectConstructor extends ObjectTranslator

public Object on Instantiate(

p j (

(33)

Translators

public class Person { String name;

String name;

Calendar birthdate;

transient int age;

public Person(String name, Calendar birthdate) { public Person(String name, Calendar birthdate) {

this.name = name;

this.birthdate = birthdate;

Calendar today = Calendar.getInstance();

( )

int years = today.get(Calendar.YEAR) – this.birthdate.get(Calendar.YEAR);

int diff = birth.add(Calendar.YEAR, age);

this.age = (today.before(birth)) ? age-- : age;g ( y ( )) g g ; }

public String getName() { ... }

public Calendar getBirthdate() { } public Calendar getBirthdate() { ... } public int getAge() { ... }

...

(34)

Translators

public class PersonTranslator implements ObjectConstructor { // map Person object to storage representation

// map Person object to storage representation

public Object onStore(ObjectContainer c, Object appObject) { Person person = (Person) appObject;

return new Object[] { person.getName(), person.getBirthdate() };

}

// reconstruct Person object from storage representation

public Object onInstantiate(ObjectContainer c, Object storedObject) {

( )

Object[] raw = (Object[]) storedObject;

return new Person((String) raw[0], (Calendar) raw[1]);

}

public void onActivate(ObjectContainer c Object appObject public void onActivate(ObjectContainer c, Object appObject,

Object storedObject) { }

// return metadata about storage representation public Class storedClass() {

public Class storedClass() { return Object[].class;

} } }

(35)

Literature

ƒ db4o Tutorial

ƒ http://www.db4o.com/about/productinformation/resources/

ƒ db4o Reference Documentation

ƒ http://developer.db4o.com/Resources/view.aspx/Reference

ƒ db4o API Reference

ƒ http://developers.db4o.com/resources/api/db4o-java/

ƒ Jim Paterson, Stefan Edlich, Henrik Hörning, and Reidar Jim Paterson, Stefan Edlich, Henrik Hörning, and Reidar

Hörning: The Definitive Guide to db4o, APress 2006

(36)

N t W k Next Week

ODMG Standard

Object Model, Object Definition Language, Object Query Language

Programming Language Bindings

Outlook

Referências

Documentos relacionados

The median values of PSAD differentiates BPH from prostate carcinoma only for the entire series of patients, or for the subset with prostates smaller than 40ml, at a PSA cut-off

object Figure object UIControl objects UIMenu objects Axes object Figure object Surface object Line objects Text objects UIControl objects UIMenu objects Axes object Figure

Serra Geral, são menos restritivos e evidenciam um maior grau de suscetibilidade nas regiões onde há o registro de deslizamentos. Figura 5.4 Comparação entre os modelos que

We found that salicylate application can generate tinnitus perception in mice, decrease exploration in an open field test and drastically increase the power of low frequency

Você sabe do que estamos falando: certamente já tentou estudar para uma prova ou prestar atenção no seu chefe, mas simplesmente não conseguiu.. A culpa para a sua mente avoada está

The drop in the DS as the OM levels in the soil increased (Figure 1a), Table 1 - Location of the 18 soils sampled under the no-tillage system.. B) Optimum compaction humidity

public E get(int index) public int indexOf(Object o) public boolean isEmpty() public Iterator&lt;E&gt; iterator() public int lastIndexOf(Object o) public Object remove(int index)

Dados de ordem de trabalho são associados a cada buraco, e eles incluem localização e tamanho do buraco, número de identificação da equipe de reparos, número de pessoas na