Class RedisStreamHelper

java.lang.Object
net.bugreaper.modules.redis.helpers.RedisHelper<RedisStreamHelper>
net.bugreaper.modules.redis.helpers.RedisStreamHelper
All Implemented Interfaces:
RedisHelperInt

public class RedisStreamHelper extends RedisHelper<RedisStreamHelper> implements RedisHelperInt
Helper class for interacting with Redis STREAM data types.

This class provides convenient methods for storing and retrieving JSON records in Redis STREAMs. Unlike the native Redis STREAM API, which works with field-value pairs, this helper exposes a JSON-based interface similar to the other Redis helpers in this library.

When adding a record, the provided JSON object is automatically converted into STREAM fields, where each top-level JSON property becomes a field-value pair. When reading records, STREAM fields are reconstructed into JSON strings.

Example:

 Input JSON:
 {
   "id": 1,
   "name": "Alex",
   "active": true
 }

 Stored STREAM entry:
 1723456789012-0
     id     -> "1"
     name   -> "Alex"
     active -> "true"

 Returned JSON:
 {
   "id":"1",
   "name":"Alex",
   "active":"true"
 }
 

Notes:

  • Only flat JSON objects are supported.
  • Each top-level JSON property becomes a Redis STREAM field.
  • All field values are stored as Redis strings. Primitive JSON values such as numbers and booleans are converted to their string representation.
  • Nested JSON objects and arrays are not supported.
  • Redis STREAM entry IDs are generated automatically.
  • TTL is applied to the entire STREAM key, not individual entries.
  • This helper uses JedisPool internally to manage Redis connections.
See Also:
  • Jedis
  • JedisPool
  • Constructor Details

    • RedisStreamHelper

      public RedisStreamHelper(String host, int port, String username, String password)
      This constructor initializes client for interaction with Redis STREAM data types
      Parameters:
      host - host of Redis "localhost"
      port - port of Redis
      username - admin username
      password - admin password
  • Method Details

    • addRecord

      @Step("(Redis) Insert record into key <{key}>") public void addRecord(String key, String jsonValue)
      Adds a JSON record to the specified Redis STREAM.

      The provided JSON object is validated and converted into Redis STREAM fields.

      Each top-level JSON property becomes a STREAM field and its value is stored as a Redis string.

      Example:

       Input JSON:
      
       addRecord("users:admins",
        """
        {"id": 1, "name": "Alex", "active": true}""");
      
       Stored STREAM entry:
       1723456789012-0
           id     -> "1"
           name   -> "Alex"
           active -> "true"
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis STREAM key
      jsonValue - JSON record to add
      Throws:
      net.bugreaper.core.exceptions.JsonMappersException - if the provided JSON is invalid
    • addRecord

      @Step("(Redis) Insert record into key <{key}> with ttl: <{ttlSeconds}>") public void addRecord(String key, String jsonValue, long ttlSeconds)
      Adds a JSON record to the specified Redis STREAM.

      The provided JSON object is validated and converted into Redis STREAM fields, and sets its time-to-live (TTL)

      Each top-level JSON property becomes a STREAM field and its value is stored as a Redis string.

      Example:

       Input JSON:
      
       addRecord("users:admins",
        """
        {"id": 1, "name": "Alex", "active": true}""",
        600);
      
       Stored STREAM entry:
       1723456789012-0
           id     -> "1"
           name   -> "Alex"
           active -> "true"
       
      Specified by:
      addRecord in interface RedisHelperInt
      Parameters:
      key - Redis STREAM key
      jsonValue - JSON record to add
      ttlSeconds - expiration time in seconds
      Throws:
      net.bugreaper.core.exceptions.JsonMappersException - if the provided JSON is invalid
    • grabRecordsByKey

      @Step("(Redis) Grab records from key: {key}") public net.bugreaper.core.assertable.AssertableStringList grabRecordsByKey(String key)
      Returns all records stored in the specified Redis STREAM.

      Uses await until the key exists.

      Each STREAM entry is reconstructed into a JSON string by converting its field-value pairs back into a JSON object. Records are returned in STREAM insertion order.

      Because Redis STREAM fields store string values only, all JSON property values in the returned records are represented as JSON strings, even if the original JSON contained numbers, booleans, or other primitive types.

      Examples:

       users:admins
      
       1723456789012-0
           name   -> Alex
           age    -> 30
      
       1723456789050-0
           name   -> John
           age    -> 25
       

      Returned result:

       [
           {"name":"Alex","age":"30"},
           {"name":"John","age":"25"}
       ]
       
       grabRecordsByKey("users:admins")
         .seeListAnyContainsJson("""
             {"name": "Alex"}""");
       
      Specified by:
      grabRecordsByKey in interface RedisHelperInt
      Parameters:
      key - Redis STREAM key
      Returns:
      AssertableStringList list of stored JSON records
      Throws:
      AssertionError - if the key does not exist or not a Redis STREAM
    • getRecordsCountByKey

      public long getRecordsCountByKey(String key)
      Returns the number of records stored in the specified Redis STREAM.

      This method uses Redis XLEN command.

      Example:

       users:admins
       ├── 1723456789012-0 -> {"data": "record1"}
       ├── 1723456789050-0 -> {"data": "record2"}
      
       getRecordsCountByKey("users:admins") returns 2
       
      Specified by:
      getRecordsCountByKey in interface RedisHelperInt
      Parameters:
      key - Redis STREAM key
      Returns:
      number of records in the STREAM