Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2024 openGemini Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.opengemini.client.api;

import java.util.List;

/**
// * Interface to access a OpenGemini database provides a set of blocking methods.
*/
public interface OpenGeminiSyncClient extends AutoCloseable {

/**
* Create a new database.
*
* @param database the name of the new database.
*/
void createDatabase(String database) throws OpenGeminiException;

/**
* Drop a database.
*
* @param database the name of the database to drop.
*/
void dropDatabase(String database) throws OpenGeminiException;

/**
* Show all available databases.
*/
List<String> showDatabases() throws OpenGeminiException;

/**
* Create a retention policy.
*
* @param database the name of the database.
* @param rpConfig the config of the retention policy
* @param isDefault if the retention policy is the default retention policy for the database or not
*/
void createRetentionPolicy(String database, RpConfig rpConfig, boolean isDefault) throws OpenGeminiException;

/**
* Show all available retention policies.
*
* @param database the name of the database.
*/
List<RetentionPolicy> showRetentionPolicies(String database) throws OpenGeminiException;

/**
* Drop a retention policy.
*
* @param database the name of the database.
* @param retentionPolicy the name of the retention policy to drop.
*/
void dropRetentionPolicy(String database, String retentionPolicy) throws OpenGeminiException;

/**
* Execute a query against a database.
*
* @param query the query to execute.
*/
QueryResult query(Query query) throws OpenGeminiException;

/**
* Write a single point to the database.
*
* @param database the name of the database.
* @param point the point to write.
*/
void write(String database, Point point) throws OpenGeminiException;

/**
* Write points to the database.
*
* @param database the name of the database.
* @param points the points to write.
*/
void write(String database, List<Point> points) throws OpenGeminiException;

/**
* Write a single point to the database.
*
* @param database the name of the database.
* @param retentionPolicy the name of the retention policy.
* @param point the point to write.
*/
void write(String database, String retentionPolicy, Point point) throws OpenGeminiException;

/**
* Write points to the database.
*
* @param database the name of the database.
* @param retentionPolicy the name of the retention policy.
* @param points the points to write.
*/
void write(String database, String retentionPolicy, List<Point> points) throws OpenGeminiException;

/**
* Ping the OpenGemini server
*/
Pong ping() throws OpenGeminiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@
import io.opengemini.client.api.AuthType;
import io.opengemini.client.api.BatchConfig;
import io.opengemini.client.api.Configuration;
import io.opengemini.client.api.OpenGeminiAsyncClient;
import io.opengemini.client.api.OpenGeminiException;
import io.opengemini.client.api.OpenGeminiSyncClient;
import org.jetbrains.annotations.NotNull;

public class OpenGeminiClientFactory {
public static OpenGeminiClient create(@NotNull Configuration configuration) throws OpenGeminiException {
public static OpenGeminiAsyncClient create(@NotNull Configuration configuration) throws OpenGeminiException {
validateConf(configuration);
return new OpenGeminiClient(configuration);
}

public static OpenGeminiSyncClient createSyncClient(@NotNull Configuration configuration)
throws OpenGeminiException {
validateConf(configuration);
return new OpenGeminiSyncClientImpl(configuration);
}

private static void validateConf(@NotNull Configuration configuration) throws OpenGeminiException {
if (configuration.getAddresses() == null || configuration.getAddresses().isEmpty()) {
throw new OpenGeminiException("must have at least one address");
}
Expand Down Expand Up @@ -52,6 +65,5 @@ public static OpenGeminiClient create(@NotNull Configuration configuration) thro
throw new OpenGeminiException("batch enabled, batch size must be great than 0");
}
}
return new OpenGeminiClient(configuration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.opengemini.client.impl;

import io.opengemini.client.api.Configuration;
import io.opengemini.client.api.OpenGeminiException;
import io.opengemini.client.api.OpenGeminiSyncClient;
import io.opengemini.client.api.Point;
import io.opengemini.client.api.Pong;
import io.opengemini.client.api.Query;
import io.opengemini.client.api.QueryResult;
import io.opengemini.client.api.RetentionPolicy;
import io.opengemini.client.api.RpConfig;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class OpenGeminiSyncClientImpl implements OpenGeminiSyncClient {
protected Configuration conf;
private OpenGeminiClient openGeminiAsyncClient;

OpenGeminiSyncClientImpl(Configuration conf) {
this.conf = conf;
this.openGeminiAsyncClient = new OpenGeminiClient(conf);
}

@Override
public void createDatabase(String database) throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.createDatabase(database));
}

@Override
public void dropDatabase(String database) throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.dropDatabase(database));
}

@Override
public List<String> showDatabases() throws OpenGeminiException {
return wrapFuture(openGeminiAsyncClient.showDatabases());
}

@Override
public void createRetentionPolicy(String database, RpConfig rpConfig, boolean isDefault)
throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.createRetentionPolicy(database, rpConfig, isDefault));
}

@Override
public List<RetentionPolicy> showRetentionPolicies(String database) throws OpenGeminiException {
return wrapFuture(openGeminiAsyncClient.showRetentionPolicies(database));
}

@Override
public void dropRetentionPolicy(String database, String retentionPolicy) throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.dropRetentionPolicy(database, retentionPolicy));
}

@Override
public QueryResult query(Query query) throws OpenGeminiException {
return wrapFuture(openGeminiAsyncClient.query(query));
}

@Override
public void write(String database, Point point) throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.write(database, point));
}

@Override
public void write(String database, List<Point> points) throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.write(database, points));
}

@Override
public void write(String database, String retentionPolicy, Point point) throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.write(database, retentionPolicy, point));
}

@Override
public void write(String database, String retentionPolicy, List<Point> points) throws OpenGeminiException {
wrapFuture(openGeminiAsyncClient.write(database, retentionPolicy, points));
}

@Override
public Pong ping() throws OpenGeminiException {
return wrapFuture(openGeminiAsyncClient.ping());
}

@Override
public void close() throws IOException {
openGeminiAsyncClient.close();
}

private <T> T wrapFuture(CompletableFuture<T> future) throws OpenGeminiException {
try {
return future.get(conf.getHttpConfig().timeout().toMillis(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new OpenGeminiException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2025 openGemini Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.opengemini.client.impl;

import org.junit.jupiter.api.Test;

public interface FactoryTestCase {

@Test
void testGetClientWithNullAddresses();

@Test
void testGetClientWithEmptyAddresses();

@Test
void testGetClientWithEmptyToken();

@Test
void testGetClientWithEmptyUserName();

@Test
void testGetClientWithNullPassword();

@Test
void testGetClientWithInvalidBatchInterval();

@Test
void testGetClientWithInvalidBatchSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import java.util.ArrayList;
import java.util.List;

public class OpenGeminiClientFactoryTest {

public class OpenGeminiAsyncClientFactoryTest implements FactoryTestCase {
private static Configuration configuration;

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

@Override
@Test
public void testGetClientWithNullAddresses() {
configuration.setAddresses(null);
Expand All @@ -53,6 +53,7 @@ public void testGetClientWithNullAddresses() {
Assertions.assertEquals("must have at least one address", actualException.getMessage());
}

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

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

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

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

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

@Override
@Test
public void testGetClientWithInvalidBatchSize() {
configuration.setAddresses(List.of(new Address()));
Expand Down
Loading
Loading