Package org.basex.data
Class Data
java.lang.Object
org.basex.data.Data
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
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
FieldsModifier and TypeFieldDescriptionstatic final byteNode kind: attribute (code:3).Attribute value index.Attribute names.protected booleanClosed flag.static final byteNode kind: comment (code:4).final intUnique id.static final byteNode kind: document (code:0).static final byteNode kind: element (code:1).Element names.Full-text index.ID-PRE mapping.final MetaDataMeta data.Namespace index.Path index.static final byteNode kind: processing instruction (code:5).final ResourcesResource index.protected TableAccessTable access file.static final byteNode kind: text (code:2).Text index.Token index.booleanIndicates if distances are to be updated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal byte[]atom(int pre) Returns an atomized content for any node kind.final voidattr(int dist, int nameId, byte[] value, int uriId) Adds an attribute to the internal update buffer.final intattSize(int pre, int kind) Returns a number of attributes.final byte[]attValue(int att, int pre) Finds the specified attribute and returns its value.voidclose()Closes the database.final booleanclosed()Indicates if the database has been closed.final IndexCostscosts(IndexSearch search) Returns a cost estimation for searching the specified token.abstract voidcreateIndex(IndexType type, Command cmd) Drops the specified index.byte[]Returns the common default namespace of all documents of the database.final voiddelete(int pre) Deletes a node and its descendants.protected abstract voiddelete(int pre, boolean text) Deletes the specified text entry.final intdist(int pre, int kind) Returns the distance of the specified node.final voiddist(int pre, int kind, int value) Sets the distance.final voiddoc(int size, byte[] value) Adds a document to the internal update buffer.abstract voidDrops the specified index.final voidelem(int dist, int nameId, int asize, int size, int uriId, boolean nsFlag) Adds an element to the internal update buffer.abstract voidfinishUpdate(MainOptions opts) Finishes an update operation: removes the update file and the exclusive lock.abstract voidflush(boolean all) Flushes updated data.final intid(int pre) Returns a unique node id.final voidid(int pre, int value) Sets the node id.final IndexReturns an index for the specified index type.protected final voidInserts new entries in the index structure.protected final voidindexDelete(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 booleaninMemory()Indicates if this data instance is in main memory or on disk.final voidinsert(int pre) Inserts the internal buffer to the storage without updating the table structure.final voidInserts a data instance at the specified pre value.final voidinsertAttr(int pre, int par, DataClip source) Inserts standalone attributes (without root element).final IndexIteratoriter(IndexSearch search) Returns an index iterator for the specified token.final intkind(int pre) final byte[]name(int pre, int kind) Returns the name of an element, attribute or processing instruction.final intnameId(int pre) Returns the id of the name of an element, attribute or processing instruction.final Attsnamespaces(int pre) Returns all namespace prefixes and uris that are defined for the specified pre value.final booleannsFlag(int pre) Returns the namespace flag of the addressed element.final voidnsFlag(int pre, boolean nsFlag) Sets the namespace flag.final intparent(int pre, int kind) Returns a pre value of the parent node.final intpre(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 voidRapid Replace implementation.final intsize(int pre, int kind) Returns a size value (number of descendant table entries).final voidsize(int pre, int kind, int value) Sets the size value.abstract voidstartUpdate(MainOptions opts) 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 voidtext(int dist, byte[] value, int kind) Adds a text, comment or processing instruction to the internal update buffer.abstract doubletextDbl(int pre, boolean text) Returns a text (text, comment, pi, document) or attribute value as double value.abstract longtextItr(int pre, boolean text) Returns a text (text, comment, pi, document) or attribute value as integer value.abstract inttextLen(int pre, boolean text) Returns the byte length of a (possibly compressed) text (text, comment, pi, document).protected abstract longtextRef(byte[] value, boolean text) Generates a reference for a text (text, comment, pi, pi, document) or attribute value.final longtextRef(int pre) Returns the reference to a text (text, comment, pi, pi, document) or attribute value.protected final voidtextRef(int pre, long off) Sets the reference to a text/attribute value.final StringtoString()final voidupdate(int pre, int kind, byte[] value) Updates (replaces) the value of a single text, comment, pi, attribute or document node.final voidupdate(int pre, int kind, byte[] name, byte[] uri) Updates (renames) the name of an element, attribute or processing instruction.protected abstract voidupdateText(int pre, byte[] value, int kind) Updates the specified text or attribute value.final inturiId(int pre, int kind) Returns the id of the namespace uri of the addressed element or attribute.
-
Field Details
-
DOC
public static final byte DOCNode kind: document (code:0).- See Also:
-
ELEM
public static final byte ELEMNode kind: element (code:1).- See Also:
-
TEXT
public static final byte TEXTNode kind: text (code:2).- See Also:
-
ATTR
public static final byte ATTRNode kind: attribute (code:3).- See Also:
-
COMM
public static final byte COMMNode kind: comment (code:4).- See Also:
-
PI
public static final byte PINode kind: processing instruction (code:5).- See Also:
-
dbid
public final int dbidUnique id. ID can get negative, as subtraction of ids is used for all comparisons. -
resources
Resource index. -
meta
Meta data. -
elemNames
Element names. -
attrNames
Attribute names. -
nspaces
Namespace index. -
paths
Path index. -
textIndex
Text index. -
attrIndex
Attribute value index. -
tokenIndex
Token index. -
ftIndex
Full-text index. -
updateDists
public boolean updateDistsIndicates if distances are to be updated. -
idmap
ID-PRE mapping. -
table
Table access file. -
closed
protected boolean closedClosed flag.
-
-
Constructor Details
-
Data
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
Drops the specified index.- Parameters:
type- index to be droppedcmd- calling command- Throws:
IOException- I/O exception
-
dropIndex
Drops the specified index.- Parameters:
type- index to be dropped- Throws:
BaseXException- database exception
-
startUpdate
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
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
Returns an index iterator for the specified token.- Parameters:
search- index search definition- Returns:
- index iterator
-
costs
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
nullif index access is not possible
-
info
Returns info on the specified index structure.- Parameters:
type- index typeoptions- main options- Returns:
- info
-
index
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
nullif 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
-1if 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) - 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 valuekind- 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 valuekind- 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 valuekind- node kind- Returns:
- size value
-
attSize
public final int attSize(int pre, int kind) Returns a number of attributes.- Parameters:
pre- pre valuekind- 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 foundpre- 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 valuekind- 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 valuekind- 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 valuekind- 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
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 valuetext- 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_VALUEis returned if the input is no valid integer.- Parameters:
pre- pre valuetext- 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.NaNis returned if the input is no valid double.- Parameters:
pre- pre valuetext- 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 valuetext- 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 updatedkind- node kind of the updated nodename- name of the new element, attribute or processing instructionuri- 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 updatedkind- node kindvalue- value to be updated (text, comment, pi, attribute, document name)
-
replace
Rapid Replace implementation. Replaces parts of the database with the specified data instance.- Parameters:
pre- pre value of the node to be replacedsource- 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
Inserts standalone attributes (without root element).- Parameters:
pre- target pre value (insertion position)par- target parent pre value of nodesource- clip with source data
-
insert
Inserts a data instance at the specified pre value. Notes:- The data instance in the specified data clip must differ from this instance.
- Attributes must be inserted via
insertAttr(int, int, org.basex.data.DataClip).
- Parameters:
pre- target pre value (insertion position)par- target parent pre value of node (-1if document is added)source- clip with source data
-
id
public final void id(int pre, int value) Sets the node id.- Parameters:
pre- pre valuevalue- value to be stored
-
size
public final void size(int pre, int kind, int value) Sets the size value.- Parameters:
pre- pre valuekind- node kindvalue- value to be stored
-
textRef
protected final void textRef(int pre, long off) Sets the reference to a text/attribute value.- Parameters:
pre- pre valueoff- offset
-
updateText
protected abstract void updateText(int pre, byte[] value, int kind) Updates the specified text or attribute value.- Parameters:
pre- pre valuevalue- contentkind- node kind
-
dist
public final void dist(int pre, int kind, int value) Sets the distance.- Parameters:
pre- pre valuekind- node kindvalue- value
-
nsFlag
public final void nsFlag(int pre, boolean nsFlag) Sets the namespace flag. Should be only called for element nodes.- Parameters:
pre- pre valuensFlag- 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 valuetext- 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 sizevalue- 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 distancenameId- id of element nameasize- number of attributessize- node sizeuriId- id of namespace urinsFlag- 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 distancevalue- string valuekind- 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 distancenameId- id of attribute namevalue- attribute valueuriId- 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 indexedtext- 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 deleteid- id (a simple value update is indicated by the value-1)size- number of descendants
-
indexAdd
Inserts new entries in the index structure.- Parameters:
pre- first pre value of the nodes to insertid- id (a simple value update is indicated by the value-1)size- number of descendantsclip- 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
-