Class Data

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

public abstract class Data extends 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:
  • The table is limited to 2^31 entries (pre values are signed int's)
  • A maximum of 2^15 different element and attribute names is allowed
  • A maximum of 2^8 different namespaces is allowed
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-21, BSD License, Christian Gruen
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte
    Node kind: attribute (code: 3).
    Attribute value index.
    Attribute names.
    protected boolean
    Closed flag.
    static final byte
    Node kind: comment (code: 4).
    final int
    Unique id.
    static final byte
    Node kind: document (code: 0).
    static final byte
    Node kind: element (code: 1).
    Element names.
    Full-text index.
    ID-PRE mapping.
    final MetaData
    Meta data.
    Namespace index.
    Path index.
    static final byte
    Node kind: processing instruction (code: 5).
    final Resources
    Resource index.
    protected TableAccess
    Table access file.
    static final byte
    Node kind: text (code: 2).
    Text index.
    Token index.
    boolean
    Indicates if distances are to be updated.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Data(MetaData meta)
    Default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    final byte[]
    atom(int pre)
    Returns an atomized content for any node kind.
    final void
    attr(int dist, int nameId, byte[] value, int uriId)
    Adds an attribute to the internal update buffer.
    final int
    attSize(int pre, int kind)
    Returns a number of attributes.
    final byte[]
    attValue(int att, int pre)
    Finds the specified attribute and returns its value.
    void
    Closes the database.
    final boolean
    Indicates if the database has been closed.
    Returns a cost estimation for searching the specified token.
    abstract void
    Drops the specified index.
    byte[]
    Returns the common default namespace of all documents of the database.
    final void
    delete(int pre)
    Deletes a node and its descendants.
    protected abstract void
    delete(int pre, boolean text)
    Deletes the specified text entry.
    final int
    dist(int pre, int kind)
    Returns the distance of the specified node.
    final void
    dist(int pre, int kind, int value)
    Sets the distance.
    final void
    doc(int size, byte[] value)
    Adds a document to the internal update buffer.
    abstract void
    Drops the specified index.
    final void
    elem(int dist, int nameId, int asize, int size, int uriId, boolean nsFlag)
    Adds an element to the internal update buffer.
    abstract void
    Finishes an update operation: removes the update file and the exclusive lock.
    abstract void
    flush(boolean all)
    Flushes updated data.
    final int
    id(int pre)
    Returns a unique node id.
    final void
    id(int pre, int value)
    Sets the node id.
    final Index
    Returns an index for the specified index type.
    protected final void
    indexAdd(int pre, int id, int size, DataClip clip)
    Inserts new entries in the index structure.
    protected final void
    indexDelete(int pre, int id, int size)
    Deletes entries from the index structures.
    final byte[]
    info(IndexType type, MainOptions options)
    Returns info on the specified index structure.
    abstract boolean
    Indicates if this data instance is in main memory or on disk.
    final void
    insert(int pre)
    Inserts the internal buffer to the storage without updating the table structure.
    final void
    insert(int pre, int par, DataClip source)
    Inserts a data instance at the specified pre value.
    final void
    insertAttr(int pre, int par, DataClip source)
    Inserts standalone attributes (without root element).
    iter(IndexSearch search)
    Returns an index iterator for the specified token.
    final int
    kind(int pre)
    Returns the node kind, which may be DOC, ELEM, TEXT, ATTR, COMM or PI.
    final byte[]
    name(int pre, int kind)
    Returns the name of an element, attribute or processing instruction.
    final int
    nameId(int pre)
    Returns the id of the name of an element, attribute or processing instruction.
    final Atts
    namespaces(int pre)
    Returns all namespace prefixes and uris that are defined for the specified pre value.
    final boolean
    nsFlag(int pre)
    Returns the namespace flag of the addressed element.
    final void
    nsFlag(int pre, boolean nsFlag)
    Sets the namespace flag.
    final int
    parent(int pre, int kind)
    Returns a pre value of the parent node.
    final int
    pre(int id)
    Returns a pre value for the specified id.
    final byte[][]
    qname(int pre, int kind)
    Returns the name and namespace uri of the addressed element or attribute.
    final void
    replace(int pre, DataClip source)
    Rapid Replace implementation.
    final int
    size(int pre, int kind)
    Returns a size value (number of descendant table entries).
    final void
    size(int pre, int kind, int value)
    Sets the size value.
    abstract void
    Starts an update operation: writes a file to disk to indicate that an update is going on, and exclusively locks the table file.
    abstract byte[]
    text(int pre, boolean text)
    Returns a text (text, comment, pi, document) or attribute value.
    final void
    text(int dist, byte[] value, int kind)
    Adds a text, comment or processing instruction to the internal update buffer.
    abstract double
    textDbl(int pre, boolean text)
    Returns a text (text, comment, pi, document) or attribute value as double value.
    abstract long
    textItr(int pre, boolean text)
    Returns a text (text, comment, pi, document) or attribute value as integer value.
    abstract int
    textLen(int pre, boolean text)
    Returns the byte length of a (possibly compressed) text (text, comment, pi, document).
    protected abstract long
    textRef(byte[] value, boolean text)
    Generates a reference for a text (text, comment, pi, pi, document) or attribute value.
    final long
    textRef(int pre)
    Returns the reference to a text (text, comment, pi, pi, document) or attribute value.
    protected final void
    textRef(int pre, long off)
    Sets the reference to a text/attribute value.
    final String
     
    final void
    update(int pre, int kind, byte[] value)
    Updates (replaces) the value of a single text, comment, pi, attribute or document node.
    final void
    update(int pre, int kind, byte[] name, byte[] uri)
    Updates (renames) the name of an element, attribute or processing instruction.
    protected abstract void
    updateText(int pre, byte[] value, int kind)
    Updates the specified text or attribute value.
    final int
    uriId(int pre, int kind)
    Returns the id of the namespace uri of the addressed element or attribute.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • DOC

      public static final byte DOC
      Node kind: document (code: 0).
      See Also:
    • ELEM

      public static final byte ELEM
      Node kind: element (code: 1).
      See Also:
    • TEXT

      public static final byte TEXT
      Node kind: text (code: 2).
      See Also:
    • ATTR

      public static final byte ATTR
      Node kind: attribute (code: 3).
      See Also:
    • COMM

      public static final byte COMM
      Node kind: comment (code: 4).
      See Also:
    • PI

      public static final byte PI
      Node kind: processing instruction (code: 5).
      See Also:
    • dbid

      public final int dbid
      Unique id. ID can get negative, as subtraction of ids is used for all comparisons.
    • resources

      public final Resources resources
      Resource index.
    • meta

      public final MetaData meta
      Meta data.
    • elemNames

      public Names elemNames
      Element names.
    • attrNames

      public Names attrNames
      Attribute names.
    • nspaces

      public Namespaces nspaces
      Namespace index.
    • paths

      public PathIndex paths
      Path index.
    • textIndex

      public ValueIndex textIndex
      Text index.
    • attrIndex

      public ValueIndex attrIndex
      Attribute value index.
    • tokenIndex

      public ValueIndex tokenIndex
      Token index.
    • ftIndex

      public ValueIndex ftIndex
      Full-text index.
    • updateDists

      public boolean updateDists
      Indicates if distances are to be updated.
    • idmap

      public IdPreMap idmap
      ID-PRE mapping.
    • table

      protected TableAccess table
      Table access file.
    • closed

      protected boolean closed
      Closed flag.
  • Constructor Details

    • Data

      protected Data(MetaData meta)
      Default constructor.
      Parameters:
      meta - meta data
  • Method Details

    • close

      public void close()
      Closes the database.
    • closed

      public final boolean closed()
      Indicates if the database has been closed.
      Returns:
      result of check
    • createIndex

      public abstract void createIndex(IndexType type, Command cmd) throws IOException
      Drops the specified index.
      Parameters:
      type - index to be dropped
      cmd - calling command
      Throws:
      IOException - I/O exception
    • dropIndex

      public abstract void dropIndex(IndexType type) throws BaseXException
      Drops the specified index.
      Parameters:
      type - index to be dropped
      Throws:
      BaseXException - database exception
    • startUpdate

      public abstract void startUpdate(MainOptions opts) throws BaseXException
      Starts an update operation: writes a file to disk to indicate that an update is going on, and exclusively locks the table file.
      Parameters:
      opts - main options
      Throws:
      BaseXException - database exception
    • finishUpdate

      public abstract void finishUpdate(MainOptions opts)
      Finishes an update operation: removes the update file and the exclusive lock.
      Parameters:
      opts - main options
    • flush

      public abstract void flush(boolean all)
      Flushes updated data.
      Parameters:
      all - flush all data
    • iter

      public final IndexIterator iter(IndexSearch search)
      Returns an index iterator for the specified token.
      Parameters:
      search - index search definition
      Returns:
      index iterator
    • costs

      public final IndexCosts costs(IndexSearch search)
      Returns a cost estimation for searching the specified token. Smaller values are better, a value of zero indicates that no results will be returned.
      Parameters:
      search - index search definition
      Returns:
      cost estimation, or null if index access is not possible
    • info

      public final byte[] info(IndexType type, MainOptions options)
      Returns info on the specified index structure.
      Parameters:
      type - index type
      options - main options
      Returns:
      info
    • index

      public final Index index(IndexType type)
      Returns an index for the specified index type.
      Parameters:
      type - index type
      Returns:
      index
    • 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
    • defaultNs

      public byte[] defaultNs()
      Returns the common default namespace of all documents of the database.
      Returns:
      namespace, or null if there is no common namespace
    • pre

      public final int pre(int id)
      Returns a pre value for the specified id.
      Parameters:
      id - unique node id
      Returns:
      pre value or -1 if id was not found
    • 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 final 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 or null
    • nameId

      public final int nameId(int pre)
      Returns the id of the name of an element, attribute or processing instruction.
      Parameters:
      pre - pre value
      Returns:
      name id
    • name

      public final byte[] name(int pre, int kind)
      Returns the name of an element, attribute or processing instruction.
      Parameters:
      pre - pre value
      kind - node kind
      Returns:
      name
    • uriId

      public final int uriId(int pre, int kind)
      Returns the id of the namespace uri of the addressed element or attribute.
      Parameters:
      pre - pre value
      kind - node kind
      Returns:
      id of the namespace uri
    • qname

      public final byte[][] qname(int pre, int kind)
      Returns the name and namespace uri of the addressed element or attribute.
      Parameters:
      pre - pre value
      kind - node kind
      Returns:
      array with name and namespace uri
    • nsFlag

      public final boolean nsFlag(int pre)
      Returns the namespace flag of the addressed element.
      Parameters:
      pre - pre value
      Returns:
      namespace flag
    • namespaces

      public final Atts namespaces(int pre)
      Returns all namespace prefixes and uris that are defined for the specified pre value. Should only be called for element nodes.
      Parameters:
      pre - pre value
      Returns:
      key and value ids
    • textRef

      public final long textRef(int pre)
      Returns the reference to a text (text, comment, pi, pi, document) or attribute value.
      Parameters:
      pre - pre value
      Returns:
      disk offset
    • text

      public abstract byte[] text(int pre, boolean text)
      Returns a text (text, comment, pi, document) 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, document) or attribute value as integer value. Long.MIN_VALUE is returned if the input is no valid integer.
      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, document) or attribute value as double value. Double.NaN is returned if the input is no valid double.
      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 (possibly compressed) text (text, comment, pi, document).
      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) the name of an element, attribute or processing instruction.
      Parameters:
      pre - pre value of the node to be updated
      kind - node kind of the updated node
      name - name of the new element, attribute or processing instruction
      uri - namespace uri
    • update

      public final void update(int pre, int kind, byte[] value)
      Updates (replaces) the value of a single text, comment, pi, attribute or document node.
      Parameters:
      pre - pre value of the node to be updated
      kind - node kind
      value - value to be updated (text, comment, pi, attribute, document name)
    • replace

      public final void replace(int pre, DataClip source)
      Rapid Replace implementation. Replaces parts of the database with the specified data instance.
      Parameters:
      pre - pre value of the 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 be deleted
    • insertAttr

      public final void insertAttr(int pre, int par, DataClip source)
      Inserts standalone attributes (without root element).
      Parameters:
      pre - target pre value (insertion position)
      par - target parent pre value of node
      source - clip with source data
    • insert

      public final void insert(int pre, int par, DataClip source)
      Inserts a data instance at the specified pre value. Notes:
      Parameters:
      pre - target pre value (insertion position)
      par - target parent pre value of node (-1 if document is added)
      source - clip with source data
    • id

      public final void id(int pre, int value)
      Sets the node id.
      Parameters:
      pre - pre value
      value - value to be stored
    • size

      public final void size(int pre, int kind, int value)
      Sets the size value.
      Parameters:
      pre - pre value
      kind - node kind
      value - value to be stored
    • textRef

      protected final void textRef(int pre, long off)
      Sets the reference to a text/attribute value.
      Parameters:
      pre - pre value
      off - offset
    • 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 final 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 nsFlag)
      Sets the namespace flag. Should be only called for element nodes.
      Parameters:
      pre - pre value
      nsFlag - 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 size, byte[] value)
      Adds a document to the internal update buffer.
      Parameters:
      size - node size
      value - document name
    • elem

      public final void elem(int dist, int nameId, int asize, int size, int uriId, boolean nsFlag)
      Adds an element to the internal update buffer.
      Parameters:
      dist - parent distance
      nameId - id of element name
      asize - number of attributes
      size - node size
      uriId - id of namespace uri
      nsFlag - namespace flag
    • text

      public final void text(int dist, byte[] value, int kind)
      Adds a text, comment or processing instruction to the internal update buffer.
      Parameters:
      dist - parent distance
      value - string value
      kind - node kind
    • attr

      public final void attr(int dist, int nameId, byte[] value, int uriId)
      Adds an attribute to the internal update buffer.
      Parameters:
      dist - parent distance
      nameId - id of attribute name
      value - attribute value
      uriId - id of namespace uri
    • textRef

      protected abstract long textRef(byte[] value, boolean text)
      Generates a reference for a text (text, comment, pi, pi, document) or attribute value.
      Parameters:
      value - text to be indexed
      text - text/attribute flag
      Returns:
      reference
    • indexDelete

      protected final void indexDelete(int pre, int id, int size)
      Deletes entries from the index structures.
      Parameters:
      pre - first pre value of the nodes to delete
      id - id (a simple value update is indicated by the value -1)
      size - number of descendants
    • indexAdd

      protected final void indexAdd(int pre, int id, int size, DataClip clip)
      Inserts new entries in the index structure.
      Parameters:
      pre - first pre value of the nodes to insert
      id - id (a simple value update is indicated by the value -1)
      size - number of descendants
      clip - data clip to be inserted
    • inMemory

      public abstract boolean inMemory()
      Indicates if this data instance is in main memory or on disk.
      Returns:
      result of check
    • toString

      public final String toString()
      Overrides:
      toString in class Object