Skip to content

Commit 83096a0

Browse files
authored
feat: add hint api for Context (#43)
1 parent e9d0860 commit 83096a0

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

ingester-rpc/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,17 @@
3131
<groupId>${project.groupId}</groupId>
3232
<artifactId>ingester-common</artifactId>
3333
</dependency>
34+
35+
<!-- test -->
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.mockito</groupId>
43+
<artifactId>mockito-all</artifactId>
44+
<scope>test</scope>
45+
</dependency>
3446
</dependencies>
3547
</project>

ingester-rpc/src/main/java/io/greptime/rpc/Context.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
@SuppressWarnings({"unchecked", "unused"})
2929
public class Context implements Copiable<Context> {
3030

31+
private static final String HINT_PREFIX = "x-greptime-hint:";
32+
3133
private final Map<String, Object> ctx = new HashMap<>();
3234

3335
/**
@@ -48,6 +50,17 @@ public static Context of(String key, Object value) {
4850
return new Context().with(key, value);
4951
}
5052

53+
/**
54+
* Creates a new {@link Context} with the specified hint key-value pair.
55+
*
56+
* @param key the hint key
57+
* @param value the value
58+
* @return the new {@link Context}
59+
*/
60+
public static Context hint(String key, Object value) {
61+
return Context.of(HINT_PREFIX + key, value);
62+
}
63+
5164
/**
5265
* Adds the specified key-value pair to this {@link Context}.
5366
*
@@ -62,6 +75,17 @@ public Context with(String key, Object value) {
6275
return this;
6376
}
6477

78+
/**
79+
* Adds the specified hint key-value pair to this {@link Context}.
80+
*
81+
* @param key the hint key
82+
* @param value the value
83+
* @return this {@link Context}
84+
*/
85+
public Context withHint(String key, Object value) {
86+
return with(HINT_PREFIX + key, value);
87+
}
88+
6589
/**
6690
* Gets the value of the specified key.
6791
*
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2023 Greptime Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.greptime.rpc;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
/**
23+
* @author jiachun.fjc
24+
*/
25+
public class ContextTest {
26+
@Test
27+
public void newDefaultShouldReturnEmptyContextTest() {
28+
Context context = Context.newDefault();
29+
Assert.assertTrue(context.entrySet().isEmpty());
30+
}
31+
32+
@Test
33+
public void ofShouldReturnContextWithOneEntryTest() {
34+
Context context = Context.of("key", "value");
35+
Assert.assertEquals("value", context.get("key"));
36+
}
37+
38+
@Test
39+
public void hintShouldReturnContextWithHintEntryTest() {
40+
Context context = Context.hint("key", "value");
41+
Assert.assertEquals("value", context.get("x-greptime-hint:key"));
42+
}
43+
44+
@Test
45+
public void withShouldAddEntryToContextTest() {
46+
Context context = Context.newDefault();
47+
context.with("key", "value");
48+
Assert.assertEquals("value", context.get("key"));
49+
}
50+
51+
@Test
52+
public void withHintShouldAddHintEntryToContextTest() {
53+
Context context = Context.newDefault();
54+
context.withHint("key", "value").with("key2", "value");
55+
Assert.assertEquals("value", context.get("x-greptime-hint:key"));
56+
Assert.assertEquals("value", context.get("key2"));
57+
}
58+
59+
@Test
60+
public void getShouldReturnNullForNonExistingKeyTest() {
61+
Context context = Context.newDefault();
62+
Assert.assertNull(context.get("key"));
63+
}
64+
65+
@Test
66+
public void removeShouldRemoveEntryFromContextTest() {
67+
Context context = Context.of("key", "value");
68+
String value = context.remove("key");
69+
Assert.assertNull(context.get("key"));
70+
Assert.assertEquals("value", value);
71+
}
72+
73+
@Test
74+
public void getOrDefaultShouldReturnDefaultValueForNonExistingKeyTest() {
75+
Context context = Context.newDefault();
76+
Assert.assertEquals("default", context.getOrDefault("key", "default"));
77+
}
78+
79+
@Test
80+
public void clearShouldRemoveAllEntriesFromContextTest() {
81+
Context context = Context.of("key", "value");
82+
context.clear();
83+
Assert.assertTrue(context.entrySet().isEmpty());
84+
}
85+
86+
@Test
87+
public void copyShouldReturnIdenticalContextTest() {
88+
Context context = Context.of("key", "value");
89+
Context copy = context.copy();
90+
Assert.assertEquals(context.get("key").toString(), copy.get("key").toString());
91+
}
92+
}

0 commit comments

Comments
 (0)