Skip to content

Commit 473e9d1

Browse files
author
Milder Hernandez Cagua
committed
Add suggestions and update docs
1 parent ae7ee50 commit 473e9d1

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

api-test/integration-tests/src/test/java/com/microsoft/semantickernel/tests/connectors/memory/jdbc/JDBCVectorStoreRecordCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@Testcontainers
2929
public class JDBCVectorStoreRecordCollectionTest {
3030
@Container
31-
private static final MySQLContainer<?> CONTAINER = new MySQLContainer<>("mysql:5.7.34");
31+
private static final MySQLContainer<?> CONTAINER = new MySQLContainer<>("mysql:latest");
3232
private static final String MYSQL_USER = "test";
3333
private static final String MYSQL_PASSWORD = "test";
3434
private static MysqlDataSource dataSource;

semantickernel-experimental/src/main/java/com/microsoft/semantickernel/connectors/data/jdbc/JDBCVectorStoreDefaultQueryProvider.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ protected String getCollectionTableName(String collectionName) {
114114
return validateSQLidentifier(prefixForCollectionTables + collectionName);
115115
}
116116

117+
/**
118+
* Prepares the vector store.
119+
* Executes any necessary setup steps for the vector store.
120+
*
121+
* @throws SKException if an error occurs while preparing the vector store
122+
*/
117123
@Override
118124
public void prepareVectorStore() {
119125
String createCollectionsTable = "CREATE TABLE IF NOT EXISTS "
@@ -128,6 +134,13 @@ public void prepareVectorStore() {
128134
}
129135
}
130136

137+
/**
138+
* Checks if the types of the record class fields are supported.
139+
*
140+
* @param recordClass the record class
141+
* @param recordDefinition the record definition
142+
* @throws IllegalArgumentException if the types are not supported
143+
*/
131144
@Override
132145
public void validateSupportedTypes(Class<?> recordClass,
133146
VectorStoreRecordDefinition recordDefinition) {
@@ -140,6 +153,13 @@ public void validateSupportedTypes(Class<?> recordClass,
140153
recordDefinition.getVectorDeclaredFields(recordClass), supportedVectorTypes.keySet());
141154
}
142155

156+
/**
157+
* Checks if a collection exists.
158+
*
159+
* @param collectionName the collection name
160+
* @return true if the collection exists, false otherwise
161+
* @throws SKException if an error occurs while checking if the collection exists
162+
*/
143163
@Override
144164
public boolean collectionExists(String collectionName) {
145165
String query = "SELECT 1 FROM " + validateSQLidentifier(collectionsTable)
@@ -155,6 +175,14 @@ public boolean collectionExists(String collectionName) {
155175
}
156176
}
157177

178+
/**
179+
* Creates a collection.
180+
*
181+
* @param collectionName the collection name
182+
* @param recordClass the record class
183+
* @param recordDefinition the record definition
184+
* @throws SKException if an error occurs while creating the collection
185+
*/
158186
@Override
159187
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING") // SQL query is generated dynamically with valid identifiers
160188
public void createCollection(String collectionName, Class<?> recordClass,
@@ -188,6 +216,12 @@ public void createCollection(String collectionName, Class<?> recordClass,
188216
}
189217
}
190218

219+
/**
220+
* Deletes a collection.
221+
*
222+
* @param collectionName the collection name
223+
* @throws SKException if an error occurs while deleting the collection
224+
*/
191225
@Override
192226
public void deleteCollection(String collectionName) {
193227
String deleteCollectionOperation = "DELETE FROM " + validateSQLidentifier(collectionsTable)
@@ -211,6 +245,12 @@ public void deleteCollection(String collectionName) {
211245
}
212246
}
213247

248+
/**
249+
* Gets the collection names.
250+
*
251+
* @return the collection names
252+
* @throws SKException if an error occurs while getting the collection names
253+
*/
214254
@Override
215255
public List<String> getCollectionNames() {
216256
String query = "SELECT collectionId FROM " + validateSQLidentifier(collectionsTable);
@@ -230,6 +270,18 @@ public List<String> getCollectionNames() {
230270
}
231271
}
232272

273+
/**
274+
* Gets a list of records from the store.
275+
*
276+
* @param collectionName the collection name
277+
* @param keys the keys
278+
* @param recordDefinition the record definition
279+
* @param mapper the mapper
280+
* @param options the options
281+
* @return the records
282+
* @param <Record> the record type
283+
* @throws SKException if an error occurs while getting the records
284+
*/
233285
@Override
234286
public <Record> List<Record> getRecords(String collectionName, List<String> keys,
235287
VectorStoreRecordDefinition recordDefinition, JDBCVectorStoreRecordMapper<Record> mapper,
@@ -272,6 +324,15 @@ public void upsertRecords(String collectionName, List<?> records,
272324
"Upsert is not supported. Try with a specific query provider.");
273325
}
274326

327+
/**
328+
* Deletes records.
329+
*
330+
* @param collectionName the collection name
331+
* @param keys the keys
332+
* @param recordDefinition the record definition
333+
* @param options the options
334+
* @throws SKException if an error occurs while deleting the records
335+
*/
275336
@Override
276337
public void deleteRecords(String collectionName, List<String> keys,
277338
VectorStoreRecordDefinition recordDefinition, DeleteRecordOptions options) {
@@ -291,6 +352,13 @@ public void deleteRecords(String collectionName, List<String> keys,
291352
}
292353
}
293354

355+
/**
356+
* Validates an SQL identifier.
357+
*
358+
* @param identifier the identifier
359+
* @return the identifier if it is valid
360+
* @throws IllegalArgumentException if the identifier is invalid
361+
*/
294362
public static String validateSQLidentifier(String identifier) {
295363
if (identifier.matches("[a-zA-Z_][a-zA-Z0-9_]*")) {
296364
return identifier;

semantickernel-experimental/src/main/java/com/microsoft/semantickernel/connectors/data/jdbc/JDBCVectorStoreRecordCollection.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public String getCollectionName() {
8383
* Checks if the collection exists in the store.
8484
*
8585
* @return A Mono emitting a boolean indicating if the collection exists.
86+
* @throws SKException if the operation fails
8687
*/
8788
@Override
8889
public Mono<Boolean> collectionExistsAsync() {
@@ -95,6 +96,7 @@ public Mono<Boolean> collectionExistsAsync() {
9596
* Creates the collection in the store.
9697
*
9798
* @return A Mono representing the completion of the creation operation.
99+
* @throws SKException if the operation fails
98100
*/
99101
@Override
100102
public Mono<Void> createCollectionAsync() {
@@ -109,6 +111,7 @@ public Mono<Void> createCollectionAsync() {
109111
* Creates the collection in the store if it does not exist.
110112
*
111113
* @return A Mono representing the completion of the creation operation.
114+
* @throws SKException if the operation fails
112115
*/
113116
@Override
114117
public Mono<Void> createCollectionIfNotExistsAsync() {
@@ -127,6 +130,7 @@ public Mono<Void> createCollectionIfNotExistsAsync() {
127130
* Deletes the collection from the store.
128131
*
129132
* @return A Mono representing the completion of the deletion operation.
133+
* @throws SKException if the operation fails
130134
*/
131135
@Override
132136
public Mono<Void> deleteCollectionAsync() {
@@ -142,6 +146,7 @@ public Mono<Void> deleteCollectionAsync() {
142146
* @param key The key of the record to get.
143147
* @param options The options for getting the record.
144148
* @return A Mono emitting the record.
149+
* @throws SKException if the operation fails
145150
*/
146151
@Override
147152
public Mono<Record> getAsync(String key, GetRecordOptions options) {
@@ -160,6 +165,7 @@ public Mono<Record> getAsync(String key, GetRecordOptions options) {
160165
* @param keys The keys of the records to get.
161166
* @param options The options for getting the records.
162167
* @return A Mono emitting a collection of records.
168+
* @throws SKException if the operation fails
163169
*/
164170
@Override
165171
public Mono<List<Record>> getBatchAsync(List<String> keys, GetRecordOptions options) {
@@ -187,6 +193,7 @@ protected String getKeyFromRecord(Record data) {
187193
* @param data The record to upsert.
188194
* @param options The options for upserting the record.
189195
* @return A Mono emitting the key of the upserted record.
196+
* @throws SKException if the operation fails
190197
*/
191198
@Override
192199
public Mono<String> upsertAsync(Record data, UpsertRecordOptions options) {
@@ -205,6 +212,7 @@ public Mono<String> upsertAsync(Record data, UpsertRecordOptions options) {
205212
* @param data The records to upsert.
206213
* @param options The options for upserting the records.
207214
* @return A Mono emitting a collection of keys of the upserted records.
215+
* @throws SKException if the operation fails
208216
*/
209217
@Override
210218
public Mono<List<String>> upsertBatchAsync(List<Record> data, UpsertRecordOptions options) {
@@ -222,6 +230,7 @@ public Mono<List<String>> upsertBatchAsync(List<Record> data, UpsertRecordOption
222230
* @param key The key of the record to delete.
223231
* @param options The options for deleting the record.
224232
* @return A Mono representing the completion of the deletion operation.
233+
* @throws SKException if the operation fails
225234
*/
226235
@Override
227236
public Mono<Void> deleteAsync(String key, DeleteRecordOptions options) {
@@ -234,6 +243,7 @@ public Mono<Void> deleteAsync(String key, DeleteRecordOptions options) {
234243
* @param keys The keys of the records to delete.
235244
* @param options The options for deleting the records.
236245
* @return A Mono representing the completion of the deletion operation.
246+
* @throws SKException if the operation fails
237247
*/
238248
@Override
239249
public Mono<Void> deleteBatchAsync(List<String> keys, DeleteRecordOptions options) {
@@ -247,6 +257,7 @@ public Mono<Void> deleteBatchAsync(List<String> keys, DeleteRecordOptions option
247257
* Prepares the collection for use.
248258
*
249259
* @return A Mono representing the completion of the preparation operation.
260+
* @throws SKException if the operation fails
250261
*/
251262
@Override
252263
public Mono<Void> prepareAsync() {

semantickernel-experimental/src/main/java/com/microsoft/semantickernel/connectors/data/jdbc/MySQLVectorStoreQueryProvider.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ private void setStatementValues(PreparedStatement statement, Object record,
7171
}
7272
}
7373

74+
/**
75+
* Upserts records into the collection.
76+
* @param collectionName the collection name
77+
* @param records the records to upsert
78+
* @param recordDefinition the record definition
79+
* @param options the upsert options
80+
* @throws SKException if the upsert fails
81+
*/
7482
@Override
7583
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING") // SQL query is generated dynamically with valid identifiers
7684
public void upsertRecords(String collectionName, List<?> records,

semantickernel-experimental/src/main/java/com/microsoft/semantickernel/data/recorddefinition/VectorStoreRecordDefinition.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ public List<VectorStoreRecordField> getNonVectorFields() {
5656
return fields;
5757
}
5858

59+
private enum DeclaredFieldType {
60+
KEY, DATA, VECTOR
61+
}
62+
5963
private List<Field> getDeclaredFields(Class<?> recordClass, List<VectorStoreRecordField> fields,
60-
String fieldType) {
64+
DeclaredFieldType fieldType) {
6165
List<Field> declaredFields = new ArrayList<>();
6266
for (VectorStoreRecordField field : fields) {
6367
try {
@@ -85,14 +89,14 @@ public List<Field> getDataDeclaredFields(Class<?> recordClass) {
8589
return getDeclaredFields(
8690
recordClass,
8791
dataFields.stream().map(f -> (VectorStoreRecordField) f).collect(Collectors.toList()),
88-
"Data");
92+
DeclaredFieldType.DATA);
8993
}
9094

9195
public List<Field> getVectorDeclaredFields(Class<?> recordClass) {
9296
return getDeclaredFields(
9397
recordClass,
9498
vectorFields.stream().map(f -> (VectorStoreRecordField) f).collect(Collectors.toList()),
95-
"Vector");
99+
DeclaredFieldType.VECTOR);
96100
}
97101

98102
private VectorStoreRecordDefinition(

0 commit comments

Comments
 (0)