| 1 | package net.spy.memcached.internal; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.HashSet; |
| 6 | import java.util.Map; |
| 7 | import java.util.concurrent.CountDownLatch; |
| 8 | import java.util.concurrent.ExecutionException; |
| 9 | import java.util.concurrent.Future; |
| 10 | import java.util.concurrent.TimeUnit; |
| 11 | import java.util.concurrent.TimeoutException; |
| 12 | |
| 13 | import net.spy.memcached.ops.Operation; |
| 14 | import net.spy.memcached.ops.OperationState; |
| 15 | |
| 16 | /** |
| 17 | * Future for handling results from bulk gets. |
| 18 | * |
| 19 | * Not intended for general use. |
| 20 | * |
| 21 | * @param <T> types of objects returned from the GET |
| 22 | */ |
| 23 | public class BulkGetFuture<T> implements Future<Map<String, T>> { |
| 24 | private final Map<String, Future<T>> rvMap; |
| 25 | private final Collection<Operation> ops; |
| 26 | private final CountDownLatch latch; |
| 27 | private boolean cancelled=false; |
| 28 | |
| 29 | public BulkGetFuture(Map<String, Future<T>> m, |
| 30 | Collection<Operation> getOps, CountDownLatch l) { |
| 31 | super(); |
| 32 | rvMap = m; |
| 33 | ops = getOps; |
| 34 | latch=l; |
| 35 | } |
| 36 | |
| 37 | public boolean cancel(boolean ign) { |
| 38 | boolean rv=false; |
| 39 | for(Operation op : ops) { |
| 40 | rv |= op.getState() == OperationState.WRITING; |
| 41 | op.cancel(); |
| 42 | } |
| 43 | for (Future<T> v : rvMap.values()) { |
| 44 | v.cancel(ign); |
| 45 | } |
| 46 | cancelled=true; |
| 47 | return rv; |
| 48 | } |
| 49 | |
| 50 | public Map<String, T> get() |
| 51 | throws InterruptedException, ExecutionException { |
| 52 | try { |
| 53 | return get(Long.MAX_VALUE, TimeUnit.MILLISECONDS); |
| 54 | } catch (TimeoutException e) { |
| 55 | throw new RuntimeException("Timed out waiting forever", e); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public Map<String, T> get(long timeout, TimeUnit unit) |
| 60 | throws InterruptedException, |
| 61 | ExecutionException, TimeoutException { |
| 62 | if(!latch.await(timeout, unit)) { |
| 63 | Collection<Operation> timedoutOps = new HashSet<Operation>(); |
| 64 | for(Operation op : ops) { |
| 65 | if(op.getState() != OperationState.COMPLETE) { |
| 66 | timedoutOps.add(op); |
| 67 | } |
| 68 | } |
| 69 | throw new CheckedOperationTimeoutException("Operation timed out.", |
| 70 | timedoutOps); |
| 71 | } |
| 72 | for(Operation op : ops) { |
| 73 | if(op.isCancelled()) { |
| 74 | throw new ExecutionException( |
| 75 | new RuntimeException("Cancelled")); |
| 76 | } |
| 77 | if(op.hasErrored()) { |
| 78 | throw new ExecutionException(op.getException()); |
| 79 | } |
| 80 | } |
| 81 | Map<String, T> m = new HashMap<String, T>(); |
| 82 | for (Map.Entry<String, Future<T>> me : rvMap.entrySet()) { |
| 83 | m.put(me.getKey(), me.getValue().get()); |
| 84 | } |
| 85 | return m; |
| 86 | } |
| 87 | |
| 88 | public boolean isCancelled() { |
| 89 | return cancelled; |
| 90 | } |
| 91 | |
| 92 | public boolean isDone() { |
| 93 | return latch.getCount() == 0; |
| 94 | } |
| 95 | } |