org.basex.data
Class Data

java.lang.Object
  extended by org.basex.data.Data
Direct Known Subclasses:
DiskData, MemData

public abstract class Data
extends java.lang.Object

This class represents a database instance. It provides low-level access to all properties and values stored in a single database. An XML node is accessed by its pre value, which is not stored itself, but given by the table position. The following restrictions are imposed on the data:

Each node occupies 128 bits. The current storage layout looks as follows:
 COMMON ATTRIBUTES:
 - Byte     0:  KIND: Node kind (bits: 2-0)
 - Byte 12-15:  UNID: Unique Node ID
 DOCUMENT NODES (kind = 0):
 - Byte  3- 7:  TEXT: Text reference
 - Byte  8-11:  SIZE: Number of descendants
 ELEMENT NODES (kind = 1):
 - Byte     0:  ATTS: Number of attributes (bits: 7-3).
                      Calculated in real-time, if bit range is too small
 - Byte  1- 2:  NAME: Namespace Flag (bit: 15), Name (bits: 14-0)
 - Byte     3:  NURI: Namespace URI
 - Byte  4- 7:  DIST: Distance to parent node
 - Byte  8-11:  SIZE: Number of descendants
 TEXT, COMMENT, PI NODES (kind = 2, 4, 5):
 - Byte  3- 7:  TEXT: Text reference
 - Byte  8-11:  DIST: Distance to parent node
 ATTRIBUTE NODES (kind = 3):
 - Byte     0:  DIST: Distance to parent node (bits: 7-3)
                      Calculated in real-time, if bit range is too small
 - Byte  1- 2:  NAME: Namespace Flag (bit: 15), Name (bits: 14-0)
 - Byte  3- 7:  TEXT: Attribute value reference
 - Byte    11:  NURI: Namespace (bits: 7-3)
 
As all methods of this class are optimized for performance, no checks are performed on the arguments (e.g.: if the string value of a text node is requested, the specified pre value must point to a text node). NOTE: the class is not thread-safe. It is imperative that all read/write accesses are synchronized over a single context's read/write lock.

Author:
BaseX Team 2005-12, BSD License, Christian Gruen

Field Summary
 Names atnindex
          Attribute name index.
static byte ATTR
          Node kind: attribute (code: 3).
 Index atvindex
          Attribute value index.
 boolean cache
          States if distance caching is active.
static byte COMM
          Node kind: comment (code: 4).
static byte DOC
          Node kind: document (code: 0).
static byte ELEM
          Node kind: element (code: 1).
 Index ftxindex
          Full-text index instance.
 MetaData meta
          Meta data.
 Namespaces nspaces
          Namespace index.
 PathSummary paths
          Path summary index.
static byte PI
          Node kind: processing instruction (code: 5).
 int pins
          Number of current database users.
 Resources resources
          Resource index.
 Names tagindex
          Tag index.
static byte TEXT
          Node kind: text (code: 2).
 Index txtindex
          Text index.
 
Constructor Summary
Data()
           
 
Method Summary
 byte[] atom(int pre)
          Returns an atomized content for any node kind.
 void attr(int pre, int dist, int name, byte[] value, int uri, boolean ne)
          Adds an attribute entry to the internal update buffer.
 int attSize(int pre, int kind)
          Returns a number of attributes.
 byte[] attValue(int att, int pre)
          Finds the specified attribute and returns its value.
abstract  void close()
          Closes the database.
abstract  void closeIndex(IndexType type)
          Closes the specified index.
 int count(IndexToken token)
          Returns the number of indexed pre references for the specified token.
 void delete(int pre)
          Deletes a node and its descendants.
protected abstract  void delete(int pre, boolean text)
          Deletes the specified text entry.
 int dist(int pre, int kind)
          Returns the distance of the specified node.
 void dist(int pre, int kind, int value)
          Sets the distance.
 void doc(int pre, int size, byte[] value)
          Adds a document entry to the internal update buffer.
 void elem(int dist, int name, int asize, int size, int uri, boolean ne)
          Adds an element entry to the internal update buffer.
abstract  void finishUpdate()
          Finishes an update operation: removes the update file and the exclusive lock.
 int id(int pre)
          Returns a unique node id.
protected abstract  long index(int pre, int id, byte[] value, int kind)
          Indexes a text and returns the reference.
protected abstract  void indexDelete(int pre, int size)
          Delete a node and its descendants from the corresponding indexes.
 byte[] info(IndexType type)
          Returns info on the specified index structure.
 boolean inMemory()
          Indicates if this data instance is in main memory or on disk.
 void insert(int pre)
          Inserts the internal buffer to the storage without updating the table structure.
 void insert(int tpre, int tpar, DataClip source)
          Inserts a data instance at the specified pre value.
 void insertAttr(int pre, int par, DataClip source)
          Inserts attributes.
 IndexIterator iter(IndexToken token)
          Returns the indexed pre references for the specified token.
 int kind(int pre)
          Returns the node kind, which may be DOC, ELEM, TEXT, ATTR, COMM or PI.
 int name(int pre)
          Returns a reference to the tag or attribute name id.
 byte[] name(int pre, int kind)
          Returns a tag, attribute or pi name.
 Atts ns(int pre)
          Returns all namespace keys and values.
 boolean nsFlag(int pre)
          Returns the namespace flag of the addressed element.
 void nsFlag(int pre, boolean ne)
          Sets the namespace flag.
 int parent(int pre, int kind)
          Returns a pre value of the parent node.
 int pre(int id)
          Returns a pre value.
 int[] pre(int[] ids, int off, int len)
          Returns pre values.
 void replace(int tpre, DataClip source)
          Replaces parts of the database with the specified data instance.
abstract  void setIndex(IndexType type, Index index)
          Assigns the specified index.
 int size(int pre, int kind)
          Returns a size value (number of descendant table entries).
 void size(int pre, int kind, int value)
          Sets the size value.
abstract  boolean startUpdate()
          Starts an update operation: writes a file to disk to indicate that an update is going on, and exclusively locks the table.
abstract  byte[] text(int pre, boolean text)
          Returns a text (text, comment, pi) or attribute value.
 void text(int pre, int dist, byte[] value, int kind)
          Adds a text entry to the internal update buffer.
abstract  double textDbl(int pre, boolean text)
          Returns a text (text, comment, pi) or attribute value as double value.
abstract  long textItr(int pre, boolean text)
          Returns a text (text, comment, pi) or attribute value as integer value.
abstract  int textLen(int pre, boolean text)
          Returns the byte length of a text (text, comment, pi).
 java.lang.String toString()
           
 void update(int pre, int kind, byte[] value)
          Updates (replaces) the value of a single document, text, comment, pi or attribute node.
 void update(int pre, int kind, byte[] name, byte[] uri)
          Updates (renames) an element, attribute or pi name.
protected abstract  void updateText(int pre, byte[] value, int kind)
          Updates the specified text or attribute value.
 int uri(int pre, int kind)
          Returns a reference to the namespace of the addressed element or attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DOC

public static final byte DOC
Node kind: document (code: 0).

See Also:
Constant Field Values

ELEM

public static final byte ELEM
Node kind: element (code: 1).

See Also:
Constant Field Values

TEXT

public static final byte TEXT
Node kind: text (code: 2).

See Also:
Constant Field Values

ATTR

public static final byte ATTR
Node kind: attribute (code: 3).

See Also:
Constant Field Values

COMM

public static final byte COMM
Node kind: comment (code: 4).

See Also:
Constant Field Values

PI

public static final byte PI
Node kind: processing instruction (code: 5).

See Also:
Constant Field Values

resources

public final Resources resources
Resource index.


meta

public MetaData meta
Meta data.


tagindex

public Names tagindex
Tag index.


atnindex

public Names atnindex
Attribute name index.


nspaces

public Namespaces nspaces
Namespace index.


paths

public PathSummary paths
Path summary index.


txtindex

public Index txtindex
Text index.


atvindex

public Index atvindex
Attribute value index.


ftxindex

public Index ftxindex
Full-text index instance.


pins

public int pins
Number of current database users.


cache

public boolean cache
States if distance caching is active.

Constructor Detail

Data

public Data()
Method Detail

close

public abstract void close()
Closes the database.


closeIndex

public abstract void closeIndex(IndexType type)
Closes the specified index.

Parameters:
type - index to be closed

setIndex

public abstract void setIndex(IndexType type,
                              Index index)
Assigns the specified index.

Parameters:
type - index to be opened
index - index instance

startUpdate

public abstract boolean startUpdate()
Starts an update operation: writes a file to disk to indicate that an update is going on, and exclusively locks the table.

Returns:
success flag

finishUpdate

public abstract void finishUpdate()
Finishes an update operation: removes the update file and the exclusive lock.


iter

public final IndexIterator iter(IndexToken token)
Returns the indexed pre references for the specified token.

Parameters:
token - index token reference
Returns:
array of sorted pre values

count

public final int count(IndexToken token)
Returns the number of indexed pre references for the specified token.

Parameters:
token - text to be found
Returns:
number of hits

info

public final byte[] info(IndexType type)
Returns info on the specified index structure.

Parameters:
type - index type
Returns:
info

atom

public final byte[] atom(int pre)
Returns an atomized content for any node kind. The atomized value can be an attribute value or XML content.

Parameters:
pre - pre value
Returns:
atomized value

pre

public final int pre(int id)
Returns a pre value.

Parameters:
id - unique node id
Returns:
pre value or -1 if id was not found

pre

public final int[] pre(int[] ids,
                       int off,
                       int len)
Returns pre values.

Parameters:
ids - unique node ids
off - start offset
len - number of ids
Returns:
sorted pre values

id

public final int id(int pre)
Returns a unique node id.

Parameters:
pre - pre value
Returns:
node id

kind

public final int kind(int pre)
Returns the node kind, which may be DOC, ELEM, TEXT, ATTR, COMM or PI.

Parameters:
pre - pre value
Returns:
node kind

parent

public final int parent(int pre,
                        int kind)
Returns a pre value of the parent node.

Parameters:
pre - pre value
kind - node kind
Returns:
pre value of the parent node

dist

public int dist(int pre,
                int kind)
Returns the distance of the specified node.

Parameters:
pre - pre value
kind - node kind
Returns:
distance

size

public final int size(int pre,
                      int kind)
Returns a size value (number of descendant table entries).

Parameters:
pre - pre value
kind - node kind
Returns:
size value

attSize

public final int attSize(int pre,
                         int kind)
Returns a number of attributes.

Parameters:
pre - pre value
kind - node kind
Returns:
number of attributes

attValue

public final byte[] attValue(int att,
                             int pre)
Finds the specified attribute and returns its value.

Parameters:
att - the attribute id of the attribute to be found
pre - pre value
Returns:
attribute value

name

public final int name(int pre)
Returns a reference to the tag or attribute name id.

Parameters:
pre - pre value
Returns:
token reference

name

public final byte[] name(int pre,
                         int kind)
Returns a tag, attribute or pi name.

Parameters:
pre - pre value
kind - node kind
Returns:
name reference

uri

public final int uri(int pre,
                     int kind)
Returns a reference to the namespace of the addressed element or attribute.

Parameters:
pre - pre value
kind - node kind
Returns:
namespace URI

nsFlag

public final boolean nsFlag(int pre)
Returns the namespace flag of the addressed element.

Parameters:
pre - pre value
Returns:
namespace flag

ns

public final Atts ns(int pre)
Returns all namespace keys and values. Should be only called for element nodes.

Parameters:
pre - pre value
Returns:
key and value ids

text

public abstract byte[] text(int pre,
                            boolean text)
Returns a text (text, comment, pi) or attribute value.

Parameters:
pre - pre value
text - text/attribute flag
Returns:
atomized value

textItr

public abstract long textItr(int pre,
                             boolean text)
Returns a text (text, comment, pi) or attribute value as integer value.

Parameters:
pre - pre value
text - text/attribute flag
Returns:
numeric value

textDbl

public abstract double textDbl(int pre,
                               boolean text)
Returns a text (text, comment, pi) or attribute value as double value.

Parameters:
pre - pre value
text - text/attribute flag
Returns:
numeric value

textLen

public abstract int textLen(int pre,
                            boolean text)
Returns the byte length of a text (text, comment, pi).

Parameters:
pre - pre value
text - text/attribute flag
Returns:
length

update

public final void update(int pre,
                         int kind,
                         byte[] name,
                         byte[] uri)
Updates (renames) an element, attribute or pi name.

Parameters:
pre - pre value
kind - node kind
name - new tag, attribute or pi name
uri - uri

update

public final void update(int pre,
                         int kind,
                         byte[] value)
Updates (replaces) the value of a single document, text, comment, pi or attribute node.

Parameters:
pre - pre value to be replaced
kind - node kind
value - value to be updated (tag name, text, comment, pi)

replace

public final void replace(int tpre,
                          DataClip source)
Replaces parts of the database with the specified data instance.

Parameters:
tpre - pre value of target node to be replaced
source - clip with source data

delete

public final void delete(int pre)
Deletes a node and its descendants.

Parameters:
pre - pre value of the node to delete

insertAttr

public final void insertAttr(int pre,
                             int par,
                             DataClip source)
Inserts attributes.

Parameters:
pre - pre value
par - parent of node
source - clip with source data

insert

public final void insert(int tpre,
                         int tpar,
                         DataClip source)
Inserts a data instance at the specified pre value. Note that the specified data instance must differ from this instance.

Parameters:
tpre - target pre value
tpar - target parent pre value of node (-1 if document is added)
source - clip with source data

size

public final void size(int pre,
                       int kind,
                       int value)
Sets the size value.

Parameters:
pre - pre reference
kind - node kind
value - value to be stored

updateText

protected abstract void updateText(int pre,
                                   byte[] value,
                                   int kind)
Updates the specified text or attribute value.

Parameters:
pre - pre value
value - content
kind - node kind

dist

public void dist(int pre,
                 int kind,
                 int value)
Sets the distance.

Parameters:
pre - pre value
kind - node kind
value - value

nsFlag

public final void nsFlag(int pre,
                         boolean ne)
Sets the namespace flag. Should be only called for element nodes.

Parameters:
pre - pre value
ne - namespace flag

insert

public final void insert(int pre)
Inserts the internal buffer to the storage without updating the table structure.

Parameters:
pre - insert position

delete

protected abstract void delete(int pre,
                               boolean text)
Deletes the specified text entry.

Parameters:
pre - pre value
text - text (text, comment or pi) or attribute flag

doc

public final void doc(int pre,
                      int size,
                      byte[] value)
Adds a document entry to the internal update buffer.

Parameters:
pre - pre value
size - node size
value - document name

elem

public final void elem(int dist,
                       int name,
                       int asize,
                       int size,
                       int uri,
                       boolean ne)
Adds an element entry to the internal update buffer.

Parameters:
dist - parent distance
name - tag name index
asize - number of attributes
size - node size
uri - namespace uri reference
ne - namespace flag

text

public final void text(int pre,
                       int dist,
                       byte[] value,
                       int kind)
Adds a text entry to the internal update buffer.

Parameters:
pre - pre value
dist - parent distance
value - string value
kind - node kind

attr

public final void attr(int pre,
                       int dist,
                       int name,
                       byte[] value,
                       int uri,
                       boolean ne)
Adds an attribute entry to the internal update buffer.

Parameters:
pre - pre value
dist - parent distance
name - attribute name
value - attribute value
uri - namespace uri reference
ne - namespace flag (only true if this is a stand-alone attribute)

index

protected abstract long index(int pre,
                              int id,
                              byte[] value,
                              int kind)
Indexes a text and returns the reference.

Parameters:
pre - pre value
id - id value
value - text to be indexed
kind - node kind
Returns:
reference

indexDelete

protected abstract void indexDelete(int pre,
                                    int size)
Delete a node and its descendants from the corresponding indexes.

Parameters:
pre - pre value of the node to delete
size - number of descendants

inMemory

public final boolean inMemory()
Indicates if this data instance is in main memory or on disk.

Returns:
result of check

toString

public final java.lang.String toString()
Overrides:
toString in class java.lang.Object