1 | /** |
2 | * |
3 | */ |
4 | package net.spy.memcached.protocol; |
5 | |
6 | import net.spy.memcached.ops.GetOperation; |
7 | import net.spy.memcached.ops.OperationStatus; |
8 | |
9 | /** |
10 | * Wrapper callback for use in optimized gets. |
11 | */ |
12 | public class GetCallbackWrapper implements GetOperation.Callback { |
13 | |
14 | private static final OperationStatus END= |
15 | new OperationStatus(true, "END"); |
16 | |
17 | private boolean completed=false; |
18 | private int remainingKeys=0; |
19 | private GetOperation.Callback cb=null; |
20 | |
21 | public GetCallbackWrapper(int k, GetOperation.Callback c) { |
22 | super(); |
23 | remainingKeys=k; |
24 | cb=c; |
25 | } |
26 | |
27 | public void gotData(String key, int flags, byte[] data) { |
28 | assert !completed : "Got data for a completed wrapped op"; |
29 | cb.gotData(key, flags, data); |
30 | if(--remainingKeys == 0) { |
31 | // Fake a status line |
32 | receivedStatus(END); |
33 | } |
34 | } |
35 | |
36 | public void receivedStatus(OperationStatus status) { |
37 | if(!completed) { |
38 | cb.receivedStatus(status); |
39 | } |
40 | } |
41 | |
42 | public void complete() { |
43 | assert !completed; |
44 | cb.complete(); |
45 | completed=true; |
46 | } |
47 | |
48 | } |