Package titan.storage

Class TitanJRedisAdapter

java.lang.Object
titan.storage.TitanJRedisAdapter
All Implemented Interfaces:
AutoCloseable

public class TitanJRedisAdapter extends Object implements 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 Details

  • Constructor Details

    • TitanJRedisAdapter

      public TitanJRedisAdapter(String host, int port)
      Constructs a new TitanJRedisAdapter instance. This constructor initializes the adapter but does not establish a connection to Redis. Call connect() 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

      public void connect() throws IOException
      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:
      true if connected, false otherwise.
    • execute

      public Object execute(String... args) throws IOException
      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

      public String get(String key) throws IOException
      Retrieves the string value associated with the specified key from Redis. If the adapter is not connected, or if an error occurs, it returns null.
      Parameters:
      key - The key whose value is to be retrieved.
      Returns:
      The string value associated with the key, or null if 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

      public String set(String key, String value) throws IOException
      Sets the string value of a key in Redis. If the adapter is not connected, or if an error occurs, it returns null.
      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 null if the adapter is not connected or an error occurs.
      Throws:
      IOException - If an I/O error occurs during the Redis operation.
    • sadd

      public long sadd(String key, String member) throws IOException
      Adds the specified member to the set stored at key in Redis. If the adapter is not connected, or if an error occurs, it returns 0.
      Parameters:
      key - The key of the set.
      member - The member to add to the set.
      Returns:
      1 if the member was added successfully and was new, 0 if 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

      public Set<String> smembers(String key) throws IOException
      Throws:
      IOException
    • srem

      public long srem(String key, String member)
      Removes the specified member from the set stored at key in Redis. If the adapter is not connected, or if an error occurs, it returns 0.
      Parameters:
      key - The key of the set.
      member - The member to remove from the set.
      Returns:
      1 if the member was removed successfully, 0 if the member was not found, or if the adapter is not connected or an error occurs.
    • sendCommand

      private void sendCommand(String... args) throws IOException
      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

      private Object readResponse() throws IOException
      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

      private String readLine() throws IOException
      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

      private String readBulkString() throws IOException
      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 null if 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

      private List<Object> readArray() throws IOException
      Reads a RESP Array response from the input stream. This method first reads the number of elements in the array, then recursively calls readResponse() for each element to deserialize them.
      Returns:
      A List of objects representing the elements of the array, or null if Redis sent a null array (count -1).
      Throws:
      IOException - If an I/O error occurs while reading from the input stream.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException