Package com.redis.vl.storage
Class BaseStorage
java.lang.Object
com.redis.vl.storage.BaseStorage
- Direct Known Subclasses:
HashStorage,JsonStorage
Base class for internal storage handling in Redis.
Provides foundational methods for key management, data preprocessing, validation, and basic read/write operations.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classHelper class for key-value pairs used during preprocessing and validation. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intDefault batch size for bulk operations.protected final IndexSchemaThe index schema for this storage instance. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconvertBytes(Map<String, Object> map) Convert byte arrays in the map to appropriate types.protected static StringCreate a Redis key using a combination of a prefix, separator, and the identifier.protected StringcreateKeyForObject(Map<String, Object> obj, String idField) Construct a Redis key for a given object, optionally using a specified field from the object as the key.get(UnifiedJedis redisClient, Collection<String> keys) Retrieve objects from Redis by keys with default batch size.get(UnifiedJedis redisClient, Collection<String> keys, Integer batchSize) Retrieve objects from Redis by keys.protected IndexSchemaGet the index schema.Generate Redis keys for a list of objects.getResponse(Pipeline pipeline, String key) Get a response for retrieving a value from Redis using a pipeline.protected List<BaseStorage.KeyValuePair> preprocessAndValidateObjects(List<Map<String, Object>> objects, String idField, List<String> keys, Function<Map<String, Object>, Map<String, Object>> preprocess, boolean doValidate) Preprocess and validate a list of objects.preprocessObject(Map<String, Object> obj, Function<Map<String, Object>, Map<String, Object>> preprocess) Apply a preprocessing function to the object if provided.protected abstract voidSet a key-value pair in Redis using a pipeline.Validate an object against the schema.Write a batch of objects to Redis with default parameters.write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys) Write with id field.write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl) Write with TTL.write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl, Integer batchSize) Write with batch size.write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl, Integer batchSize, Function<Map<String, Object>, Map<String, Object>> preprocess) Write with preprocessing.write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl, Integer batchSize, Function<Map<String, Object>, Map<String, Object>> preprocess, boolean doValidate) Write a batch of objects to Redis.
-
Field Details
-
indexSchema
The index schema for this storage instance. -
defaultBatchSize
protected int defaultBatchSizeDefault batch size for bulk operations.
-
-
Constructor Details
-
BaseStorage
Creates a new BaseStorage instance.- Parameters:
indexSchema- The index schema for this storage
-
-
Method Details
-
createKey
Create a Redis key using a combination of a prefix, separator, and the identifier.- Parameters:
id- The unique identifier for the Redis entryprefix- A prefix to append before the key valuekeySeparator- A separator to insert between prefix and key value- Returns:
- The fully formed Redis key
-
preprocessObject
protected static Map<String,Object> preprocessObject(Map<String, Object> obj, Function<Map<String, Object>, Map<String, Object>> preprocess) Apply a preprocessing function to the object if provided.- Parameters:
obj- Object to preprocesspreprocess- Function to process the object- Returns:
- Processed object as a map
-
getIndexSchema
Get the index schema.- Returns:
- The index schema for this storage
-
createKeyForObject
Construct a Redis key for a given object, optionally using a specified field from the object as the key.- Parameters:
obj- The object from which to construct the keyidField- The field to use as the key, if provided- Returns:
- The constructed Redis key for the object
- Throws:
IllegalArgumentException- If the id_field is not found in the object
-
getKeys
Generate Redis keys for a list of objects.- Parameters:
objects- List of objectskeys- Optional iterable of keysidField- Field to use as the key- Returns:
- List of generated keys
-
validate
Validate an object against the schema.- Parameters:
obj- The object to validate- Returns:
- Validated object
-
preprocessAndValidateObjects
protected List<BaseStorage.KeyValuePair> preprocessAndValidateObjects(List<Map<String, Object>> objects, String idField, List<String> keys, Function<Map<String, Object>, Map<String, Object>> preprocess, boolean doValidate) Preprocess and validate a list of objects.- Parameters:
objects- List of objects to preprocess and validateidField- Field to use as the keykeys- Optional list of keyspreprocess- Optional preprocessing functiondoValidate- Whether to validate against schema- Returns:
- List of tuples (key, processed_obj) for valid objects
-
write
public List<String> write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl, Integer batchSize, Function<Map<String, Object>, Map<String, Object>> preprocess, boolean doValidate) Write a batch of objects to Redis. This method returns a list of Redis keys written to the database.- Parameters:
redisClient- A Redis client used for writing dataobjects- An iterable of objects to storeidField- Field used as the key for each objectkeys- Optional list of keys, must match the length of objects if providedttl- Time-to-live in seconds for each keybatchSize- Number of objects to write in a single Redis pipeline executionpreprocess- A function to preprocess objects before storagedoValidate- Whether to validate objects against schema- Returns:
- List of keys written to Redis
-
write
Write a batch of objects to Redis with default parameters.- Parameters:
redisClient- Redis clientobjects- Objects to write- Returns:
- List of keys written
-
write
public List<String> write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys) Write with id field.- Parameters:
redisClient- Redis clientobjects- Objects to writeidField- Field to use as keykeys- Custom keys- Returns:
- List of keys written
-
write
public List<String> write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl) Write with TTL.- Parameters:
redisClient- Redis clientobjects- Objects to writeidField- Field to use as keykeys- Custom keysttl- Time to live in seconds- Returns:
- List of keys written
-
write
public List<String> write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl, Integer batchSize) Write with batch size.- Parameters:
redisClient- Redis clientobjects- Objects to writeidField- Field to use as keykeys- Custom keysttl- Time to livebatchSize- Batch size for pipeline- Returns:
- List of keys written
-
write
public List<String> write(UnifiedJedis redisClient, List<Map<String, Object>> objects, String idField, List<String> keys, Integer ttl, Integer batchSize, Function<Map<String, Object>, Map<String, Object>> preprocess) Write with preprocessing.- Parameters:
redisClient- Redis clientobjects- Objects to writeidField- Field to use as keykeys- Custom keysttl- Time to livebatchSize- Batch sizepreprocess- Preprocessing function- Returns:
- List of keys written
-
get
public List<Map<String,Object>> get(UnifiedJedis redisClient, Collection<String> keys, Integer batchSize) Retrieve objects from Redis by keys.- Parameters:
redisClient- Synchronous Redis clientkeys- Keys to retrieve from RedisbatchSize- Number of objects to retrieve in a single Redis pipeline execution- Returns:
- List of objects pulled from redis
-
get
Retrieve objects from Redis by keys with default batch size.- Parameters:
redisClient- Redis clientkeys- Keys to retrieve- Returns:
- List of objects
-
convertBytes
Convert byte arrays in the map to appropriate types.- Parameters:
map- Map with potential byte arrays- Returns:
- Map with converted values
-
set
Set a key-value pair in Redis using a pipeline.- Parameters:
pipeline- The Redis pipeline to usekey- The Redis keyobj- The object to store
-
getResponse
Get a response for retrieving a value from Redis using a pipeline.- Parameters:
pipeline- The Redis pipeline to usekey- The Redis key- Returns:
- Response containing the retrieved object
-