com.redis.lettucemod.search.AggregateOptions Maven / Gradle / Ivy
package com.redis.lettucemod.search;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import com.redis.lettucemod.protocol.SearchCommandKeyword;
public class AggregateOptions extends BaseSearchOptions {
private static final Load LOAD_ALL = Load.identifier("*").build();
private List> operations = new ArrayList<>();
private List loads = new ArrayList<>();
public void setOperations(List> operations) {
this.operations = operations;
}
public void setLoads(List loads) {
this.loads = loads;
}
public AggregateOptions() {
}
private AggregateOptions(Builder builder) {
super(builder);
this.operations = builder.operations;
this.loads = builder.loads;
}
public List> getOperations() {
return operations;
}
public List getLoads() {
return loads;
}
@Override
public void build(SearchCommandArgs args) {
if (!loads.isEmpty()) {
args.add(SearchCommandKeyword.LOAD);
if (loads.size() == 1 && loads.get(0) == LOAD_ALL) {
args.add(LOAD_ALL.identifier);
} else {
args.add(loads.stream().mapToInt(Load::getNargs).sum());
loads.forEach(l -> l.build(args));
}
}
operations.forEach(op -> op.build(args));
}
@Override
public String toString() {
return "AggregateOptions [operations=" + operations + ", loads=" + loads + ", getTimeout()=" + getTimeout()
+ ", isVerbatim()=" + isVerbatim() + ", getLimit()=" + getLimit() + ", getParams()=" + getParams()
+ ", getDialect()=" + getDialect() + "]";
}
@SuppressWarnings("rawtypes")
public static class Load implements RediSearchArgument {
private final String identifier;
private final Optional as;
private Load(Builder builder) {
this.identifier = builder.identifier;
this.as = builder.as;
}
public int getNargs() {
int nargs = 1;
if (as.isPresent()) {
nargs += 2;
}
return nargs;
}
public static Builder identifier(String identifier) {
return new Builder(identifier);
}
public static class Builder {
private final String identifier;
private Optional as = Optional.empty();
public Builder(String identifier) {
this.identifier = identifier;
}
public Builder as(String field) {
as = Optional.of(field);
return this;
}
public Load build() {
return new Load(this);
}
}
@Override
public void build(SearchCommandArgs args) {
args.add(identifier);
as.ifPresent(a -> args.add(SearchCommandKeyword.AS).add(a));
}
}
public static Builder builder() {
return new Builder<>();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Builder operation(AggregateOperation operation) {
return new Builder<>(operation);
}
public static class Builder extends BaseSearchOptions.Builder> {
private final List> operations = new ArrayList<>();
private List loads = new ArrayList<>();
private Builder() {
}
private Builder(AggregateOperation operation) {
operations.add(operation);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Builder operation(AggregateOperation operation) {
this.operations.add(operation);
return this;
}
public Builder loadAll() {
this.loads = Collections.singletonList(LOAD_ALL);
return this;
}
public Builder load(String identifier) {
this.loads.add(Load.identifier(identifier).build());
return this;
}
public Builder loads(String... identifiers) {
Collections.addAll(this.loads,
Stream.of(identifiers).map(i -> Load.identifier(i).build()).toArray(Load[]::new));
return this;
}
public Builder load(Load load) {
this.loads.add(load);
return this;
}
public Builder loads(Load... loads) {
Collections.addAll(this.loads, loads);
return this;
}
public AggregateOptions build() {
return new AggregateOptions<>(this);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy