Krishnan

Software Developer

In Memory Server side pagination

Are you dealing with tens-of-thousands of data that supposed to show in the UI, but your data source doesn’t support pagination and filtering provisons. Server Side pagination will be the best suit for that, No worries this plugin will help you to achive pagination, sorting and filtering using in-memory

Server Side Pagination

As we are developing so many software in today’s world and dealing with millions and millions of data. Imagine a situation where you have millions of data to show on your web page. A browser will have not able to render all the data at one shot. So the best option is to go for server-side pagination, where a server will return the bounded number of rows to show in the UI.


About this Plugin

In most cases, we have a relational database or no-SQL databases which will run the query with limit, offset, ordering along with filtering/searching criteria.

There may be a situation arise where you cannot afford to run a SQL query to get the data, but you want to do support pagination.

Yes here, this plugin will have all the data’s that loaded in to server and does magic of (pagination, searching) for you.

Note: Assuming you have very good memory allocated for JVM, and GC will help us to remove the unused objects at an earlier stage.


Things that this plugin supports

  1. This allows you to use pagination with help of limit and offset
  2. One can sort the data by column
  3. Also, search the text in all column (case-insensitive) and get the relevant data
  4. It also includes the regex supports for searching, (i.e) you can pass a regular expression to find relevant data,

Let’s Learn how to use this plugin

This plugin is written in JAVA 1.8 version.

  1. Import the source code from here into your application
  2. In your repository class extends the PagainationDataService and just override the getData() method as follow, This method will return the complete source data from which the plugin will take care of performing pagination and searching for you.

    public class EmployeeRepository extends PaginationDataServiceBase<Employee> {
    
    @Override
    protected List getData(PaginationCriteria paginationCriteria) throws PaginationException {
        return "YOUR ENTIRE SOURCE DATA";
    }
    }
  3. Now, From your service or controller class, create a simple paginator object and call getPageResult as follow,

  4. A getPageResult() method will take PaginationCritera as an input which follows

    public class PaginationCriteria {
    /**
     * Number of items that need to to displayed in a page
     * If it is 0 then it will return all records
     */
    private int limit;
    
    /**
     * Paging first record indicator. This is the start point in the current data
     * set (0 index based - i.e. 0 is the first record).
     */
    private int offset;
    
    /**
     * search text that will do searching in all columns
     */
    private String query;
    
    /**
     * Column's ordering criteria.
     */
    private OrderingCriteria sort;
    
    /**
     * Name of the column to sort
     */
    private String column;
    }

This plugin also supports search with regex as well.

public class DemoApplication {

    static ITablePaginator<Employee> paginator = new SimplePaginator<Employee>(new EmployeeRepository());

    public static void main(String[] args) throws PaginationException {
        PaginationCriteria criteria = new PaginationCriteria();

        IPage<Object> pageResult = paginator.getPageResult(criteria);

        System.out.println(pageResult);
    }
}
  1. This Plugin allows you to fully customize the implementations, for instance, the SimplePaginator will be used for performing all pagination, searching, and sorting of an object by holding it into a memory.
  2. SimplePaginator will build the output and returns the values as below

    {
     totalCount : 10, // This represent the total record count
     items : [], //Array of custom objects 
     limit : 10,// This specifies the number of records that need to be returned for a page.
     offset : 10, //This specifies the number of rows to skip before starting to    return rows
     filteredCount : 2, // This specified the number of records mathced with search text
    }
  3. You can also implement your own ITablePaginator if you wish you have any other behavior

  4. Now, assume your application uses any relation or NO-SQL database which supports pagination and filtering option by using a query.

  5. In such a case, you can still this plugin by overriding the getPageEntries(PaginationCriteria criteria) method from PaginationDataServiceBase, but remember you need to write a query with ordering, limiting, offset, and filtering clause


POINTS TO REMEMBER

if you want to sort a nested column, for example, An Employee object has a Company object inside it that if you wish to sort the employee using company name then you need to set the column value as “company.name” of PaginationCriteria class.


Caveats

The performance of in-memory pagination is fully dependent on the machine that uses. This plugin will really help when you have tens-of-thousands of data to show on your webpage with pagination.