7
7
import io .objectbox .model .PropertyType ;
8
8
9
9
import javax .annotation .Nullable ;
10
+ import java .io .Closeable ;
10
11
import java .util .concurrent .Callable ;
11
12
12
13
/**
13
14
* Points to a root branch, can traverse child branches and read and write data in leafs.
15
+ * <p>
16
+ * To navigate in the tree, you typically start with {@link #getRoot()}, which returns a {@link Branch}.
17
+ * From one branch you can navigate to other branches and also {@link Leaf}s, which carry data attributes.
18
+ * <p>
19
+ * You can easily navigate the tree by using a path, which is either a string or a string array.
20
+ * A path refers to the names of the meta tree nodes, e.g. "Book.Author.Name".
21
+ * <p>
22
+ * If you already know the IDs, you can efficiently access branches, data leaves, and data values directly.
23
+ * <p>
24
+ * To access any data in the tree, you must use explicit transactions offer by methods such as
25
+ * {@link #runInTx(Runnable)}, {@link #runInReadTx(Runnable)}, {@link #callInTx(Callable)}, or
26
+ * {@link #callInReadTx(Callable)}.
14
27
*/
15
28
@ SuppressWarnings ("SameParameterValue" )
16
29
@ Experimental
17
- public class Tree {
30
+ public class Tree implements Closeable {
18
31
19
32
private long handle ;
20
33
private final BoxStore store ;
34
+ private long rootId ;
21
35
22
36
/**
23
37
* Create a tree instance for the given meta-branch root {@code uid}, or find a singular root if 0 is given.
@@ -36,10 +50,11 @@ public Tree(BoxStore store, String uid) {
36
50
}
37
51
38
52
/**
39
- * Create a tree instance for the given meta -branch root {@code uid}, or find a singular root if 0 is given .
53
+ * Create a tree instance for the given data -branch root ID .
40
54
*/
41
55
public Tree (BoxStore store , long rootId ) {
42
56
this .store = store ;
57
+ this .rootId = rootId ;
43
58
//noinspection ConstantConditions Nullability annotations are not enforced.
44
59
if (store == null ) {
45
60
throw new IllegalArgumentException ("store must not be null" );
@@ -51,29 +66,51 @@ long getHandle() {
51
66
return handle ;
52
67
}
53
68
69
+ /**
70
+ * The root ID, which the tree was constructed with.
71
+ */
72
+ public long getRootId () {
73
+ return rootId ;
74
+ }
75
+
54
76
public BoxStore getStore () {
55
77
return store ;
56
78
}
57
79
58
- public Branch root () {
80
+ /**
81
+ * Gets the root of the data tree.
82
+ */
83
+ public Branch getRoot () {
59
84
long dataBranchId = nativeGetRootId (handle );
60
85
return new Branch (this , dataBranchId );
61
86
}
62
87
88
+ /**
89
+ * Cleans up any (native) resources associated with this tree.
90
+ */
63
91
public void close () {
64
92
long handle = this .handle ;
65
93
nativeDelete (handle );
66
94
this .handle = 0 ;
67
95
}
68
96
97
+ /**
98
+ * Similar to {@link BoxStore#runInTx(Runnable)}, but allows Tree functions.
99
+ */
69
100
public void runInTx (Runnable runnable ) {
70
101
store .runInTx (createTxRunnable (runnable ));
71
102
}
72
103
104
+ /**
105
+ * Similar to {@link BoxStore#runInReadTx(Runnable)}, but allows Tree functions.
106
+ */
73
107
public void runInReadTx (Runnable runnable ) {
74
108
store .runInReadTx (createTxRunnable (runnable ));
75
109
}
76
110
111
+ /**
112
+ * Similar to {@link BoxStore#callInReadTx(Callable)}, but allows Tree functions.
113
+ */
77
114
public <T > T callInTx (Callable <T > callable ) throws Exception {
78
115
return store .callInTx (createTxCallable (callable ));
79
116
}
@@ -89,6 +126,9 @@ public <T> T callInTxNoThrow(Callable<T> callable) {
89
126
}
90
127
}
91
128
129
+ /**
130
+ * Similar to {@link BoxStore#callInReadTx(Callable)}, but allows Tree functions.
131
+ */
92
132
public <T > T callInReadTx (Callable <T > callable ) {
93
133
return store .callInReadTx (createTxCallable (callable ));
94
134
}
@@ -154,33 +194,54 @@ public Double getDouble(long id) {
154
194
return leaf != null ? leaf .asDouble () : null ;
155
195
}
156
196
197
+ /**
198
+ * Puts (persists) a branch in the metamodel.
199
+ */
157
200
public long putMetaBranch (long id , long parentBranchId , String name ) {
158
201
return nativePutMetaBranch (handle , id , parentBranchId , name , null );
159
202
}
160
203
204
+ /**
205
+ * Puts (persists) a branch in the metamodel with an optional description.
206
+ */
161
207
public long putMetaBranch (long id , long parentBranchId , String name , @ Nullable String description ) {
162
208
return nativePutMetaBranch (handle , id , parentBranchId , name , description );
163
209
}
164
210
211
+ /**
212
+ * Puts (persists) several branches in the metamodel to create the given path from the root.
213
+ */
165
214
public long [] putMetaBranches (String [] path ) {
166
215
return nativePutMetaBranches (handle , 0 , path );
167
216
}
168
217
218
+ /**
219
+ * Puts (persists) several branches in the metamodel from the given parent ID (must be a meta branch).
220
+ */
169
221
public long [] putMetaBranches (long parentBranchId , String [] path ) {
170
222
return nativePutMetaBranches (handle , parentBranchId , path );
171
223
}
172
224
225
+ /**
226
+ * Puts (persists) a data leaf in the metamodel (describes values).
227
+ */
173
228
public long putMetaLeaf (long id , long parentBranchId , String name , short valueType ) {
174
229
return nativePutMetaLeaf (handle , id , parentBranchId , name , valueType , false , null );
175
230
}
176
231
232
+ /**
233
+ * Puts (persists) a data leaf in the metamodel (describes values).
234
+ */
177
235
public long putMetaLeaf (long id , long parentBranchId , String name , short valueType ,
178
- boolean isUnsigned ) {
236
+ boolean isUnsigned ) {
179
237
return nativePutMetaLeaf (handle , id , parentBranchId , name , valueType , isUnsigned , null );
180
238
}
181
239
240
+ /**
241
+ * Puts (persists) a data leaf in the metamodel (describes values).
242
+ */
182
243
public long putMetaLeaf (long id , long parentBranchId , String name , short valueType ,
183
- boolean isUnsigned , @ Nullable String description ) {
244
+ boolean isUnsigned , @ Nullable String description ) {
184
245
return nativePutMetaLeaf (handle , id , parentBranchId , name , valueType , isUnsigned , description );
185
246
}
186
247
@@ -205,30 +266,87 @@ public long putBranch(long parentBranchId, long metaId) {
205
266
return nativePutBranch (handle , 0 , parentBranchId , metaId , null );
206
267
}
207
268
269
+
270
+ /**
271
+ * Puts (persists) a data value (using a data leaf). If a data leaf exists at the given ID, it's overwritten.
272
+ *
273
+ * @param id Existing ID or zero to insert a new data leaf.
274
+ * @param parentBranchId ID of the data branch, this data value belongs to.
275
+ * @param metaId ID of the metadata leaf "describing" this data value.
276
+ * @param value the actual data value.
277
+ * @return the ID of the put data leaf.
278
+ */
208
279
public long putValue (long id , long parentBranchId , long metaId , long value ) {
209
280
return nativePutValueInteger (handle , id , parentBranchId , metaId , value );
210
281
}
211
282
283
+ /**
284
+ * Puts (inserts) a new data value (using a data leaf).
285
+ *
286
+ * @param parentBranchId ID of the data branch, this data value belongs to.
287
+ * @param metaId ID of the metadata leaf "describing" this data value.
288
+ * @param value the actual data value.
289
+ * @return the ID of the new data leaf.
290
+ */
212
291
public long putValue (long parentBranchId , long metaId , long value ) {
213
292
return nativePutValueInteger (handle , 0 , parentBranchId , metaId , value );
214
293
}
215
294
295
+ /**
296
+ * Puts (inserts) a new data value (using a data leaf).
297
+ *
298
+ * @param parentBranchId ID of the data branch, this data value belongs to.
299
+ * @param metaId ID of the metadata leaf "describing" this data value.
300
+ * @param value the actual data value.
301
+ * @return the ID of the new data leaf.
302
+ */
216
303
public long putValue (long parentBranchId , long metaId , double value ) {
217
304
return nativePutValueFP (handle , 0 , parentBranchId , metaId , value );
218
305
}
219
306
307
+ /**
308
+ * Puts (persists) a data value (using a data leaf). If a data leaf exists at the given ID, it's overwritten.
309
+ *
310
+ * @param id Existing ID or zero to insert a new data leaf.
311
+ * @param parentBranchId ID of the data branch, this data value belongs to.
312
+ * @param metaId ID of the metadata leaf "describing" this data value.
313
+ * @param value the actual data value.
314
+ * @return the ID of the put data leaf.
315
+ */
220
316
public long putValue (long id , long parentBranchId , long metaId , double value ) {
221
317
return nativePutValueFP (handle , id , parentBranchId , metaId , value );
222
318
}
223
319
320
+ /**
321
+ * Puts (persists) a data value (using a data leaf). If a data leaf exists at the given ID, it's overwritten.
322
+ *
323
+ * @param id Existing ID or zero to insert a new data leaf.
324
+ * @param parentBranchId ID of the data branch, this data value belongs to.
325
+ * @param metaId ID of the metadata leaf "describing" this data value.
326
+ * @param value the actual data value.
327
+ * @return the ID of the put data leaf.
328
+ */
224
329
public long putValue (long id , long parentBranchId , long metaId , String value ) {
225
330
return nativePutValueString (handle , id , parentBranchId , metaId , value );
226
331
}
227
332
333
+ /**
334
+ * Puts (inserts) a new data value (using a data leaf).
335
+ *
336
+ * @param parentBranchId ID of the data branch, this data value belongs to.
337
+ * @param metaId ID of the metadata leaf "describing" this data value.
338
+ * @param value the actual data value.
339
+ * @return the ID of the new data leaf.
340
+ */
228
341
public long putValue (long parentBranchId , long metaId , String value ) {
229
342
return nativePutValueString (handle , 0 , parentBranchId , metaId , value );
230
343
}
231
344
345
+ /**
346
+ * Puts (persists) a data leaf (containing a data value). If a data leaf exists with the same ID, it's overwritten.
347
+ *
348
+ * @return the ID of the put data leaf.
349
+ */
232
350
public long put (Leaf leaf ) {
233
351
long id = leaf .getId ();
234
352
long parentId = leaf .getParentBranchId ();
0 commit comments