1 | package net.spy.memcached.protocol.binary; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | |
6 | import net.spy.memcached.ops.BaseOperationFactory; |
7 | import net.spy.memcached.ops.CASOperation; |
8 | import net.spy.memcached.ops.ConcatenationOperation; |
9 | import net.spy.memcached.ops.ConcatenationType; |
10 | import net.spy.memcached.ops.DeleteOperation; |
11 | import net.spy.memcached.ops.FlushOperation; |
12 | import net.spy.memcached.ops.GetOperation; |
13 | import net.spy.memcached.ops.GetsOperation; |
14 | import net.spy.memcached.ops.KeyedOperation; |
15 | import net.spy.memcached.ops.MultiGetOperationCallback; |
16 | import net.spy.memcached.ops.MultiGetsOperationCallback; |
17 | import net.spy.memcached.ops.Mutator; |
18 | import net.spy.memcached.ops.MutatorOperation; |
19 | import net.spy.memcached.ops.NoopOperation; |
20 | import net.spy.memcached.ops.Operation; |
21 | import net.spy.memcached.ops.OperationCallback; |
22 | import net.spy.memcached.ops.StatsOperation; |
23 | import net.spy.memcached.ops.StoreOperation; |
24 | import net.spy.memcached.ops.StoreType; |
25 | import net.spy.memcached.ops.VersionOperation; |
26 | import net.spy.memcached.ops.GetOperation.Callback; |
27 | |
28 | /** |
29 | * Factory for binary operations. |
30 | */ |
31 | public class BinaryOperationFactory extends BaseOperationFactory { |
32 | |
33 | public DeleteOperation delete(String key, |
34 | OperationCallback operationCallback) { |
35 | return new DeleteOperationImpl(key, operationCallback); |
36 | } |
37 | |
38 | public FlushOperation flush(int delay, OperationCallback cb) { |
39 | return new FlushOperationImpl(cb); |
40 | } |
41 | |
42 | public GetOperation get(String key, Callback callback) { |
43 | return new GetOperationImpl(key, callback); |
44 | } |
45 | |
46 | public GetOperation get(Collection<String> value, Callback cb) { |
47 | return new MultiGetOperationImpl(value, cb); |
48 | } |
49 | |
50 | public GetsOperation gets(String key, GetsOperation.Callback cb) { |
51 | return new GetOperationImpl(key, cb); |
52 | } |
53 | |
54 | public MutatorOperation mutate(Mutator m, String key, int by, |
55 | long def, int exp, OperationCallback cb) { |
56 | return new MutatorOperationImpl(m, key, by, def, exp, cb); |
57 | } |
58 | |
59 | public StatsOperation stats(String arg, |
60 | net.spy.memcached.ops.StatsOperation.Callback cb) { |
61 | return new StatsOperationImpl(arg, cb); |
62 | } |
63 | |
64 | public StoreOperation store(StoreType storeType, String key, int flags, |
65 | int exp, byte[] data, OperationCallback cb) { |
66 | return new StoreOperationImpl(storeType, key, flags, exp, data, 0, cb); |
67 | } |
68 | |
69 | public VersionOperation version(OperationCallback cb) { |
70 | return new VersionOperationImpl(cb); |
71 | } |
72 | |
73 | public NoopOperation noop(OperationCallback cb) { |
74 | return new NoopOperationImpl(cb); |
75 | } |
76 | |
77 | public CASOperation cas(StoreType type, String key, long casId, int flags, |
78 | int exp, byte[] data, OperationCallback cb) { |
79 | return new StoreOperationImpl(type, key, flags, exp, data, |
80 | casId, cb); |
81 | } |
82 | |
83 | public ConcatenationOperation cat(ConcatenationType catType, long casId, |
84 | String key, byte[] data, OperationCallback cb) { |
85 | return new ConcatenationOperationImpl(catType, key, data, casId, cb); |
86 | } |
87 | |
88 | @Override |
89 | protected Collection<? extends Operation> cloneGet(KeyedOperation op) { |
90 | Collection<Operation> rv=new ArrayList<Operation>(); |
91 | GetOperation.Callback getCb = null; |
92 | GetsOperation.Callback getsCb = null; |
93 | if(op.getCallback() instanceof GetOperation.Callback) { |
94 | getCb=new MultiGetOperationCallback( |
95 | op.getCallback(), op.getKeys().size()); |
96 | } else { |
97 | getsCb=new MultiGetsOperationCallback( |
98 | op.getCallback(), op.getKeys().size()); |
99 | } |
100 | for(String k : op.getKeys()) { |
101 | rv.add(getCb == null ? gets(k, getsCb) : get(k, getCb)); |
102 | } |
103 | return rv; |
104 | } |
105 | |
106 | } |