Skip to content

Commit 76c16ce

Browse files
committed
feat: add sync client
Signed-off-by: moxiaoying <[email protected]>
1 parent 2249dea commit 76c16ce

File tree

14 files changed

+1204
-42
lines changed

14 files changed

+1204
-42
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2024 openGemini Authors
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.opengemini.client.api;
18+
19+
import java.util.List;
20+
21+
/**
22+
// * Interface to access a OpenGemini database provides a set of blocking methods.
23+
*/
24+
public interface OpenGeminiSyncClient extends AutoCloseable {
25+
26+
/**
27+
* Create a new database.
28+
*
29+
* @param database the name of the new database.
30+
*/
31+
void createDatabase(String database) throws OpenGeminiException;
32+
33+
/**
34+
* Drop a database.
35+
*
36+
* @param database the name of the database to drop.
37+
*/
38+
void dropDatabase(String database) throws OpenGeminiException;
39+
40+
/**
41+
* Show all available databases.
42+
*/
43+
List<String> showDatabases() throws OpenGeminiException;
44+
45+
/**
46+
* Create a retention policy.
47+
*
48+
* @param database the name of the database.
49+
* @param rpConfig the config of the retention policy
50+
* @param isDefault if the retention policy is the default retention policy for the database or not
51+
*/
52+
void createRetentionPolicy(String database, RpConfig rpConfig, boolean isDefault) throws OpenGeminiException;
53+
54+
/**
55+
* Show all available retention policies.
56+
*
57+
* @param database the name of the database.
58+
*/
59+
List<RetentionPolicy> showRetentionPolicies(String database) throws OpenGeminiException;
60+
61+
/**
62+
* Drop a retention policy.
63+
*
64+
* @param database the name of the database.
65+
* @param retentionPolicy the name of the retention policy to drop.
66+
*/
67+
void dropRetentionPolicy(String database, String retentionPolicy) throws OpenGeminiException;
68+
69+
/**
70+
* Execute a query against a database.
71+
*
72+
* @param query the query to execute.
73+
*/
74+
QueryResult query(Query query) throws OpenGeminiException;
75+
76+
/**
77+
* Write a single point to the database.
78+
*
79+
* @param database the name of the database.
80+
* @param point the point to write.
81+
*/
82+
void write(String database, Point point) throws OpenGeminiException;
83+
84+
/**
85+
* Write points to the database.
86+
*
87+
* @param database the name of the database.
88+
* @param points the points to write.
89+
*/
90+
void write(String database, List<Point> points) throws OpenGeminiException;
91+
92+
/**
93+
* Write a single point to the database.
94+
*
95+
* @param database the name of the database.
96+
* @param retentionPolicy the name of the retention policy.
97+
* @param point the point to write.
98+
*/
99+
void write(String database, String retentionPolicy, Point point) throws OpenGeminiException;
100+
101+
/**
102+
* Write points to the database.
103+
*
104+
* @param database the name of the database.
105+
* @param retentionPolicy the name of the retention policy.
106+
* @param points the points to write.
107+
*/
108+
void write(String database, String retentionPolicy, List<Point> points) throws OpenGeminiException;
109+
110+
/**
111+
* Ping the OpenGemini server
112+
*/
113+
Pong ping() throws OpenGeminiException;
114+
}

opengemini-client/src/main/java/io/opengemini/client/impl/OpenGeminiClientFactory.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@
2020
import io.opengemini.client.api.AuthType;
2121
import io.opengemini.client.api.BatchConfig;
2222
import io.opengemini.client.api.Configuration;
23+
import io.opengemini.client.api.OpenGeminiAsyncClient;
2324
import io.opengemini.client.api.OpenGeminiException;
25+
import io.opengemini.client.api.OpenGeminiSyncClient;
2426
import org.jetbrains.annotations.NotNull;
2527

2628
public class OpenGeminiClientFactory {
27-
public static OpenGeminiClient create(@NotNull Configuration configuration) throws OpenGeminiException {
29+
public static OpenGeminiAsyncClient create(@NotNull Configuration configuration) throws OpenGeminiException {
30+
validateConf(configuration);
31+
return new OpenGeminiClient(configuration);
32+
}
33+
34+
public static OpenGeminiSyncClient createSyncClient(@NotNull Configuration configuration)
35+
throws OpenGeminiException {
36+
validateConf(configuration);
37+
return new OpenGeminiSyncClientImpl(configuration);
38+
}
39+
40+
private static void validateConf(@NotNull Configuration configuration) throws OpenGeminiException {
2841
if (configuration.getAddresses() == null || configuration.getAddresses().isEmpty()) {
2942
throw new OpenGeminiException("must have at least one address");
3043
}
@@ -52,6 +65,5 @@ public static OpenGeminiClient create(@NotNull Configuration configuration) thro
5265
throw new OpenGeminiException("batch enabled, batch size must be great than 0");
5366
}
5467
}
55-
return new OpenGeminiClient(configuration);
5668
}
5769
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.opengemini.client.impl;
2+
3+
import io.opengemini.client.api.Configuration;
4+
import io.opengemini.client.api.OpenGeminiException;
5+
import io.opengemini.client.api.OpenGeminiSyncClient;
6+
import io.opengemini.client.api.Point;
7+
import io.opengemini.client.api.Pong;
8+
import io.opengemini.client.api.Query;
9+
import io.opengemini.client.api.QueryResult;
10+
import io.opengemini.client.api.RetentionPolicy;
11+
import io.opengemini.client.api.RpConfig;
12+
13+
import java.io.IOException;
14+
import java.util.List;
15+
import java.util.concurrent.CompletableFuture;
16+
import java.util.concurrent.TimeUnit;
17+
18+
public class OpenGeminiSyncClientImpl implements OpenGeminiSyncClient {
19+
protected Configuration conf;
20+
private OpenGeminiClient openGeminiAsyncClient;
21+
22+
OpenGeminiSyncClientImpl(Configuration conf) {
23+
this.conf = conf;
24+
this.openGeminiAsyncClient = new OpenGeminiClient(conf);
25+
}
26+
27+
@Override
28+
public void createDatabase(String database) throws OpenGeminiException {
29+
wrapFuture(openGeminiAsyncClient.createDatabase(database));
30+
}
31+
32+
@Override
33+
public void dropDatabase(String database) throws OpenGeminiException {
34+
wrapFuture(openGeminiAsyncClient.dropDatabase(database));
35+
}
36+
37+
@Override
38+
public List<String> showDatabases() throws OpenGeminiException {
39+
return wrapFuture(openGeminiAsyncClient.showDatabases());
40+
}
41+
42+
@Override
43+
public void createRetentionPolicy(String database, RpConfig rpConfig, boolean isDefault)
44+
throws OpenGeminiException {
45+
wrapFuture(openGeminiAsyncClient.createRetentionPolicy(database, rpConfig, isDefault));
46+
}
47+
48+
@Override
49+
public List<RetentionPolicy> showRetentionPolicies(String database) throws OpenGeminiException {
50+
return wrapFuture(openGeminiAsyncClient.showRetentionPolicies(database));
51+
}
52+
53+
@Override
54+
public void dropRetentionPolicy(String database, String retentionPolicy) throws OpenGeminiException {
55+
wrapFuture(openGeminiAsyncClient.dropRetentionPolicy(database, retentionPolicy));
56+
}
57+
58+
@Override
59+
public QueryResult query(Query query) throws OpenGeminiException {
60+
return wrapFuture(openGeminiAsyncClient.query(query));
61+
}
62+
63+
@Override
64+
public void write(String database, Point point) throws OpenGeminiException {
65+
wrapFuture(openGeminiAsyncClient.write(database, point));
66+
}
67+
68+
@Override
69+
public void write(String database, List<Point> points) throws OpenGeminiException {
70+
wrapFuture(openGeminiAsyncClient.write(database, points));
71+
}
72+
73+
@Override
74+
public void write(String database, String retentionPolicy, Point point) throws OpenGeminiException {
75+
wrapFuture(openGeminiAsyncClient.write(database, retentionPolicy, point));
76+
}
77+
78+
@Override
79+
public void write(String database, String retentionPolicy, List<Point> points) throws OpenGeminiException {
80+
wrapFuture(openGeminiAsyncClient.write(database, retentionPolicy, points));
81+
}
82+
83+
@Override
84+
public Pong ping() throws OpenGeminiException {
85+
return wrapFuture(openGeminiAsyncClient.ping());
86+
}
87+
88+
@Override
89+
public void close() throws IOException {
90+
openGeminiAsyncClient.close();
91+
}
92+
93+
private <T> T wrapFuture(CompletableFuture<T> future) throws OpenGeminiException {
94+
try {
95+
return future.get(conf.getHttpConfig().timeout().toMillis(), TimeUnit.MILLISECONDS);
96+
} catch (Exception e) {
97+
throw new OpenGeminiException(e);
98+
}
99+
}
100+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2025 openGemini Authors
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.opengemini.client.impl;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
public interface FactoryTestCase {
22+
23+
@Test
24+
void testGetClientWithNullAddresses();
25+
26+
@Test
27+
void testGetClientWithEmptyAddresses();
28+
29+
@Test
30+
void testGetClientWithEmptyToken();
31+
32+
@Test
33+
void testGetClientWithEmptyUserName();
34+
35+
@Test
36+
void testGetClientWithNullPassword();
37+
38+
@Test
39+
void testGetClientWithInvalidBatchInterval();
40+
41+
@Test
42+
void testGetClientWithInvalidBatchSize();
43+
}

opengemini-client/src/test/java/io/opengemini/client/impl/OpenGeminiClientFactoryTest.java renamed to opengemini-client/src/test/java/io/opengemini/client/impl/OpenGeminiAsyncClientFactoryTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
import java.util.ArrayList;
3030
import java.util.List;
3131

32-
public class OpenGeminiClientFactoryTest {
33-
32+
public class OpenGeminiAsyncClientFactoryTest implements FactoryTestCase {
3433
private static Configuration configuration;
3534

3635
private static AuthConfig authConfig;
@@ -44,6 +43,7 @@ public static void setUp() {
4443
batchConfig = new BatchConfig();
4544
}
4645

46+
@Override
4747
@Test
4848
public void testGetClientWithNullAddresses() {
4949
configuration.setAddresses(null);
@@ -53,6 +53,7 @@ public void testGetClientWithNullAddresses() {
5353
Assertions.assertEquals("must have at least one address", actualException.getMessage());
5454
}
5555

56+
@Override
5657
@Test
5758
public void testGetClientWithEmptyAddresses() {
5859
configuration.setAddresses(new ArrayList<>());
@@ -63,6 +64,7 @@ public void testGetClientWithEmptyAddresses() {
6364
Assertions.assertEquals("must have at least one address", actualException.getMessage());
6465
}
6566

67+
@Override
6668
@Test
6769
public void testGetClientWithEmptyToken() {
6870
configuration.setAddresses(List.of(new Address()));
@@ -76,6 +78,7 @@ public void testGetClientWithEmptyToken() {
7678
Assertions.assertEquals("invalid auth config due to empty token", actualException.getMessage());
7779
}
7880

81+
@Override
7982
@Test
8083
public void testGetClientWithEmptyUserName() {
8184
configuration.setAddresses(List.of(new Address()));
@@ -90,6 +93,7 @@ public void testGetClientWithEmptyUserName() {
9093
Assertions.assertEquals("invalid auth config due to empty username", actualException.getMessage());
9194
}
9295

96+
@Override
9397
@Test
9498
public void testGetClientWithNullPassword() {
9599
configuration.setAddresses(List.of(new Address()));
@@ -104,6 +108,7 @@ public void testGetClientWithNullPassword() {
104108
Assertions.assertEquals("invalid auth config due to empty password", actualException.getMessage());
105109
}
106110

111+
@Override
107112
@Test
108113
public void testGetClientWithInvalidBatchInterval() {
109114
configuration.setAddresses(List.of(new Address()));
@@ -117,6 +122,7 @@ public void testGetClientWithInvalidBatchInterval() {
117122
Assertions.assertEquals("batch enabled, batch interval must be great than 0", actualException.getMessage());
118123
}
119124

125+
@Override
120126
@Test
121127
public void testGetClientWithInvalidBatchSize() {
122128
configuration.setAddresses(List.of(new Address()));

0 commit comments

Comments
 (0)