forked from infinispan/infinispan
/
ReadWriteManyEntriesCommand.java
174 lines (143 loc) · 4.97 KB
/
ReadWriteManyEntriesCommand.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package org.infinispan.commands.functional;
import static org.infinispan.functional.impl.EntryViews.snapshot;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import org.infinispan.commands.CommandInvocationId;
import org.infinispan.commands.Visitor;
import org.infinispan.commons.marshall.MarshallUtil;
import org.infinispan.functional.EntryView.ReadWriteEntryView;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.functional.impl.EntryViews;
import org.infinispan.functional.impl.Params;
// TODO: the command does not carry previous values to backup, so it can cause
// the values on primary and backup owners to diverge in case of topology change
public final class ReadWriteManyEntriesCommand<K, V, R> extends AbstractWriteManyCommand<K, V> {
public static final byte COMMAND_ID = 53;
private Map<? extends K, ? extends V> entries;
private BiFunction<V, ReadWriteEntryView<K, V>, R> f;
private int topologyId = -1;
boolean isForwarded = false;
public ReadWriteManyEntriesCommand(Map<? extends K, ? extends V> entries, BiFunction<V, ReadWriteEntryView<K, V>, R> f, Params params, CommandInvocationId commandInvocationId) {
super(commandInvocationId, params);
this.entries = entries;
this.f = f;
}
public ReadWriteManyEntriesCommand(ReadWriteManyEntriesCommand command) {
super(command);
this.entries = command.entries;
this.f = command.f;
}
public ReadWriteManyEntriesCommand() {
}
public Map<? extends K, ? extends V> getEntries() {
return entries;
}
public void setEntries(Map<? extends K, ? extends V> entries) {
this.entries = entries;
}
public final ReadWriteManyEntriesCommand<K, V, R> withEntries(Map<? extends K, ? extends V> entries) {
setEntries(entries);
return this;
}
@Override
public byte getCommandId() {
return COMMAND_ID;
}
@Override
public void writeTo(ObjectOutput output) throws IOException {
CommandInvocationId.writeTo(output, commandInvocationId);
MarshallUtil.marshallMap(entries, output);
output.writeObject(f);
output.writeBoolean(isForwarded);
Params.writeObject(output, params);
output.writeInt(topologyId);
output.writeLong(flags);
}
@Override
public void readFrom(ObjectInput input) throws IOException, ClassNotFoundException {
commandInvocationId = CommandInvocationId.readFrom(input);
// We use LinkedHashMap in order to guarantee the same order of iteration
entries = MarshallUtil.unmarshallMap(input, LinkedHashMap::new);
f = (BiFunction<V, ReadWriteEntryView<K, V>, R>) input.readObject();
isForwarded = input.readBoolean();
params = Params.readObject(input);
topologyId = input.readInt();
flags = input.readLong();
}
public boolean isForwarded() {
return isForwarded;
}
public void setForwarded(boolean forwarded) {
isForwarded = forwarded;
}
@Override
public boolean isReturnValueExpected() {
return true;
}
@Override
public int getTopologyId() {
return topologyId; // TODO: Customise this generated block
}
@Override
public void setTopologyId(int topologyId) {
this.topologyId = topologyId;
}
@Override
public Object acceptVisitor(InvocationContext ctx, Visitor visitor) throws Throwable {
return visitor.visitReadWriteManyEntriesCommand(ctx, this);
}
@Override
public Object perform(InvocationContext ctx) throws Throwable {
List<R> returns = new ArrayList<>(entries.size());
entries.forEach((k, v) -> {
CacheEntry<K, V> entry = ctx.lookupEntry(k);
if (entry == null) {
throw new IllegalStateException();
}
R r = f.apply(v, EntryViews.readWrite(entry));
returns.add(snapshot(r));
});
return returns;
}
@Override
public boolean isSuccessful() {
return true;
}
@Override
public boolean isConditional() {
return false;
}
@Override
public Collection<?> getAffectedKeys() {
return entries.keySet();
}
public LoadType loadType() {
return LoadType.OWNER;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ReadWriteManyEntriesCommand{");
sb.append("entries=").append(entries);
sb.append(", f=").append(f.getClass().getName());
sb.append(", isForwarded=").append(isForwarded);
sb.append('}');
return sb.toString();
}
@Override
public Collection<Object> getKeysToLock() {
// TODO: fixup the generics
return (Collection<Object>) entries.keySet();
}
@Override
public Mutation<K, V, ?> toMutation(K key) {
return new Mutations.ReadWriteWithValue(entries.get(key), f);
}
}