Class Tree
- All Implemented Interfaces:
Closeable
,AutoCloseable
Depends on a compatible tree model (entity types), which is matched by type and property names. E.g. names like "DataLeaf.valueString" are fixed and may not be changed. Adding properties to tree types is allowed.
Note there are TWO ways to work with tree data (both ways can be mixed): - Standard ObjectBox entity types with e.g. Box<DataLeaf> - Higher level tree API via this Tree class
To navigate in the tree, you typically start with getRoot()
, which returns a Branch
.
From one branch you can navigate to other branches and also Leaf
s, which carry data attributes.
You can easily navigate the tree by using a path, which is either a string or a string array. A path refers to the names of the meta tree nodes, e.g. "Book.Author.Name".
If you already know the IDs, you can efficiently access branches, data leaves, and data values directly.
To access any data in the tree, you must use explicit transactions offer by methods such as
runInTx(Runnable)
, runInReadTx(Runnable)
, callInTx(Callable)
, or
callInReadTx(Callable)
.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> T
callInReadTx
(Callable<T> callable) Similar toBoxStore.callInReadTx(Callable)
, but allows Tree functions.<T> T
Similar toBoxStore.callInReadTx(Callable)
, but allows Tree functions.<T> T
callInTxNoThrow
(Callable<T> callable) Wraps any Exception thrown by the callable into a RuntimeException.void
close()
Cleans up any (native) resources associated with this tree.getDouble
(long id) Get data value for the given ID or null if no data leaf exists with that ID.getInteger
(long id) Get data value for the given ID or null if no data leaf exists with that ID.getLeaf
(long id) Get the leaf for the given ID or null if no leaf exists with that ID.The path separator regex is used to split a string path into individual path names.getRoot()
Gets the root of the data tree.long
The root ID, which the tree was constructed with.getStore()
getString
(long id) Get data value for the given ID or null if no data leaf exists with that ID.long
Puts (persists) a data leaf (containing a data value).long
putBranch
(long parentBranchId, long metaId) Put a new (inserts) data branchlong
Put a new or existing data branchlong
Put a new (inserts) data branchlong
putMetaBranch
(long id, long parentBranchId, String name) Puts (persists) a branch in the metamodel.long
putMetaBranch
(long id, long parentBranchId, String name, String description) Puts (persists) a branch in the metamodel with an optional description.long[]
putMetaBranches
(long parentBranchId, String[] path) Puts (persists) several branches in the metamodel from the given parent ID (must be a meta branch).long[]
putMetaBranches
(String[] path) Puts (persists) several branches in the metamodel to create the given path from the root.long
putMetaLeaf
(long id, long parentBranchId, String name, short valueType) Puts (persists) a data leaf in the metamodel (describes values).long
putMetaLeaf
(long id, long parentBranchId, String name, short valueType, boolean isUnsigned) Puts (persists) a data leaf in the metamodel (describes values).long
putMetaLeaf
(long id, long parentBranchId, String name, short valueType, boolean isUnsigned, String description) Puts (persists) a data leaf in the metamodel (describes values).long
putValue
(long parentBranchId, long metaId, double value) Puts (inserts) a new data value (using a data leaf).long
putValue
(long parentBranchId, long metaId, long value) Puts (inserts) a new data value (using a data leaf).long
putValue
(long id, long parentBranchId, long metaId, double value) Puts (persists) a data value (using a data leaf).long
putValue
(long id, long parentBranchId, long metaId, long value) Puts (persists) a data value (using a data leaf).long
Puts (persists) a data value (using a data leaf).long
Puts (inserts) a new data value (using a data leaf).void
runInReadTx
(Runnable runnable) Similar toBoxStore.runInReadTx(Runnable)
, but allows Tree functions.void
Similar toBoxStore.runInTx(Runnable)
, but allows Tree functions.void
setPathSeparatorRegex
(String pathSeparatorRegex) E.g.
-
Constructor Details
-
Tree
Create a tree instance for the given meta-branch rootuid
, or find a singular root if 0 is given. -
Tree
Create a tree instance for the given data-branch root ID.
-
-
Method Details
-
getPathSeparatorRegex
The path separator regex is used to split a string path into individual path names. Example: with the default separator, e.g. "Book.Author" becomes ["Book", "Author"]. -
setPathSeparatorRegex
E.g. use "\\/" to change path strings to "Book/Author"; seegetPathSeparatorRegex()
for details. -
getRootId
public long getRootId()The root ID, which the tree was constructed with. -
getStore
-
getRoot
Gets the root of the data tree. -
close
public void close()Cleans up any (native) resources associated with this tree.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
runInTx
Similar toBoxStore.runInTx(Runnable)
, but allows Tree functions. -
runInReadTx
Similar toBoxStore.runInReadTx(Runnable)
, but allows Tree functions. -
callInTx
Similar toBoxStore.callInReadTx(Callable)
, but allows Tree functions.- Throws:
Exception
-
callInTxNoThrow
Wraps any Exception thrown by the callable into a RuntimeException. -
callInReadTx
Similar toBoxStore.callInReadTx(Callable)
, but allows Tree functions. -
getLeaf
Get the leaf for the given ID or null if no leaf exists with that ID. -
getString
Get data value for the given ID or null if no data leaf exists with that ID. -
getInteger
Get data value for the given ID or null if no data leaf exists with that ID. -
getDouble
Get data value for the given ID or null if no data leaf exists with that ID. -
putMetaBranch
Puts (persists) a branch in the metamodel. -
putMetaBranch
Puts (persists) a branch in the metamodel with an optional description. -
putMetaBranches
Puts (persists) several branches in the metamodel to create the given path from the root. -
putMetaBranches
Puts (persists) several branches in the metamodel from the given parent ID (must be a meta branch). -
putMetaLeaf
Puts (persists) a data leaf in the metamodel (describes values). -
putMetaLeaf
public long putMetaLeaf(long id, long parentBranchId, String name, short valueType, boolean isUnsigned) Puts (persists) a data leaf in the metamodel (describes values). -
putMetaLeaf
public long putMetaLeaf(long id, long parentBranchId, String name, short valueType, boolean isUnsigned, @Nullable String description) Puts (persists) a data leaf in the metamodel (describes values). -
putBranch
Put a new or existing data branch -
putBranch
Put a new (inserts) data branch -
putBranch
public long putBranch(long parentBranchId, long metaId) Put a new (inserts) data branch -
putValue
public long putValue(long id, long parentBranchId, long metaId, long value) Puts (persists) a data value (using a data leaf). If a data leaf exists at the given ID, it's overwritten.- Parameters:
id
- Existing ID or zero to insert a new data leaf.parentBranchId
- ID of the data branch, this data value belongs to.metaId
- ID of the metadata leaf "describing" this data value.value
- the actual data value.- Returns:
- the ID of the put data leaf.
-
putValue
public long putValue(long parentBranchId, long metaId, long value) Puts (inserts) a new data value (using a data leaf).- Parameters:
parentBranchId
- ID of the data branch, this data value belongs to.metaId
- ID of the metadata leaf "describing" this data value.value
- the actual data value.- Returns:
- the ID of the new data leaf.
-
putValue
public long putValue(long parentBranchId, long metaId, double value) Puts (inserts) a new data value (using a data leaf).- Parameters:
parentBranchId
- ID of the data branch, this data value belongs to.metaId
- ID of the metadata leaf "describing" this data value.value
- the actual data value.- Returns:
- the ID of the new data leaf.
-
putValue
public long putValue(long id, long parentBranchId, long metaId, double value) Puts (persists) a data value (using a data leaf). If a data leaf exists at the given ID, it's overwritten.- Parameters:
id
- Existing ID or zero to insert a new data leaf.parentBranchId
- ID of the data branch, this data value belongs to.metaId
- ID of the metadata leaf "describing" this data value.value
- the actual data value.- Returns:
- the ID of the put data leaf.
-
putValue
Puts (persists) a data value (using a data leaf). If a data leaf exists at the given ID, it's overwritten.- Parameters:
id
- Existing ID or zero to insert a new data leaf.parentBranchId
- ID of the data branch, this data value belongs to.metaId
- ID of the metadata leaf "describing" this data value.value
- the actual data value.- Returns:
- the ID of the put data leaf.
-
putValue
Puts (inserts) a new data value (using a data leaf).- Parameters:
parentBranchId
- ID of the data branch, this data value belongs to.metaId
- ID of the metadata leaf "describing" this data value.value
- the actual data value.- Returns:
- the ID of the new data leaf.
-
put
Puts (persists) a data leaf (containing a data value). If a data leaf exists with the same ID, it's overwritten.- Returns:
- the ID of the put data leaf.
-