Skip to content

Commit cdd4ddb

Browse files
committed
feat: add OpenGeminiSyncClient
Signed-off-by: moxiaoying <[email protected]>
1 parent de5cc41 commit cdd4ddb

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
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+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.opengemini.client.api;
2+
3+
import lombok.AllArgsConstructor;
4+
5+
import java.util.List;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.concurrent.TimeUnit;
8+
9+
@AllArgsConstructor
10+
public class OpenGeminiSyncClientImpl implements OpenGeminiSyncClient {
11+
private OpenGeminiAsyncClient openGeminiAsyncClient;
12+
private Configuration conf;
13+
14+
@Override
15+
public void createDatabase(String database) throws OpenGeminiException {
16+
wrapFuture(openGeminiAsyncClient.createDatabase(database));
17+
}
18+
19+
@Override
20+
public void dropDatabase(String database) throws OpenGeminiException {
21+
wrapFuture(openGeminiAsyncClient.dropDatabase(database));
22+
}
23+
24+
@Override
25+
public List<String> showDatabases() throws OpenGeminiException {
26+
return wrapFuture(openGeminiAsyncClient.showDatabases());
27+
}
28+
29+
@Override
30+
public void createRetentionPolicy(String database, RpConfig rpConfig, boolean isDefault)
31+
throws OpenGeminiException {
32+
wrapFuture(openGeminiAsyncClient.createRetentionPolicy(database, rpConfig, isDefault));
33+
}
34+
35+
@Override
36+
public List<RetentionPolicy> showRetentionPolicies(String database) throws OpenGeminiException {
37+
return wrapFuture(openGeminiAsyncClient.showRetentionPolicies(database));
38+
}
39+
40+
@Override
41+
public void dropRetentionPolicy(String database, String retentionPolicy) throws OpenGeminiException {
42+
wrapFuture(openGeminiAsyncClient.dropRetentionPolicy(database, retentionPolicy));
43+
}
44+
45+
@Override
46+
public QueryResult query(Query query) throws OpenGeminiException {
47+
return wrapFuture(openGeminiAsyncClient.query(query));
48+
}
49+
50+
@Override
51+
public void write(String database, Point point) throws OpenGeminiException {
52+
wrapFuture(openGeminiAsyncClient.write(database, point));
53+
}
54+
55+
@Override
56+
public void write(String database, List<Point> points) throws OpenGeminiException {
57+
wrapFuture(openGeminiAsyncClient.write(database, points));
58+
}
59+
60+
@Override
61+
public void write(String database, String retentionPolicy, Point point) throws OpenGeminiException {
62+
wrapFuture(openGeminiAsyncClient.write(database, retentionPolicy, point));
63+
}
64+
65+
@Override
66+
public void write(String database, String retentionPolicy, List<Point> points) throws OpenGeminiException {
67+
wrapFuture(openGeminiAsyncClient.write(database, retentionPolicy, points));
68+
}
69+
70+
@Override
71+
public Pong ping() throws OpenGeminiException {
72+
return wrapFuture(openGeminiAsyncClient.ping());
73+
}
74+
75+
@Override
76+
public void close() throws Exception {
77+
openGeminiAsyncClient.close();
78+
}
79+
80+
private <T> T wrapFuture(CompletableFuture<T> future) throws OpenGeminiException {
81+
try {
82+
return future.get(conf.httpConfig.timeout().toMillis(), TimeUnit.MILLISECONDS);
83+
} catch (Exception e) {
84+
throw new OpenGeminiException(e);
85+
}
86+
}
87+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import io.opengemini.client.api.BatchConfig;
2222
import io.opengemini.client.api.Configuration;
2323
import io.opengemini.client.api.OpenGeminiException;
24+
import io.opengemini.client.api.OpenGeminiSyncClient;
25+
import io.opengemini.client.api.OpenGeminiSyncClientImpl;
2426
import org.jetbrains.annotations.NotNull;
2527

2628
public class OpenGeminiClientFactory {
@@ -54,4 +56,10 @@ public static OpenGeminiClient create(@NotNull Configuration configuration) thro
5456
}
5557
return new OpenGeminiClient(configuration);
5658
}
59+
60+
public static OpenGeminiSyncClient createSyncClient(@NotNull Configuration configuration)
61+
throws OpenGeminiException {
62+
OpenGeminiClient openGeminiSyncClient = create(configuration);
63+
return new OpenGeminiSyncClientImpl(openGeminiSyncClient, configuration);
64+
}
5765
}

spring/opengemini-spring-boot-starter/src/main/java/io/opengemini/client/spring/data/config/OpenGeminiAutoConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.opengemini.client.api.OpenGeminiAsyncClient;
2020
import io.opengemini.client.api.OpenGeminiException;
21+
import io.opengemini.client.api.OpenGeminiSyncClient;
2122
import io.opengemini.client.impl.OpenGeminiClientFactory;
2223
import io.opengemini.client.spring.data.core.ClientConfigurationBuilderCustomizer;
2324
import io.opengemini.client.spring.data.core.DefaultOpenGeminiSerializerFactory;
@@ -56,6 +57,15 @@ public OpenGeminiAsyncClient openGeminiAsyncClient(OpenGeminiProperties properti
5657
return OpenGeminiClientFactory.create(converter.toConfiguration());
5758
}
5859

60+
@Bean
61+
@ConditionalOnMissingBean(OpenGeminiSyncClient.class)
62+
public OpenGeminiSyncClient openGeminiSyncClient(OpenGeminiProperties properties,
63+
ObjectProvider<ClientConfigurationBuilderCustomizer> customizers)
64+
throws OpenGeminiException {
65+
OpenGeminiPropertiesConverter converter = new OpenGeminiPropertiesConverter(properties, customizers);
66+
return OpenGeminiClientFactory.createSyncClient(converter.toConfiguration());
67+
}
68+
5969
@Bean
6070
@ConditionalOnMissingBean(OpenGeminiSerializerFactory.class)
6171
public OpenGeminiSerializerFactory openGeminiSerializerFactory() {

0 commit comments

Comments
 (0)