1111 * Contributors:
1212 *
1313 * Otavio Santana
14+ * Maximillian Arruda
1415 */
1516package org .eclipse .jnosql .databases .couchbase .communication ;
1617
2223import com .couchbase .client .java .manager .collection .CollectionManager ;
2324import com .couchbase .client .java .manager .collection .CollectionSpec ;
2425import com .couchbase .client .java .manager .collection .ScopeSpec ;
25- import com .couchbase .client .java .manager .query .QueryIndex ;
2626import com .couchbase .client .java .manager .query .QueryIndexManager ;
2727
28- import java .util .Collections ;
29- import java .util .List ;
30- import java .util .Objects ;
31- import java .util .Optional ;
28+ import java .time .Duration ;
29+ import java .time .temporal .ChronoUnit ;
30+ import java .util .*;
3231import java .util .logging .Level ;
3332import java .util .logging .Logger ;
33+ import java .util .stream .Collectors ;
34+ import java .util .stream .Stream ;
3435
3536import static com .couchbase .client .java .manager .query .CreatePrimaryQueryIndexOptions .createPrimaryQueryIndexOptions ;
3637import static com .couchbase .client .java .manager .query .GetAllQueryIndexesOptions .getAllQueryIndexesOptions ;
@@ -154,8 +155,19 @@ public Cluster getCluster() {
154155 * @throws NullPointerException when parameter is null
155156 */
156157 public void setUp (String database ) {
158+
157159 Objects .requireNonNull (database , "database is required" );
158160
161+ var collections = getCollections ().stream ().map (String ::trim )
162+ .filter (index -> !index .isBlank ()).toList ();
163+
164+ var collectionsToIndex = Arrays
165+ .stream (Optional .ofNullable (getIndex ()).orElse ("" ).split ("," ))
166+ .map (String ::trim )
167+ .filter (index -> !index .isBlank ()).collect (Collectors .toSet ());
168+
169+ var scope = getScope ();
170+
159171 long start = System .currentTimeMillis ();
160172 LOGGER .log (Level .FINEST , "starting the setup with database: " + database );
161173
@@ -168,47 +180,54 @@ public void setUp(String database) {
168180 LOGGER .log (Level .FINEST , "The database/bucket does not exist, creating it: " + database );
169181 buckets .createBucket (BucketSettings .create (database ));
170182 }
183+
171184 Bucket bucket = cluster .bucket (database );
172185
186+ waitUntilReady (bucket );
187+
173188 CollectionManager manager = bucket .collections ();
189+
174190 List <ScopeSpec > scopes = manager .getAllScopes ();
175- String finalScope = getScope () .orElseGet (() -> bucket .defaultScope ().name ());
191+ String finalScope = scope .orElseGet (() -> bucket .defaultScope ().name ());
176192 ScopeSpec spec = scopes .stream ().filter (s -> finalScope .equals (s .name ()))
177193 .findFirst ().get ();
178- for (String collection : collections ) {
179- if (spec .collections ().stream ().noneMatch (c -> collection .equals (c .name ()))) {
194+
195+ collections .forEach (collection -> {
196+ if (spec .collections ().stream ().noneMatch (c -> collectionsToIndex .contains (c .name ()))) {
180197 manager .createCollection (CollectionSpec .create (collection , finalScope ));
181198 }
182- }
183- if (index != null ) {
199+ });
200+
201+ waitUntilReady (bucket );
202+
203+ if (!collectionsToIndex .isEmpty ()) {
204+
184205 QueryIndexManager queryIndexManager = cluster .queryIndexes ();
185- List <QueryIndex > indexes = queryIndexManager .getAllIndexes (database , getAllQueryIndexesOptions ()
186- .scopeName (finalScope ).collectionName (index ));
187- if (indexes .isEmpty ()) {
188- LOGGER .log (Level .FINEST , "Index does not exist, creating primary key with scope "
189- + scope + " collection " + index + " at database " + database );
190- queryIndexManager .createPrimaryIndex (database , createPrimaryQueryIndexOptions ()
191- .scopeName (finalScope ).collectionName (index ));
192- }
193206
194- for (String collection : collections ) {
195- queryIndexManager = cluster .queryIndexes ();
196- indexes = queryIndexManager .getAllIndexes (database , getAllQueryIndexesOptions ()
197- .scopeName (finalScope ).collectionName (collection ));
198- if (indexes .isEmpty ()) {
199- LOGGER .log (Level .FINEST , "Index for " + collection + " collection does not exist, creating primary key with scope "
200- + scope + " collection " + collection + " at database " + database );
201- queryIndexManager .createPrimaryIndex (database , createPrimaryQueryIndexOptions ()
202- .scopeName (finalScope ).collectionName (collection ));
203- }
204- }
207+ Stream .concat (collectionsToIndex .stream (), collectionsToIndex .stream ())
208+ .distinct ()
209+ .forEach (collection -> {
210+ var allIndexes = queryIndexManager .getAllIndexes (database , getAllQueryIndexesOptions ()
211+ .scopeName (finalScope ).collectionName (collection ));
212+ if (allIndexes .isEmpty ()) {
213+ LOGGER .log (Level .FINEST , "Index for " + collection + " collection does not exist, creating primary key with scope "
214+ + finalScope + " collection " + collection + " at database " + database );
215+ queryIndexManager .createPrimaryIndex (database , createPrimaryQueryIndexOptions ()
216+ .scopeName (finalScope ).collectionName (collection ));
217+ }
218+ });
205219
206220 }
207221
208222 long end = System .currentTimeMillis () - start ;
209223 LOGGER .log (Level .FINEST , "Finished the setup with database: " + database + " end with millis "
210224 + end );
211225 }
226+
227+ }
228+
229+ private void waitUntilReady (Bucket bucket ) {
230+ bucket .waitUntilReady (Duration .of (4 , ChronoUnit .SECONDS ));
212231 }
213232
214233 @ Override
0 commit comments