Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apache Solr support: implement DELETE collection #589

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,9 @@ public void initialize(AssetDefinition assetDefinition) throws Exception {

@Override
public boolean assetExists() throws Exception {
String collectionName = getCollectionName();
log.info("Handling SOLR collection {}", collectionName);
return describeCollection();
}

private String getCollectionName() {
return datasource.getClientConfig().getCollectionName();
}

@Override
public void deployAsset() throws Exception {
List<Map<String, Object>> statements =
Expand All @@ -84,20 +78,16 @@ public void deployAsset() throws Exception {
}

private boolean describeCollection() throws IOException, InterruptedException {
String schemaUrl = datasource.getCollectionUrl() + "/schema";
HttpResponse<String> get =
httpClient.send(
HttpRequest.newBuilder().uri(URI.create(schemaUrl)).GET().build(),
HttpResponse.BodyHandlers.ofString());
String currentSchema = get.body();
log.info("Describe collection {}", getCollectionName());
log.info("Result: {}", currentSchema);
if (get.statusCode() == 404) {
String collectionUrl = datasource.getRESTCollectionUrl();
String result = execute("GET", collectionUrl);
if (result == null) { // this should have been 404, but actually Solr 9.3 returns 400
log.info("Collection {} does not exist", datasource.getCollectionName());
return false;
}
if (get.statusCode() != 200) {
throw new IOException("Error while querying collection schema: " + get.body());
}
String schemaUrl = datasource.getCollectionUrl() + "/schema";
String currentSchema = execute("GET", schemaUrl);
log.info("Describe collection {}", datasource.getCollectionName());
log.info("Result: {}", currentSchema);
return true;
}

Expand Down Expand Up @@ -140,7 +130,39 @@ private void execStatements(List<Map<String, Object>> statements) throws Excepti

@Override
public boolean deleteAssetIfExists() throws Exception {
return false;
boolean exists = describeCollection();
if (!exists) {
return false;
}
String collectionUrl = datasource.getRESTCollectionUrl();
;
log.info("Deleting collection {}", datasource.getCollectionName());
execute("DELETE", collectionUrl);
return true;
}

private String execute(String method, String url) throws IOException, InterruptedException {
HttpResponse<String> response =
httpClient.send(
HttpRequest.newBuilder()
.uri(URI.create(url))
.method(method, HttpRequest.BodyPublishers.noBody())
.build(),
HttpResponse.BodyHandlers.ofString());
log.info("{} {}: Status code {}", method, url, response.statusCode());
if (response.statusCode()
== 400 // this should have been 404, but actually Solr 9.3 returns 400
|| response.statusCode() == 404) {
return null;
}
if (response.statusCode() != 200) {
throw new IOException(
"Error while calling url "
+ url
+ ", http status code is "
+ response.statusCode());
}
return response.body();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -148,5 +150,15 @@ public void close() {
client.close();
}
}

public String getRESTCollectionUrl() {
return getBaseUrl()
+ "/api/collections/"
+ URLEncoder.encode(getCollectionName(), StandardCharsets.UTF_8);
}

public String getCollectionName() {
return getClientConfig().getCollectionName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ void testWrite() throws Exception {
assertEquals(0, results3.size());

assertTrue(tableManager.assetExists());
tableManager.deleteAssetIfExists();
assertTrue(tableManager.deleteAssetIfExists());
assertFalse(tableManager.assetExists());
assertFalse(tableManager.deleteAssetIfExists());
}
}
}
Expand Down
Loading