/
SetState.java
173 lines (156 loc) · 4.55 KB
/
SetState.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
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.kuujo.copycat.collections.state;
import net.kuujo.copycat.PersistenceMode;
import net.kuujo.copycat.raft.Commit;
import net.kuujo.copycat.raft.StateMachine;
import net.kuujo.copycat.raft.StateMachineExecutor;
import net.kuujo.copycat.raft.session.Session;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Distributed set state machine.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class SetState extends StateMachine {
private Map<Integer, Commit<? extends SetCommands.TtlCommand>> map;
private final Set<Long> sessions = new HashSet<>();
@Override
public void configure(StateMachineExecutor executor) {
executor.register(SetCommands.Add.class, this::add);
executor.register(SetCommands.Contains.class, this::contains);
executor.register(SetCommands.Remove.class, this::remove);
executor.register(SetCommands.Size.class, this::size);
executor.register(SetCommands.IsEmpty.class, this::isEmpty);
executor.register(SetCommands.Clear.class, this::clear);
}
@Override
public void register(Session session) {
sessions.add(session.id());
}
@Override
public void expire(Session session) {
sessions.remove(session.id());
}
@Override
public void close(Session session) {
sessions.remove(session.id());
}
/**
* Returns a boolean value indicating whether the given commit is active.
*/
private boolean isActive(Commit<? extends SetCommands.TtlCommand> commit, Instant instant) {
if (commit == null) {
return false;
} else if (commit.operation().mode() == PersistenceMode.EPHEMERAL && !sessions.contains(commit.session().id())) {
return false;
} else if (commit.operation().ttl() != 0 && commit.operation().ttl() < instant.toEpochMilli() - commit.time().toEpochMilli()) {
return false;
}
return true;
}
/**
* Handles a contains commit.
*/
protected boolean contains(Commit<SetCommands.Contains> commit) {
try {
if (map == null) {
return false;
}
Commit<? extends SetCommands.TtlCommand> command = map.get(commit.operation().value());
if (!isActive(command, commit.time())) {
map.remove(commit.operation().value());
return false;
}
return true;
} finally {
commit.close();
}
}
/**
* Handles an add commit.
*/
protected boolean add(Commit<SetCommands.Add> commit) {
if (map == null) {
map = new HashMap<>();
}
Commit<? extends SetCommands.TtlCommand> previous = map.get(commit.operation().value());
if (!isActive(commit, now())) {
commit.clean();
return false;
} else if (!isActive(previous, commit.time())) {
if (previous != null)
previous.clean();
map.put(commit.operation().value(), commit);
return true;
} else {
commit.clean();
return false;
}
}
/**
* Handles a remove commit.
*/
protected boolean remove(Commit<SetCommands.Remove> commit) {
if (map == null) {
commit.clean();
return false;
} else {
Commit<? extends SetCommands.TtlCommand> previous = map.remove(commit.operation().value());
if (previous == null) {
commit.clean();
return false;
} else {
previous.clean();
return isActive(previous, commit.time());
}
}
}
/**
* Handles a count commit.
*/
protected int size(Commit<SetCommands.Size> commit) {
try {
return map != null ? map.size() : 0;
} finally {
commit.close();
}
}
/**
* Handles an is empty commit.
*/
protected boolean isEmpty(Commit<SetCommands.IsEmpty> commit) {
try {
return map == null || map.isEmpty();
} finally {
commit.close();
}
}
/**
* Handles a clear commit.
*/
protected void clear(Commit<SetCommands.Clear> commit) {
if (map == null) {
commit.clean();
} else {
map.clear();
}
}
}