Relationen-Algebra und Persistenz Teil I Implementierungskonzepte und Anforderungen an Attributdatentypen LG Datenbanksysteme für neue Anwendungen
Inhalt Flobs DbArrays Attributsdatentypen Folie 2
Bisher: Folie 3
Bisher: Ich kann bereits einfache Daten fester Größe verwalten. Folie 4
Bisher: Ich kann bereits einfache Daten fester Größe verwalten. Mein Datentyp verwaltet aber große Datenmengen. Folie 5
Bisher: Ich kann bereits einfache Daten fester Größe verwalten. Mein Datentyp verwaltet aber große Datenmengen. Mein Datentyp hat eine veränderliche Größe! Folie 6
Bisher: Ich kann bereits einfache Daten fester Größe verwalten. Mein Datentyp verwaltet aber große Datenmengen. Mein Datentyp hat eine veränderliche Größe! Beispiele: Bilder, Videos, Webseiten Folie 7
Bisher: Ich kann bereits einfache Daten fester Größe verwalten. Mein Datentyp verwaltet aber große Datenmengen. Mein Datentyp hat eine veränderliche Größe! Beispiele: Bilder, Videos, Webseiten Wie speichere ich solche Daten persistent und effizient? Folie 8
Flob Bisher: Ich kann bereits einfache Daten fester Größe verwalten. Mein Datentyp verwaltet aber große Datenmengen. Mein Datentyp hat eine veränderliche Größe! Beispiele: Bilder, Videos, Webseiten Wie speichere ich solche Daten persistent und effizient? Folie 9
Flob Bisher: Ich kann bereits einfache Daten fester Größe verwalten. Mein Datentyp verwaltet aber große Datenmengen. Mein Datentyp hat eine veränderliche Größe! Beispiele: Bilder, Videos, Webseiten Wie speichere ich solche Daten persistent und effizient? Flob = Faked Large OBject Folie 10
Flob Folie 11
Flob Datenstruktur zur Speicherung beliebiger Datenmengen Folie 12
Flob Datenstruktur zur Speicherung beliebiger Datenmengen Aber: Das Objekt muss insgesamt im Hauptspeicher darstellbar sein! (Zumindest für einige Operationen) Folie 13
Flob Datenstruktur zur Speicherung beliebiger Datenmengen Eingebauter Persistenzmechanismus Folie 14
Flob Datenstruktur zur Speicherung beliebiger Datenmengen Eingebauter Persistenzmechanismus Verwaltet unstrukturierte Speicherblöcke (wie malloc) Folie 15
Flob Datenstruktur zur Speicherung beliebiger Datenmengen Eingebauter Persistenzmechanismus Verwaltet unstrukturierte Speicherblöcke (wie malloc) Zugriff mittels spezieller Funktionen und eines offsets Folie 16
Flob Datenstruktur zur Speicherung beliebiger Datenmengen Eingebauter Persistenzmechanismus Verwaltet unstrukturierte Speicherblöcke (wie malloc) Zugriff mittels spezieller Funktionen und eines offsets Folie 17
Flob Datenstruktur zur Speicherung beliebiger Datenmengen Eingebauter Persistenzmechanismus Verwaltet unstrukturierte Speicherblöcke (wie malloc) Zugriff mittels spezieller Funktionen und eines offsets class Flob { Flob( const SmiSize size ); size_t Size() const; bool write(const char* buffer, const SmiSize length, const SmiSize offset); bool read(char* buffer, const SmiSize length, const SmiSize offset); void resize( size_t size ); void clean(); void destroy(); } Folie 18
Flob Achtung! Datenstruktur zur Speicherung beliebiger Datenmengen Eingebauter Flobs werden Persistenzmechanismus direkt en bloc auf die Platte geschrieben. Flobs Verwaltet werden unstrukturierte an irgendeine Stelle Speicherblöcke in den Speicher (wie eingelesen. malloc) Zugriff Verwenden mittels Sie spezieller keine Zeiger Funktionen in Flobs, und eines sondern offsets offsets! class Flob { Flob( const SmiSize size ); size_t Size() const; bool write(const char* buffer, const SmiSize length, const SmiSize offset); bool read(char* buffer, const SmiSize length, const SmiSize offset); void resize( size_t size ); void clean(); void destroy(); } Folie 19
Flob Problem gelöst! Folie 20
Flob Problem gelöst? Folie 21
Flob Problem gelöst? Flobs sind mir zu unstrukturiert. Folie 22
Flob Problem gelöst? Flobs sind mir zu unstrukturiert. Ich bräuchte aber eher etwas, um strukturierte Daten persistent speichern zu können. Folie 23
Flob Problem gelöst? Flobs sind mir zu unstrukturiert. Ich bräuchte aber eher etwas, um strukturierte Daten persistent speichern zu können. Ich will variabel viele Einträge fester Größe verwalten. Folie 24
Flob Problem gelöst? Flobs sind mir zu unstrukturiert. Ich bräuchte aber eher etwas, um strukturierte Daten persistent speichern zu können. Ich will variabel viele Einträge fester Größe verwalten. Eigentlich hätte ich gerne ein dynamisches array. Folie 25
Flob Problem gelöst? Flobs sind mir zu unstrukturiert. Ich bräuchte aber eher etwas, um strukturierte Daten persistent speichern zu können. Ich will variabel viele Einträge fester Größe verwalten. Eigentlich hätte ich gerne ein dynamisches array.??? Folie 26
DbArray Problem gelöst? Flobs sind mir zu unstrukturiert. Ich bräuchte aber eher etwas, um strukturierte Daten persistent speichern zu können. Ich will variabel viele Einträge fester Größe verwalten. Eigentlich hätte ich gerne ein dynamisches array. Folie 27
DbArray Container für kompakte Datentypen fester Länge. Folie 28
DbArray Container für kompakte Datentypen fester Länge. Elementtyp muss einfach sein: Keine echten Zeiger! Keine Flobs oder DbArrays als Elemente! Folie 29
DbArray Container für kompakte Datentypen fester Länge. Elementtyp muss einfach sein: Keine echten Zeiger! Keine Flobs oder DbArrays als Elemente! DbArray ist eine Template-Klasse, die von Flob abgeleitet ist. Ein- und Auslagerung von Datenteilen muss daher (wie beim Flob) nicht selbst programmiert werden! Folie 30
DbArray Container für kompakte Datentypen fester Länge. Elementtyp muss einfach sein: Keine echten Zeiger! Keine Flobs oder DbArrays als Elemente! DbArray ist eine Template-Klasse, die von Flob abgeleitet ist. Ein- und Auslagerung von Datenteilen muss daher (wie beim Flob) nicht selbst programmiert werden! Wird ein DbArray sortiert abgespeichert, so ist eine effiziente externe binäre Suche möglich. Folie 31
DbArray template<class DbArrayElement> class DbArray : public Flob { DbArray( const int n ); int Size() const; void resize( const int newsize ); void clean(); void Put( const int index, const DbArrayElement& elem ); void Append( const DbArrayElement& elem ); void Get( const int index, DbArrayElement &elem ) const; void TrimToSize(); void Sort( int (*cmp)( const void *a, const void *b) ); bool Find( const void *key, int (*cmp)( const void *a, const void *b), int& result ) const void Destroy(); [...] } Folie 32
Bisher: Einfache Datentypen. Was ging damit? Folie 33
Bisher: Einfache Datentypen. Was ging damit? Erzeugen von Objekten Anwenden von Operatoren Speichern in Datenbank, Löschen aus Datenbank Umwandlung von/zu Nested List Folie 34
Bisher: Einfache Datentypen. Was ging damit? Erzeugen von Objekten Anwenden von Operatoren Speichern in Datenbank, Löschen aus Datenbank Umwandlung von/zu Nested List Was fehlt bislang? Folie 35
Bisher: Einfache Datentypen. Was ging damit? Erzeugen von Objekten Anwenden von Operatoren Speichern in Datenbank, Löschen aus Datenbank Umwandlung von/zu Nested List Was fehlt bislang? Verwendung als Attribut in Relationen Folie 36
Attributdatentypen Bisher: Einfache Datentypen. Was ging damit? Erzeugen von Objekten Anwenden von Operatoren Speichern in Datenbank, Löschen aus Datenbank Umwandlung von/zu Nested List Was fehlt bislang? Verwendung als Attribut in Relationen Folie 37
Attributdatentypen Können in Relationen als Attribute verwendet werden. Attributdatentyp: Oberklasse Attribute, Kind DATA. Erfordernisse: Folie 38
Attributdatentypen Können in Relationen als Attribute verwendet werden. Attributdatentyp: Oberklasse Attribute, Kind DATA. Erfordernisse: Sicherstellen einer Mindest-Funktionalität (Compare, IsDefined, SetDefined, Adjacent, Print,...) Folie 39
Attributdatentypen Können in Relationen als Attribute verwendet werden. Attributdatentyp: Oberklasse Attribute, Kind DATA. Erfordernisse: Sicherstellen einer Mindest-Funktionalität (Compare, IsDefined, SetDefined, Adjacent, Print,...) Support für den automatischen Persistenzmechanismus von Tupeln (C++ kennt keine Serialisierung!) (NumOfFLOBs, GetFLOB, SizeOf, Open, Save) Folie 40
Attributdatentypen Können in Relationen als Attribute verwendet werden. Attributdatentyp: Oberklasse Attribute, Kind DATA. Erfordernisse: Sicherstellen einer Mindest-Funktionalität (Compare, IsDefined, SetDefined, Adjacent, Print,...) Support für den automatischen Persistenzmechanismus von Tupeln (C++ kennt keine Serialisierung!) (NumOfFLOBs, GetFLOB, SizeOf, Open, Save) Verwenden keine dynamischen Objekte/Zeiger! Folie 41
Attributdatentypen Können in Relationen als Attribute verwendet werden. Attributdatentyp: Oberklasse Attribute, Kind DATA. Erfordernisse: Beispiel Sicherstellen einer Mindest-Funktionalität (Compare, IsDefined, SetDefined, Adjacent, Print,...) Der Support Datentyp für Polygon den automatischen aus der Polygon Persistenzmechanismus Algebra stellt einen von Datentyp Tupeln dar, (C++ derkennt keine Serialisierung!) (NumOfFLOBs, als Attribut in Relationen verwendet werden kann und GetFLOB, SizeOf, Open, Save) den Speicherverwaltungstyp DbArray verwendet: Verwenden keine dynamischen Objekte/Zeiger Folie 42
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 43
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Klasse Polygon wird von Klasse Attribute abgeleitet Folie 44
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const Wichtig: int *X = 0, Bei Mehrfachvererbung darf keine const Klasse int mehrfach *Y = 0 von ); ~Polygon(); derselben Klasse (z.b. Attribute) erben. Sonst: Probleme bei der Typkonvertierung (cast). int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Klasse Polygon wird von Klasse Attribute abgeleitet Folie 45
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 46
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Standard-Konstruktor Wird vom Persistenzmechanismus verwendet. Darf das Objekt nicht verändern. Folie 47
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, Wichtig: const int *X = 0, Vermeiden Sie die Nutzung des Standard-Konstruktors. const int *Y = 0 ); ~Polygon(); Diese führt häufig wegen uninitialisierter Werte zu Fehlern. Initialisieren Sie Objekte mittels anderer Konstruktoren. int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Standard-Konstruktor Wird vom Persistenzmechanismus verwendet. Darf das Objekt nicht verändern. Folie 48
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 49
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Nicht-Standard-Konstruktor Mindestens einer muss implementiert werden (z.b. mit einem bool Parameter zum Setzen von defined). Folie 50
Attributdatentyp: Polygon Wichtig: In der Implentierung von Nicht-Standard-Konstruktoren muss zuerst ein Nicht-Standardkonstruktor der direkten Oberklasse als Initialisierer class Polygon : public Attribute { public: aufgerufen Polygon() werden. {} Beispiel: Polygon( const int n, const int *X = 0, Polygon( const int n, const const int *X, int const *Y = 0 int ); *Y) ~Polygon(); : Attribute(true) {... } int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Nicht-Standard-Konstruktor Mindestens einer muss implementiert werden (z.b. mit einem bool Parameter zum Setzen von defined). Folie 51
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); }; int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; Einziges Flob Objekt in Polygon Folie 52
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 53
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 54
Attributdatentyp: Polygon int class Polygon : public Attribute { public: Polygon() {} Anzahl der Flobs zurückgeben: Polygon::NumOfFLOBs() const { return 1; } Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 55
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 56
Attributdatentyp: Polygon Den i-ten Flob zurückgeben: Flob *Polygon::GetFLOB(const int i) class Polygon : public Attribute { { public: assert( i >= 0 && Polygon() {} i < NumOfFLOBs() ); Polygon( const int return n, const &vertices; int *X = 0, } const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 57
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 58
Attributdatentyp: Polygon int Compare(const Attribute* rhs) const; Falls beide Objekte definiert: class Polygon : public Attribute { *this < *rhs 1 public: *this = *rhs 0 Polygon() {} *this > *rhs 1 Polygon( Sonst: const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); undef x def 1: *this < *rhs undef x undef 0: *this = *rhs def x undef 1: *this > *rhs int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 59
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, const int *Y = 0 ); ~Polygon(); int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 60
Attributdatentyp: Polygon class Polygon : public Attribute { public: Weitere zu überschreibende Funktionen: bool Adjacent(const Attribute*) const; Polygon *Clone() const; size_t Sizeof() const; void CopyFrom(const Attribute* right); size_t HashValue() const; Polygon() {} Polygon( const int n, const int *X = 0, Ererbte Funktionen: const int *Y = 0 ); ~Polygon(); bool IsDefined() const; NICHT überschreiben! void SetDefined( bool defined ); Bei Überschreiben Attribute::SetDefined(...) int NumOfFLOBs() const; Flob *GetFLOB(const aufrufen! int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 61
Attributdatentyp: Polygon class Polygon : public Attribute { public: Polygon() {} Polygon( const int n, const int *X = 0, Wichtig: const int *Y = 0 ); ~Polygon(); Überschreibt man diese Funktionen nicht, z.b. weil man sich im Namen mit Groß- oder Kleinschreibung vertan hat, führt dies mitunter zu Speicherfehlern! int NumOfFLOBs() const; Flob *GetFLOB(const int i); int Compare(const Attribute*) const; [...] private: DbArray<Vertex> vertices; PolygonState state; }; Folie 62
Noch Fragen?! Folie 63