Skip to content

Commit

Permalink
Separate builder factory for each module (#3277)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored Jan 24, 2023
1 parent 8d5f45c commit c4506e4
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 351 deletions.
250 changes: 0 additions & 250 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1537,53 +1537,6 @@ public List<LibraryInfo> build(Object data) {
}
};

public static final Builder<Class<?>> JSON_TYPE = new Builder<Class<?>>() {
@Override
public Class<?> build(Object data) {
if (data == null) return null;
String str = STRING.build(data);
switch (str) {
case "null":
return null;
case "boolean":
return boolean.class;
case "integer":
return int.class;
case "number":
return float.class;
case "string":
return String.class;
case "object":
return Object.class;
case "array":
return List.class;
default:
throw new JedisException("Unknown type: " + str);
}
}

@Override
public String toString() {
return "Class<?>";
}
};

public static final Builder<List<Class<?>>> JSON_TYPE_LIST = new Builder<List<Class<?>>>() {
@Override
public List<Class<?>> build(Object data) {
List<Object> list = (List<Object>) data;
List<Class<?>> classes = new ArrayList<>(list.size());
for (Object elem : list) {
try {
classes.add(JSON_TYPE.build(elem));
} catch (JedisException je) {
classes.add(null);
}
}
return classes;
}
};

public static final Builder<List<List<String>>> STRING_LIST_LIST = new Builder<List<List<String>>>() {
@Override
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1612,209 +1565,6 @@ public String toString() {
}
};

public static final Builder<AggregationResult> SEARCH_AGGREGATION_RESULT = new Builder<AggregationResult>() {
@Override
public AggregationResult build(Object data) {
return new AggregationResult(data);
}
};

public static final Builder<AggregationResult> SEARCH_AGGREGATION_RESULT_WITH_CURSOR = new Builder<AggregationResult>() {
@Override
public AggregationResult build(Object data) {
List<Object> list = (List<Object>) data;
return new AggregationResult(list.get(0), (long) list.get(1));
}
};

public static final Builder<Map<String, Object>> SEARCH_PROFILE_PROFILE = new Builder<Map<String, Object>>() {

private final String ITERATORS_PROFILE_STR = "Iterators profile";
private final String CHILD_ITERATORS_STR = "Child iterators";
private final String RESULT_PROCESSORS_PROFILE_STR = "Result processors profile";

@Override
public Map<String, Object> build(Object data) {
List<Object> list = (List<Object>) SafeEncoder.encodeObject(data);
Map<String, Object> profileMap = new HashMap<>(list.size(), 1f);

for (Object listObject : list) {
List<Object> attributeList = (List<Object>) listObject;
String attributeName = (String) attributeList.get(0);
Object attributeValue;

if (attributeList.size() == 2) {

Object value = attributeList.get(1);
if (attributeName.equals(ITERATORS_PROFILE_STR)) {
attributeValue = parseIterators(value);
} else if (attributeName.endsWith(" time")) {
attributeValue = DoublePrecision.parseFloatingPointNumber((String) value);
} else {
attributeValue = value;
}

} else if (attributeList.size() > 2) {

if (attributeName.equals(RESULT_PROCESSORS_PROFILE_STR)) {
List<Map<String, Object>> resultProcessorsProfileList = new ArrayList<>(attributeList.size() - 1);
for (int i = 1; i < attributeList.size(); i++) {
resultProcessorsProfileList.add(parseResultProcessors(attributeList.get(i)));
}
attributeValue = resultProcessorsProfileList;
} else {
attributeValue = attributeList.subList(1, attributeList.size());
}

} else {
attributeValue = null;
}

profileMap.put(attributeName, attributeValue);
}
return profileMap;
}

private Map<String, Object> parseResultProcessors(Object data) {
List<Object> list = (List<Object>) data;
Map<String, Object> map = new HashMap<>(list.size() / 2, 1f);
for (int i = 0; i < list.size(); i += 2) {
String key = (String) list.get(i);
Object value = list.get(i + 1);
if (key.equals("Time")) {
value = DoublePrecision.parseFloatingPointNumber((String) value);
}
map.put(key, value);
}
return map;
}

private Object parseIterators(Object data) {
if (!(data instanceof List)) return data;
List iteratorsAttributeList = (List) data;
int childIteratorsIndex = iteratorsAttributeList.indexOf(CHILD_ITERATORS_STR);
// https://github.com/RediSearch/RediSearch/issues/3205 patch. TODO: Undo if resolved in RediSearch.
if (childIteratorsIndex < 0) childIteratorsIndex = iteratorsAttributeList.indexOf("Child iterator");

Map<String, Object> iteratorsProfile;
if (childIteratorsIndex < 0) {
childIteratorsIndex = iteratorsAttributeList.size();
iteratorsProfile = new HashMap<>(childIteratorsIndex / 2, 1f);
} else {
iteratorsProfile = new HashMap<>(1 + childIteratorsIndex / 2, 1f);
}

for (int i = 0; i < childIteratorsIndex; i += 2) {
String key = (String) iteratorsAttributeList.get(i);
Object value = iteratorsAttributeList.get(i + 1);
if (key.equals("Time")) {
value = DoublePrecision.parseFloatingPointNumber((String) value);
}
iteratorsProfile.put(key, value);
}

if (childIteratorsIndex + 1 < iteratorsAttributeList.size()) {
List childIteratorsList = new ArrayList(iteratorsAttributeList.size() - childIteratorsIndex - 1);
for (int i = childIteratorsIndex + 1; i < iteratorsAttributeList.size(); i++) {
childIteratorsList.add(parseIterators(iteratorsAttributeList.get(i)));
}
iteratorsProfile.put(CHILD_ITERATORS_STR, childIteratorsList);
}
return iteratorsProfile;
}
};

public static final Builder<Map<String, List<String>>> SEARCH_SYNONYM_GROUPS = new Builder<Map<String, List<String>>>() {
@Override
public Map<String, List<String>> build(Object data) {
List<Object> list = (List<Object>) data;
Map<String, List<String>> dump = new HashMap<>(list.size() / 2, 1f);
for (int i = 0; i < list.size(); i += 2) {
dump.put(STRING.build(list.get(i)), STRING_LIST.build(list.get(i + 1)));
}
return dump;
}
};

public static final Builder<Map<String, Map<String, Double>>> SEARCH_SPELLCHECK_RESPONSE
= new Builder<Map<String, Map<String, Double>>>() {

private final String TERM = "TERM";

@Override
public Map<String, Map<String, Double>> build(Object data) {
List<Object> rawTerms = (List<Object>) data;
Map<String, Map<String, Double>> returnTerms = new LinkedHashMap<>(rawTerms.size());

for (Object rawTerm : rawTerms) {
List<Object> rawElements = (List<Object>) rawTerm;

String header = STRING.build(rawElements.get(0));
if (!TERM.equals(header)) {
throw new IllegalStateException("Unrecognized header: " + header);
}
String term = STRING.build(rawElements.get(1));

List<List<Object>> list = (List<List<Object>>) rawElements.get(2);
Map<String, Double> entries = new LinkedHashMap<>(list.size());
list.forEach(entry -> entries.put(STRING.build(entry.get(1)), DOUBLE.build(entry.get(0))));

returnTerms.put(term, entries);
}
return returnTerms;
}
};

public static final Builder<TSElement> TIMESERIES_ELEMENT = new Builder<TSElement>() {
@Override
public TSElement build(Object data) {
List<Object> list = (List<Object>) data;
if (list == null || list.isEmpty()) return null;
return new TSElement(LONG.build(list.get(0)), DOUBLE.build(list.get(1)));
}
};

public static final Builder<List<TSElement>> TIMESERIES_ELEMENT_LIST = new Builder<List<TSElement>>() {
@Override
public List<TSElement> build(Object data) {
return ((List<Object>) data).stream().map((pairObject) -> (List<Object>) pairObject)
.map((pairList) -> new TSElement(LONG.build(pairList.get(0)),
DOUBLE.build(pairList.get(1))))
.collect(Collectors.toList());
}
};

public static final Builder<List<TSKeyedElements>> TIMESERIES_MRANGE_RESPONSE = new Builder<List<TSKeyedElements>>() {
@Override
public List<TSKeyedElements> build(Object data) {
return ((List<Object>) data).stream().map((tsObject) -> (List<Object>) tsObject)
.map((tsList) -> new TSKeyedElements(STRING.build(tsList.get(0)),
STRING_MAP_FROM_PAIRS.build(tsList.get(1)),
TIMESERIES_ELEMENT_LIST.build(tsList.get(2))))
.collect(Collectors.toList());
}
};

public static final Builder<List<TSKeyValue<TSElement>>> TIMESERIES_MGET_RESPONSE
= new Builder<List<TSKeyValue<TSElement>>>() {
@Override
public List<TSKeyValue<TSElement>> build(Object data) {
return ((List<Object>) data).stream().map((tsObject) -> (List<Object>) tsObject)
.map((tsList) -> new TSKeyValue<>(STRING.build(tsList.get(0)),
STRING_MAP_FROM_PAIRS.build(tsList.get(1)),
TIMESERIES_ELEMENT.build(tsList.get(2))))
.collect(Collectors.toList());
}
};

public static final Builder<Map.Entry<Long, byte[]>> BLOOM_SCANDUMP_RESPONSE = new Builder<Map.Entry<Long, byte[]>>() {
@Override
public Map.Entry<Long, byte[]> build(Object data) {
List<Object> list = (List<Object>) data;
return new KeyValue<>(LONG.build(list.get(0)), BINARY.build(list.get(1)));
}
};

/**
* A decorator to implement Set from List. Assume that given List do not contains duplicated
* values. The resulting set displays the same ordering, concurrency, and performance
Expand Down
Loading

0 comments on commit c4506e4

Please sign in to comment.