Skip to content

Commit

Permalink
✨ Add build and wait for deferred N1QL indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
tchlyah committed Oct 3, 2020
1 parent fa69c19 commit b18e94c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/github/couchmove/Couchmove.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ void executeMigration(ChangeLog changeLog, int order) {
}
}

/**
* Instruct the query engine to trigger the build of indexes that have been deferred, within the default management
*/
public void buildN1qlDeferredIndexes() {
dbService.buildN1qlDeferredIndexes();
}

/**
* Watches all indexes, polling the query service until they become
* "online" or the timeout has expired
*/
public void waitForN1qlIndexes(long timeout, TimeUnit timeunit) {
dbService.waitForN1qlIndexes(timeout, timeunit);
}

/**
* Applies the {@link ChangeLog} according to it's {@link ChangeLog#type} :
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.couchbase.client.java.view.DesignDocument;
import com.github.couchmove.pojo.CouchbaseEntity;

import java.util.concurrent.TimeUnit;

/**
* A repository for encapsulating storage, retrieval, and removal of json documents to Couchbase {@link Bucket}
*
Expand Down Expand Up @@ -98,4 +100,18 @@ public interface CouchbaseRepository<E extends CouchbaseEntity> {
* @return name of the repository Couchbase {@link Bucket}
*/
String getBucketName();

/**
* Instruct the query engine to trigger the build of indexes that have been deferred, within the default management
*/
void buildN1qlDeferredIndexes();

/**
* Watches all indexes, polling the query service until they become
* "online" or the timeout has expired
*
* @param timeout the maximum duration for which to poll for the index to become online.
* @param timeunit the time unit for the timeout.
*/
void watchN1qlIndexes(long timeout, TimeUnit timeunit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ public String getBucketName() {
return bucket.name();
}

@Override
public void buildN1qlDeferredIndexes() {
bucket.bucketManager().buildN1qlDeferredIndexes();
}

@Override
public void watchN1qlIndexes(long timeout, TimeUnit timeunit) {
List<String> indexes = bucket.bucketManager().listN1qlIndexes().stream()
.map(IndexInfo::name)
.collect(Collectors.toList());
bucket.bucketManager().watchN1qlIndexes(indexes, timeout, timeunit);
}

//<editor-fold desc="Helpers">
@Nullable
private <T> T single(Observable<T> observable) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/github/couchmove/service/ChangeLogDBService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -140,6 +141,24 @@ public void importFtsIndex(String name, String content) {
repository.importFtsIndex(name, content);
}

/**
* Watches all indexes, polling the query service until they become
* "online" or the timeout has expired
*
* @param timeout the maximum duration for which to poll for the index to become online.
* @param timeunit the time unit for the timeout.
*/
public void waitForN1qlIndexes(long timeout, TimeUnit timeunit) {
repository.watchN1qlIndexes(timeout, timeunit);
}

/**
* Instruct the query engine to trigger the build of indexes that have been deferred, within the default management
*/
public void buildN1qlDeferredIndexes() {
repository.buildN1qlDeferredIndexes();
}

/**
* Extract multiple requests, separated by ';' ignoring :
* <ul>
Expand Down
13 changes: 9 additions & 4 deletions src/test/java/com/github/couchmove/CouchmoveIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -190,7 +191,7 @@ public void should_update_changeLog() {
}

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

Expand All @@ -206,14 +207,18 @@ public void should_build_multiple_index_not_fail() {
assertEquals(2, changeLogs.size());
assertLike(changeLogs.get(0),
"0", 1, "create deferred index", N1QL, "V0__create_deferred_index.n1ql",
"060f486279932b3838a90f23032a135ad20f8a364fbbda9305f6e20a5b065085",
"8987fdc8782fe4f8321cfae8f388d9005ac6c2eca726105a2739170cc4870a66",
EXECUTED);
assertLike(changeLogs.get(1),
"1", 2, "create second deferred index", N1QL, "V1__create_second_deferred_index.n1ql",
"49fed597ee5f7012b6ab7eb66825e20de4906ecfb10ee9b5ae8f74dfe242b74a",
"77492051f8633e40032881e474207d97d87c3eb1e239a832b1ad11b22c933fe6",
EXECUTED);

// And successfully executed
// Trigger deferred index build
couchmove.buildN1qlDeferredIndexes();

// Wait for indexes to be built
couchmove.waitForN1qlIndexes(5, TimeUnit.SECONDS);

// Index inserted
Optional<IndexInfo> userIndexInfo = getBucket().bucketManager().listN1qlIndexes().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
CREATE INDEX buyer_index ON `${bucket}`(username)
WHERE type = 'buyer'
WITH { "defer_build" : true };

BUILD INDEX ON `${bucket}`(buyer_index);
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
CREATE INDEX merchant_index ON `${bucket}`(username)
WHERE type = 'merchant'
WITH { "defer_build" : true };

BUILD INDEX ON `${bucket}`(merchant_index);

0 comments on commit b18e94c

Please sign in to comment.