forked from infinispan/infinispan
/
WriteOnlyManyEntriesCommand.java
143 lines (119 loc) · 4.3 KB
/
WriteOnlyManyEntriesCommand.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
package org.infinispan.commands.functional;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import org.infinispan.commands.CommandInvocationId;
import org.infinispan.commands.Visitor;
import org.infinispan.commons.marshall.MarshallUtil;
import org.infinispan.functional.EntryView.WriteEntryView;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.functional.impl.EntryViews;
import org.infinispan.functional.impl.Params;
public final class WriteOnlyManyEntriesCommand<K, V> extends AbstractWriteManyCommand<K, V> {
public static final byte COMMAND_ID = 57;
private Map<? extends K, ? extends V> entries;
private BiConsumer<V, WriteEntryView<V>> f;
public WriteOnlyManyEntriesCommand(Map<? extends K, ? extends V> entries, BiConsumer<V, WriteEntryView<V>> f, Params params, CommandInvocationId commandInvocationId) {
super(commandInvocationId, params);
this.entries = entries;
this.f = f;
}
public WriteOnlyManyEntriesCommand(WriteOnlyManyEntriesCommand<K, V> command) {
super(command);
this.entries = command.entries;
this.f = command.f;
}
public WriteOnlyManyEntriesCommand() {
}
public Map<? extends K, ? extends V> getEntries() {
return entries;
}
public void setEntries(Map<? extends K, ? extends V> entries) {
this.entries = entries;
}
public final WriteOnlyManyEntriesCommand<K, V> 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 = (BiConsumer<V, WriteEntryView<V>>) input.readObject();
isForwarded = input.readBoolean();
params = Params.readObject(input);
topologyId = input.readInt();
flags = input.readLong();
}
@Override
public Object perform(InvocationContext ctx) throws Throwable {
for (Map.Entry<? extends K, ? extends V> entry : entries.entrySet()) {
CacheEntry<K, V> cacheEntry = ctx.lookupEntry(entry.getKey());
// Could be that the key is not local, 'null' is how this is signalled
if (cacheEntry == null) {
throw new IllegalStateException();
}
f.accept(entry.getValue(), EntryViews.writeOnly(cacheEntry));
}
return null;
}
@Override
public boolean isReturnValueExpected() {
// Scattered cache always needs some response.
return true;
}
@Override
public Collection<?> getAffectedKeys() {
return entries.keySet();
}
@Override
public Object acceptVisitor(InvocationContext ctx, Visitor visitor) throws Throwable {
return visitor.visitWriteOnlyManyEntriesCommand(ctx, this);
}
@Override
public LoadType loadType() {
return LoadType.DONT_LOAD;
}
@Override
public boolean isWriteOnly() {
return true;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("WriteOnlyManyEntriesCommand{");
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.WriteWithValue<>(entries.get(key), f);
}
}