1+ // Copyright (c) Microsoft. All rights reserved.
12package com .microsoft .semantickernel .connectors .data .jdbc ;
23
34import com .microsoft .semantickernel .data .recorddefinition .VectorStoreRecordDefinition ;
4- import com . microsoft . semantickernel . exceptions . SKException ;
5+ import edu . umd . cs . findbugs . annotations . SuppressFBWarnings ;
56import reactor .core .publisher .Mono ;
7+ import reactor .core .scheduler .Schedulers ;
68
79import javax .annotation .Nonnull ;
810import javax .annotation .Nullable ;
911import java .sql .Connection ;
10- import java .sql .ResultSet ;
11- import java .sql .SQLException ;
12- import java .util .ArrayList ;
1312import java .util .List ;
1413
1514/**
@@ -27,14 +26,18 @@ public class JDBCVectorStore implements SQLVectorStore<JDBCVectorStoreRecordColl
2726 * @param connection the connection
2827 * @param options the options
2928 */
30- public JDBCVectorStore (@ Nonnull Connection connection , @ Nullable JDBCVectorStoreOptions options ) {
29+ @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
30+ public JDBCVectorStore (@ Nonnull Connection connection ,
31+ @ Nullable JDBCVectorStoreOptions options ) {
3132 this .connection = connection ;
3233 this .options = options ;
3334
3435 if (this .options != null && this .options .getQueryProvider () != null ) {
3536 this .queryProvider = this .options .getQueryProvider ();
3637 } else {
37- this .queryProvider = new JDBCVectorStoreDefaultQueryProvider (connection );
38+ this .queryProvider = JDBCVectorStoreDefaultQueryProvider .builder ()
39+ .withConnection (connection )
40+ .build ();
3841 }
3942 }
4043
@@ -57,30 +60,30 @@ public static Builder builder() {
5760 */
5861 @ Override
5962 public <Key , Record > JDBCVectorStoreRecordCollection <?> getCollection (
60- @ Nonnull String collectionName ,
61- @ Nonnull Class <Record > recordClass ,
62- @ Nullable VectorStoreRecordDefinition recordDefinition ) {
63+ @ Nonnull String collectionName ,
64+ @ Nonnull Class <Record > recordClass ,
65+ @ Nullable VectorStoreRecordDefinition recordDefinition ) {
6366
6467 if (this .options != null && this .options .getVectorStoreRecordCollectionFactory () != null ) {
6568 return this .options .getVectorStoreRecordCollectionFactory ()
6669 .createVectorStoreRecordCollection (
6770 connection ,
6871 collectionName ,
6972 JDBCVectorStoreRecordCollectionOptions .<Record >builder ()
70- .withRecordClass (recordClass )
71- .withRecordDefinition (recordDefinition )
72- .withQueryProvider (this .queryProvider )
73- .build ());
74- }
75-
76- return new JDBCVectorStoreRecordCollection <>(
77- connection ,
78- collectionName ,
79- JDBCVectorStoreRecordCollectionOptions .<Record >builder ()
8073 .withRecordClass (recordClass )
8174 .withRecordDefinition (recordDefinition )
8275 .withQueryProvider (this .queryProvider )
8376 .build ());
77+ }
78+
79+ return new JDBCVectorStoreRecordCollection <>(
80+ connection ,
81+ collectionName ,
82+ JDBCVectorStoreRecordCollectionOptions .<Record >builder ()
83+ .withRecordClass (recordClass )
84+ .withRecordDefinition (recordDefinition )
85+ .withQueryProvider (this .queryProvider )
86+ .build ());
8487 }
8588
8689 /**
@@ -90,30 +93,17 @@ public <Key, Record> JDBCVectorStoreRecordCollection<?> getCollection(
9093 */
9194 @ Override
9295 public Mono <List <String >> getCollectionNamesAsync () {
93- return Mono .fromCallable (() -> {
94- List <String > collectionNames = new ArrayList <>();
95- try {
96- ResultSet resultSet = queryProvider .getCollectionNames ();
97- while (resultSet .next ()) {
98- collectionNames .add (resultSet .getString (1 ));
99- }
100-
101- return collectionNames ;
102- } catch (SQLException e ) {
103- throw new SKException ("Failed to get collection names." , e );
104- }
105- });
96+ return Mono .fromCallable (queryProvider ::getCollectionNames )
97+ .subscribeOn (Schedulers .boundedElastic ());
10698 }
10799
100+ /**
101+ * Prepares the vector store.
102+ */
108103 @ Override
109104 public Mono <Void > prepareAsync () {
110- return Mono .fromRunnable (() -> {
111- try {
112- queryProvider .prepareVectorStore ();
113- } catch (SQLException e ) {
114- throw new SKException ("Failed to prepare vector store." , e );
115- }
116- });
105+ return Mono .fromRunnable (queryProvider ::prepareVectorStore )
106+ .subscribeOn (Schedulers .boundedElastic ()).then ();
117107 }
118108
119109 /**
@@ -129,6 +119,7 @@ public static class Builder {
129119 * @param connection the connection
130120 * @return the builder
131121 */
122+ @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
132123 public Builder withConnection (Connection connection ) {
133124 this .connection = connection ;
134125 return this ;
0 commit comments