Package titan.storage
Class TitanJRedisAdapter
java.lang.Object
titan.storage.TitanJRedisAdapter
- All Implemented Interfaces:
AutoCloseable
A synchronous adapter to connect Titan to RedisJava.
Implements the RESP (Redis Serialization Protocol) for sending commands.
This class provides a low-level client for interacting with a Redis server,
handling connection management and RESP serialization/deserialization.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Stringprivate BufferedInputStreamprivate booleanprivate OutputStreamprivate final intprivate Socket -
Constructor Summary
ConstructorsConstructorDescriptionTitanJRedisAdapter(String host, int port) Constructs a newTitanJRedisAdapterinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidconnect()Establishes a connection to the Redis server using the configured host and port.Executes a raw Redis command by sending it to the server and reading its response.Retrieves the string value associated with the specified key from Redis.booleanChecks if the adapter is currently connected to the Redis server.Reads a RESP Array response from the input stream.private StringReads a RESP Bulk String response from the input stream.private StringreadLine()Reads a line from the input stream until a CR-LF sequence is encountered.private ObjectReads and deserializes a single RESP response from the Redis server.longAdds the specified member to the set stored atkeyin Redis.private voidsendCommand(String... args) Serializes and sends a Redis command to the server using the RESP protocol.Sets the string value of a key in Redis.longRemoves the specified member from the set stored atkeyin Redis.
-
Field Details
-
host
-
port
private final int port -
socket
-
out
-
in
-
isConnected
private boolean isConnected
-
-
Constructor Details
-
TitanJRedisAdapter
Constructs a newTitanJRedisAdapterinstance. This constructor initializes the adapter but does not establish a connection to Redis. Callconnect()to establish the connection.- Parameters:
host- The hostname or IP address of the Redis server.port- The port number of the Redis server.
-
-
Method Details
-
connect
Establishes a connection to the Redis server using the configured host and port. If the host is null or empty, or the port is invalid (less than or equal to 0), the adapter will operate in an "in-memory" mode, meaning no connection is attempted and all operations will fail or return default values, simulating a disconnected state. If a connection fails, the adapter also falls back to an "in-memory" (disconnected) mode.- Throws:
IOException- If an I/O error occurs during socket creation or stream setup, though most connection errors are caught and result in a disconnected state.
-
isConnected
public boolean isConnected()Checks if the adapter is currently connected to the Redis server.- Returns:
trueif connected,falseotherwise.
-
execute
Executes a raw Redis command by sending it to the server and reading its response. This method handles the RESP serialization of the command arguments and deserialization of the response. It is synchronized to ensure only one command is processed at a time, maintaining command-response order.- Parameters:
args- The command and its arguments, e.g., "SET", "mykey", "myvalue".- Returns:
- The deserialized response from Redis. The type of the returned object depends on the Redis command and its response type (e.g., String for simple strings, Long for integers, List for arrays, null for null bulk strings).
- Throws:
IOException- If an I/O error occurs during sending the command or reading the response, or if Redis returns an error response.
-
get
Retrieves the string value associated with the specified key from Redis. If the adapter is not connected, or if an error occurs, it returnsnull.- Parameters:
key- The key whose value is to be retrieved.- Returns:
- The string value associated with the key, or
nullif the key does not exist, the adapter is not connected, or an error occurs. - Throws:
IOException- If an I/O error occurs during the Redis operation.
-
set
Sets the string value of a key in Redis. If the adapter is not connected, or if an error occurs, it returnsnull.- Parameters:
key- The key to set.value- The string value to associate with the key.- Returns:
- The status string from Redis (e.g., "OK"), or
nullif the adapter is not connected or an error occurs. - Throws:
IOException- If an I/O error occurs during the Redis operation.
-
sadd
Adds the specified member to the set stored atkeyin Redis. If the adapter is not connected, or if an error occurs, it returns0.- Parameters:
key- The key of the set.member- The member to add to the set.- Returns:
1if the member was added successfully and was new,0if the member already existed or if the adapter is not connected or an error occurs.- Throws:
IOException- If an I/O error occurs during the Redis operation.
-
smembers
- Throws:
IOException
-
srem
Removes the specified member from the set stored atkeyin Redis. If the adapter is not connected, or if an error occurs, it returns0.- Parameters:
key- The key of the set.member- The member to remove from the set.- Returns:
1if the member was removed successfully,0if the member was not found, or if the adapter is not connected or an error occurs.
-
sendCommand
Serializes and sends a Redis command to the server using the RESP protocol. This method constructs the RESP array format from the given arguments and writes it to the output stream.- Parameters:
args- The command and its arguments to be sent.- Throws:
IOException- If an I/O error occurs while writing to the output stream.
-
readResponse
Reads and deserializes a single RESP response from the Redis server. This method determines the type of the response (Simple String, Error, Integer, Bulk String, Array) based on the first byte and delegates to appropriate helper methods for parsing.- Returns:
- The deserialized Redis response as a Java object (String, Long, List, or null).
- Throws:
IOException- If an I/O error occurs while reading from the input stream, or if Redis returns an error response, or if an unknown RESP type is encountered.
-
readLine
Reads a line from the input stream until a CR-LF sequence is encountered. This is a helper method for parsing RESP simple strings, errors, and integer responses.- Returns:
- The string read from the stream, excluding the CR-LF terminator.
- Throws:
IOException- If an I/O error occurs while reading from the input stream.
-
readBulkString
Reads a RESP Bulk String response from the input stream. This method first reads the length of the bulk string, then reads the specified number of bytes, and finally consumes the trailing CR-LF.- Returns:
- The deserialized bulk string, or
nullif Redis sent a null bulk string (length -1). - Throws:
IOException- If an I/O error occurs while reading from the input stream, or if the stream ends unexpectedly.
-
readArray
Reads a RESP Array response from the input stream. This method first reads the number of elements in the array, then recursively callsreadResponse()for each element to deserialize them.- Returns:
- A
Listof objects representing the elements of the array, ornullif Redis sent a null array (count -1). - Throws:
IOException- If an I/O error occurs while reading from the input stream.
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-