-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathBaseOperationFactory.java
118 lines (110 loc) · 4.94 KB
/
BaseOperationFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright (C) 2006-2009 Dustin Sallings
* Copyright (C) 2009-2011 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package net.spy.memcached.ops;
import net.spy.memcached.MemcachedNode;
import net.spy.memcached.OperationFactory;
import java.util.ArrayList;
import java.util.Collection;
/**
* Base class for operation factories.
*
* <p>
* There is little common code between OperationFactory implementations, but
* some exists, and is complicated and likely to cause problems.
* </p>
*/
public abstract class BaseOperationFactory implements OperationFactory {
private String first(Collection<String> keys) {
return keys.iterator().next();
}
public Collection<Operation> clone(KeyedOperation op) {
assert (op.getState() == OperationState.WRITE_QUEUED || op.getState()
== OperationState.RETRY) : "Who passed me an operation in the "
+ op.getState() + "state?";
assert !op.isCancelled() : "Attempted to clone a canceled op";
assert !op.hasErrored() : "Attempted to clone an errored op";
Collection<Operation> rv = new ArrayList<Operation>(op.getKeys().size());
if (op instanceof GetOperation || op instanceof ReplicaGetOperation) {
rv.addAll(cloneGet(op));
} else if (op instanceof ReplicaGetsOperation) {
ReplicaGetsOperation.Callback callback =
(ReplicaGetsOperation.Callback) op.getCallback();
for (String k : op.getKeys()) {
rv.add(replicaGets(k,
((ReplicaGetsOperation) op).getReplicaIndex(), callback));
}
} else if (op instanceof GetsOperation) {
GetsOperation.Callback callback =
(GetsOperation.Callback) op.getCallback();
for (String k : op.getKeys()) {
rv.add(gets(k, callback));
}
} else if (op instanceof CASOperation) {
CASOperation cop = (CASOperation) op;
rv.add(cas(cop.getStoreType(), first(op.getKeys()), cop.getCasValue(),
cop.getFlags(), cop.getExpiration(), cop.getData(),
(StoreOperation.Callback) cop.getCallback()));
} else if(op instanceof DeleteOperation) {
rv.add(delete(first(op.getKeys()),
(DeleteOperation.Callback)op.getCallback()));
} else if (op instanceof MutatorOperation) {
MutatorOperation mo = (MutatorOperation) op;
rv.add(mutate(mo.getType(), first(op.getKeys()), mo.getBy(),
mo.getDefault(), mo.getExpiration(), op.getCallback()));
} else if (op instanceof StoreOperation) {
StoreOperation so = (StoreOperation) op;
rv.add(store(so.getStoreType(), first(op.getKeys()), so.getFlags(),
so.getExpiration(), so.getData(),
(StoreOperation.Callback) op.getCallback()));
} else if (op instanceof ConcatenationOperation) {
ConcatenationOperation c = (ConcatenationOperation) op;
rv.add(cat(c.getStoreType(), c.getCasValue(), first(op.getKeys()),
c.getData(), c.getCallback()));
} else if(op instanceof GetAndTouchOperation) {
GetAndTouchOperation gt = (GetAndTouchOperation) op;
rv.add(getAndTouch(first(gt.getKeys()), gt.getExpiration(),
(GetAndTouchOperation.Callback) gt.getCallback()));
} else if (op instanceof ObserveOperation) {
ObserveOperation oo = (ObserveOperation) op;
rv.add(observe(first(oo.getKeys()), oo.getCasValue(), oo.getIndex(),
(ObserveOperation.Callback) oo.getCallback()));
} else {
assert false : "Unhandled operation type: " + op.getClass();
}
if (op instanceof VBucketAware) {
VBucketAware vop = (VBucketAware) op;
if (!vop.getNotMyVbucketNodes().isEmpty()) {
for (Operation operation : rv) {
if (operation instanceof VBucketAware) {
Collection<MemcachedNode> notMyVbucketNodes =
vop.getNotMyVbucketNodes();
((VBucketAware) operation).setNotMyVbucketNodes(notMyVbucketNodes);
}
}
}
}
return rv;
}
protected abstract Collection<? extends Operation>
cloneGet(KeyedOperation op);
}