Skip to content

Commit 09f6e0a

Browse files
committed
add time unit for ttl
1 parent 1b5edcd commit 09f6e0a

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

src/main/java/org/tikv/raw/RawKVClientBase.java

+69
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Optional;
24+
import java.util.concurrent.TimeUnit;
2425
import org.tikv.common.TiSession;
2526
import org.tikv.common.util.Pair;
2627
import org.tikv.common.util.ScanOption;
2728
import org.tikv.kvproto.Kvrpcpb;
2829

2930
public interface RawKVClientBase extends AutoCloseable {
31+
3032
// https://www.github.com/pingcap/tidb/blob/master/store/tikv/rawkv.go
3133
int MAX_RAW_SCAN_LIMIT = 10240;
3234
int MAX_RAW_BATCH_LIMIT = 1024;
@@ -51,6 +53,20 @@ public interface RawKVClientBase extends AutoCloseable {
5153
*/
5254
void put(ByteString key, ByteString value, long ttl);
5355

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+
5470
/**
5571
* Put a key-value pair if it does not exist. This API is atomic.
5672
*
@@ -76,6 +92,25 @@ public interface RawKVClientBase extends AutoCloseable {
7692
*/
7793
Optional<ByteString> putIfAbsent(ByteString key, ByteString value, long ttl);
7894

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+
79114
/**
80115
* Put a key-value pair if the prevValue matched the value in TiKV. This API is atomic.
81116
*
@@ -97,6 +132,27 @@ public interface RawKVClientBase extends AutoCloseable {
97132
*/
98133
void compareAndSet(ByteString key, Optional<ByteString> prevValue, ByteString value, long ttl);
99134

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+
100156
/**
101157
* Put a set of raw key-value pair to TiKV.
102158
*
@@ -112,6 +168,19 @@ public interface RawKVClientBase extends AutoCloseable {
112168
*/
113169
void batchPut(Map<ByteString, ByteString> kvPairs, long ttl);
114170

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+
115184
/**
116185
* Get a raw key-value pair from TiKV if key exists
117186
*

src/test/java/org/tikv/raw/CASTest.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.google.protobuf.ByteString;
2424
import java.util.Optional;
25+
import java.util.concurrent.TimeUnit;
2526
import org.junit.After;
2627
import org.junit.Assert;
2728
import org.junit.Before;
@@ -34,6 +35,7 @@
3435
import org.tikv.common.exception.RawCASConflictException;
3536

3637
public class CASTest extends BaseRawKVTest {
38+
3739
private RawKVClient client;
3840
private boolean initialized;
3941
private static final Logger logger = LoggerFactory.getLogger(RawKVClientTest.class);
@@ -81,21 +83,22 @@ public void rawCASTest() {
8183

8284
@Test
8385
public void rawPutIfAbsentTest() {
84-
long ttl = 10;
86+
long duration = 10;
87+
TimeUnit timeUnit = TimeUnit.SECONDS;
8588
ByteString key = ByteString.copyFromUtf8("key_atomic");
8689
ByteString value = ByteString.copyFromUtf8("value");
8790
ByteString value2 = ByteString.copyFromUtf8("value2");
8891
client.delete(key);
89-
Optional<ByteString> res1 = client.putIfAbsent(key, value, ttl);
92+
Optional<ByteString> res1 = client.putIfAbsent(key, value, duration, timeUnit);
9093
assertFalse(res1.isPresent());
91-
Optional<ByteString> res2 = client.putIfAbsent(key, value2, ttl);
94+
Optional<ByteString> res2 = client.putIfAbsent(key, value2, duration, timeUnit);
9295
assertEquals(res2.get(), value);
9396
try {
94-
Thread.sleep(ttl * 1000 + 100);
97+
Thread.sleep(duration * 1000 + 100);
9598
} catch (InterruptedException e) {
9699
Thread.currentThread().interrupt();
97100
}
98-
Optional<ByteString> res3 = client.putIfAbsent(key, value, ttl);
101+
Optional<ByteString> res3 = client.putIfAbsent(key, value, duration, timeUnit);
99102
assertFalse(res3.isPresent());
100103
}
101104
}

src/test/java/org/tikv/raw/RawKVClientTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.tikv.kvproto.Kvrpcpb.KvPair;
6969

7070
public class RawKVClientTest extends BaseRawKVTest {
71+
7172
private static final String RAW_PREFIX = "raw_\u0001_";
7273
private static final int KEY_POOL_SIZE = 1000000;
7374
private static final int TEST_CASES = 10000;
@@ -143,7 +144,7 @@ public void getKeyTTLTest() {
143144
long ttl = 10;
144145
ByteString key = ByteString.copyFromUtf8("key_ttl");
145146
ByteString value = ByteString.copyFromUtf8("value");
146-
client.put(key, value, ttl);
147+
client.put(key, value, ttl, TimeUnit.SECONDS);
147148
for (int i = 0; i < 9; i++) {
148149
Optional<Long> t = client.getKeyTTL(key);
149150
logger.info("current ttl of key is " + t.orElse(null));

0 commit comments

Comments
 (0)