예제 #1
0
  /**
   * Read a simple or bulk string response from the server, throwing an exception if the result is
   * not one of those two types.
   *
   * <p>This is a blocking operation.
   *
   * @return The response as a string
   * @throws BajaTypeMismatchException If the response was not a simple or bulk string
   * @throws BajaResourceException If there was an error reading from the stream
   * @throws BajaProtocolErrorException If the server responded with an error result
   */
  public String readSimpleOrBulkString() {
    final Set<RespType> expected = new HashSet<>();
    expected.add(RespType.BULK_STRING);
    expected.add(RespType.SIMPLE_STRING);

    final RespType type = verifyResponseType(expected);
    if (type == RespType.BULK_STRING) {
      return IOFunction.runCommand(() -> parser.readBulkString(inputStream));
    }

    return IOFunction.runCommand(() -> parser.readSimpleString(inputStream));
  }
예제 #2
0
  /**
   * Read any type of response from the server, throwing an exception if the result is an Redis
   * error.
   *
   * <p>This might be useful for Redis commands that return different types based on the arguments
   * supplied to them. In this case, the caller will have to inspect the type of the result
   * afterwards to determine the appropriate course of action.
   *
   * <p>This is a blocking operation.
   *
   * @return The response as an Object
   * @throws BajaResourceException If there was an error reading from the stream
   * @throws BajaProtocolErrorException If the server responded with an error result
   */
  public Object readAnyType() {
    final Set<RespType> expected = new HashSet<>();
    expected.add(RespType.BULK_STRING);
    expected.add(RespType.SIMPLE_STRING);
    expected.add(RespType.INTEGER);
    expected.add(RespType.ARRAY);

    final RespType type = verifyResponseType(expected);
    switch (type) {
      case BULK_STRING:
        return IOFunction.runCommand(() -> parser.readBulkString(inputStream));
      case SIMPLE_STRING:
        return IOFunction.runCommand(() -> parser.readSimpleString(inputStream));
      case INTEGER:
        return IOFunction.runCommand(() -> parser.readLong(inputStream));
      case ARRAY:
        return IOFunction.runCommand(() -> parser.readArray(inputStream));
    }

    throw new IllegalStateException("Got unexpected result type " + type);
  }
예제 #3
0
 /**
  * Read a simple string response from the server, throwing an exception if the response is not a
  * simple string type.
  *
  * <p>This is a blocking operation.
  *
  * @return The response as a string
  * @throws BajaTypeMismatchException If the response was not a simple string
  * @throws BajaResourceException If there was an error reading from the stream
  * @throws BajaProtocolErrorException If the server responded with an error result
  */
 public String readSimpleString() {
   verifyResponseType(Collections.singleton(RespType.SIMPLE_STRING));
   return IOFunction.runCommand(() -> parser.readSimpleString(inputStream));
 }