Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 19 | import java.util.HashMap; |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | import java.util.NavigableMap; |
| 22 | import java.util.NavigableSet; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 23 | import java.util.TreeMap; |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 24 | import java.util.concurrent.CompletableFuture; |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 25 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 26 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 27 | import org.onlab.util.KryoNamespace; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 28 | import org.onosproject.store.primitives.TransactionId; |
| 29 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FloorEntry; |
| 30 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HigherEntry; |
| 31 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LowerEntry; |
| 32 | import org.onosproject.store.serializers.KryoNamespaces; |
| 33 | import org.onosproject.store.service.AsyncConsistentTreeMap; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.Serializer; |
| 35 | import org.onosproject.store.service.TransactionLog; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 36 | import org.onosproject.store.service.Versioned; |
| 37 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 38 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CEILING_ENTRY; |
| 39 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CEILING_KEY; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 40 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CeilingEntry; |
| 41 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CeilingKey; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 42 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FIRST_ENTRY; |
| 43 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FIRST_KEY; |
| 44 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FLOOR_ENTRY; |
| 45 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FLOOR_KEY; |
| 46 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FloorKey; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 47 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HIGHER_ENTRY; |
| 48 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HIGHER_KEY; |
| 49 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HigherKey; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 50 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LAST_ENTRY; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LAST_KEY; |
| 52 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LOWER_ENTRY; |
| 53 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LOWER_KEY; |
| 54 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LowerKey; |
| 55 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.POLL_FIRST_ENTRY; |
| 56 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.POLL_LAST_ENTRY; |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Implementation of {@link AsyncConsistentTreeMap}. |
| 60 | */ |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 61 | public class AtomixConsistentTreeMap extends AtomixConsistentMap implements AsyncConsistentTreeMap<byte[]> { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 62 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 63 | .register(KryoNamespaces.BASIC) |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 64 | .register(AtomixConsistentMapOperations.NAMESPACE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 65 | .register(AtomixConsistentTreeMapOperations.NAMESPACE) |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 66 | .register(AtomixConsistentMapEvents.NAMESPACE) |
| 67 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID + 150) |
| 68 | .register(AtomixConsistentMapService.TransactionScope.class) |
| 69 | .register(TransactionLog.class) |
| 70 | .register(TransactionId.class) |
| 71 | .register(AtomixConsistentMapService.MapEntryValue.class) |
| 72 | .register(AtomixConsistentMapService.MapEntryValue.Type.class) |
| 73 | .register(new HashMap().keySet().getClass()) |
| 74 | .register(TreeMap.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 75 | .build()); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 76 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 77 | public AtomixConsistentTreeMap(RaftProxy proxy) { |
| 78 | super(proxy); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 82 | protected Serializer serializer() { |
| 83 | return SERIALIZER; |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<String> firstKey() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 88 | return proxy.invoke(FIRST_KEY, serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public CompletableFuture<String> lastKey() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 93 | return proxy.invoke(LAST_KEY, serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> ceilingEntry(String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 98 | return proxy.invoke(CEILING_ENTRY, serializer()::encode, new CeilingEntry(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> floorEntry(String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 103 | return proxy.invoke(FLOOR_ENTRY, serializer()::encode, new FloorEntry(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> higherEntry( |
| 108 | String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 109 | return proxy.invoke(HIGHER_ENTRY, serializer()::encode, new HigherEntry(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> lowerEntry( |
| 114 | String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 115 | return proxy.invoke(LOWER_ENTRY, serializer()::encode, new LowerEntry(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> firstEntry() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 120 | return proxy.invoke(FIRST_ENTRY, serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> lastEntry() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 125 | return proxy.invoke(LAST_ENTRY, serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> pollFirstEntry() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 130 | return proxy.invoke(POLL_FIRST_ENTRY, serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> pollLastEntry() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 135 | return proxy.invoke(POLL_LAST_ENTRY, serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public CompletableFuture<String> lowerKey(String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 140 | return proxy.invoke(LOWER_KEY, serializer()::encode, new LowerKey(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public CompletableFuture<String> floorKey(String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 145 | return proxy.invoke(FLOOR_KEY, serializer()::encode, new FloorKey(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public CompletableFuture<String> ceilingKey(String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 150 | return proxy.invoke(CEILING_KEY, serializer()::encode, new CeilingKey(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public CompletableFuture<String> higherKey(String key) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 155 | return proxy.invoke(HIGHER_KEY, serializer()::encode, new HigherKey(key), serializer()::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public CompletableFuture<NavigableSet<String>> navigableKeySet() { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 160 | throw new UnsupportedOperationException("This operation is not yet supported."); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public CompletableFuture<NavigableMap<String, byte[]>> subMap( |
| 165 | String upperKey, String lowerKey, boolean inclusiveUpper, |
| 166 | boolean inclusiveLower) { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 167 | throw new UnsupportedOperationException("This operation is not yet supported."); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 168 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 169 | } |