Class Tree
- java.lang.Object
-
- io.objectbox.tree.Tree
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
@Experimental public class Tree extends java.lang.Object implements java.io.Closeable
A higher level tree API operating on branch and leaf nodes. Points to a root branch, can traverse child branches and read and write data in leafs.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 aBranch. From one branch you can navigate to other branches and alsoLeafs, 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), orcallInReadTx(Callable).
-
-
Field Summary
Fields Modifier and Type Field Description private longhandleprivate java.lang.StringpathSeparatorRegexprivate longrootIdprivate BoxStorestore
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> TcallInReadTx(java.util.concurrent.Callable<T> callable)Similar toBoxStore.callInReadTx(Callable), but allows Tree functions.<T> TcallInTx(java.util.concurrent.Callable<T> callable)Similar toBoxStore.callInReadTx(Callable), but allows Tree functions.<T> TcallInTxNoThrow(java.util.concurrent.Callable<T> callable)Wraps any Exception thrown by the callable into a RuntimeException.voidclose()Cleans up any (native) resources associated with this tree.private <T> java.util.concurrent.Callable<T>createTxCallable(java.util.concurrent.Callable<T> callable)private java.lang.RunnablecreateTxRunnable(java.lang.Runnable runnable)java.lang.DoublegetDouble(long id)Get data value for the given ID or null if no data leaf exists with that ID.(package private) longgetHandle()java.lang.LonggetInteger(long id)Get data value for the given ID or null if no data leaf exists with that ID.LeafgetLeaf(long id)Get the leaf for the given ID or null if no leaf exists with that ID.java.lang.StringgetPathSeparatorRegex()The path separator regex is used to split a string path into individual path names.BranchgetRoot()Gets the root of the data tree.longgetRootId()The root ID, which the tree was constructed with.BoxStoregetStore()java.lang.StringgetString(long id)Get data value for the given ID or null if no data leaf exists with that ID.(package private) voidnativeClearTransaction(long handle)(package private) static longnativeCreate(long store, long rootId)Create a (Data)Tree instance for the given meta-branch root, or find a singular root if 0 is given.(package private) static longnativeCreateWithUid(long store, java.lang.String uid)Not usable yet; TX is not aligned(package private) static voidnativeDelete(long handle)(package private) LeafNodenativeGetLeafById(long treeHandle, long leafId)(package private) longnativeGetRootId(long handle)Get the root data branch ID.(package private) longnativePutBranch(long treeHandle, long id, long parentBranchId, long metaId, java.lang.String uid)(package private) longnativePutMetaBranch(long treeHandle, long id, long parentBranchId, java.lang.String name, java.lang.String description)(package private) long[]nativePutMetaBranches(long treeHandle, long parentBranchId, java.lang.String[] path)(package private) longnativePutMetaLeaf(long treeHandle, long id, long parentBranchId, java.lang.String name, short valueType, boolean isUnsigned, java.lang.String description)(package private) longnativePutValueFP(long treeHandle, long id, long parentBranchId, long metaId, double value)(package private) longnativePutValueInteger(long treeHandle, long id, long parentBranchId, long metaId, long value)(package private) longnativePutValueString(long treeHandle, long id, long parentBranchId, long metaId, java.lang.String value)(package private) booleannativeSetTransaction(long handle, long txHandle)longput(Leaf leaf)Puts (persists) a data leaf (containing a data value).longputBranch(long parentBranchId, long metaId)Put a new (inserts) data branchlongputBranch(long id, long parentBranchId, long metaId, java.lang.String uid)Put a new or existing data branchlongputBranch(long parentBranchId, long metaId, java.lang.String uid)Put a new (inserts) data branchlongputMetaBranch(long id, long parentBranchId, java.lang.String name)Puts (persists) a branch in the metamodel.longputMetaBranch(long id, long parentBranchId, java.lang.String name, java.lang.String description)Puts (persists) a branch in the metamodel with an optional description.long[]putMetaBranches(long parentBranchId, java.lang.String[] path)Puts (persists) several branches in the metamodel from the given parent ID (must be a meta branch).long[]putMetaBranches(java.lang.String[] path)Puts (persists) several branches in the metamodel to create the given path from the root.longputMetaLeaf(long id, long parentBranchId, java.lang.String name, short valueType)Puts (persists) a data leaf in the metamodel (describes values).longputMetaLeaf(long id, long parentBranchId, java.lang.String name, short valueType, boolean isUnsigned)Puts (persists) a data leaf in the metamodel (describes values).longputMetaLeaf(long id, long parentBranchId, java.lang.String name, short valueType, boolean isUnsigned, java.lang.String description)Puts (persists) a data leaf in the metamodel (describes values).longputValue(long parentBranchId, long metaId, double value)Puts (inserts) a new data value (using a data leaf).longputValue(long parentBranchId, long metaId, long value)Puts (inserts) a new data value (using a data leaf).longputValue(long id, long parentBranchId, long metaId, double value)Puts (persists) a data value (using a data leaf).longputValue(long id, long parentBranchId, long metaId, long value)Puts (persists) a data value (using a data leaf).longputValue(long id, long parentBranchId, long metaId, java.lang.String value)Puts (persists) a data value (using a data leaf).longputValue(long parentBranchId, long metaId, java.lang.String value)Puts (inserts) a new data value (using a data leaf).voidrunInReadTx(java.lang.Runnable runnable)Similar toBoxStore.runInReadTx(Runnable), but allows Tree functions.voidrunInTx(java.lang.Runnable runnable)Similar toBoxStore.runInTx(Runnable), but allows Tree functions.voidsetPathSeparatorRegex(java.lang.String pathSeparatorRegex)E.g.
-
-
-
Field Detail
-
handle
private long handle
-
store
private final BoxStore store
-
rootId
private long rootId
-
pathSeparatorRegex
private java.lang.String pathSeparatorRegex
-
-
Method Detail
-
getHandle
long getHandle()
-
getPathSeparatorRegex
public java.lang.String 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
public void setPathSeparatorRegex(java.lang.String pathSeparatorRegex)
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
public BoxStore getStore()
-
getRoot
public Branch getRoot()
Gets the root of the data tree.
-
close
public void close()
Cleans up any (native) resources associated with this tree.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
runInTx
public void runInTx(java.lang.Runnable runnable)
Similar toBoxStore.runInTx(Runnable), but allows Tree functions.
-
runInReadTx
public void runInReadTx(java.lang.Runnable runnable)
Similar toBoxStore.runInReadTx(Runnable), but allows Tree functions.
-
callInTx
public <T> T callInTx(java.util.concurrent.Callable<T> callable) throws java.lang.ExceptionSimilar toBoxStore.callInReadTx(Callable), but allows Tree functions.- Throws:
java.lang.Exception
-
callInTxNoThrow
public <T> T callInTxNoThrow(java.util.concurrent.Callable<T> callable)
Wraps any Exception thrown by the callable into a RuntimeException.
-
callInReadTx
public <T> T callInReadTx(java.util.concurrent.Callable<T> callable)
Similar toBoxStore.callInReadTx(Callable), but allows Tree functions.
-
createTxRunnable
private java.lang.Runnable createTxRunnable(java.lang.Runnable runnable)
-
createTxCallable
private <T> java.util.concurrent.Callable<T> createTxCallable(java.util.concurrent.Callable<T> callable)
-
getLeaf
@Nullable public Leaf getLeaf(long id)
Get the leaf for the given ID or null if no leaf exists with that ID.
-
getString
@Nullable public java.lang.String getString(long id)
Get data value for the given ID or null if no data leaf exists with that ID.
-
getInteger
@Nullable public java.lang.Long getInteger(long id)
Get data value for the given ID or null if no data leaf exists with that ID.
-
getDouble
@Nullable public java.lang.Double getDouble(long id)
Get data value for the given ID or null if no data leaf exists with that ID.
-
putMetaBranch
public long putMetaBranch(long id, long parentBranchId, java.lang.String name)Puts (persists) a branch in the metamodel.
-
putMetaBranch
public long putMetaBranch(long id, long parentBranchId, java.lang.String name, @Nullable java.lang.String description)Puts (persists) a branch in the metamodel with an optional description.
-
putMetaBranches
public long[] putMetaBranches(java.lang.String[] path)
Puts (persists) several branches in the metamodel to create the given path from the root.
-
putMetaBranches
public long[] putMetaBranches(long parentBranchId, java.lang.String[] path)Puts (persists) several branches in the metamodel from the given parent ID (must be a meta branch).
-
putMetaLeaf
public long putMetaLeaf(long id, long parentBranchId, java.lang.String name, short valueType)Puts (persists) a data leaf in the metamodel (describes values).
-
putMetaLeaf
public long putMetaLeaf(long id, long parentBranchId, java.lang.String name, short valueType, boolean isUnsigned)Puts (persists) a data leaf in the metamodel (describes values).
-
putMetaLeaf
public long putMetaLeaf(long id, long parentBranchId, java.lang.String name, short valueType, boolean isUnsigned, @Nullable java.lang.String description)Puts (persists) a data leaf in the metamodel (describes values).
-
putBranch
public long putBranch(long id, long parentBranchId, long metaId, @Nullable java.lang.String uid)Put a new or existing data branch
-
putBranch
public long putBranch(long parentBranchId, long metaId, @Nullable java.lang.String uid)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
public long putValue(long id, long parentBranchId, long metaId, java.lang.String 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, java.lang.String 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.
-
put
public long put(Leaf leaf)
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.
-
nativeCreate
static long nativeCreate(long store, long rootId)Create a (Data)Tree instance for the given meta-branch root, or find a singular root if 0 is given.
-
nativeCreateWithUid
static long nativeCreateWithUid(long store, java.lang.String uid)Not usable yet; TX is not aligned
-
nativeDelete
static void nativeDelete(long handle)
-
nativeSetTransaction
boolean nativeSetTransaction(long handle, long txHandle)
-
nativeClearTransaction
void nativeClearTransaction(long handle)
-
nativeGetRootId
long nativeGetRootId(long handle)
Get the root data branch ID.
-
nativeGetLeafById
LeafNode nativeGetLeafById(long treeHandle, long leafId)
-
nativePutMetaBranch
long nativePutMetaBranch(long treeHandle, long id, long parentBranchId, java.lang.String name, @Nullable java.lang.String description)
-
nativePutMetaBranches
long[] nativePutMetaBranches(long treeHandle, long parentBranchId, java.lang.String[] path)
-
nativePutMetaLeaf
long nativePutMetaLeaf(long treeHandle, long id, long parentBranchId, java.lang.String name, short valueType, boolean isUnsigned, @Nullable java.lang.String description)
-
nativePutBranch
long nativePutBranch(long treeHandle, long id, long parentBranchId, long metaId, @Nullable java.lang.String uid)
-
nativePutValueInteger
long nativePutValueInteger(long treeHandle, long id, long parentBranchId, long metaId, long value)
-
nativePutValueFP
long nativePutValueFP(long treeHandle, long id, long parentBranchId, long metaId, double value)
-
nativePutValueString
long nativePutValueString(long treeHandle, long id, long parentBranchId, long metaId, @Nullable java.lang.String value)
-
-