Skip to content

Commit b18e94c

Browse files
committed
✨ Add build and wait for deferred N1QL indexes
1 parent fa69c19 commit b18e94c

7 files changed

Lines changed: 72 additions & 8 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ void executeMigration(ChangeLog changeLog, int order) {
199199
}
200200
}
201201

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

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

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

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

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ public String getBucketName() {
199199
return bucket.name();
200200
}
201201

202+
@Override
203+
public void buildN1qlDeferredIndexes() {
204+
bucket.bucketManager().buildN1qlDeferredIndexes();
205+
}
206+
207+
@Override
208+
public void watchN1qlIndexes(long timeout, TimeUnit timeunit) {
209+
List<String> indexes = bucket.bucketManager().listN1qlIndexes().stream()
210+
.map(IndexInfo::name)
211+
.collect(Collectors.toList());
212+
bucket.bucketManager().watchN1qlIndexes(indexes, timeout, timeunit);
213+
}
214+
202215
//<editor-fold desc="Helpers">
203216
@Nullable
204217
private <T> T single(Observable<T> observable) {

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

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

1515
import java.util.*;
16+
import java.util.concurrent.TimeUnit;
1617
import java.util.stream.Collectors;
1718

1819
/**
@@ -140,6 +141,24 @@ public void importFtsIndex(String name, String content) {
140141
repository.importFtsIndex(name, content);
141142
}
142143

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

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Date;
1919
import java.util.List;
2020
import java.util.Optional;
21+
import java.util.concurrent.TimeUnit;
2122
import java.util.stream.Collectors;
2223
import java.util.stream.Stream;
2324

@@ -190,7 +191,7 @@ public void should_update_changeLog() {
190191
}
191192

192193
@Test
193-
public void should_build_multiple_index_not_fail() {
194+
public void should_build_deferred_indexes() {
194195
// Given a Couchmove instance configured for success migration folder
195196
Couchmove couchmove = getCouchmove("multiple-deferred-indexes");
196197

@@ -206,14 +207,18 @@ public void should_build_multiple_index_not_fail() {
206207
assertEquals(2, changeLogs.size());
207208
assertLike(changeLogs.get(0),
208209
"0", 1, "create deferred index", N1QL, "V0__create_deferred_index.n1ql",
209-
"060f486279932b3838a90f23032a135ad20f8a364fbbda9305f6e20a5b065085",
210+
"8987fdc8782fe4f8321cfae8f388d9005ac6c2eca726105a2739170cc4870a66",
210211
EXECUTED);
211212
assertLike(changeLogs.get(1),
212213
"1", 2, "create second deferred index", N1QL, "V1__create_second_deferred_index.n1ql",
213-
"49fed597ee5f7012b6ab7eb66825e20de4906ecfb10ee9b5ae8f74dfe242b74a",
214+
"77492051f8633e40032881e474207d97d87c3eb1e239a832b1ad11b22c933fe6",
214215
EXECUTED);
215216

216-
// And successfully executed
217+
// Trigger deferred index build
218+
couchmove.buildN1qlDeferredIndexes();
219+
220+
// Wait for indexes to be built
221+
couchmove.waitForN1qlIndexes(5, TimeUnit.SECONDS);
217222

218223
// Index inserted
219224
Optional<IndexInfo> userIndexInfo = getBucket().bucketManager().listN1qlIndexes().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)