Skip to content
This repository has been archived by the owner on Aug 25, 2024. It is now read-only.

Commit

Permalink
Refactor CouchbaseAssetsManagerProvider scope validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrank241 committed Aug 2, 2024
1 parent e39f951 commit 572ceb1
Showing 1 changed file with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.couchbase.client.java.manager.bucket.BucketType;
import com.couchbase.client.java.manager.collection.CollectionSpec;
import com.couchbase.client.java.manager.collection.ScopeSpec;
import com.couchbase.client.java.manager.search.SearchIndex;
import com.couchbase.client.java.manager.search.SearchIndexManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -198,29 +196,60 @@ private boolean scopeExists() {
private boolean searchIndexExists() {
try {
List<ScopeSpec> scopes = bucket.collections().getAllScopes();
// Check that there is exactly one scope
if (scopes.size() != 1) {

// Check if there are exactly 2 or 3 scopes
if (scopes.size() != 2 && scopes.size() != 3) {
System.out.println(
"There must be exactly 2 or 3 scopes, found: " + scopes.size());
return false;
}
// Check that the scope has no collections or only the "_default" collection
ScopeSpec singleScope = scopes.get(0);
List<CollectionSpec> collections = (List<CollectionSpec>) singleScope.collections();
if (!collections.isEmpty()
&& (collections.size() != 1
|| !collections.get(0).name().equals("_default"))) {
return false;

boolean hasDefaultScope = false;
boolean hasSystemScope = false;
boolean hasCustomScope = false;

// Check for required scopes and collections
for (ScopeSpec scope : scopes) {
if (scope.name().equals("_default")) {
hasDefaultScope = true;
} else if (scope.name().equals("_system")) {
hasSystemScope = true;
} else if (scope.name().equals(scopeName)) {
hasCustomScope = true;
} else {
System.out.println("Unexpected scope found: " + scope.name());
return false;
}
ScopeSpec singleScope = scopes.get(0);
List<CollectionSpec> collections =
(List<CollectionSpec>) singleScope.collections();
// Check collections only if the scope is not the custom scope
if (!scope.name().equals(scopeName) && !collections.isEmpty()) {
System.out.println(
"Scope " + scope.name() + " has collections but should not.");
return false;
}
}
// Ensure the single scope name matches the expected scope name
if (!singleScope.name().equals(scopeName)) {
return false;

// Validate scope names based on the number of scopes
if (scopes.size() == 2) {
if (!hasDefaultScope || !hasSystemScope) {
System.out.println(
"When there are 2 scopes, they must be _default and _system.");
return false;
}
} else if (scopes.size() == 3) {
if (!hasDefaultScope || !hasSystemScope || !hasCustomScope) {
System.out.println(
"When there are 3 scopes, they must be _default, _system, and "
+ scopeName);
return false;
}
}

SearchIndexManager searchIndexManager = cluster.searchIndexes();
String indexLabel = "vectorize-index";
String indexName = bucketName + "." + scopeName + "." + indexLabel;
SearchIndex searchIndex = searchIndexManager.getIndex(indexName);
return searchIndex != null;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Expand Down

0 comments on commit 572ceb1

Please sign in to comment.