21
21
import java .util .List ;
22
22
import java .util .Map ;
23
23
import java .util .Optional ;
24
+ import java .util .concurrent .TimeUnit ;
24
25
import org .tikv .common .TiSession ;
25
26
import org .tikv .common .util .Pair ;
26
27
import org .tikv .common .util .ScanOption ;
27
28
import org .tikv .kvproto .Kvrpcpb ;
28
29
29
30
public interface RawKVClientBase extends AutoCloseable {
31
+
30
32
// https://www.github.com/pingcap/tidb/blob/master/store/tikv/rawkv.go
31
33
int MAX_RAW_SCAN_LIMIT = 10240 ;
32
34
int MAX_RAW_BATCH_LIMIT = 1024 ;
@@ -51,6 +53,20 @@ public interface RawKVClientBase extends AutoCloseable {
51
53
*/
52
54
void put (ByteString key , ByteString value , long ttl );
53
55
56
+ /**
57
+ * Put a raw key-value pair to TiKV
58
+ *
59
+ * @see #put(ByteString, ByteString, long)
60
+ * @param key raw key
61
+ * @param value raw value
62
+ * @param duration the duration of the key, 0 means the key will never be outdated
63
+ * @param timeUnit the time unit of duration
64
+ */
65
+ default void put (ByteString key , ByteString value , long duration , TimeUnit timeUnit ) {
66
+ Long seconds = timeUnit .toSeconds (duration );
67
+ put (key , value , seconds );
68
+ }
69
+
54
70
/**
55
71
* Put a key-value pair if it does not exist. This API is atomic.
56
72
*
@@ -76,6 +92,25 @@ public interface RawKVClientBase extends AutoCloseable {
76
92
*/
77
93
Optional <ByteString > putIfAbsent (ByteString key , ByteString value , long ttl );
78
94
95
+ /**
96
+ * Put a key-value pair with TTL if it does not exist. This API is atomic.
97
+ *
98
+ * <p>To use this API, please enable `tikv.enable_atomic_for_cas`.
99
+ *
100
+ * @see #putIfAbsent(ByteString, ByteString, long)
101
+ * @param key key
102
+ * @param value value
103
+ * @param duration duration of key, 0 means the key will never be outdated.
104
+ * @param timeUnit the time unit of duration
105
+ * @return a ByteString. returns Optional.EMPTY if the value is written successfully. returns the
106
+ * previous key if the value already exists, and does not write to TiKV.
107
+ */
108
+ default Optional <ByteString > putIfAbsent (
109
+ ByteString key , ByteString value , long duration , TimeUnit timeUnit ) {
110
+ Long seconds = timeUnit .toSeconds (duration );
111
+ return putIfAbsent (key , value , seconds );
112
+ }
113
+
79
114
/**
80
115
* Put a key-value pair if the prevValue matched the value in TiKV. This API is atomic.
81
116
*
@@ -97,6 +132,27 @@ public interface RawKVClientBase extends AutoCloseable {
97
132
*/
98
133
void compareAndSet (ByteString key , Optional <ByteString > prevValue , ByteString value , long ttl );
99
134
135
+ /**
136
+ * pair if the prevValue matched the value in TiKV. This API is atomic.
137
+ *
138
+ * <p>To use this API, please enable `tikv.enable_atomic_for_cas`.
139
+ *
140
+ * @see #compareAndSet(ByteString, Optional, ByteString, long)
141
+ * @param key key
142
+ * @param value value
143
+ * @param duration duration of key , 0 means the key will never be outdated.
144
+ * @param timeUnit time unit of duration
145
+ */
146
+ default void compareAndSet (
147
+ ByteString key ,
148
+ Optional <ByteString > prevValue ,
149
+ ByteString value ,
150
+ long duration ,
151
+ TimeUnit timeUnit ) {
152
+ long seconds = timeUnit .toSeconds (duration );
153
+ compareAndSet (key , prevValue , value , seconds );
154
+ }
155
+
100
156
/**
101
157
* Put a set of raw key-value pair to TiKV.
102
158
*
@@ -112,6 +168,19 @@ public interface RawKVClientBase extends AutoCloseable {
112
168
*/
113
169
void batchPut (Map <ByteString , ByteString > kvPairs , long ttl );
114
170
171
+ /**
172
+ * Put a set of raw key-value pair to TiKV.
173
+ *
174
+ * @see #batchPut(Map, long)
175
+ * @param kvPairs kvPairs
176
+ * @param duration the duration of keys to be put, 0 means the keys will never be outdated
177
+ * @param timeUnit time unit of duration
178
+ */
179
+ default void batchPut (Map <ByteString , ByteString > kvPairs , long duration , TimeUnit timeUnit ) {
180
+ long seconds = timeUnit .toSeconds (duration );
181
+ batchPut (kvPairs , seconds );
182
+ }
183
+
115
184
/**
116
185
* Get a raw key-value pair from TiKV if key exists
117
186
*
0 commit comments