Skip to content

Commit 716e2bd

Browse files
author
Milder Hernandez Cagua
committed
Format
1 parent 0121be8 commit 716e2bd

5 files changed

Lines changed: 77 additions & 52 deletions

File tree

‎api-test/integration-tests/src/test/java/com/microsoft/semantickernel/tests/connectors/memory/Hotel.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Hotel {
2222

2323
public Hotel() {
2424
}
25+
2526
public Hotel(String id, String name, int code, String description, List<Float> descriptionEmbedding, double rating) {
2627
this.id = id;
2728
this.name = name;

‎api-test/integration-tests/src/test/java/com/microsoft/semantickernel/tests/connectors/memory/redis/RedisVectorRecordStoreTest.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import javax.annotation.Nonnull;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.Collection;
2223
import java.util.HashMap;
2324
import java.util.List;
@@ -31,10 +32,13 @@
3132
public class RedisVectorRecordStoreTest {
3233

3334
@Container private static final RedisContainer redisContainer = new RedisContainer("redis/redis-stack:latest");
35+
3436
private static final Map<Options, RedisVectorStoreOptions<Hotel>> optionsMap = new HashMap<>();
37+
3538
public enum Options {
3639
DEFAULT, WITH_CUSTOM_DEFINITION
3740
}
41+
3842
@BeforeAll
3943
static void setup() {
4044
optionsMap.put(Options.DEFAULT, RedisVectorStoreOptions.<Hotel>builder()
@@ -74,11 +78,11 @@ public void buildRecordStore(Options options) {
7478

7579
private List<Hotel> getHotels() {
7680
return List.of(
77-
new Hotel("id_1", "Hotel 1", 1, "Hotel 1 description", List.of(1.0f, 2.0f, 3.0f), 4.0),
78-
new Hotel("id_2", "Hotel 2", 2, "Hotel 2 description", List.of(2.0f, 3.0f, 4.0f), 5.0),
79-
new Hotel("id_3", "Hotel 3", 3, "Hotel 3 description", List.of(3.0f, 4.0f, 5.0f), 3.0),
80-
new Hotel("id_4", "Hotel 4", 4, "Hotel 4 description", List.of(4.0f, 5.0f, 6.0f), 2.0),
81-
new Hotel("id_5", "Hotel 5", 5, "Hotel 5 description", List.of(5.0f, 6.0f, 7.0f), 3.5)
81+
new Hotel("id_1", "Hotel 1", 1, "Hotel 1 description", Arrays.asList(1.0f, 2.0f, 3.0f), 4.0),
82+
new Hotel("id_2", "Hotel 2", 2, "Hotel 2 description", Arrays.asList(1.0f, 2.0f, 3.0f), 3.0),
83+
new Hotel("id_3", "Hotel 3", 3, "Hotel 3 description", Arrays.asList(1.0f, 2.0f, 3.0f), 5.0),
84+
new Hotel("id_4", "Hotel 4", 4, "Hotel 4 description", Arrays.asList(1.0f, 2.0f, 3.0f), 4.0),
85+
new Hotel("id_5", "Hotel 5", 5, "Hotel 5 description", Arrays.asList(1.0f, 2.0f, 3.0f), 5.0)
8286
);
8387
}
8488

@@ -102,7 +106,7 @@ public void upsertAndGetRecordAsync(Options options) {
102106
@ParameterizedTest
103107
@EnumSource(Options.class)
104108
public void getBatchAsync(Options options) {
105-
RedisVectorRecordStore<Hotel> recordStore = buildRecordStore(optionsMap.get(options), "getBatchAsync" + options.toString());
109+
RedisVectorRecordStore<Hotel> recordStore = buildRecordStore(optionsMap.get(options), "getBatchAsync");
106110

107111
List<Hotel> hotels = getHotels();
108112
for (Hotel hotel : hotels) {
@@ -128,6 +132,7 @@ public void upsertBatchAsync(Options options) {
128132

129133
List<Hotel> hotels = getHotels();
130134
Collection<String> keys = recordStore.upsertBatchAsync(hotels, null).block();
135+
assertNotNull(keys);
131136

132137
List<Hotel> retrievedHotels = (List<Hotel>) recordStore.getBatchAsync(keys, null).block();
133138

‎connectors/memory/redis/src/main/java/com/microsoft/semantickernel/connectors/memory/redis/RedisVectorRecordStore.java‎

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) Microsoft. All rights reserved.
12
package com.microsoft.semantickernel.connectors.memory.redis;
23

34
import com.fasterxml.jackson.databind.JsonNode;
@@ -9,6 +10,7 @@
910
import com.microsoft.semantickernel.memory.recordoptions.DeleteRecordOptions;
1011
import com.microsoft.semantickernel.memory.recordoptions.GetRecordOptions;
1112
import com.microsoft.semantickernel.memory.recordoptions.UpsertRecordOptions;
13+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1214
import reactor.core.publisher.Mono;
1315
import redis.clients.jedis.JedisPooled;
1416
import redis.clients.jedis.Pipeline;
@@ -17,7 +19,6 @@
1719
import javax.annotation.Nullable;
1820
import java.util.ArrayList;
1921
import java.util.Collection;
20-
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324
import 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 -> {

‎connectors/memory/redis/src/main/java/com/microsoft/semantickernel/connectors/memory/redis/RedisVectorStoreOptions.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) Microsoft. All rights reserved.
12
package com.microsoft.semantickernel.connectors.memory.redis;
23

34
import com.microsoft.semantickernel.memory.VectorStoreRecordMapper;
@@ -14,10 +15,10 @@ public class RedisVectorStoreOptions<Record> {
1415
private final boolean prefixCollectionName;
1516

1617
private RedisVectorStoreOptions(String defaultCollectionName,
17-
Class<Record> recordClass,
18-
VectorStoreRecordMapper<Record, Entry<String, Object>> vectorStoreRecordMapper,
19-
VectorStoreRecordDefinition recordDefinition,
20-
boolean prefixCollectionName) {
18+
Class<Record> recordClass,
19+
VectorStoreRecordMapper<Record, Entry<String, Object>> vectorStoreRecordMapper,
20+
VectorStoreRecordDefinition recordDefinition,
21+
boolean prefixCollectionName) {
2122
this.defaultCollectionName = defaultCollectionName;
2223
this.recordClass = recordClass;
2324
this.vectorStoreRecordMapper = vectorStoreRecordMapper;
@@ -121,7 +122,7 @@ public Builder<Record> withDefaultCollectionName(String defaultCollectionName) {
121122
* @return the builder
122123
*/
123124
public Builder<Record> withVectorStoreRecordMapper(
124-
VectorStoreRecordMapper<Record, Entry<String, Object>> vectorStoreRecordMapper) {
125+
VectorStoreRecordMapper<Record, Entry<String, Object>> vectorStoreRecordMapper) {
125126
this.vectorStoreRecordMapper = vectorStoreRecordMapper;
126127
return this;
127128
}
@@ -159,10 +160,10 @@ public RedisVectorStoreOptions<Record> build() {
159160
}
160161

161162
return new RedisVectorStoreOptions<>(defaultCollectionName,
162-
recordClass,
163-
vectorStoreRecordMapper,
164-
recordDefinition,
165-
prefixCollectionName);
163+
recordClass,
164+
vectorStoreRecordMapper,
165+
recordDefinition,
166+
prefixCollectionName);
166167
}
167168
}
168169
}

‎connectors/memory/redis/src/main/java/com/microsoft/semantickernel/connectors/memory/redis/RedisVectorStoreRecordMapper.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
// Copyright (c) Microsoft. All rights reserved.
12
package com.microsoft.semantickernel.connectors.memory.redis;
23

34
import com.fasterxml.jackson.annotation.JsonAutoDetect;
45
import com.fasterxml.jackson.core.JsonProcessingException;
5-
import com.fasterxml.jackson.databind.DeserializationFeature;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.fasterxml.jackson.databind.node.ObjectNode;
88
import com.microsoft.semantickernel.builders.SemanticKernelBuilder;
@@ -13,10 +13,11 @@
1313
import java.util.Map.Entry;
1414
import java.util.function.Function;
1515

16-
public class RedisVectorStoreRecordMapper<Record> extends VectorStoreRecordMapper<Record, Entry<String, Object>> {
16+
public class RedisVectorStoreRecordMapper<Record>
17+
extends VectorStoreRecordMapper<Record, Entry<String, Object>> {
1718
private RedisVectorStoreRecordMapper(
18-
Function<Record, Entry<String, Object>> toStorageModelMapper,
19-
Function<Entry<String, Object>, Record> toRecordMapper) {
19+
Function<Record, Entry<String, Object>> toStorageModelMapper,
20+
Function<Entry<String, Object>, Record> toRecordMapper) {
2021
super(toStorageModelMapper, toRecordMapper);
2122
}
2223

@@ -26,7 +27,8 @@ private RedisVectorStoreRecordMapper(
2627
* @param <Record> the record type
2728
* @return the builder
2829
*/
29-
public static class Builder<Record> implements SemanticKernelBuilder<RedisVectorStoreRecordMapper<Record>> {
30+
public static class Builder<Record>
31+
implements SemanticKernelBuilder<RedisVectorStoreRecordMapper<Record>> {
3032
private String keyField;
3133
private Class<Record> recordClass;
3234

@@ -80,7 +82,8 @@ public RedisVectorStoreRecordMapper<Record> build() {
8082
}
8183
}, storageModel -> {
8284
ObjectMapper mapper = new ObjectMapper();
83-
mapper.setVisibility(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
85+
mapper.setVisibility(VisibilityChecker.Std.defaultInstance()
86+
.withFieldVisibility(JsonAutoDetect.Visibility.ANY));
8487

8588
ObjectNode jsonNode = mapper.valueToTree(storageModel.getValue());
8689

0 commit comments

Comments
 (0)