1 | /** |
2 | * |
3 | */ |
4 | package net.spy.memcached.protocol; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.HashMap; |
9 | import java.util.Map; |
10 | |
11 | import net.spy.memcached.ops.GetOperation; |
12 | import net.spy.memcached.ops.OperationStatus; |
13 | |
14 | /** |
15 | * Proxy callback used for dispatching callbacks over optimized gets. |
16 | */ |
17 | public class ProxyCallback implements GetOperation.Callback { |
18 | |
19 | private final Map<String, Collection<GetOperation.Callback>> callbacks= |
20 | new HashMap<String, Collection<GetOperation.Callback>>(); |
21 | private final Collection<GetOperation.Callback> allCallbacks= |
22 | new ArrayList<GetOperation.Callback>(); |
23 | |
24 | public void addCallbacks(GetOperation o) { |
25 | GetOperation.Callback c=new GetCallbackWrapper(o.getKeys().size(), |
26 | (GetOperation.Callback)o.getCallback()); |
27 | allCallbacks.add(c); |
28 | for(String s : o.getKeys()) { |
29 | Collection<GetOperation.Callback> cbs=callbacks.get(s); |
30 | if(cbs == null) { |
31 | cbs=new ArrayList<GetOperation.Callback>(); |
32 | callbacks.put(s, cbs); |
33 | } |
34 | cbs.add(c); |
35 | } |
36 | } |
37 | |
38 | public void gotData(String key, int flags, byte[] data) { |
39 | Collection<GetOperation.Callback> cbs=callbacks.get(key); |
40 | assert cbs != null : "No callbacks for key " + key; |
41 | for(GetOperation.Callback c : cbs) { |
42 | c.gotData(key, flags, data); |
43 | } |
44 | } |
45 | |
46 | public void receivedStatus(OperationStatus status) { |
47 | for(GetOperation.Callback c : allCallbacks) { |
48 | c.receivedStatus(status); |
49 | } |
50 | } |
51 | |
52 | public void complete() { |
53 | for(GetOperation.Callback c : allCallbacks) { |
54 | c.complete(); |
55 | } |
56 | } |
57 | |
58 | public int numKeys() { |
59 | return callbacks.size(); |
60 | } |
61 | |
62 | public int numCallbacks() { |
63 | return allCallbacks.size(); |
64 | } |
65 | } |