diff --git a/src/main/java/ai/chalk/client/ChalkClientImpl.java b/src/main/java/ai/chalk/client/ChalkClientImpl.java index bd4ed38..5a977e3 100644 --- a/src/main/java/ai/chalk/client/ChalkClientImpl.java +++ b/src/main/java/ai/chalk/client/ChalkClientImpl.java @@ -268,6 +268,13 @@ public UploadFeaturesResult uploadFeatures(UploadFeaturesParams params) throws C @Override public DeleteFeaturesResult deleteFeatures(DeleteFeaturesParams params) throws ChalkException { + if (Boolean.FALSE.equals(params.getRetainMaterializedAggregates()) + && (params.getFeatures() != null || params.getTags() != null)) { + throw new ClientException( + "retainMaterializedAggregates=false deletes every materialized aggregation of the primary keys " + + "and cannot be combined with features or tags." + ); + } String branch = params.getBranch() != null ? params.getBranch() : this.branch; if (branch != null) { throw new ClientException( @@ -284,7 +291,8 @@ public DeleteFeaturesResult deleteFeatures(DeleteFeaturesParams params) throws C params.getTags(), params.getPrimaryKeys(), params.isRetainOffline(), - params.isRetainOnline() + params.isRetainOnline(), + params.getRetainMaterializedAggregates() )) .method("DELETE") .environmentOverride(params.getEnvironmentId()) diff --git a/src/main/java/ai/chalk/internal/request/models/FeatureObservationDeletionRequest.java b/src/main/java/ai/chalk/internal/request/models/FeatureObservationDeletionRequest.java index bc11df1..a0c649b 100644 --- a/src/main/java/ai/chalk/internal/request/models/FeatureObservationDeletionRequest.java +++ b/src/main/java/ai/chalk/internal/request/models/FeatureObservationDeletionRequest.java @@ -8,6 +8,7 @@ public record FeatureObservationDeletionRequest( List tags, List primaryKeys, boolean retainOffline, - boolean retainOnline + boolean retainOnline, + Boolean retainMaterializedAggregates ) { } diff --git a/src/main/java/ai/chalk/models/DeleteFeaturesParams.java b/src/main/java/ai/chalk/models/DeleteFeaturesParams.java index 59bf1f2..a7d26a5 100644 --- a/src/main/java/ai/chalk/models/DeleteFeaturesParams.java +++ b/src/main/java/ai/chalk/models/DeleteFeaturesParams.java @@ -54,6 +54,26 @@ public class DeleteFeaturesParams { * If true, the given observations will not be dropped from the online store. */ private final boolean retainOnline; + /** + * If false, also delete the keys' materialized aggregation buckets (not with features or tags). + * Null leaves the default to the API server, which keeps the buckets. + */ + @Nullable + private final Boolean retainMaterializedAggregates; + + /** Equivalent to the full constructor with {@code retainMaterializedAggregates = null}. */ + public DeleteFeaturesParams( + String namespace, + @Nullable List features, + @Nullable List tags, + List primaryKeys, + @Nullable String environmentId, + @Nullable String branch, + boolean retainOffline, + boolean retainOnline + ) { + this(namespace, features, tags, primaryKeys, environmentId, branch, retainOffline, retainOnline, null); + } @AllArgsConstructor @NoArgsConstructor @@ -66,6 +86,21 @@ public static class Builder { protected String branch; protected boolean retainOffline = false; protected boolean retainOnline = false; + protected Boolean retainMaterializedAggregates = null; + + /** Equivalent to the full constructor with {@code retainMaterializedAggregates = null}. */ + public Builder( + String namespace, + List features, + List tags, + List primaryKeys, + String environmentId, + String branch, + boolean retainOffline, + boolean retainOnline + ) { + this(namespace, features, tags, primaryKeys, environmentId, branch, retainOffline, retainOnline, null); + } public DeleteFeaturesParams build() { return new DeleteFeaturesParams( @@ -76,7 +111,8 @@ public DeleteFeaturesParams build() { this.environmentId, this.branch, this.retainOffline, - this.retainOnline); + this.retainOnline, + this.retainMaterializedAggregates); } public Builder withNamespace(String namespace) { @@ -120,6 +156,12 @@ public Builder withRetainOnline(boolean retainOnline) { this.retainOnline = retainOnline; return this; } + + /** Pass false to also delete the keys' materialized aggregation buckets. Defaults to {@code true}. */ + public Builder withRetainMaterializedAggregates(boolean retainMaterializedAggregates) { + this.retainMaterializedAggregates = retainMaterializedAggregates; + return this; + } } public static Builder builder() { diff --git a/src/main/java/ai/chalk/models/DeleteFeaturesResult.java b/src/main/java/ai/chalk/models/DeleteFeaturesResult.java index 26e1bea..5bf2bbd 100644 --- a/src/main/java/ai/chalk/models/DeleteFeaturesResult.java +++ b/src/main/java/ai/chalk/models/DeleteFeaturesResult.java @@ -6,6 +6,7 @@ import lombok.Data; import lombok.NoArgsConstructor; +import javax.annotation.Nullable; import java.util.List; /** @@ -17,4 +18,11 @@ @AllArgsConstructor public class DeleteFeaturesResult { private List errors; + /** Online store entries removed when {@code retainMaterializedAggregates} is false, else null. */ + @Nullable + private Long materializedAggregateKeysDeleted; + + public DeleteFeaturesResult(List errors) { + this(errors, null); + } } diff --git a/src/test/java/ai/chalk/client/TestDeleteFeatures.java b/src/test/java/ai/chalk/client/TestDeleteFeatures.java index 3766060..be277ca 100644 --- a/src/test/java/ai/chalk/client/TestDeleteFeatures.java +++ b/src/test/java/ai/chalk/client/TestDeleteFeatures.java @@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -66,13 +67,97 @@ public void testHttpDeleteFeatures() throws Exception { assertEquals("DELETE", method.get()); assertEquals( "{\"namespace\":\"user\",\"features\":[\"name\",\"email\"],\"tags\":null," - + "\"primary_keys\":[\"1\",\"2\"],\"retain_offline\":false,\"retain_online\":false}", + + "\"primary_keys\":[\"1\",\"2\"],\"retain_offline\":false,\"retain_online\":false," + + "\"retain_materialized_aggregates\":null}", body.get() ); assertEquals("Bearer test-token", authorization.get()); assertEquals(ENVIRONMENT_ID, environmentHeader.get()); assertEquals(1, result.getErrors().size()); assertEquals("partial failure", result.getErrors().get(0).getMessage()); + assertNull(result.getMaterializedAggregateKeysDeleted()); + } finally { + server.stop(0); + } + } + + @Test + public void testHttpDeleteFeaturesDeletingMaterializedAggregates() throws Exception { + AtomicReference body = new AtomicReference<>(); + HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 0), 0); + server.createContext("/v1/oauth/token", exchange -> respondJson( + exchange, + """ + { + "access_token": "test-token", + "expires_in": 3600, + "primary_environment": "%s", + "engines": {} + } + """.formatted(ENVIRONMENT_ID) + )); + server.createContext("/v1/features/rows", exchange -> { + body.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8)); + respondJson(exchange, "{\"errors\": [], \"materialized_aggregate_keys_deleted\": 7}"); + }); + server.start(); + + String serverUrl = "http://localhost:" + server.getAddress().getPort(); + try (ChalkClient client = ChalkClient.builder() + .withApiServer(serverUrl) + .withClientId("test-client") + .withClientSecret("test-secret") + .withEnvironmentId(ENVIRONMENT_ID) + .build()) { + DeleteFeaturesResult result = client.deleteFeatures(DeleteFeaturesParams.builder() + .withNamespace("user") + .withPrimaryKeys(List.of("1")) + .withRetainMaterializedAggregates(false) + .build()); + + assertEquals( + "{\"namespace\":\"user\",\"features\":null,\"tags\":null,\"primary_keys\":[\"1\"]," + + "\"retain_offline\":false,\"retain_online\":false," + + "\"retain_materialized_aggregates\":false}", + body.get() + ); + assertTrue(result.getErrors().isEmpty()); + assertEquals(7L, result.getMaterializedAggregateKeysDeleted()); + } finally { + server.stop(0); + } + } + + @Test + public void testNotRetainingMaterializedAggregatesRejectsFeatureSelection() throws Exception { + AtomicReference deleteCalled = new AtomicReference<>(false); + HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 0), 0); + server.createContext("/v1/features/rows", exchange -> { + deleteCalled.set(true); + respondJson(exchange, "{\"errors\": []}"); + }); + server.start(); + + String serverUrl = "http://localhost:" + server.getAddress().getPort(); + try (ChalkClient client = ChalkClient.builder() + .withApiServer(serverUrl) + .withClientId("test-client") + .withClientSecret("test-secret") + .withEnvironmentId(ENVIRONMENT_ID) + .build()) { + for (DeleteFeaturesParams.Builder builder : List.of( + DeleteFeaturesParams.builder().withFeatures(List.of("name")), + DeleteFeaturesParams.builder().withTags(List.of("pii")) + )) { + ClientException e = assertThrows(ClientException.class, () -> client.deleteFeatures( + builder.withNamespace("user") + .withPrimaryKeys(List.of("1")) + .withRetainMaterializedAggregates(false) + .build() + )); + assertTrue(e.getMessage().contains("cannot be combined")); + } + assertFalse(deleteCalled.get()); } finally { server.stop(0); }