Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Updated some variable names for clarity's sake
Browse files Browse the repository at this point in the history
  • Loading branch information
cip999 authored and cip999 committed Aug 15, 2021
1 parent 880c258 commit 5072613
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected void init() throws Exception {
loadIndex(Index.PHRASE);
loadIndex(Index.GAME_OF_THRONES);
loadIndex(Index.NESTED);
loadIndex(Index.BOOKS);
loadIndex(Index.BOOKS_NESTED_WITH_SUBPROPERTIES);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public enum Index {
"_doc",
getDataTypeNonnumericIndexMapping(),
"src/test/resources/datatypes.json"),
BOOKS(TestsConstants.TEST_INDEX_BOOKS,
BOOKS_NESTED_WITH_SUBPROPERTIES(TestsConstants.TEST_INDEX_BOOKS,
"nestedType",
getBooksIndexMapping(),
"src/test/resources/books.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class TestsConstants {
public final static String TEST_INDEX_STRINGS = TEST_INDEX + "_strings";
public final static String TEST_INDEX_DATATYPE_NUMERIC = TEST_INDEX + "_datatypes_numeric";
public final static String TEST_INDEX_DATATYPE_NONNUMERIC = TEST_INDEX + "_datatypes_nonnumeric";
public final static String TEST_INDEX_BOOKS = TEST_INDEX + "_nested";
public final static String TEST_INDEX_BOOKS = TEST_INDEX + "_nested_subproperties";

public final static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public final static String TS_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class SelectResultSet extends ResultSet {
private long size;
private long totalHits;
private long internalTotalHits;
private Integer rowCount;
private Integer limitRowCount;
private List<DataRows.Row> rows;
private Cursor cursor;

Expand Down Expand Up @@ -120,7 +120,7 @@ public SelectResultSet(Client client,
this.head = schema.getHeaders();
this.dateFieldFormatter = new DateFieldFormatter(indexName, columns, fieldAliasMap);
if (query instanceof Select) {
this.rowCount = ((Select) query).getRowCount();
this.limitRowCount = ((Select) query).getRowCount();
}

extractData();
Expand Down Expand Up @@ -647,7 +647,7 @@ private long rowsLeft(Integer fetchSize, Integer limit) {

private List<DataRows.Row> populateRows(SearchHits searchHits) {
List<DataRows.Row> rows = new ArrayList<>();
List<Map<String, Object>> rowsAsMap = populateRows(head, searchHits, rowCount);
List<Map<String, Object>> rowsAsMap = populateRows(head, searchHits, limitRowCount);
for (Map<String, Object> rowAsMap : rowsAsMap) {
rows.add(new DataRows.Row(rowAsMap));
}
Expand All @@ -659,12 +659,27 @@ private List<DataRows.Row> populateRows(SearchHits searchHits) {
* The core idea is to flatten non-nested hits, and recur on inner hits
* by calling the method flatNestedField(), which in turn calls back
* populateRows.
* <p>
* Sample input:
* keys = {"authors.info.name"} (authors is nested)
* searchHits = {
* {
* key = "authors"
* innerHits = {
* {"info.name": "Andrea"},
* {"info.name": "Carmen}
* }
* }
* }
* Sample output:
* rows = [{"authors.info.name": "Andrea"}, {"authors.info.name": "Carmen"}]
* </p>
*/
private List<Map<String, Object>> populateRows(List<String> keys, SearchHits searchHits, Integer rowsLeft) {
private List<Map<String, Object>> populateRows(List<String> keys, SearchHits searchHits, Integer remainingRows) {
List<Map<String, Object>> rows = new ArrayList<>();

for (SearchHit hit : searchHits) {
if (rowsLeft != null && rowsLeft == 0) {
if (remainingRows != null && remainingRows == 0) {
return rows;
}

Expand All @@ -673,9 +688,9 @@ private List<Map<String, Object>> populateRows(List<String> keys, SearchHits sea

if (!isJoinQuery()) {
// Row already flatten in source in join. And join doesn't support nested fields for now.
result = flatNestedField(keys, hit.getInnerHits(), rowsLeft);
if (rowsLeft != null) {
rowsLeft -= result.size();
result = flatNestedField(keys, hit.getInnerHits(), remainingRows);
if (remainingRows != null) {
remainingRows -= result.size();
}

rowSource = flatRow(keys, rowSource);
Expand All @@ -696,8 +711,8 @@ private List<Map<String, Object>> populateRows(List<String> keys, SearchHits sea
dateFieldFormatter.applyJDBCDateFormat(rowSource);
}
result.add(rowSource);
if (rowsLeft != null) {
rowsLeft--;
if (remainingRows != null) {
remainingRows--;
}
}

Expand Down Expand Up @@ -855,7 +870,7 @@ private Map<String, Object> flatRow(List<String> keys, Map<String, Object> row)
*/
private List<Map<String, Object>> flatNestedField(List<String> keys,
Map<String, SearchHits> innerHits,
Integer rowsLeft) {
Integer remainingRows) {
List<Map<String, Object>> result = new ArrayList<>();
result.add(new HashMap<>());

Expand All @@ -872,8 +887,8 @@ private List<Map<String, Object>> flatNestedField(List<String> keys,
}

List<Map<String, Object>> innerResult = populateRows(newKeys, innerHits.get(colName),
(rowsLeft == null) ? null :
((result.isEmpty() ? 0 : ratioCeil(rowsLeft, result.size()))));
(remainingRows == null) ? null :
((result.isEmpty() ? 0 : ratioCeil(remainingRows, result.size()))));
for (Map<String, Object> innerRow : innerResult) {
Map<String, Object> row = new HashMap<>();
for (String path : innerRow.keySet()) {
Expand All @@ -888,7 +903,7 @@ private List<Map<String, Object>> flatNestedField(List<String> keys,
}

// In the case of multiple sets of inner hits, returns all possible combinations of entries
result = cartesianProduct(result, innerResult, rowsLeft);
result = cartesianProduct(result, innerResult, remainingRows);
}

return result;
Expand All @@ -900,15 +915,15 @@ private List<Map<String, Object>> flatNestedField(List<String> keys,
*/
private List<Map<String, Object>> cartesianProduct(List<Map<String, Object>> rowsLeft,
List<Map<String, Object>> rowsRight,
Integer maxRows) {
Integer remainingRows) {
List<Map<String, Object>> result = new ArrayList<>();
for (Map<String, Object> rowLeft : rowsLeft) {
if (maxRows != null && result.size() == maxRows) {
if (remainingRows != null && result.size() == remainingRows) {
break;
}

for (Map<String, Object> rowRight : rowsRight) {
if (maxRows != null && result.size() == maxRows) {
if (remainingRows != null && result.size() == remainingRows) {
break;
}

Expand Down

0 comments on commit 5072613

Please sign in to comment.