All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ydespreaux.spring.data.elasticsearch.ScrolledPageable Maven / Gradle / Ivy

There is a newer version: 0.0.3
Show newest version
/*
 * Copyright (C) 2018 Yoann Despréaux
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING . If not, write to the
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Please send bugreports with examples or suggestions to [email protected]
 */

package com.github.ydespreaux.spring.data.elasticsearch;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.time.Duration;
import java.util.Optional;

/**
 * Pagination criteria when searching for big data
 * @author Yoann Despréaux
 * @since 0.0.1
 */
public class ScrolledPageable extends PageRequest {

    private static final Duration DEFAULT_SCROLL_TIME_VALUE = Duration.ofMinutes(60);

    private static final long serialVersionUID = -5506726303312911158L;

    /**
     * Current scrollId
     */
    @Getter
    @Setter
    private String scrollId;

    /**
     * Scroll time in minutes
     */
    @Setter
    private transient Duration scrollTime = DEFAULT_SCROLL_TIME_VALUE;

    /**
     * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return the first
     * page.
     *
     * @param size the size of the page to be returned.
     * @deprecated use static method of(...)
     */
    @Deprecated
    public ScrolledPageable(int size) {
        super(0, size);
    }

    /**
     * Creates a new {@link PageRequest} with sort parameters applied.
     *
     * @param size       the size of the page to be returned.
     * @param direction  the direction of the {@link Sort} to be specified, can be {@literal null}.
     * @param properties the properties to sort by, must not be {@literal null} or empty.
     * @deprecated use static method of(...)
     */
    @Deprecated
    public ScrolledPageable(int size, Sort.Direction direction, String... properties) {
        super(0, size, direction, properties);
    }

    /**
     * Creates a new {@link ScrolledPageable} with sort parameters applied.
     *
     * @param size the size of the page to be returned.
     * @param sort can be {@literal null}.
     * @deprecated use static method of(...)
     */
    @Deprecated
    private ScrolledPageable(int size, Sort sort) {
        super(0, size, sort);
    }

    /**
     * Creates a new {@link ScrolledPageable} with size
     * @param size the size of the page to returned
     * @return a new {@link ScrolledPageable} with size
     */
    public static ScrolledPageable of(int size) {
        return of(size, Sort.unsorted());
    }

    /**
     * Creates a new {@link ScrolledPageable} with size and sort parameters.
     * @param size the size of the page to be retourned
     * @param sort the sort parameters applied (can be {@literal null}
     * @return a new {@link ScrolledPageable} with size and sort parameters.
     */
    public static ScrolledPageable of(int size, Sort sort) {
        return new ScrolledPageable(size, sort == null ? Sort.unsorted() : sort);
    }

    /**
     * @param size       the size of the page
     * @param direction  the sort direction of tyhe page
     * @param properties the properties to sort
     * @return a new {@link ScrolledPageable} withe size and sort
     */
    public static ScrolledPageable of(int size, Sort.Direction direction, String... properties) {
        return of(size, Sort.by(direction, properties));
    }

    public static ScrolledPageable of(Duration scrollTime, int size) {
        return of(scrollTime, size, (Sort) null, null);
    }

    public static ScrolledPageable of(Duration scrollTime, int size, Sort sort) {
        return of(scrollTime, size, sort, null);
    }

    public static ScrolledPageable of(Duration scrollTime, int size, Sort sort, String scrollId) {
        ScrolledPageable pageable = of(size, sort);
        pageable.setScrollTime(scrollTime);
        pageable.setScrollId(scrollId);
        return pageable;
    }

    /**
     * @return Returns the scroll time
     */
    public Duration getScrollTime() {
        if (scrollTime == null) {
            return DEFAULT_SCROLL_TIME_VALUE;
        }
        return scrollTime;
    }


    public Optional optional() {
        return this.isUnpaged() ? Optional.empty() : Optional.of(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy