Attempts to store the local tree in a new row, automatically assigning a new tree ID and returning the value. This method will scan the UID table for the maximum tree ID, increment it, store the new tree, and return the new ID. If no trees have been created, the returned ID will be "1". If we have r
(final TSDB tsdb)
| 405 | * an error |
| 406 | */ |
| 407 | public Deferred<Integer> createNewTree(final TSDB tsdb) { |
| 408 | if (tree_id > 0) { |
| 409 | throw new IllegalArgumentException("Tree ID has already been set"); |
| 410 | } |
| 411 | if (name == null || name.isEmpty()) { |
| 412 | throw new IllegalArgumentException("Tree was missing the name"); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Called after a successful CAS to store the new tree with the new ID. |
| 417 | * Returns the new ID if successful, 0 if there was an error |
| 418 | */ |
| 419 | final class CreatedCB implements Callback<Deferred<Integer>, Boolean> { |
| 420 | |
| 421 | @Override |
| 422 | public Deferred<Integer> call(final Boolean cas_success) |
| 423 | throws Exception { |
| 424 | return Deferred.fromResult(tree_id); |
| 425 | } |
| 426 | |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Called after fetching all trees. Loops through the tree definitions and |
| 431 | * determines the max ID so we can increment and write a new one |
| 432 | */ |
| 433 | final class CreateNewCB implements Callback<Deferred<Integer>, List<Tree>> { |
| 434 | |
| 435 | @Override |
| 436 | public Deferred<Integer> call(List<Tree> trees) throws Exception { |
| 437 | int max_id = 0; |
| 438 | if (trees != null) { |
| 439 | for (Tree tree : trees) { |
| 440 | if (tree.tree_id > max_id) { |
| 441 | max_id = tree.tree_id; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | tree_id = max_id + 1; |
| 447 | if (tree_id > 65535) { |
| 448 | throw new IllegalStateException("Exhausted all Tree IDs"); |
| 449 | } |
| 450 | |
| 451 | return storeTree(tsdb, true).addCallbackDeferring(new CreatedCB()); |
| 452 | } |
| 453 | |
| 454 | } |
| 455 | |
| 456 | // starts the process by fetching all tree definitions from storage |
| 457 | return fetchAllTrees(tsdb).addCallbackDeferring(new CreateNewCB()); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Attempts to fetch the given tree from storage, loading the rule set at |