|
| 1 | +package org.hypertrace.config.objectstore; |
| 2 | + |
| 3 | +import io.grpc.Status; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.stream.Collectors; |
| 7 | +import javax.annotation.Nullable; |
| 8 | +import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator; |
| 9 | +import org.hypertrace.config.service.v1.ConfigServiceGrpc; |
| 10 | +import org.hypertrace.config.service.v1.Filter; |
| 11 | +import org.hypertrace.config.service.v1.GetAllConfigsRequest; |
| 12 | +import org.hypertrace.config.service.v1.Pagination; |
| 13 | +import org.hypertrace.config.service.v1.SortBy; |
| 14 | +import org.hypertrace.core.grpcutils.context.RequestContext; |
| 15 | + |
| 16 | +public abstract class IdentifiedFilterPushedDownObjectStore<T, F, S> |
| 17 | + extends IdentifiedObjectStore<T> { |
| 18 | + |
| 19 | + private final ConfigServiceGrpc.ConfigServiceBlockingStub configServiceBlockingStub; |
| 20 | + private final String resourceNamespace; |
| 21 | + private final String resourceName; |
| 22 | + |
| 23 | + protected IdentifiedFilterPushedDownObjectStore( |
| 24 | + ConfigServiceGrpc.ConfigServiceBlockingStub configServiceBlockingStub, |
| 25 | + String resourceNamespace, |
| 26 | + String resourceName, |
| 27 | + ConfigChangeEventGenerator configChangeEventGenerator, |
| 28 | + ClientConfig clientConfig) { |
| 29 | + super( |
| 30 | + configServiceBlockingStub, |
| 31 | + resourceNamespace, |
| 32 | + resourceName, |
| 33 | + configChangeEventGenerator, |
| 34 | + clientConfig); |
| 35 | + this.configServiceBlockingStub = configServiceBlockingStub; |
| 36 | + this.resourceNamespace = resourceNamespace; |
| 37 | + this.resourceName = resourceName; |
| 38 | + } |
| 39 | + |
| 40 | + public List<ContextualConfigObject<T>> getMatchingObjects( |
| 41 | + RequestContext context, F filterInput, List<S> sortInput, @Nullable Pagination pagination) { |
| 42 | + Filter filter = buildFilter(filterInput); |
| 43 | + List<SortBy> sortByList = sortInput.stream().map(this::buildSort).collect(Collectors.toList()); |
| 44 | + return getMatchingObjects(context, filter, sortByList, pagination); |
| 45 | + } |
| 46 | + |
| 47 | + public List<ContextualConfigObject<T>> getMatchingObjects( |
| 48 | + RequestContext context, F filterInput, List<S> sortInput) { |
| 49 | + return getMatchingObjects(context, filterInput, sortInput, null); |
| 50 | + } |
| 51 | + |
| 52 | + public List<T> getMatchingData( |
| 53 | + RequestContext context, F filterInput, List<S> sortInput, @Nullable Pagination pagination) { |
| 54 | + return getMatchingObjects(context, filterInput, sortInput, pagination).stream() |
| 55 | + .map(ConfigObject::getData) |
| 56 | + .collect(Collectors.toUnmodifiableList()); |
| 57 | + } |
| 58 | + |
| 59 | + public Optional<ContextualConfigObject<T>> getMatchingObject( |
| 60 | + RequestContext context, F filterInput, List<S> sortInput) { |
| 61 | + List<ContextualConfigObject<T>> results = getMatchingObjects(context, filterInput, sortInput); |
| 62 | + if (results.size() > 1) { |
| 63 | + throw Status.FAILED_PRECONDITION |
| 64 | + .withDescription("Multiple objects found when expecting at most one") |
| 65 | + .asRuntimeException(); |
| 66 | + } |
| 67 | + return results.stream().findFirst(); |
| 68 | + } |
| 69 | + |
| 70 | + public Optional<T> getMatchingData(RequestContext context, F filterInput, List<S> sortInput) { |
| 71 | + return getMatchingObject(context, filterInput, sortInput).map(ConfigObject::getData); |
| 72 | + } |
| 73 | + |
| 74 | + List<ContextualConfigObject<T>> getMatchingObjects( |
| 75 | + RequestContext context, Filter filter, List<SortBy> sortByList, Pagination pagination) { |
| 76 | + return context |
| 77 | + .call( |
| 78 | + () -> |
| 79 | + this.configServiceBlockingStub |
| 80 | + .withDeadline(getDeadline()) |
| 81 | + .getAllConfigs(buildGetAllConfigsRequest(filter, sortByList, pagination))) |
| 82 | + .getContextSpecificConfigsList() |
| 83 | + .stream() |
| 84 | + .map( |
| 85 | + contextSpecificConfig -> |
| 86 | + ContextualConfigObjectImpl.tryBuild( |
| 87 | + contextSpecificConfig, this::buildDataFromValue)) |
| 88 | + .flatMap(Optional::stream) |
| 89 | + .collect( |
| 90 | + Collectors.collectingAndThen( |
| 91 | + Collectors.toUnmodifiableList(), this::orderFetchedObjects)); |
| 92 | + } |
| 93 | + |
| 94 | + private GetAllConfigsRequest buildGetAllConfigsRequest( |
| 95 | + Filter filter, List<SortBy> sortByList, Pagination pagination) { |
| 96 | + GetAllConfigsRequest.Builder getAllConfigsRequest = |
| 97 | + GetAllConfigsRequest.newBuilder() |
| 98 | + .setResourceName(this.resourceName) |
| 99 | + .setResourceNamespace(this.resourceNamespace) |
| 100 | + .setFilter(filter) |
| 101 | + .addAllSortBy(sortByList); |
| 102 | + if (pagination != null) { |
| 103 | + getAllConfigsRequest.setPagination(pagination); |
| 104 | + } |
| 105 | + return getAllConfigsRequest.build(); |
| 106 | + } |
| 107 | + |
| 108 | + protected abstract SortBy buildSort(S sortInput); |
| 109 | + |
| 110 | + protected abstract Filter buildFilter(F filterInput); |
| 111 | +} |
0 commit comments