Skip to content

Commit

Permalink
Separating metadata filtering for pagination strategies
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Garg <[email protected]>
  • Loading branch information
Harsh Garg committed Oct 3, 2024
1 parent ede0700 commit 757b837
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public IndexPaginationStrategy(PageParams pageParams, ClusterState clusterState)
clusterState,
pageParams.getSort(),
Objects.isNull(requestedToken) ? null : requestedToken.lastIndexName,
Objects.isNull(requestedToken) ? null : requestedToken.lastIndexCreationTime,
false
Objects.isNull(requestedToken) ? null : requestedToken.lastIndexCreationTime
);

// Trim sortedIndicesList to get the list of indices metadata to be sent as response
Expand All @@ -69,40 +68,37 @@ public IndexPaginationStrategy(PageParams pageParams, ClusterState clusterState)
);
}

protected static List<IndexMetadata> getEligibleIndices(
private static List<IndexMetadata> getEligibleIndices(
ClusterState clusterState,
String sortOrder,
String lastIndexName,
Long lastIndexCreationTime,
boolean includeLastIndex
Long lastIndexCreationTime
) {
if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) {
return PaginationStrategy.getSortedIndexMetadata(
clusterState,
PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR
);
} else {
return PaginationStrategy.getSortedIndexMetadata(
clusterState,
getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime),
PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR
);
}
return PaginationStrategy.getSortedIndexMetadata(
clusterState,
getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime, includeLastIndex),
PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR
);
}

protected static Predicate<IndexMetadata> getMetadataFilter(
String sortOrder,
String lastIndexName,
Long lastIndexCreationTime,
boolean includeLastIndex
) {
private static Predicate<IndexMetadata> getMetadataFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) {
if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) {
return indexMetadata -> true;
}
return getIndexCreateTimeFilter(sortOrder, lastIndexName, lastIndexCreationTime);
}

protected static Predicate<IndexMetadata> getIndexCreateTimeFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) {
boolean isAscendingSort = sortOrder.equals(PageParams.PARAM_ASC_SORT_VALUE);
return metadata -> {
if (metadata.getIndex().getName().equals(lastIndexName)) {
return includeLastIndex;
} else if (metadata.getCreationDate() == lastIndexCreationTime) {
if (metadata.getCreationDate() == lastIndexCreationTime) {
return isAscendingSort
? metadata.getIndex().getName().compareTo(lastIndexName) > 0
: metadata.getIndex().getName().compareTo(lastIndexName) < 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

import static org.opensearch.action.pagination.IndexPaginationStrategy.ASC_COMPARATOR;
import static org.opensearch.action.pagination.IndexPaginationStrategy.DESC_COMPARATOR;

/**
* This strategy can be used by the Rest APIs wanting to paginate the responses based on Shards.
Expand All @@ -35,12 +39,11 @@ public class ShardPaginationStrategy implements PaginationStrategy<ShardRouting>
public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState) {
ShardStrategyToken shardStrategyToken = getShardStrategyToken(pageParams.getRequestedToken());
// Get list of indices metadata sorted by their creation time and filtered by the last sent index
List<IndexMetadata> filteredIndices = IndexPaginationStrategy.getEligibleIndices(
List<IndexMetadata> filteredIndices = getEligibleIndices(
clusterState,
pageParams.getSort(),
Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexName,
Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime,
true
Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime
);
// Get the list of shards and indices belonging to current page.
this.pageData = getPageData(
Expand All @@ -51,6 +54,39 @@ public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState)
);
}

private static List<IndexMetadata> getEligibleIndices(
ClusterState clusterState,
String sortOrder,
String lastIndexName,
Long lastIndexCreationTime
) {
if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) {
return PaginationStrategy.getSortedIndexMetadata(
clusterState,
PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR
);
} else {
return PaginationStrategy.getSortedIndexMetadata(
clusterState,
getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime),
PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR
);
}
}

private static Predicate<IndexMetadata> getMetadataFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) {
if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) {
return indexMetadata -> true;
}
return indexNameFilter(lastIndexName).or(
IndexPaginationStrategy.getIndexCreateTimeFilter(sortOrder, lastIndexName, lastIndexCreationTime)
);
}

private static Predicate<IndexMetadata> indexNameFilter(String lastIndexName) {
return metadata -> metadata.getIndex().getName().equals(lastIndexName);
}

/**
* Will be used to get the list of shards and respective indices to which they belong,
* which are to be displayed in a page.
Expand Down

0 comments on commit 757b837

Please sign in to comment.