Skip to content

Commit

Permalink
Merge pull request #667 from scireum/ili/sort-value-constant
Browse files Browse the repository at this point in the history
Defines an important constant for a fallback value
  • Loading branch information
idlira authored Dec 11, 2024
2 parents fb3010a + 9b68a2b commit 1be0695
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/main/java/sirius/db/es/ElasticQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
* @param <E> the type of entities to be queried
*/
public class ElasticQuery<E extends ElasticEntity> extends Query<ElasticQuery<E>, E, ElasticConstraint> {

/**
* Defines the fallback value used when no last sort value is present.
*
* @see #searchAfter(String)
* @see #getLastSortValue()
*/
public static final String NO_LAST_SORT_VALUE = "-";

/**
* If we only fetch from a single shard (as we use a routing), we fetch up to {@link #BLOCK_SIZE_FOR_SINGLE_SHARD}
* entities at once and hope to process them within {@link #STREAM_BLOCKWISE_PIT_TTL}.
Expand Down Expand Up @@ -448,15 +457,15 @@ public ElasticQuery<E> searchAfter(List<String> searchAfter) {
* Sets a singular "last sort value" to continue a previous query.
* <p>
* It is recommended to sort by {@link ElasticEntity#ID} when using this. Note that this gracefully
* handles empty values as well as "-" which is properly generated by {@link #getLastSortValue()} if no more
* results are expected.
* handles empty values as well as {@link #NO_LAST_SORT_VALUE} which is properly generated by {@link #getLastSortValue()}
* if no more results are expected.
*
* @param searchAfter the last sort value of the previous result
* @return the query itself for fluent method calls
*/
public ElasticQuery<E> searchAfter(String searchAfter) {
if (Strings.isFilled(searchAfter)) {
if ("-".equals(searchAfter)) {
if (NO_LAST_SORT_VALUE.equals(searchAfter)) {
return this.fail();
}

Expand Down Expand Up @@ -494,17 +503,17 @@ public List<String> getLastSortValues() {
/**
* Obtains the last sort value of this result.
* <p>
* Note that this will automatically return "-" if no more results are expected. Use this in conjunction with
* {@link #searchAfter(String)} and sort queries by {@link ElasticEntity#ID}.
* Note that this will automatically return {@link #NO_LAST_SORT_VALUE} if no more results are expected.
* Use this in conjunction with {@link #searchAfter(String)} and sort queries by {@link ElasticEntity#ID}.
*
* @return the last sort value within this result
*/
public String getLastSortValue() {
if (limit > 0 && Json.getArrayAt(getRawResponse(), HITS_POINTER).size() < limit) {
return "-";
return NO_LAST_SORT_VALUE;
}

return getLastSortValues().stream().filter(Strings::isFilled).findFirst().orElse("-");
return getLastSortValues().stream().filter(Strings::isFilled).findFirst().orElse(NO_LAST_SORT_VALUE);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/sirius/db/mixing/annotations/Length.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

/**
* Specifies the column length, most probably of string (CHAR) columns.
* <p>
* This annotation has no effect on properties other than a string, except the columns
* definitions for Clickhouse, where the length is used also for other types, for example int,
* to define the int size to use.
*
* @see sirius.db.jdbc.schema.ClickhouseDatabaseDialect
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
Expand Down

0 comments on commit 1be0695

Please sign in to comment.