|
11 | 11 | * Contributors: |
12 | 12 | * |
13 | 13 | * Otavio Santana |
| 14 | + * Maximillian Arruda |
14 | 15 | */ |
15 | 16 | package org.eclipse.jnosql.databases.couchbase.communication; |
16 | 17 |
|
|
22 | 23 | import com.couchbase.client.java.manager.collection.CollectionManager; |
23 | 24 | import com.couchbase.client.java.manager.collection.CollectionSpec; |
24 | 25 | import com.couchbase.client.java.manager.collection.ScopeSpec; |
25 | | -import com.couchbase.client.java.manager.query.QueryIndex; |
26 | 26 | import com.couchbase.client.java.manager.query.QueryIndexManager; |
27 | 27 |
|
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.*; |
32 | 31 | import java.util.logging.Level; |
33 | 32 | import java.util.logging.Logger; |
| 33 | +import java.util.stream.Collectors; |
34 | 34 |
|
35 | 35 | import static com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions; |
36 | 36 | import static com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions; |
@@ -154,61 +154,78 @@ public Cluster getCluster() { |
154 | 154 | * @throws NullPointerException when parameter is null |
155 | 155 | */ |
156 | 156 | public void setUp(String database) { |
| 157 | + |
157 | 158 | Objects.requireNonNull(database, "database is required"); |
158 | 159 |
|
| 160 | + CouchbaseSettings settings=this; |
| 161 | + |
| 162 | + var collections = settings.getCollections().stream().map(String::trim) |
| 163 | + .filter(index -> !index.isBlank()).toList(); |
| 164 | + |
| 165 | + var collectionsToIndex = Arrays |
| 166 | + .stream(Optional.ofNullable(settings.getIndex()).orElse("").split(",")) |
| 167 | + .map(String::trim) |
| 168 | + .filter(index -> !index.isBlank()).collect(Collectors.toSet()); |
| 169 | + |
| 170 | + var scope = settings.getScope(); |
| 171 | + |
159 | 172 | long start = System.currentTimeMillis(); |
160 | | - LOGGER.log(Level.FINEST, "starting the setup with database: " + database); |
| 173 | + LOGGER.log(Level.FINEST,"starting the setup with database: " + database); |
161 | 174 |
|
162 | | - try (Cluster cluster = getCluster()) { |
| 175 | + try (Cluster cluster = settings.getCluster()) { |
163 | 176 |
|
164 | 177 | BucketManager buckets = cluster.buckets(); |
165 | 178 | try { |
166 | 179 | buckets.getBucket(database); |
167 | 180 | } catch (BucketNotFoundException exp) { |
168 | | - LOGGER.log(Level.FINEST, "The database/bucket does not exist, creating it: " + database); |
| 181 | + LOGGER.log(Level.FINEST,"The database/bucket does not exist, creating it: " + database); |
169 | 182 | buckets.createBucket(BucketSettings.create(database)); |
170 | 183 | } |
| 184 | + |
171 | 185 | Bucket bucket = cluster.bucket(database); |
172 | 186 |
|
| 187 | + waitUntilReady(bucket); |
| 188 | + |
173 | 189 | CollectionManager manager = bucket.collections(); |
| 190 | + |
174 | 191 | List<ScopeSpec> scopes = manager.getAllScopes(); |
175 | | - String finalScope = getScope().orElseGet(() -> bucket.defaultScope().name()); |
| 192 | + String finalScope = scope.orElseGet(() -> bucket.defaultScope().name()); |
176 | 193 | ScopeSpec spec = scopes.stream().filter(s -> finalScope.equals(s.name())) |
177 | 194 | .findFirst().get(); |
178 | | - for (String collection : collections) { |
179 | | - if (spec.collections().stream().noneMatch(c -> collection.equals(c.name()))) { |
| 195 | + |
| 196 | + collectionsToIndex.forEach(collection -> { |
| 197 | + if (spec.collections().stream().noneMatch(c -> collectionsToIndex.contains(c.name()))) { |
180 | 198 | manager.createCollection(CollectionSpec.create(collection, finalScope)); |
181 | 199 | } |
182 | | - } |
183 | | - if (index != null) { |
184 | | - 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 | | - } |
| 200 | + }); |
193 | 201 |
|
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 | | - } |
| 202 | + waitUntilReady(bucket); |
205 | 203 |
|
| 204 | + if (!collectionsToIndex.isEmpty()) { |
| 205 | + QueryIndexManager queryIndexManager = cluster.queryIndexes(); |
| 206 | + collections.stream() |
| 207 | + .filter(collectionsToIndex::contains) |
| 208 | + .forEach(collection -> { |
| 209 | + var allIndexes = queryIndexManager.getAllIndexes(database, getAllQueryIndexesOptions() |
| 210 | + .scopeName(finalScope).collectionName(collection)); |
| 211 | + if (allIndexes.isEmpty()) { |
| 212 | + LOGGER.log(Level.FINEST,"Index for " + collection + " collection does not exist, creating primary key with scope " |
| 213 | + + finalScope + " collection " + collection + " at database " + database); |
| 214 | + queryIndexManager.createPrimaryIndex(database, createPrimaryQueryIndexOptions() |
| 215 | + .scopeName(finalScope).collectionName(collection)); |
| 216 | + } |
| 217 | + }); |
206 | 218 | } |
207 | 219 |
|
208 | 220 | long end = System.currentTimeMillis() - start; |
209 | | - LOGGER.log(Level.FINEST, "Finished the setup with database: " + database + " end with millis " |
| 221 | + LOGGER.log(Level.FINEST,"Finished the setup with database: " + database + " end with millis " |
210 | 222 | + end); |
211 | 223 | } |
| 224 | + |
| 225 | + } |
| 226 | + |
| 227 | + private void waitUntilReady(Bucket bucket) { |
| 228 | + bucket.waitUntilReady(Duration.of(4, ChronoUnit.SECONDS)); |
212 | 229 | } |
213 | 230 |
|
214 | 231 | @Override |
|
0 commit comments