Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
| 19 | import java.util.Collection; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.Map.Entry; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.CompletableFuture; |
| 25 | import java.util.concurrent.CopyOnWriteArrayList; |
| 26 | import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 28 | import java.util.stream.Collectors; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 29 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 30 | import org.onosproject.store.service.DatabaseUpdate; |
| 31 | import org.onosproject.store.service.Transaction; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 32 | import org.onosproject.store.service.Versioned; |
| 33 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 34 | import com.google.common.collect.Lists; |
| 35 | import com.google.common.collect.Maps; |
| 36 | import com.google.common.collect.Sets; |
| 37 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 38 | import net.kuujo.copycat.Task; |
| 39 | import net.kuujo.copycat.cluster.Cluster; |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkState; |
| 41 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 42 | /** |
| 43 | * A database that partitions the keys across one or more database partitions. |
| 44 | */ |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 45 | public class PartitionedDatabase implements Database { |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 46 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 47 | private final String name; |
| 48 | private final Partitioner<String> partitioner; |
| 49 | private final List<Database> partitions; |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 50 | private final AtomicBoolean isOpen = new AtomicBoolean(false); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 51 | private static final String DB_NOT_OPEN = "Partitioned Database is not open"; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 52 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 53 | public PartitionedDatabase( |
| 54 | String name, |
| 55 | Collection<Database> partitions) { |
| 56 | this.name = name; |
| 57 | this.partitions = partitions |
| 58 | .stream() |
| 59 | .sorted((db1, db2) -> db1.name().compareTo(db2.name())) |
| 60 | .collect(Collectors.toList()); |
| 61 | this.partitioner = new SimpleKeyHashPartitioner(this.partitions); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the databases for individual partitions. |
| 66 | * @return list of database partitions |
| 67 | */ |
| 68 | public List<Database> getPartitions() { |
| 69 | return partitions; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Returns true if the database is open. |
| 74 | * @return true if open, false otherwise |
| 75 | */ |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 76 | @Override |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 77 | public boolean isOpen() { |
| 78 | return isOpen.get(); |
| 79 | } |
| 80 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 81 | @Override |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 82 | public CompletableFuture<Set<String>> tableNames() { |
| 83 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 84 | Set<String> tableNames = Sets.newConcurrentHashSet(); |
| 85 | return CompletableFuture.allOf(partitions |
| 86 | .stream() |
| 87 | .map(db -> db.tableNames().thenApply(tableNames::addAll)) |
| 88 | .toArray(CompletableFuture[]::new)) |
| 89 | .thenApply(v -> tableNames); |
| 90 | } |
| 91 | |
| 92 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 93 | public CompletableFuture<Map<String, Long>> counters() { |
| 94 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 95 | Map<String, Long> counters = Maps.newConcurrentMap(); |
| 96 | return CompletableFuture.allOf(partitions |
| 97 | .stream() |
| 98 | .map(db -> db.counters() |
| 99 | .thenApply(m -> { |
| 100 | counters.putAll(m); |
| 101 | return null; |
| 102 | })) |
| 103 | .toArray(CompletableFuture[]::new)) |
| 104 | .thenApply(v -> counters); |
| 105 | } |
| 106 | |
| 107 | @Override |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 108 | public CompletableFuture<Integer> size(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 109 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 110 | AtomicInteger totalSize = new AtomicInteger(0); |
| 111 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 112 | .stream() |
| 113 | .map(p -> p.size(tableName).thenApply(totalSize::addAndGet)) |
| 114 | .toArray(CompletableFuture[]::new)) |
| 115 | .thenApply(v -> totalSize.get()); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public CompletableFuture<Boolean> isEmpty(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 120 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 121 | return size(tableName).thenApply(size -> size == 0); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Boolean> containsKey(String tableName, String key) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 126 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 127 | return partitioner.getPartition(tableName, key).containsKey(tableName, key); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 132 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 133 | AtomicBoolean containsValue = new AtomicBoolean(false); |
| 134 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 135 | .stream() |
| 136 | .map(p -> p.containsValue(tableName, value).thenApply(v -> containsValue.compareAndSet(false, v))) |
| 137 | .toArray(CompletableFuture[]::new)) |
| 138 | .thenApply(v -> containsValue.get()); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 143 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 144 | return partitioner.getPartition(tableName, key).get(tableName, key); |
| 145 | } |
| 146 | |
| 147 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 148 | public CompletableFuture<Result<Versioned<byte[]>>> put(String tableName, String key, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 149 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 150 | return partitioner.getPartition(tableName, key).put(tableName, key, value); |
| 151 | } |
| 152 | |
| 153 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 154 | public CompletableFuture<Result<Versioned<byte[]>>> remove(String tableName, String key) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 155 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 156 | return partitioner.getPartition(tableName, key).remove(tableName, key); |
| 157 | } |
| 158 | |
| 159 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 160 | public CompletableFuture<Result<Void>> clear(String tableName) { |
| 161 | AtomicBoolean isLocked = new AtomicBoolean(false); |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 162 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 163 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 164 | .stream() |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 165 | .map(p -> p.clear(tableName) |
| 166 | .thenApply(v -> isLocked.compareAndSet(false, Result.Status.LOCKED == v.status()))) |
| 167 | .toArray(CompletableFuture[]::new)) |
| 168 | .thenApply(v -> isLocked.get() ? Result.locked() : Result.ok(null)); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public CompletableFuture<Set<String>> keySet(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 173 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 174 | Set<String> keySet = Sets.newConcurrentHashSet(); |
| 175 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 176 | .stream() |
| 177 | .map(p -> p.keySet(tableName).thenApply(keySet::addAll)) |
| 178 | .toArray(CompletableFuture[]::new)) |
| 179 | .thenApply(v -> keySet); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 184 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 185 | List<Versioned<byte[]>> values = new CopyOnWriteArrayList<>(); |
| 186 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 187 | .stream() |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 188 | .map(p -> p.values(tableName).thenApply(values::addAll)) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 189 | .toArray(CompletableFuture[]::new)) |
| 190 | .thenApply(v -> values); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public CompletableFuture<Set<Entry<String, Versioned<byte[]>>>> entrySet(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 195 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 196 | Set<Entry<String, Versioned<byte[]>>> entrySet = Sets.newConcurrentHashSet(); |
| 197 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 198 | .stream() |
| 199 | .map(p -> p.entrySet(tableName).thenApply(entrySet::addAll)) |
| 200 | .toArray(CompletableFuture[]::new)) |
| 201 | .thenApply(v -> entrySet); |
| 202 | } |
| 203 | |
| 204 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 205 | public CompletableFuture<Result<Versioned<byte[]>>> putIfAbsent(String tableName, String key, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 206 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 207 | return partitioner.getPartition(tableName, key).putIfAbsent(tableName, key, value); |
| 208 | } |
| 209 | |
| 210 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 211 | public CompletableFuture<Result<Boolean>> remove(String tableName, String key, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 212 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 213 | return partitioner.getPartition(tableName, key).remove(tableName, key, value); |
| 214 | } |
| 215 | |
| 216 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 217 | public CompletableFuture<Result<Boolean>> remove(String tableName, String key, long version) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 218 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 219 | return partitioner.getPartition(tableName, key).remove(tableName, key, version); |
| 220 | } |
| 221 | |
| 222 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 223 | public CompletableFuture<Result<Boolean>> replace( |
| 224 | String tableName, String key, byte[] oldValue, byte[] newValue) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 225 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 226 | return partitioner.getPartition(tableName, key).replace(tableName, key, oldValue, newValue); |
| 227 | } |
| 228 | |
| 229 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 230 | public CompletableFuture<Result<Boolean>> replace( |
| 231 | String tableName, String key, long oldVersion, byte[] newValue) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 232 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 233 | return partitioner.getPartition(tableName, key).replace(tableName, key, oldVersion, newValue); |
| 234 | } |
| 235 | |
| 236 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame^] | 237 | public CompletableFuture<Long> nextValue(String counterName) { |
| 238 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 239 | return partitioner.getPartition(counterName, counterName).nextValue(counterName); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public CompletableFuture<Long> currentValue(String counterName) { |
| 244 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 245 | return partitioner.getPartition(counterName, counterName).currentValue(counterName); |
| 246 | } |
| 247 | |
| 248 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 249 | public CompletableFuture<Boolean> prepareAndCommit(Transaction transaction) { |
| 250 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 251 | if (subTransactions.isEmpty()) { |
| 252 | return CompletableFuture.completedFuture(true); |
| 253 | } else if (subTransactions.size() == 1) { |
| 254 | Entry<Database, Transaction> entry = |
| 255 | subTransactions.entrySet().iterator().next(); |
| 256 | return entry.getKey().prepareAndCommit(entry.getValue()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 257 | } else { |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 258 | return new TransactionManager(this).execute(transaction); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 263 | public CompletableFuture<Boolean> prepare(Transaction transaction) { |
| 264 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 265 | AtomicBoolean status = new AtomicBoolean(true); |
| 266 | return CompletableFuture.allOf(subTransactions.entrySet() |
| 267 | .stream() |
| 268 | .map(entry -> entry |
| 269 | .getKey() |
| 270 | .prepare(entry.getValue()) |
| 271 | .thenApply(v -> status.compareAndSet(true, v))) |
| 272 | .toArray(CompletableFuture[]::new)) |
| 273 | .thenApply(v -> status.get()); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public CompletableFuture<Boolean> commit(Transaction transaction) { |
| 278 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 279 | return CompletableFuture.allOf(subTransactions.entrySet() |
| 280 | .stream() |
| 281 | .map(entry -> entry.getKey().commit(entry.getValue())) |
| 282 | .toArray(CompletableFuture[]::new)) |
| 283 | .thenApply(v -> true); |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public CompletableFuture<Boolean> rollback(Transaction transaction) { |
| 288 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 289 | return CompletableFuture.allOf(subTransactions.entrySet() |
| 290 | .stream() |
| 291 | .map(entry -> entry.getKey().rollback(entry.getValue())) |
| 292 | .toArray(CompletableFuture[]::new)) |
| 293 | .thenApply(v -> true); |
| 294 | } |
| 295 | |
| 296 | @Override |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 297 | public CompletableFuture<Database> open() { |
| 298 | return CompletableFuture.allOf(partitions |
| 299 | .stream() |
| 300 | .map(Database::open) |
| 301 | .toArray(CompletableFuture[]::new)) |
| 302 | .thenApply(v -> { |
| 303 | isOpen.set(true); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 304 | return this; |
| 305 | }); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public CompletableFuture<Void> close() { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 310 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 311 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 312 | .stream() |
| 313 | .map(database -> database.close()) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 314 | .toArray(CompletableFuture[]::new)); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 315 | } |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 316 | |
| 317 | @Override |
| 318 | public boolean isClosed() { |
| 319 | return !isOpen.get(); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public String name() { |
| 324 | return name; |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public Cluster cluster() { |
| 329 | throw new UnsupportedOperationException(); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public Database addStartupTask(Task<CompletableFuture<Void>> task) { |
| 334 | throw new UnsupportedOperationException(); |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public Database addShutdownTask(Task<CompletableFuture<Void>> task) { |
| 339 | throw new UnsupportedOperationException(); |
| 340 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 341 | |
| 342 | private Map<Database, Transaction> createSubTransactions( |
| 343 | Transaction transaction) { |
| 344 | Map<Database, List<DatabaseUpdate>> perPartitionUpdates = Maps.newHashMap(); |
| 345 | for (DatabaseUpdate update : transaction.updates()) { |
| 346 | Database partition = partitioner.getPartition(update.tableName(), update.key()); |
| 347 | List<DatabaseUpdate> partitionUpdates = |
| 348 | perPartitionUpdates.computeIfAbsent(partition, k -> Lists.newLinkedList()); |
| 349 | partitionUpdates.add(update); |
| 350 | } |
| 351 | Map<Database, Transaction> subTransactions = Maps.newHashMap(); |
| 352 | perPartitionUpdates.forEach((k, v) -> subTransactions.put(k, new DefaultTransaction(transaction.id(), v))); |
| 353 | |
| 354 | return subTransactions; |
| 355 | } |
| 356 | } |