|
1 | 1 | import cloud.xline.jxline.Client; |
2 | 2 | import cloud.xline.jxline.KV; |
| 3 | +import cloud.xline.jxline.Txn; |
3 | 4 | import cloud.xline.jxline.kv.DeleteResponse; |
4 | 5 | import cloud.xline.jxline.kv.GetResponse; |
5 | 6 | import cloud.xline.jxline.kv.PutResponse; |
| 7 | +import cloud.xline.jxline.kv.TxnResponse; |
| 8 | +import cloud.xline.jxline.op.Cmp; |
| 9 | +import cloud.xline.jxline.op.CmpTarget; |
| 10 | +import cloud.xline.jxline.op.Op; |
6 | 11 | import io.etcd.jetcd.ByteSequence; |
7 | 12 | import io.etcd.jetcd.options.DeleteOption; |
8 | 13 | import io.etcd.jetcd.options.GetOption; |
| 14 | +import io.etcd.jetcd.options.PutOption; |
9 | 15 | import org.junit.jupiter.api.BeforeAll; |
10 | 16 | import org.junit.jupiter.api.Test; |
11 | 17 | import org.junit.jupiter.api.Timeout; |
12 | 18 | import static org.assertj.core.api.Assertions.*; |
13 | 19 |
|
14 | 20 | import java.nio.charset.Charset; |
15 | 21 | import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.List; |
16 | 23 | import java.util.concurrent.CompletableFuture; |
17 | 24 | import java.util.concurrent.ExecutionException; |
18 | 25 |
|
@@ -121,4 +128,110 @@ public void testDelete() throws Exception { |
121 | 128 | DeleteResponse delResp = deleteFuture.get(); |
122 | 129 | assertThat(delResp.getDeleted()).isEqualTo(resp.getKvs().size()); |
123 | 130 | } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testGetSortedPrefix() throws Exception { |
| 134 | + String prefix = randomString(); |
| 135 | + int numPrefix = 3; |
| 136 | + putKeysWithPrefix(prefix, numPrefix); |
| 137 | + |
| 138 | + GetOption option = |
| 139 | + GetOption.builder() |
| 140 | + .withSortField(GetOption.SortTarget.KEY) |
| 141 | + .withSortOrder(GetOption.SortOrder.DESCEND) |
| 142 | + .isPrefix(true) |
| 143 | + .build(); |
| 144 | + CompletableFuture<GetResponse> getFeature = kvClient.get(bytesOf(prefix), option); |
| 145 | + GetResponse response = getFeature.get(); |
| 146 | + |
| 147 | + assertThat(response.getKvs()).hasSize(numPrefix); |
| 148 | + for (int i = 0; i < numPrefix; i++) { |
| 149 | + assertThat(response.getKvs().get(i).getKey().toString(StandardCharsets.UTF_8)) |
| 150 | + .isEqualTo(prefix + (numPrefix - i - 1)); |
| 151 | + assertThat(response.getKvs().get(i).getValue().toString(StandardCharsets.UTF_8)) |
| 152 | + .isEqualTo(String.valueOf(numPrefix - i - 1)); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testGetAndDeleteWithPrefix() throws Exception { |
| 158 | + String prefix = randomString(); |
| 159 | + ByteSequence key = bytesOf(prefix); |
| 160 | + int numPrefixes = 10; |
| 161 | + |
| 162 | + putKeysWithPrefix(prefix, numPrefixes); |
| 163 | + |
| 164 | + // verify get withPrefix. |
| 165 | + CompletableFuture<GetResponse> getFuture = |
| 166 | + kvClient.get(key, GetOption.builder().isPrefix(true).build()); |
| 167 | + GetResponse getResp = getFuture.get(); |
| 168 | + assertThat(getResp.getCount()).isEqualTo(numPrefixes); |
| 169 | + |
| 170 | + // verify del withPrefix. |
| 171 | + DeleteOption deleteOpt = DeleteOption.builder().isPrefix(true).build(); |
| 172 | + CompletableFuture<DeleteResponse> delFuture = kvClient.delete(key, deleteOpt); |
| 173 | + DeleteResponse delResp = delFuture.get(); |
| 174 | + assertThat(delResp.getDeleted()).isEqualTo(numPrefixes); |
| 175 | + } |
| 176 | + |
| 177 | + private static void putKeysWithPrefix(String prefix, int numPrefixes) |
| 178 | + throws ExecutionException, InterruptedException { |
| 179 | + for (int i = 0; i < numPrefixes; i++) { |
| 180 | + ByteSequence key = bytesOf(prefix + i); |
| 181 | + ByteSequence value = bytesOf("" + i); |
| 182 | + kvClient.put(key, value).get(); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void testTxn() throws Exception { |
| 188 | + ByteSequence sampleKey = bytesOf("txn_key"); |
| 189 | + ByteSequence sampleValue = bytesOf("xyz"); |
| 190 | + ByteSequence cmpValue = bytesOf("abc"); |
| 191 | + ByteSequence putValue = bytesOf("XYZ"); |
| 192 | + ByteSequence putValueNew = bytesOf("ABC"); |
| 193 | + // put the original txn key value pair |
| 194 | + kvClient.put(sampleKey, sampleValue).get(); |
| 195 | + |
| 196 | + // construct txn operation |
| 197 | + Txn txn = kvClient.txn(); |
| 198 | + Cmp cmp = new Cmp(sampleKey, Cmp.Op.GREATER, CmpTarget.value(cmpValue)); |
| 199 | + CompletableFuture<TxnResponse> txnResp = |
| 200 | + txn.If(cmp) |
| 201 | + .Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)) |
| 202 | + .Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)) |
| 203 | + .commit(); |
| 204 | + txnResp.get(); |
| 205 | + // get the value |
| 206 | + GetResponse getResp = kvClient.get(sampleKey).get(); |
| 207 | + assertThat(getResp.getKvs()).hasSize(1); |
| 208 | + assertThat(getResp.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) |
| 209 | + .isEqualTo(putValue.toString(StandardCharsets.UTF_8)); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void testTxnForCmpOpNotEqual() throws Exception { |
| 214 | + ByteSequence sampleKey = bytesOf("txn_key"); |
| 215 | + ByteSequence sampleValue = bytesOf("xyz"); |
| 216 | + ByteSequence cmpValue = bytesOf("abc"); |
| 217 | + ByteSequence putValue = bytesOf("XYZ"); |
| 218 | + ByteSequence putValueNew = bytesOf("ABC"); |
| 219 | + // put the original txn key value pair |
| 220 | + kvClient.put(sampleKey, sampleValue).get(); |
| 221 | + |
| 222 | + // construct txn operation |
| 223 | + Txn txn = kvClient.txn(); |
| 224 | + Cmp cmp = new Cmp(sampleKey, Cmp.Op.NOT_EQUAL, CmpTarget.value(cmpValue)); |
| 225 | + CompletableFuture<TxnResponse> txnResp = |
| 226 | + txn.If(cmp) |
| 227 | + .Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)) |
| 228 | + .Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)) |
| 229 | + .commit(); |
| 230 | + txnResp.get(); |
| 231 | + // get the value |
| 232 | + GetResponse getResp = kvClient.get(sampleKey).get(); |
| 233 | + assertThat(getResp.getKvs()).hasSize(1); |
| 234 | + assertThat(getResp.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) |
| 235 | + .isEqualTo(putValue.toString(StandardCharsets.UTF_8)); |
| 236 | + } |
124 | 237 | } |
0 commit comments