1+ // Copyright (c) Microsoft. All rights reserved.
12package com .microsoft .semantickernel .connectors .memory .redis ;
23
34import com .fasterxml .jackson .databind .JsonNode ;
910import com .microsoft .semantickernel .memory .recordoptions .DeleteRecordOptions ;
1011import com .microsoft .semantickernel .memory .recordoptions .GetRecordOptions ;
1112import com .microsoft .semantickernel .memory .recordoptions .UpsertRecordOptions ;
13+ import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
1214import reactor .core .publisher .Mono ;
1315import redis .clients .jedis .JedisPooled ;
1416import redis .clients .jedis .Pipeline ;
1719import javax .annotation .Nullable ;
1820import java .util .ArrayList ;
1921import java .util .Collection ;
20- import java .util .HashMap ;
2122import java .util .List ;
2223import java .util .Map ;
2324import java .util .Map .Entry ;
@@ -28,6 +29,13 @@ public class RedisVectorRecordStore<Record> implements VectorRecordStore<String,
2829 private final JedisPooled client ;
2930 private final RedisVectorStoreOptions <Record > options ;
3031
32+ /**
33+ * Creates a new instance of the RedisVectorRecordStore.
34+ *
35+ * @param client The Redis client.
36+ * @param options The options for the store.
37+ */
38+ @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
3139 public RedisVectorRecordStore (JedisPooled client , RedisVectorStoreOptions <Record > options ) {
3240 this .client = client ;
3341
@@ -38,21 +46,22 @@ public RedisVectorRecordStore(JedisPooled client, RedisVectorStoreOptions<Record
3846 }
3947
4048 // If mapper is not provided, add a default one
41- VectorStoreRecordMapper <Record , Map .Entry <String , Object >> mapper = options .getVectorStoreRecordMapper ();
49+ VectorStoreRecordMapper <Record , Map .Entry <String , Object >> mapper = options
50+ .getVectorStoreRecordMapper ();
4251 if (mapper == null ) {
4352 mapper = new RedisVectorStoreRecordMapper .Builder <Record >()
44- .keyField (definition .getKeyField ().getName ())
45- .recordClass (options .getRecordClass ())
46- .build ();
53+ .keyField (definition .getKeyField ().getName ())
54+ .recordClass (options .getRecordClass ())
55+ .build ();
4756 }
4857
4958 this .options = RedisVectorStoreOptions .<Record >builder ()
50- .withRecordClass (options .getRecordClass ())
51- .withDefaultCollectionName (options .getDefaultCollectionName ())
52- .withPrefixCollectionName (options .prefixCollectionName ())
53- .withRecordDefinition (definition )
54- .withVectorStoreRecordMapper (mapper )
55- .build ();
59+ .withRecordClass (options .getRecordClass ())
60+ .withDefaultCollectionName (options .getDefaultCollectionName ())
61+ .withPrefixCollectionName (options .prefixCollectionName ())
62+ .withRecordDefinition (definition )
63+ .withVectorStoreRecordMapper (mapper )
64+ .build ();
5665 }
5766
5867 private String getRedisKey (String key , String collectionName ) {
@@ -79,7 +88,7 @@ private String resolveCollectionName(@Nullable String collectionName) {
7988 @ Override
8089 public Mono <Record > getAsync (String key , GetRecordOptions options ) {
8190 String collectionName = resolveCollectionName (
82- options != null ? options .getCollectionName () : null );
91+ options != null ? options .getCollectionName () : null );
8392
8493 String redisKey = getRedisKey (key , collectionName );
8594 Object result = client .jsonGet (redisKey );
@@ -90,7 +99,8 @@ public Mono<Record> getAsync(String key, GetRecordOptions options) {
9099
91100 JsonNode jsonNode = new ObjectMapper ().valueToTree (result );
92101
93- return Mono .just (this .options .getVectorStoreRecordMapper ().mapStorageModeltoRecord (new SimpleEntry <>(key , jsonNode )));
102+ return Mono .just (this .options .getVectorStoreRecordMapper ()
103+ .mapStorageModeltoRecord (new SimpleEntry <>(key , jsonNode )));
94104 }
95105
96106 /**
@@ -101,9 +111,10 @@ public Mono<Record> getAsync(String key, GetRecordOptions options) {
101111 * @return A Mono emitting a collection of records.
102112 */
103113 @ Override
104- public Mono <Collection <Record >> getBatchAsync (Collection <String > keys , GetRecordOptions options ) {
114+ public Mono <Collection <Record >> getBatchAsync (Collection <String > keys ,
115+ GetRecordOptions options ) {
105116 String collectionName = resolveCollectionName (
106- options != null ? options .getCollectionName () : null );
117+ options != null ? options .getCollectionName () : null );
107118
108119 Pipeline pipeline = client .pipelined ();
109120 List <Entry <String , Response <Object >>> responses = new ArrayList <>(keys .size ());
@@ -115,15 +126,16 @@ public Mono<Collection<Record>> getBatchAsync(Collection<String> keys, GetRecord
115126 pipeline .sync ();
116127
117128 return Mono .just (responses .stream ()
118- .map (entry -> {
119- if (entry .getValue ().get () == null ) {
120- return null ;
121- }
122-
123- JsonNode jsonNode = new ObjectMapper ().valueToTree (entry .getValue ().get ());
124- return this .options .getVectorStoreRecordMapper ().mapStorageModeltoRecord (new SimpleEntry <>(entry .getKey (), jsonNode ));
125- })
126- .collect (Collectors .toList ()));
129+ .map (entry -> {
130+ if (entry .getValue ().get () == null ) {
131+ return null ;
132+ }
133+
134+ JsonNode jsonNode = new ObjectMapper ().valueToTree (entry .getValue ().get ());
135+ return this .options .getVectorStoreRecordMapper ()
136+ .mapStorageModeltoRecord (new SimpleEntry <>(entry .getKey (), jsonNode ));
137+ })
138+ .collect (Collectors .toList ()));
127139 }
128140
129141 /**
@@ -136,9 +148,10 @@ public Mono<Collection<Record>> getBatchAsync(Collection<String> keys, GetRecord
136148 @ Override
137149 public Mono <String > upsertAsync (Record data , UpsertRecordOptions options ) {
138150 String collectionName = resolveCollectionName (
139- options != null ? options .getCollectionName () : null );
151+ options != null ? options .getCollectionName () : null );
140152
141- Entry <String , Object > redisObject = this .options .getVectorStoreRecordMapper ().mapRecordToStorageModel (data );
153+ Entry <String , Object > redisObject = this .options .getVectorStoreRecordMapper ()
154+ .mapRecordToStorageModel (data );
142155 String redisKey = getRedisKey (redisObject .getKey (), collectionName );
143156
144157 client .jsonSet (redisKey , redisObject .getValue ());
@@ -153,15 +166,17 @@ public Mono<String> upsertAsync(Record data, UpsertRecordOptions options) {
153166 * @return A Mono emitting a collection of keys of the upserted records.
154167 */
155168 @ Override
156- public Mono <Collection <String >> upsertBatchAsync (Collection <Record > data , UpsertRecordOptions options ) {
169+ public Mono <Collection <String >> upsertBatchAsync (Collection <Record > data ,
170+ UpsertRecordOptions options ) {
157171 String collectionName = resolveCollectionName (
158- options != null ? options .getCollectionName () : null );
172+ options != null ? options .getCollectionName () : null );
159173
160174 Pipeline pipeline = client .pipelined ();
161175 List <String > keys = new ArrayList <>(data .size ());
162176
163177 data .forEach (record -> {
164- Entry <String , Object > redisObject = this .options .getVectorStoreRecordMapper ().mapRecordToStorageModel (record );
178+ Entry <String , Object > redisObject = this .options .getVectorStoreRecordMapper ()
179+ .mapRecordToStorageModel (record );
165180 String redisKey = getRedisKey (redisObject .getKey (), collectionName );
166181
167182 keys .add (redisObject .getKey ());
@@ -183,7 +198,7 @@ public Mono<Collection<String>> upsertBatchAsync(Collection<Record> data, Upsert
183198 @ Override
184199 public Mono <Void > deleteAsync (String key , DeleteRecordOptions options ) {
185200 String collectionName = resolveCollectionName (
186- options != null ? options .getCollectionName () : null );
201+ options != null ? options .getCollectionName () : null );
187202
188203 String redisKey = getRedisKey (key , collectionName );
189204 client .del (redisKey );
@@ -201,7 +216,7 @@ public Mono<Void> deleteAsync(String key, DeleteRecordOptions options) {
201216 @ Override
202217 public Mono <Void > deleteBatchAsync (Collection <String > strings , DeleteRecordOptions options ) {
203218 String collectionName = resolveCollectionName (
204- options != null ? options .getCollectionName () : null );
219+ options != null ? options .getCollectionName () : null );
205220
206221 Pipeline pipeline = client .pipelined ();
207222 strings .forEach (key -> {
0 commit comments