Skip to content

Commit b9e49a2

Browse files
committed
🚧 15: Get scope name from Collection
1 parent 6be9eba commit b9e49a2

6 files changed

Lines changed: 42 additions & 9 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
public interface CouchbaseRepository<E extends CouchbaseEntity> {
1717

18+
CouchbaseRepository<E> withCollection(String collection);
19+
1820
CouchbaseRepository<E> withCollection(String scope, String collection);
1921

2022
/**
@@ -102,6 +104,11 @@ public interface CouchbaseRepository<E extends CouchbaseEntity> {
102104
*/
103105
String getBucketName();
104106

107+
/**
108+
* @return name of the repository Couchbase {@link com.couchbase.client.java.Scope}
109+
*/
110+
String getScopeName();
111+
105112
/**
106113
* Instruct the query engine to trigger the build of indexes that have been deferred, within the default management
107114
*/

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class CouchbaseRepositoryImpl<E extends CouchbaseEntity> implements Couch
6363
private static final ObjectMapper jsonMapper = new ObjectMapper();
6464

6565
public static final String BUCKET_PARAM = "bucket";
66+
public static final String SCOPE_PARAM = "scope";
6667
public static final int MAX_ATTEMPTS = 5;
6768
public static final String DEFAULT = "_default";
6869

@@ -114,6 +115,11 @@ public CouchbaseRepositoryImpl(Cluster cluster, Bucket bucket, Class<E> entityCl
114115
this.entityClass = entityClass;
115116
}
116117

118+
@Override
119+
public CouchbaseRepositoryImpl<E> withCollection(String collection) {
120+
return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(this.collection.scopeName()).collection(collection), entityClass);
121+
}
122+
117123
@Override
118124
public CouchbaseRepositoryImpl<E> withCollection(String scope, String collection) {
119125
return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(scope).collection(collection), entityClass);
@@ -228,14 +234,22 @@ public boolean isFtsIndexExists(String name) {
228234
}
229235

230236
String injectParameters(String statement) {
231-
return StrSubstitutor.replace(statement, of(BUCKET_PARAM, getBucketName()));
237+
return StrSubstitutor.replace(statement, of(
238+
BUCKET_PARAM, getBucketName(),
239+
SCOPE_PARAM, getScopeName()
240+
));
232241
}
233242

234243
@Override
235244
public String getBucketName() {
236245
return collection.bucketName();
237246
}
238247

248+
@Override
249+
public String getScopeName() {
250+
return collection.scopeName();
251+
}
252+
239253
@Override
240254
public void buildN1qlDeferredIndexes() {
241255
logger.info("Build N1QL Deferred Indexes for default");

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ public void importDocuments(Collection<Document> documents) {
137137
for (Document document : documents) {
138138
var repo = repository;
139139
if (document.getCollection() != null) {
140-
repo = repository.withCollection(document.getScope(), document.getCollection());
140+
if (document.getScope() != null) {
141+
repo = repository.withCollection(document.getScope(), document.getCollection());
142+
} else {
143+
repo = repository.withCollection(document.getCollection());
144+
}
141145
}
142146
repo.save(FilenameUtils.getBaseName(document.getFileName()), document.getContent());
143147
}

‎src/main/java/com/github/couchmove/utils/FileUtils.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ private static Document getDocument(Path directoryPath, Path path) {
120120
switch (relativePath.getNameCount()) {
121121
case 1:
122122
return new Document(null, null, fileName, content);
123+
case 2:
124+
return new Document(null, relativePath.getName(0).toString(), fileName, content);
123125
case 3:
124126
return new Document(
125127
relativePath.getName(0).toString(),

‎src/test/java/com/github/couchmove/repository/CouchbaseRepositoryIT.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.github.couchmove.pojo.Type;
1313
import com.github.couchmove.utils.*;
1414
import org.apache.commons.io.IOUtils;
15-
import org.junit.Assert;
1615
import org.junit.jupiter.api.BeforeAll;
1716
import org.junit.jupiter.params.ParameterizedTest;
1817
import org.junit.jupiter.params.provider.Arguments;
@@ -180,10 +179,10 @@ public void should_check_fts_index_not_exists(String description, CouchbaseRepos
180179

181180
@ParameterizedTest(name = "{0}")
182181
@MethodSource("repositoryParams")
183-
public void should_inject_bucket_name(String description, CouchbaseRepository<ChangeLog> repository) {
184-
String format = "SELECT * FROM `%s`";
185-
String statement = format(format, "${bucket}");
186-
assertThat(((CouchbaseRepositoryImpl) repository).injectParameters(statement)).isEqualTo(format(format, getBucket().name()));
182+
public void should_inject_params(String description, CouchbaseRepository<ChangeLog> repository) {
183+
String format = "SELECT * FROM `%s`.`%s`.test";
184+
String statement = format(format, "${bucket}", "${scope}");
185+
assertThat(((CouchbaseRepositoryImpl) repository).injectParameters(statement)).isEqualTo(format(format, getBucket().name(), repository.getScopeName()));
187186
}
188187

189188
@ParameterizedTest(name = "{0}")

‎src/test/java/com/github/couchmove/utils/FileUtilsTest.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,23 @@ public void should_read_files_in_directory() throws IOException {
9898
File file3 = File.createTempFile("file3", ".json", collectionDir);
9999
String content3 = "content3";
100100
Files.write(content3.getBytes(), file3);
101+
// json file with collection directories
102+
collectionDir = new File(tempDir, format("%s", collection));
103+
collectionDir.mkdirs();
104+
File file4 = File.createTempFile("file4", ".json", collectionDir);
105+
String content4 = "content4";
106+
Files.write(content4.getBytes(), file4);
101107

102108
// When we read files in this directory with extension filter
103109
var results = FileUtils.readFilesInDirectory(tempDir.toPath(), "json", "n1ql");
104110

105111
// Then we should have file content matching this extension
106-
assertThat(results).hasSize(3);
112+
assertThat(results).hasSize(4);
107113
assertThat(results).containsExactlyInAnyOrder(
108114
new Document(null, null, file1.getName(), content1),
109115
new Document(null, null, file2.getName(), content2),
110-
new Document(scope, collection, file3.getName(), content3)
116+
new Document(scope, collection, file3.getName(), content3),
117+
new Document(null, collection, file4.getName(), content4)
111118
);
112119
}
113120
}

0 commit comments

Comments
 (0)