3131import  java .util .logging .Level ;
3232import  java .util .logging .Logger ;
3333import  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 ;
@@ -157,7 +158,7 @@ public void setUp(String database) {
157158
158159        Objects .requireNonNull (database , "database is required" );
159160
160-         CouchbaseSettings  settings = this ;
161+         CouchbaseSettings  settings  =  this ;
161162
162163        var  collections  = settings .getCollections ().stream ().map (String ::trim )
163164                .filter (index  -> !index .isBlank ()).toList ();
@@ -170,15 +171,15 @@ public void setUp(String database) {
170171        var  scope  = settings .getScope ();
171172
172173        long  start  = System .currentTimeMillis ();
173-         LOGGER .log (Level .FINEST ,"starting the setup with database: "  + database );
174+         LOGGER .log (Level .FINEST ,  "starting the setup with database: "  + database );
174175
175176        try  (Cluster  cluster  = settings .getCluster ()) {
176177
177178            BucketManager  buckets  = cluster .buckets ();
178179            try  {
179180                buckets .getBucket (database );
180181            } catch  (BucketNotFoundException  exp ) {
181-                 LOGGER .log (Level .FINEST ,"The database/bucket does not exist, creating it: "  + database );
182+                 LOGGER .log (Level .FINEST ,  "The database/bucket does not exist, creating it: "  + database );
182183                buckets .createBucket (BucketSettings .create (database ));
183184            }
184185
@@ -193,7 +194,7 @@ public void setUp(String database) {
193194            ScopeSpec  spec  = scopes .stream ().filter (s  -> finalScope .equals (s .name ()))
194195                    .findFirst ().get ();
195196
196-             collectionsToIndex .forEach (collection  -> {
197+             collections .forEach (collection  -> {
197198                if  (spec .collections ().stream ().noneMatch (c  -> collectionsToIndex .contains (c .name ()))) {
198199                    manager .createCollection (CollectionSpec .create (collection , finalScope ));
199200                }
@@ -202,23 +203,26 @@ public void setUp(String database) {
202203            waitUntilReady (bucket );
203204
204205            if  (!collectionsToIndex .isEmpty ()) {
206+ 
205207                QueryIndexManager  queryIndexManager  = cluster .queryIndexes ();
206-                 collections .stream ()
207-                         .filter (collectionsToIndex ::contains )
208+ 
209+                 Stream .concat (collectionsToIndex .stream (), collectionsToIndex .stream ())
210+                         .distinct ()
208211                        .forEach (collection  -> {
209212                            var  allIndexes  = queryIndexManager .getAllIndexes (database , getAllQueryIndexesOptions ()
210213                                    .scopeName (finalScope ).collectionName (collection ));
211214                            if  (allIndexes .isEmpty ()) {
212-                                 LOGGER .log (Level .FINEST ,"Index for "  + collection  + " collection does not exist, creating primary key with scope " 
215+                                 LOGGER .log (Level .FINEST ,  "Index for "  + collection  + " collection does not exist, creating primary key with scope " 
213216                                        + finalScope  + " collection "  + collection  + " at database "  + database );
214217                                queryIndexManager .createPrimaryIndex (database , createPrimaryQueryIndexOptions ()
215218                                        .scopeName (finalScope ).collectionName (collection ));
216219                            }
217220                        });
221+ 
218222            }
219223
220224            long  end  = System .currentTimeMillis () - start ;
221-             LOGGER .log (Level .FINEST ,"Finished the setup with database: "  + database  + " end with millis " 
225+             LOGGER .log (Level .FINEST ,  "Finished the setup with database: "  + database  + " end with millis " 
222226                    + end );
223227        }
224228
0 commit comments