Skip to content

Commit c47a342

Browse files
committed
✨ Add build and wait for deferred N1QL indexes
1 parent 0ff8e59 commit c47a342

7 files changed

Lines changed: 76 additions & 8 deletions

File tree

src/main/java/com/github/couchmove/Couchmove.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020

21+
import java.time.Duration;
2122
import java.util.Comparator;
2223
import java.util.Date;
2324
import java.util.List;
@@ -196,6 +197,21 @@ void executeMigration(ChangeLog changeLog, int order) {
196197
}
197198
}
198199

200+
/**
201+
* Instruct the query engine to trigger the build of indexes that have been deferred, within the default management
202+
*/
203+
public void buildN1qlDeferredIndexes() {
204+
dbService.buildN1qlDeferredIndexes();
205+
}
206+
207+
/**
208+
* Watches all indexes, polling the query service until they become
209+
* "online" or the timeout has expired
210+
*/
211+
public void waitForN1qlIndexes(Duration duration) {
212+
dbService.waitForN1qlIndexes(duration);
213+
}
214+
199215
/**
200216
* Applies the {@link ChangeLog} according to it's {@link ChangeLog#type} :
201217
* <ul>

src/main/java/com/github/couchmove/repository/CouchbaseRepository.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.couchbase.client.java.manager.view.DesignDocument;
55
import com.github.couchmove.pojo.CouchbaseEntity;
66

7+
import java.time.Duration;
8+
79
/**
810
* A repository for encapsulating storage, retrieval, and removal of json documents to Couchbase {@link Bucket}
911
*
@@ -97,4 +99,17 @@ public interface CouchbaseRepository<E extends CouchbaseEntity> {
9799
* @return name of the repository Couchbase {@link Bucket}
98100
*/
99101
String getBucketName();
102+
103+
/**
104+
* Instruct the query engine to trigger the build of indexes that have been deferred, within the default management
105+
*/
106+
void buildN1qlDeferredIndexes();
107+
108+
/**
109+
* Watches all indexes, polling the query service until they become
110+
* "online" or the timeout has expired
111+
*
112+
* @param duration the maximum duration for which to poll for the index to become online.
113+
*/
114+
void watchN1qlIndexes(Duration duration);
100115
}

src/main/java/com/github/couchmove/repository/CouchbaseRepositoryImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.couchbase.client.java.kv.GetResult;
2020
import com.couchbase.client.java.kv.MutationResult;
2121
import com.couchbase.client.java.kv.UpsertOptions;
22+
import com.couchbase.client.java.manager.query.QueryIndex;
2223
import com.couchbase.client.java.manager.search.SearchIndex;
2324
import com.couchbase.client.java.manager.view.DesignDocument;
2425
import com.couchbase.client.java.manager.view.View;
@@ -34,7 +35,10 @@
3435
import org.jetbrains.annotations.NotNull;
3536
import org.slf4j.Logger;
3637

38+
import java.time.Duration;
39+
import java.util.List;
3740
import java.util.Map;
41+
import java.util.stream.Collectors;
3842

3943
import static com.couchbase.client.java.kv.InsertOptions.insertOptions;
4044
import static com.couchbase.client.java.kv.ReplaceOptions.replaceOptions;
@@ -182,6 +186,19 @@ public String getBucketName() {
182186
return getCollection().bucketName();
183187
}
184188

189+
@Override
190+
public void buildN1qlDeferredIndexes() {
191+
cluster.queryIndexes().buildDeferredIndexes(getBucketName());
192+
}
193+
194+
@Override
195+
public void watchN1qlIndexes(Duration duration) {
196+
List<String> indexes = cluster.queryIndexes().getAllIndexes(getBucketName()).stream()
197+
.map(QueryIndex::name)
198+
.collect(Collectors.toList());
199+
cluster.queryIndexes().watchIndexes(getBucketName(), indexes, duration);
200+
}
201+
185202
private static <SELF extends CommonOptions<SELF>> SELF withRetry(SELF options) {
186203
return options.retryStrategy(BestEffortRetryStrategy.INSTANCE);
187204
}

src/main/java/com/github/couchmove/service/ChangeLogDBService.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15+
import java.time.Duration;
1516
import java.util.*;
1617
import java.util.stream.Collectors;
1718

@@ -144,6 +145,23 @@ public void importFtsIndex(String name, String content) {
144145
repository.importFtsIndex(name, content);
145146
}
146147

148+
/**
149+
* Watches all indexes, polling the query service until they become
150+
* "online" or the timeout has expired
151+
*
152+
* @param duration the maximum duration for which to poll for the index to become online.
153+
*/
154+
public void waitForN1qlIndexes(Duration duration) {
155+
repository.watchN1qlIndexes(duration);
156+
}
157+
158+
/**
159+
* Instruct the query engine to trigger the build of indexes that have been deferred, within the default management
160+
*/
161+
public void buildN1qlDeferredIndexes() {
162+
repository.buildN1qlDeferredIndexes();
163+
}
164+
147165
/**
148166
* Extract multiple requests, separated by ';' ignoring :
149167
* <ul>

src/test/java/com/github/couchmove/CouchmoveIT.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
import org.junit.jupiter.api.BeforeEach;
1818
import org.junit.jupiter.api.Test;
1919

20+
import java.time.Duration;
2021
import java.util.Date;
2122
import java.util.List;
2223
import java.util.Optional;
24+
import java.util.concurrent.TimeUnit;
2325
import java.util.stream.Collectors;
2426
import java.util.stream.Stream;
2527

@@ -192,7 +194,7 @@ public void should_update_changeLog() {
192194
}
193195

194196
@Test
195-
public void should_build_multiple_index_not_fail() {
197+
public void should_build_deferred_indexes() {
196198
// Given a Couchmove instance configured for success migration folder
197199
Couchmove couchmove = getCouchmove("multiple-deferred-indexes");
198200

@@ -208,14 +210,18 @@ public void should_build_multiple_index_not_fail() {
208210
assertEquals(2, changeLogs.size());
209211
assertLike(changeLogs.get(0),
210212
"0", 1, "create deferred index", N1QL, "V0__create_deferred_index.n1ql",
211-
"060f486279932b3838a90f23032a135ad20f8a364fbbda9305f6e20a5b065085",
213+
"8987fdc8782fe4f8321cfae8f388d9005ac6c2eca726105a2739170cc4870a66",
212214
EXECUTED);
213215
assertLike(changeLogs.get(1),
214216
"1", 2, "create second deferred index", N1QL, "V1__create_second_deferred_index.n1ql",
215-
"49fed597ee5f7012b6ab7eb66825e20de4906ecfb10ee9b5ae8f74dfe242b74a",
217+
"77492051f8633e40032881e474207d97d87c3eb1e239a832b1ad11b22c933fe6",
216218
EXECUTED);
217219

218-
// And successfully executed
220+
// Trigger deferred index build
221+
couchmove.buildN1qlDeferredIndexes();
222+
223+
// Wait for indexes to be built
224+
couchmove.waitForN1qlIndexes(Duration.ofSeconds(5));
219225

220226
// Index inserted
221227
Optional<QueryIndex> userIndexInfo = getCluster().queryIndexes().getAllIndexes(getBucket().name()).stream()

src/test/resources/db/migration/multiple-deferred-indexes/V0__create_deferred_index.n1ql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
CREATE INDEX buyer_index ON `${bucket}`(username)
33
WHERE type = 'buyer'
44
WITH { "defer_build" : true };
5-
6-
BUILD INDEX ON `${bucket}`(buyer_index);

src/test/resources/db/migration/multiple-deferred-indexes/V1__create_second_deferred_index.n1ql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
CREATE INDEX merchant_index ON `${bucket}`(username)
33
WHERE type = 'merchant'
44
WITH { "defer_build" : true };
5-
6-
BUILD INDEX ON `${bucket}`(merchant_index);

0 commit comments

Comments
 (0)