-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathParamBuilder.java
More file actions
231 lines (211 loc) · 5.87 KB
/
ParamBuilder.java
File metadata and controls
231 lines (211 loc) · 5.87 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.ecwid.consul.v1.transactions;
import com.ecwid.consul.v1.transactions.model.Operate;
import com.ecwid.consul.v1.transactions.model.TxnReqItem;
import com.ecwid.consul.v1.transactions.model.Verb;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.LinkedList;
import java.util.List;
/**
* Build consul Transaction parameters
* <p>
* https://www.consul.io/api/txn.html#tables-of-operations
*
* @author Trisia Cliven (quanguanyu@qq.com)
* @since 2019-11-11 12:21:52
*/
public class ParamBuilder {
private List<TxnReqItem> operates;
private ParamBuilder() {
operates = new LinkedList<>();
}
public static ParamBuilder getInstance() {
return new ParamBuilder();
}
/**
* Sets the Key to the given Value
*
* @param key key
* @param value value (do not base64)
* @param flag [option]
* @return this
*/
public ParamBuilder kvSet(String key, String value, int... flag) {
Operate op = new Operate(Verb.set)
.setKey(key)
.setValue(Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8)));
if (flag != null && flag.length > 0) {
op.setFlags(flag[0]);
}
operates.add(new TxnReqItem(op));
return this;
}
/**
* Sets, but with CAS semantics
*
* @param key key
* @param value value
* @param index index
* @param flag [option]
* @return this
*/
public ParamBuilder kvCas(String key, String value, int index, int... flag) {
Operate op = new Operate(Verb.cas)
.setKey(key)
.setValue(Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8)))
.setIndex(index);
if (flag != null && flag.length > 0) {
op.setFlags(flag[0]);
}
operates.add(new TxnReqItem(op));
return this;
}
/**
* Lock with the given Session
*
* @param key key
* @param value value
* @param session session id
* @param flag [option]
* @return this
*/
public ParamBuilder kvLock(String key, String value, String session, int... flag) {
Operate op = new Operate(Verb.lock)
.setKey(key)
.setValue(Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8)))
.setSession(session);
if (flag != null && flag.length > 0) {
op.setFlags(flag[0]);
}
operates.add(new TxnReqItem(op));
return this;
}
/**
* Unlock with the given Session
*
* @param key key
* @param value value
* @param session session id
* @param flag [option]
* @return this
*/
public ParamBuilder kvUnlock(String key, String value, String session, int... flag) {
Operate op = new Operate(Verb.unlock)
.setKey(key)
.setValue(Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8)))
.setSession(session);
if (flag != null && flag.length > 0) {
op.setFlags(flag[0]);
}
operates.add(new TxnReqItem(op));
return this;
}
/**
* Get the key, fails if it does not exist
*
* @param key key
* @return this
*/
public ParamBuilder kvGet(String key) {
Operate op = new Operate(Verb.get)
.setKey(key);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Gets all keys with the prefix
*
* @param key key
* @return this
*/
public ParamBuilder kvGetTree(String key) {
Operate op = new Operate(Verb.get_tree)
.setKey(key);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Fail if modify index != index
*
* @param key key
* @param index index
* @return this
*/
public ParamBuilder kvCheckIndex(String key, int index) {
Operate op = new Operate(Verb.check_index)
.setKey(key)
.setIndex(index);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Fail if not locked by session
*
* @param key key
* @param session session id
* @return this
*/
public ParamBuilder kvCheckSession(String key, String session) {
Operate op = new Operate(Verb.check_session)
.setKey(key)
.setSession(session);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Fail if key exists
*
* @param key key
* @return this
*/
public ParamBuilder kvCheckNotExists(String key) {
Operate op = new Operate(Verb.check_not_exists)
.setKey(key);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Delete the key
*
* @param key key
* @return this
*/
public ParamBuilder kvDelete(String key) {
Operate op = new Operate(Verb.delete)
.setKey(key);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Delete all keys with a prefix
*
* @param key key
* @return this
*/
public ParamBuilder kvDeleteTree(String key) {
Operate op = new Operate(Verb.delete_tree)
.setKey(key);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Delete, but with CAS semantics
*
* @param key key
* @return this
*/
public ParamBuilder kvDeleteCas(String key) {
Operate op = new Operate(Verb.delete_cas)
.setKey(key);
operates.add(new TxnReqItem(op));
return this;
}
/**
* Build Request parameters
*
* @return parameters
*/
public List<TxnReqItem> build() {
return this.operates;
}
}