Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 17 | package org.onosproject.store.primitives; |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.base.Throwables; |
| 20 | import org.onosproject.store.service.AsyncDocumentTree; |
| 21 | import org.onosproject.store.service.ConsistentMapException; |
| 22 | import org.onosproject.store.service.DocumentException; |
| 23 | import org.onosproject.store.service.DocumentPath; |
| 24 | import org.onosproject.store.service.DocumentTree; |
| 25 | import org.onosproject.store.service.DocumentTreeListener; |
| 26 | import org.onosproject.store.service.Synchronous; |
| 27 | import org.onosproject.store.service.Versioned; |
| 28 | |
| 29 | import java.util.Map; |
| 30 | import java.util.concurrent.CompletableFuture; |
| 31 | import java.util.concurrent.ExecutionException; |
| 32 | import java.util.concurrent.TimeUnit; |
| 33 | import java.util.concurrent.TimeoutException; |
| 34 | |
| 35 | /** |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 36 | * Synchronous wrapper for a {@link AsyncDocumentTree}. All operations are |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 37 | * made by making the equivalent calls to a backing {@link AsyncDocumentTree} |
| 38 | * then blocking until the operations complete or timeout. |
| 39 | * |
| 40 | * @param <V> the type of the values |
| 41 | */ |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 42 | public class DefaultDocumentTree<V> extends Synchronous<AsyncDocumentTree<V>> implements DocumentTree<V> { |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 43 | |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 44 | private final AsyncDocumentTree<V> backingTree; |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 45 | private final long operationTimeoutMillis; |
| 46 | |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 47 | public DefaultDocumentTree(AsyncDocumentTree<V> backingTree, long operationTimeoutMillis) { |
| 48 | super(backingTree); |
| 49 | this.backingTree = backingTree; |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 50 | this.operationTimeoutMillis = operationTimeoutMillis; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public DocumentPath root() { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 55 | return backingTree.root(); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Map<String, Versioned<V>> getChildren(DocumentPath path) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 60 | return complete(backingTree.getChildren(path)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Versioned<V> get(DocumentPath path) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 65 | return complete(backingTree.get(path)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Versioned<V> set(DocumentPath path, V value) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 70 | return complete(backingTree.set(path, value)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean create(DocumentPath path, V value) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 75 | return complete(backingTree.create(path, value)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean createRecursive(DocumentPath path, V value) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 80 | return complete(backingTree.createRecursive(path, value)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean replace(DocumentPath path, V newValue, long version) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 85 | return complete(backingTree.replace(path, newValue, version)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean replace(DocumentPath path, V newValue, V currentValue) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 90 | return complete(backingTree.replace(path, newValue, currentValue)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Versioned<V> removeNode(DocumentPath path) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 95 | return complete(backingTree.removeNode(path)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 100 | complete(backingTree.addListener(path, listener)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void removeListener(DocumentTreeListener<V> listener) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 105 | complete(backingTree.removeListener(listener)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void addListener(DocumentTreeListener<V> listener) { |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 110 | complete(backingTree.addListener(listener)); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | private <T> T complete(CompletableFuture<T> future) { |
| 114 | try { |
| 115 | return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
| 116 | } catch (InterruptedException e) { |
| 117 | Thread.currentThread().interrupt(); |
| 118 | throw new DocumentException.Interrupted(); |
| 119 | } catch (TimeoutException e) { |
| 120 | throw new DocumentException.Timeout(name()); |
| 121 | } catch (ExecutionException e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 122 | Throwables.throwIfUnchecked(e.getCause()); |
Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame] | 123 | throw new ConsistentMapException(e.getCause()); |
| 124 | } |
| 125 | } |
| 126 | } |