-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Transaction.java
94 lines (78 loc) · 2.04 KB
/
Transaction.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
package redis.clients.jedis;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.exceptions.JedisDataException;
/**
* Transaction is nearly identical to Pipeline, only differences are the multi/discard behaviors
*/
public class Transaction extends MultiKeyPipelineBase implements Closeable {
protected boolean inTransaction = true;
protected Transaction() {
// client will be set later in transaction block
}
public Transaction(final Client client) {
this.client = client;
}
@Override
protected Client getClient(String key) {
return client;
}
@Override
protected Client getClient(byte[] key) {
return client;
}
public void clear() {
if (inTransaction) {
discard();
}
}
public List<Object> exec() {
// Discard QUEUED or ERROR
client.getMany(getPipelinedResponseLength());
client.exec();
inTransaction = false;
List<Object> unformatted = client.getObjectMultiBulkReply();
if (unformatted == null) {
return null;
}
List<Object> formatted = new ArrayList<>();
for (Object o : unformatted) {
try {
formatted.add(generateResponse(o).get());
} catch (JedisDataException e) {
formatted.add(e);
}
}
return formatted;
}
public List<Response<?>> execGetResponse() {
// Discard QUEUED or ERROR
client.getMany(getPipelinedResponseLength());
client.exec();
inTransaction = false;
List<Object> unformatted = client.getObjectMultiBulkReply();
if (unformatted == null) {
return null;
}
List<Response<?>> response = new ArrayList<>();
for (Object o : unformatted) {
response.add(generateResponse(o));
}
return response;
}
public String discard() {
client.getMany(getPipelinedResponseLength());
client.discard();
inTransaction = false;
clean();
return client.getStatusCodeReply();
}
public void setClient(Client client) {
this.client = client;
}
@Override
public void close() {
clear();
}
}