Native XML Databases

56
Native XML Databases Lior Schejter Erez Hadad

description

Native XML Databases. Lior Schejter Erez Hadad. Outline. Introduction: What? Why? How? An example XML DBMS: Natix Query processing Natix query engine XML DBMS Storage What is stored in an XML-DBMS? Storage Management: Natix Storage Storing documents and Indexes Transaction Management - PowerPoint PPT Presentation

Transcript of Native XML Databases

Page 1: Native XML Databases

Native XML Databases

Lior Schejter

Erez Hadad

Page 2: Native XML Databases

Outline

Introduction: What? Why? How? An example XML DBMS: Natix Query processing

Natix query engine XML DBMS Storage

What is stored in an XML-DBMS? Storage Management: Natix Storage Storing documents and Indexes

Transaction Management Logging & Recovery Locking

Page 3: Native XML Databases

Native XML DBMS – What?

A DBMS native to XML Providing programming interfaces to manage and query XML

data (1 or more documents) Using a full blown, consistent API

XML is the natural way of accessing data in this kind of DBMS Defining, Querying etc.

Providing all familiar features of DBMS Transactions Recovery Multi threading

Page 4: Native XML Databases

Native XML Databases – Why?

XML structure provides information Complex data model

Ever tried representing an organizational hierarchy in a DBMS?

Flexible data model Both structured and semi structured data

Inherent XML “behavior” DBMS will be optimized for XML Think about querying \ storing \ updating thousands of

XML documents

Page 5: Native XML Databases

Native XML Databases – How?

XML can be thought of as a: Text document (tags, simple text etc.) A data model (nodes, children, siblings)

Document centric – Storing and retrieving the entire document (or large parts of it) Fast document construction and storage Slow on queries, and retrieving data

Data centric – Expressing the entire data in an internal data structure Fast queries Slow document retrieval

A question of granularity

Page 6: Native XML Databases

Natix

Natix – An XML DBMSDeveloped at Manheim University, Germany.

Designed from scratch for storing and accessing XML data

Supports XPath, XQuery Not designed to any specific language and

\ or environment

Page 7: Native XML Databases

Natix - Architecture

Page 8: Native XML Databases

Natix - Architecture

Storage layer – Manage all persistent data storage

Service Layer – Provides DBMS functionality

Binding Layer – Modules that map data and requests from other APIs to the Natix engine interface

Page 9: Native XML Databases

Natix Query Execution Engine

Able to execute all queries in a typical XML query language (e.g. XQuery).

Expressive: Small number of powerful parameterized operators

2 main components:Natix Physical Algebra (NPA)

Algebraic operators, their composition etc.Natix Virtual Machine (NVM)

Plans used in the algebraic operators

Page 10: Native XML Databases

NQE – The Rough Guide

NPA works on sequences of tuples Each tuple holds values which can be a

number, a string or a node handleXML node handles can point to any node type

NPA operators are implemented as iterators

NPA operators take programs for the NVM as parametersUsually passed at construction

Page 11: Native XML Databases

Natix Virtual Machine

NVM commands operate on register sets An NVM command may access several register

sets Global (X) register set for global (but specific to

current plan of execution) data. Z, Y register sets for arguments (pass between

operators) Y register sets are used only for binary operators

A reference to register sets is passed between operators to avoid unnecessary copying

Page 12: Native XML Databases
Page 13: Native XML Databases

CMP_LEQ_SI4_ZCX 1 55 2

EXIT_F 2

X3 = Z1 + Z2PRINT X3

ARITH_ADD_A_SI4_ZZX 1 2 3

PRINT_SI4 3STOP

if (Z1 > 55) then exit

NVM program examples

Page 14: Native XML Databases

XML NVM Commands

NVM has about 150 XML specific commandsCopying documents handles, comparing

fragments, traversing the XML document tree, printing etc.

Mainly commands which correspond to XPath axes (child, sibling, descendant etc.)

Page 15: Native XML Databases

Example: UnnestMap operator

UnnestMapLogically: takes a set valued expression and

returns a single tuple for each element in the result set, flattening hierarchy 1 level deep

Physically: takes 3 programs and uses them as an iterator:

init – Initialize the first tuple to be returned step – compute the next tuple Fin – finalize, cleanup

Page 16: Native XML Databases

UnnestMap Operator

init:

step:

fin: no finish program

XML_CHILD_ZZ 1 2

XML_VALID_ZX 2 3

EXIT_F 3

MV_XML_ZX 2 4

XML_SIBLING_NEXT_XX 4 4

XML_VALID_XX 4 3

EXIT_F 3

MV_XML_XZ 4 2

Page 17: Native XML Databases

Natix Physical Algebra

Operators for selection and binding combination are borrowed from the relational and object databases contexts select, join, map, group etc.

The main concern: variable binding and result construction operators for XML

Page 18: Native XML Databases

NPA – Query plans

Every plan of execution has a scan operation at the bottom of it: scanning a document and retrieving its root in a tuplee.g. Expression Scan

UnnestMap and PathScan are used for variable bindings as wellAn XPath expression can be translated into a

sequence of UnnestMap operationsOr a single PathScan operation, which also eliminates

duplicates, like in XPath

Page 19: Native XML Databases

NPA Operators

e.g. SELECT

Any subplan

Page 20: Native XML Databases

NPA Examples

Example DTD:

Page 21: Native XML Databases

NPA Example 1

Query: <result>

{

FOR $c IN document(“bib.xml”)/bib/conference

WHERE $c/year > 1996

RETURN

<conference>

<title>{$c/title}</title>

<year>{$c/year}</year>

</conference>

}

</result>

Page 22: Native XML Databases

NPA Example 1

The query plan: Function

Calls

Tuple

s

Page 23: Native XML Databases

NPA Example 2

Query:<bib>

{

FOR $a IN document(“bib.xml”)//conference/article/author

RETURN

<author>

<name first={$a/@first} last={$a/@last} />

<articles>

{

FOR $b IN document(“bib.xml”)//conference/article,

$c IN $b/author

WHERE $c/@first=$a/@first AND $c/@last=$a/@last

RETURN

<article>{$b/title}</article>

}

</articles>

</author>

}

</bib>

Page 24: Native XML Databases

NPA Example 2

The query plan:

Page 25: Native XML Databases

Part II:Storage & Transactions inNative XML DBMS

Page 26: Native XML Databases

What Is Stored In A Native XML-DB? XML Documents

The data itself The DBMS tries to maintain imported documents as

close to their original form as possible Data Definition Schemas

XML schemas, RelaxNG schemas, DTDs Used for:

Validating documents Organizing data on disk Validating and optimizing queries, constructing result sets Semantics: types, operations

Page 27: Native XML Databases

What Is Stored In A Native XML-DB? Collections / Roots:

Bindings of XML documents into sets According to type or relevance A document may belong to more than one set

A collection may be related to a schemaCollections are valid instances of the data

model and can be processed through queriesfor $d in collection(“foo”)where $d/Book/Author/Lastname = “Dante”return $d

Page 28: Native XML Databases

What Is Stored In A Native XML-DB? “Standard DB” components:

Indexes Speed up query execution

Stored functions / procedures / triggers Embed business logic in storage Server-local processing – reduce network traffic Create views – abstractions of data

Access control data Users, resources, groups, permissions

Page 29: Native XML Databases

Storage Management

Usually, it is impossible to hold all the DB components in main memorySeveral orders of magnitude smaller than secondary

storage (disk) A common technique is to keep only a few fixed

objects in memory and load the rest on-demandAnalogous to virtual memory / disk caching

mechanisms of operating systems

Page 30: Native XML Databases

Storage Management In Natix

Physical Storage

Partitions

Pages

Segments

Records(Slotted Page Segments)

BufferManager

PageInterpreter

PageInterpreter

Internal DatabaseStructure

Page 31: Native XML Databases

How To Store XML Documents in a DB? Flat Stream:

Each XML document is a byte stream (e.g. a file or DBMS BLOB)

Fast handling of large sequential chunks or whole documents [document-centric]

Poor random access Requires parsing of XML

Example: Web server’s HTML file tree

Page 32: Native XML Databases

How To Store XML Documents in a DB? Meta Modeling:

Separately store each element of the data model of an XML document (e.g. in a DBMS)

Analogy to RDBMS: Entities and relations (of an ERD model) stored in separate records in tables

Fast random access [data-centric]Slow processing of whole documents out of

(possibly) thousands of separate recordsMechanism required for “translating” between

data models, e.g. XML <=> Relational

Page 33: Native XML Databases

How To Store XML Documents in a DB? Mix of FS and MM:

Redundant: Store each document both as a byte stream and as a collection of records

Read access is optimal: match the case Write access has high overhead: update both types of

storage in each operation

Hybrid: Define a “granularity threshold” A “small structure” object is stored as a flat stream inside a

single record of a database A “large structure” object is divided into several records Leverage time between whole-document and per-node

operations

Page 34: Native XML Databases

Natix XML Storage

A hybrid approach:Each database record contains a single subtree

of an XML documentA dynamic granularity threshold, adapting to size

and structure of documents at runtimeA subtree can grow and split into several recordsSmall subtrees can be merged into a larger

subtree in a single record

Page 35: Native XML Databases

Types of Stored Nodes in Natix

Aggregate nodes: inner nodes of the tree, containing their respective child nodes Helper aggregate nodes: “virtual” aggregate nodes

used for grouping subsets of children of an actual aggregate node into subtrees in records

Literal nodes: leaf nodes each containing an unparsed stream of bytes

Proxy nodes: “virtual” nodes that point to subtrees contained in other records

f1

h1

p1

f1

Page 36: Native XML Databases

Natix Storage Example

XML File:

<f1>

<f2>..</f2> <f3>..</f3> <f4> <f7/> <f8>..</f8> </f4>

<f5/> <f6>..</f6> </f1>

Logical Tree: f1

f4

f8f7

f5 f6f3f2

Page 37: Native XML Databases

Natix Storage Example

Physical Tree:

f1

f4

f8f7f3f2 f5 f6

h1 h2

p3p2p1

r1

r2 r3 r4

Page 38: Native XML Databases

Modifying Documents In Natix

The physical tree is regarded as a B-Tree of records maintained balanced

If, when inserting a subtree, a record becomes too big

Split the record into a separator part, left part and a right part

Insert the separator into the parent record The algorithm may repeat in the parent

Similarly, a delete operation can result in record merger

Page 39: Native XML Databases

Modifying Documents In Natix

f1

f6

f7

f8 f9

f11 f12

f13 f14f10f5

f2

f3 f4

S

RL

2. f7 is the Split Node

1. Add node f10 to a record containingthe following subtree:

3. Separator S is the pathfrom the root up to but notincluding f7

4. Right Forest R is inducedby the subtree of f7 and allthe descendants of S locatedright of f7

5. Left Forest L is inducedby the rest of the nodes

Page 40: Native XML Databases

Modifying Documents In Natix

f12

f13 f14

f11f10f7

f8 f9

f5f2

f3 f4

f1

f6

h2h1

p1

p1p1

p1

1. In L & R, subtrees withsibling roots are groupedusing helper aggregates

S

RL

2. Each subtree is put intoa separate new partitionrecord

4. The separator eitherreplaces the proxy in theparent or forms a new root

3. The separator connectsto the partition records through proxie nodes

r4r3r1 r2

rparent

Page 41: Native XML Databases

Indexes InNative XML-DBs Using indexes accelerate evaluation of queries by

quickly locating elements / values / text in the DB Indexes may be created

as fixed parts of the storage systems, or upon user request, or automatically due to repeated use of certain queries

Index granularity may vary: Point to each node that contains a specific key, or just to

the containing document A trade-off between size & construction speed of the

index vs. its effectiveness

Page 42: Native XML Databases

Indexes InNative XML-DBs Common types of indexes:

Value indexes: list the locations of each typed value of a node:

E.g., locations of the integer value 1492.

Element indexes: list the locations of elements in documents, preserving hierarchy

Locate an element of a specific type (//footnote) or in a specific context (/appendix/footnote)

E.g., Tamino index structure, Natix XASR

Page 43: Native XML Databases

Indexes In Native XML-DBs

Full Text Indexes: List the location of text within the content of elements

Common technique: inverted files “Location of word”: offset in file and / or in

document hierarchy Becomes more useful when the document is less

structured In the future, text index mechanism is expected to

resemble a modern search engine: Handle word equivalence (single/plural, synonyms) Ranked matching (degree of proximity instead of

true/false)

Page 44: Native XML Databases

Natix eXtended AccessSupport Relations (XASR)

bioml

organism

organelle organelle

label label

“cytoskeleton” “mitochondrion”

112

211

36

710

45

89

dmin dmax eType docID parent

1 12 bioml 0 NULL

2 11 organism 0 1

3 6 organelle 0 2

4 5 label 0 3

7 10 organelle 0 2

8 9 label 0 7

1. The document is traversed in DFS order. Each node is assigned a value dmin upon entry and dmax upon exit. dmin is also the unique node id.

2. An XASR table is constructed as following:

3. During query evaluation, path connectors (‘/’ or ‘//’) are resolved through join operations on the XASR table:The join predicate for ‘/’ is: xi.docID = xi+1.docID and xi.dmin = xi+1.parentThe join predicate for ‘//’ is: xi.docID = xi+1.docID and xi.dmin < xi+1.dmin and xi.dmax > xi+1.dmax

Page 45: Native XML Databases

Transaction Management

An XML DBMS provide support for transactions A sequence of operations on XML items that can be either

committed or rolled-back altogether Transaction execution follows ACID properties:

Atomicity: Each transaction should either complete or have no effect at all

Consistency: Each transaction should transform the DB from one consistent state to another

Isolation (serialization): Concurrently-executing transactions should behave as if they’re executing in some sequential order

Durability: Once committed, a transaction’s effect on the DB is permanent

Page 46: Native XML Databases

Transaction Management

Consistency is achieved by properly defining the transaction boundaries Example: when moving money between bank

accounts, update both accounts in one transaction Responsibility of the application programmer

The other properties are provided by the DBMS Atomicity and durability are provided through logging

and recovery Isolation is maintained through locking

Page 47: Native XML Databases

Logging And Recovery

Logging and recovery provide two important functions: undo and redoUndo of transactions that are aborted (will not

complete) Enables atomicity

Redo of committed transactions in case of DBMS failure before transaction results

were completely written to disk Enables durability

Page 48: Native XML Databases

Logging And Recovery

The XML DBMS keeps a log of all operations affecting the DB

Each transaction operation that writes to a DB item generates a log recordWrite-ahead logging: log then write

All log records of the same transaction are linked chronologically forward (redo) and backward (undo)

Page 49: Native XML Databases

Logging And Recovery In Natix

Optimizing L&R for XML hierarchies:Subsidiary Logging: log records are cached

and unified into more compact records before entering the log

One log record of adding a subtree instead of many added-single-node log records

Annihilator Undo: No need to perform undo operations that are covered by later undo operations

Skip remove-node / modify-node operations that are followed by matching remove-subtree operations

Page 50: Native XML Databases

Locking Mechanism

In order to ensure that transactions are isolated from each other, each transaction locks its DB resources before operating on them Locking a resource prevents other transactions from

modifying/accessing it until unlocked Most common locking protocol: S2PL (Strict 2-Phase

Locking) Every resource is locked before its first access All resources are atomically unlocked together with

abort/commit

Page 51: Native XML Databases

Types Of Locks

Classic Locks:S: Shared lock – for reading an item

Two transaction can share read access to an item Prevents item modification by other transactions

X: eXclusive lock – for modifying an item At most one transaction can hold X lock on an item Prevents other transactions from accessing item

S X

S + -

X - -

held lock

requestedlock

Compatibility Matrix:

Page 52: Native XML Databases

Types Of Locks

Problem - While a transaction T operates on an element in an XML document:Other transactions should be allowed to operate

on other elements of the document Increase concurrency

However, no other transaction should be allowed to modify nodes that contain the element T is working on

Maintain the tree structure

Page 53: Native XML Databases

Types Of Locks

Solution - Warning Locks:Lock all the nodes on the path from the root to the

designated node from being modified by other transactions, according to the intended operation on the designated node

IS: Intension Share – intend to read sub-elements of this node

IX: Intension eXclusive – intend to modify sub-elements of this node

Page 54: Native XML Databases

Types Of Locks

SIX: Share Intension eXclusive – intend to read this node and modify sub-elements of it

Compatibility Matrix:

S X IS IX SIX

S + - + - -

X - - - - -

IS + - + + +

IX - - + + -

SIX - - + - -

held lock

requestedlock

Page 55: Native XML Databases

Types Of Locks - Natix

What if a transaction accesses a node directly without traversing down from the root? E.g., when dereferencing an IDREF value Need to first find the root to begin locking Make sure the path to node is not modified until

locking completes In Natix, nodes are first locked in special Shared

Parent Pointer (SPP) mode on way up, then regular locks on way down Minimal lock, incompatible only with X

Page 56: Native XML Databases

Lock Control In Natix

Lock Escalation: If a transaction holds too many locks, its locks are grouped and converted to bigger locks over entire documents (or even segments), if possible If the transaction still holds too many locks, it is

aborted

Deadlock Detection: If a transaction waits too long for a lock, the system starts looking for deadlocks involving the waiting transaction. If found, the transaction is aborted