Skip to content

Commit

Permalink
Fix example app
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanl-bq committed Feb 1, 2024
1 parent cddc8ac commit 0c77ba4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 192 deletions.
6 changes: 6 additions & 0 deletions java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ subprojects {
}
}

tasks.named('jar') {
manifest {
attributes('Automatic-Module-Name': 'glide')
}
}

dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: '5.9.2'
}
Expand Down
3 changes: 2 additions & 1 deletion java/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import java.nio.file.Paths

plugins {
id 'java-library'
//id 'org.gradlex.extra-java-module-info'
}

repositories {
mavenCentral()
}

dependencies {
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.24.3'
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.21.12'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.13.0'

implementation group: 'io.netty', name: 'netty-handler', version: '4.1.100.Final'
Expand Down
61 changes: 6 additions & 55 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.api;

import glide.api.models.exceptions.RedisException;
import static glide.ffi.resolvers.SocketListenerResolver.getSocket;

import glide.api.models.configuration.BaseClientConfiguration;
import glide.connectors.handlers.CallbackDispatcher;
import glide.connectors.handlers.ChannelHandler;
import glide.ffi.resolvers.RedisValueResolver;
import glide.managers.BaseCommandResponseResolver;
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -58,59 +62,6 @@ protected static <T> CompletableFuture<T> CreateClient(
}
}

/**
* Check for errors in the Response and return null Throws an error if an unexpected value is
* returned
*
* @return null if the response is empty
*/
protected static Void handleVoidResponse(Response response) {
Object value = handleObjectResponse(response);
if (value == null) {
return null;
}
throw new RedisException(
"Unexpected return type from Redis: got "
+ value.getClass().getSimpleName()
+ " expected null");
}

/**
* Extracts the response value from the Redis response and either throws an exception or returns
* the value as a String.
*
* @param response Redis protobuf message
* @return Response as a String
*/
protected static String handleStringResponse(Response response) {
Object value = handleObjectResponse(response);
if (value instanceof String) {
return (String) value;
}
throw new RedisException(
"Unexpected return type from Redis: got "
+ value.getClass().getSimpleName()
+ " expected String");
}

/**
* Extracts the response value from the Redis response and either throws an exception or returns
* the * value as a HashMap
*
* @param response Redis protobuf message
* @return Response as a String
*/
protected static HashMap<String, Object> handleMapResponse(Response response) {
Object value = handleObjectResponse(response);
if (value instanceof HashMap) {
return (HashMap<String, Object>) value;
}
throw new RedisException(
"Unexpected return type from Redis: got "
+ value.getClass().getSimpleName()
+ " expected HashMap");
}

/**
* Closes this resource, relinquishing any underlying resources. This method is invoked
* automatically on objects managed by the try-with-resources statement.
Expand Down
125 changes: 1 addition & 124 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@
package glide.api;

import glide.api.commands.BaseCommands;
import glide.api.commands.ConnectionCommands;
import glide.api.commands.GenericCommands;
import glide.api.commands.ServerCommands;
import glide.api.commands.StringCommands;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.SetOptions;
import glide.api.models.configuration.RedisClientConfiguration;
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import glide.managers.models.Command;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
* Async (non-blocking) client for Redis in Standalone mode. Use {@link #CreateClient} to request a
* client to Redis.
*/
public class RedisClient extends BaseClient
implements BaseCommands, GenericCommands, ConnectionCommands, ServerCommands, StringCommands {
public class RedisClient extends BaseClient implements BaseCommands {

protected RedisClient(ConnectionManager connectionManager, CommandManager commandManager) {
super(connectionManager, commandManager);
Expand All @@ -43,118 +34,4 @@ public CompletableFuture<Object> customCommand(String[] args) {
Command.builder().requestType(Command.RequestType.CUSTOM_COMMAND).arguments(args).build();
return commandManager.submitNewCommand(command, this::handleObjectResponse);
}

/**
* Ping the Redis server.
*
* @see <a href="https://redis.io/commands/ping/">redis.io</a> for details.
* @returns the String "PONG"
*/
@Override
public CompletableFuture<String> ping() {
Command command = Command.builder().requestType(Command.RequestType.PING).build();
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}

/**
* Ping the Redis server.
*
* @see <a href="https://redis.io/commands/ping/">redis.io</a> for details.
* @param msg - the ping argument that will be returned.
* @returns return a copy of the argument.
*/
@Override
public CompletableFuture<String> ping(String msg) {
Command command =
Command.builder()
.requestType(Command.RequestType.PING)
.arguments(new String[] {msg})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}

/**
* Get information and statistics about the Redis server. DEFAULT option is assumed
*
* @see <a href="https://redis.io/commands/info/">redis.io</a> for details.
* @return CompletableFuture with the response
*/
@Override
public CompletableFuture<Map> info() {
Command command = Command.builder().requestType(Command.RequestType.INFO).build();
return commandManager.submitNewCommand(command, BaseClient::handleMapResponse);
}

/**
* Get information and statistics about the Redis server.
*
* @see <a href="https://redis.io/commands/info/">redis.io</a> for details.
* @param options - A list of InfoSection values specifying which sections of information to
* retrieve. When no parameter is provided, the default option is assumed.
* @return CompletableFuture with the response
*/
@Override
public CompletableFuture<Map> info(InfoOptions options) {
Command command =
Command.builder()
.requestType(Command.RequestType.INFO)
.arguments(options.toInfoOptions())
.build();
return commandManager.submitNewCommand(command, BaseClient::handleMapResponse);
}

/**
* Get the value associated with the given key, or null if no such value exists.
*
* @see <a href="https://redis.io/commands/get/">redis.io</a> for details.
* @param key - The key to retrieve from the database.
* @return If `key` exists, returns the value of `key` as a string. Otherwise, return null
*/
@Override
public CompletableFuture<String> get(String key) {
Command command =
Command.builder()
.requestType(Command.RequestType.GET_STRING)
.arguments(new String[] {key})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}

/**
* Set the given key with the given value.
*
* @see <a href="https://redis.io/commands/set/">redis.io</a> for details.
* @param key - The key to store.
* @param value - The value to store with the given key.
* @return null
*/
@Override
public CompletableFuture<Void> set(String key, String value) {
Command command =
Command.builder()
.requestType(Command.RequestType.SET_STRING)
.arguments(new String[] {key, value})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleVoidResponse);
}

/**
* Set the given key with the given value. Return value is dependent on the passed options.
*
* @see <a href="https://redis.io/commands/set/">redis.io</a> for details.
* @param key - The key to store.
* @param value - The value to store with the given key.
* @param options - The Set options
* @return string or null If value isn't set because of `onlyIfExists` or `onlyIfDoesNotExist`
* conditions, return null. If `returnOldValue` is set, return the old value as a string.
*/
@Override
public CompletableFuture<String> set(String key, String value, SetOptions options) {
Command command =
Command.builder()
.requestType(Command.RequestType.SET_STRING)
.arguments(options.toSetOptions(List.of(key, value)))
.build();
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}
}
12 changes: 0 additions & 12 deletions java/examples/src/main/java/glide/examples/ExamplesApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ private static void runGlideExamples() {
try {
RedisClient client = GlideClient.connectToGlide(settings);

System.out.println("Glide PING: " + client.ping().get());
System.out.println("Glide PING(custom): " + client.ping("found you!").get());

// panic
// System.out.println("Glide INFO(): " + client.info().get());

System.out.println("Glide SET(myKey, myValue): " + client.set("myKey", "myValue").get());
System.out.println("Glide GET(myKey): " + client.get("myKey").get());
System.out.println("Glide SET(myKey, yourValue): " + client.set("myKey", "yourValue").get());
System.out.println("Glide GET(myKey): " + client.get("myKey").get());
System.out.println("Glide GET(invalid): " + client.get("invalid").get());

Object customGetMyKey = client.customCommand(new String[] {"get", "myKey"}).get();
System.out.println("Glide CUSTOM_COMMAND(get, myKey): " + customGetMyKey);

Expand Down

0 comments on commit 0c77ba4

Please sign in to comment.