Skip to content
Merged
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
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
<profiles>
<profile>
<id>native</id>
<activation>
<jdk>[21, 24]</jdk>
</activation>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -209,6 +212,40 @@
</plugins>
</build>
</profile>
<profile>
<id>native-jdk25</id>
<activation>
<jdk>25</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>1.0.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>native test</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
<configuration>
<fallback>false</fallback>
<!-- For changing environment variables in unit tests. -->
<buildArgs>
<buildArg>-J--add-opens=java.base/java.lang=ALL-UNNAMED</buildArg>
<buildArg>--initialize-at-build-time=org.junit.jupiter.engine.discovery.MethodSegmentResolver</buildArg>
<buildArg>-H:+SharedArenaSupport</buildArg>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>trace</id>
<properties>
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/com/github/sttk/sabi_redis/RedisDataConn.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
/**
* The DataConn implementation for Redis in standalone configuration.
*
* <p>This class manages a connection to a Redis server and provides ways to register handlers
* that are executed at certain points in the connection's lifecycle.
* <p>This class manages a connection to a Redis server and provides ways to register handlers that
* are executed at certain points in the connection's lifecycle.
*/
public class RedisDataConn implements DataConn {

/// Fields
// Fields

private final StatefulRedisConnection<String, String> connection;
private final List<ConnectionHandler> preCommits = new ArrayList<>();
private final List<ConnectionHandler> postCommits = new ArrayList<>();
private final List<ConnectionHandler> forceBacks = new ArrayList<>();

/// Constructors
// Constructors

/**
* Constructs a new RedisDataConn with the given Redis connection.
Expand All @@ -37,7 +37,7 @@ protected RedisDataConn(StatefulRedisConnection<String, String> connection) {
this.connection = connection;
}

/// Methods
// Methods

/**
* Returns the Redis connection.
Expand Down Expand Up @@ -124,8 +124,8 @@ public boolean shouldForceBack() {
}

/**
* Rolls back the connection. (Currently does nothing as Redis doesn't have a direct rollback for a
* simple connection)
* Rolls back the connection. (Currently does nothing as Redis doesn't have a direct rollback for
* a simple connection)
*
* @param ag an asynchronous group.
*/
Expand All @@ -147,9 +147,7 @@ public void forceBack(AsyncGroup ag) {
}
}

/**
* Closes the connection.
*/
/** Closes the connection. */
@Override
public void close() {
this.connection.close();
Expand Down
87 changes: 33 additions & 54 deletions src/main/java/com/github/sttk/sabi_redis/RedisDataSrc.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@
*/
public class RedisDataSrc implements DataSrc {

/// Error reasons
// Error reasons

/**
* The error reason that indicates the {@link RedisDataSrc} is not setup yet.
*/
/** The error reason that indicates the {@link RedisDataSrc} is not setup yet. */
public record NotSetupYet() {}

/**
* The error reason that indicates the {@link RedisDataSrc} is already setup.
*/
/** The error reason that indicates the {@link RedisDataSrc} is already setup. */
public record AlreadySetup() {}

/**
Expand All @@ -41,7 +37,8 @@ public record AlreadySetup() {}
public record FailToCreateClientFromUriString(String uri) {}

/**
* The error reason that indicates failing to create a {@link RedisClient} from a {@link URI} object.
* The error reason that indicates failing to create a {@link RedisClient} from a {@link URI}
* object.
*
* @param uri a URI object.
*/
Expand All @@ -55,14 +52,6 @@ public record FailToCreateClientFromURI(URI uri) {}
*/
public record FailToCreateClientFromRedisURI(RedisURI redisURI) {}

/**
* The error reason that indicates failing to create a {@link RedisClient} from a {@link
* ClientResources} object.
*
* @param clientResources a ClientResources object.
*/
public record FailToCreateClientFromClientResources(ClientResources clientResources) {}

/**
* The error reason that indicates failing to create a {@link RedisClient} from a {@link
* ClientResources} object and a URI string.
Expand Down Expand Up @@ -93,12 +82,12 @@ public record FailToCreateClientFromClientResourcesAndURI(
public record FailToCreateClientFromClientResourcesAndRedisURI(
ClientResources clientResources, RedisURI redisURI) {}

/// Fields
// Fields

private RedisClientFactory redisClientFactory;
private RedisClient redisClient;

/// Constructors
// Constructors

/**
* Constructs a new RedisDataSrc with the given URI string.
Expand Down Expand Up @@ -127,15 +116,6 @@ public RedisDataSrc(RedisURI redisURI) {
this.redisClientFactory = new RedisClientFactoryByRedisURI(redisURI);
}

/**
* Constructs a new RedisDataSrc with the given {@link ClientResources} object.
*
* @param cr a ClientResources object.
*/
public RedisDataSrc(ClientResources cr) {
this.redisClientFactory = new RedisClientFactoryByClientResources(cr);
}

/**
* Constructs a new RedisDataSrc with the given {@link ClientResources} object and URI string.
*
Expand Down Expand Up @@ -168,7 +148,7 @@ public RedisDataSrc(ClientResources cr, RedisURI redisURI) {
this.redisClientFactory = new RedisClientFactoryByClientResourcesAndRedisURI(cr, redisURI);
}

/// Methods
// Methods

/**
* Sets up this data source.
Expand All @@ -188,9 +168,7 @@ public void setup(AsyncGroup ag) throws Err {
this.redisClient = factory.create();
}

/**
* Closes this data source and shuts down the {@link RedisClient}.
*/
/** Closes this data source and shuts down the {@link RedisClient}. */
@Override
public void close() {
if (this.redisClient != null) {
Expand Down Expand Up @@ -228,9 +206,12 @@ private class RedisClientFactoryByUriString implements RedisClientFactory {
}

@Override
@SuppressWarnings("try")
public RedisClient create() throws Err {
try {
return RedisClient.create(this.uri);
var client = RedisClient.create(this.uri);
try (var conn = client.connect()) {}
return client;
} catch (Exception e) {
throw new Err(new FailToCreateClientFromUriString(this.uri), e);
}
Expand All @@ -245,9 +226,12 @@ private class RedisClientFactoryByUriObject implements RedisClientFactory {
}

@Override
@SuppressWarnings("try")
public RedisClient create() throws Err {
try {
return RedisClient.create(this.uri.toString());
var client = RedisClient.create(this.uri.toString());
try (var conn = client.connect()) {}
return client;
} catch (Exception e) {
throw new Err(new FailToCreateClientFromURI(this.uri), e);
}
Expand All @@ -262,32 +246,18 @@ private class RedisClientFactoryByRedisURI implements RedisClientFactory {
}

@Override
@SuppressWarnings("try")
public RedisClient create() throws Err {
try {
return RedisClient.create(this.redisURI);
var client = RedisClient.create(this.redisURI);
try (var conn = client.connect()) {}
return client;
} catch (Exception e) {
throw new Err(new FailToCreateClientFromRedisURI(this.redisURI), e);
}
}
}

private class RedisClientFactoryByClientResources implements RedisClientFactory {
final ClientResources clientResources;

RedisClientFactoryByClientResources(ClientResources cr) {
this.clientResources = cr;
}

@Override
public RedisClient create() throws Err {
try {
return RedisClient.create(this.clientResources);
} catch (Exception e) {
throw new Err(new FailToCreateClientFromClientResources(this.clientResources), e);
}
}
}

private class RedisClientFactoryByClientResourcesAndUriString implements RedisClientFactory {
final ClientResources clientResources;
final String uri;
Expand All @@ -298,9 +268,12 @@ private class RedisClientFactoryByClientResourcesAndUriString implements RedisCl
}

@Override
@SuppressWarnings("try")
public RedisClient create() throws Err {
try {
return RedisClient.create(this.clientResources, this.uri);
var client = RedisClient.create(this.clientResources, this.uri);
try (var conn = client.connect()) {}
return client;
} catch (Exception e) {
throw new Err(
new FailToCreateClientFromClientResourcesAndUriString(this.clientResources, this.uri),
Expand All @@ -319,9 +292,12 @@ private class RedisClientFactoryByClientResourcesAndUriObject implements RedisCl
}

@Override
@SuppressWarnings("try")
public RedisClient create() throws Err {
try {
return RedisClient.create(this.clientResources, this.uri.toString());
var client = RedisClient.create(this.clientResources, this.uri.toString());
try (var conn = client.connect()) {}
return client;
} catch (Exception e) {
throw new Err(
new FailToCreateClientFromClientResourcesAndURI(this.clientResources, this.uri), e);
Expand All @@ -339,9 +315,12 @@ private class RedisClientFactoryByClientResourcesAndRedisURI implements RedisCli
}

@Override
@SuppressWarnings("try")
public RedisClient create() throws Err {
try {
return RedisClient.create(this.clientResources, this.redisURI);
var client = RedisClient.create(this.clientResources, this.redisURI);
try (var conn = client.connect()) {}
return client;
} catch (Exception e) {
throw new Err(
new FailToCreateClientFromClientResourcesAndRedisURI(
Expand Down
45 changes: 6 additions & 39 deletions src/test/java/com/github/sttk/sabi_redis/StandaloneAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,45 +203,9 @@ class SampleDataHub extends DataHub implements SampleData, RedisSampleDataAcc {}

//

@Test
void test_NewRedisDataSrc() {
var data = new SampleDataHub();
data.uses("redis", new RedisDataSrc("redis://127.0.0.1:6379/0"));
try {
data.run(sampleLogic);
} catch (Err e) {
fail(e);
}
}

@Test
void test_FailDueToInvalidAddr() {
var data = new SampleDataHub();
data.uses("redis", new RedisDataSrc("xxxx"));
try {
data.run(sampleLogic);
} catch (Err err) {
switch (err.getReason()) {
case DataHub.FailToSetupLocalDataSrcs reason -> {
assertThat(reason.errors()).hasSize(1);
var err2 = reason.errors().get("redis");
switch (err2.getReason()) {
case RedisDataSrc.FailToCreateClientFromUriString reason2 -> {
assertThat(reason2.uri()).isEqualTo("xxxx");
assertThat(err2.getCause().toString())
.isEqualTo("java.lang.IllegalArgumentException: URI scheme must not be null");
}
default -> fail(err);
}
}
default -> fail(err);
}
}
}

@Test
void test_TxnAndForceBack() {
var data = new SampleDataHub();
try (var data = new SampleDataHub()) {
data.uses("redis", new RedisDataSrc("redis://127.0.0.1:6379/3"));
try {
data.txn(sampleLogicWithForceBackOk);
Expand Down Expand Up @@ -283,11 +247,12 @@ void test_TxnAndForceBack() {
cmd.del("sample_force_back_2");
assertThat(s).isNull();
}
}
}

@Test
void test_TxnAndPreCommit() {
var data = new SampleDataHub();
try (var data = new SampleDataHub()) {
data.uses("redis", new RedisDataSrc("redis://127.0.0.1:6379/4"));
try {
data.txn(sampleLogicWithPreCommit);
Expand All @@ -304,11 +269,12 @@ void test_TxnAndPreCommit() {
cmd.del("sample_pre_commit");
assertThat(s).isEqualTo("Good Evening");
}
}
}

@Test
void test_TxnAndPostCommit() {
var data = new SampleDataHub();
try (var data = new SampleDataHub()) {
data.uses("redis", new RedisDataSrc("redis://127.0.0.1:6379/5"));
try {
data.txn(sampleLogicWithPostCommit);
Expand All @@ -325,5 +291,6 @@ void test_TxnAndPostCommit() {
cmd.del("sample_post_commit");
assertThat(s).isEqualTo("Good Night");
}
}
}
}
Loading