AshScylla.DataLayer.Pagination (AshScylla v0.4.0)

Copy Markdown View Source

Token-based pagination support for AshScylla.

ScyllaDB/Cassandra doesn't support OFFSET natively. Instead, it uses Xandra's native paging state mechanism for efficient pagination.

Token-Based Pagination

When pagination :token is set in the ash_scylla DSL, queries use Xandra's built-in paging state to efficiently page through results without OFFSET.

Usage

# First page
{:ok, records, next_page_token} =
  AshScylla.DataLayer.Pagination.fetch_page(repo, table, filters, nil, 10)

# Subsequent pages
{:ok, records, next_page_token} =
  AshScylla.DataLayer.Pagination.fetch_page(repo, table, filters, page_token, 10)

Page Token Format

Page tokens are opaque binaries returned by Xandra's paging_state. They should be treated as opaque by callers and only passed back to fetch the next page.

Summary

Functions

Builds a CQL query string with LIMIT for pagination.

Builds a CQL query string with LIMIT for pagination, with optional page token.

Decodes a page token string back to a paging state binary.

Returns the default page size.

Encodes a paging state binary to an opaque page token string.

Fetches a page of results using Xandra's native paging state.

Returns the maximum allowed page size.

Types

page_result()

@type page_result() :: {:ok, [term()], String.t() | nil} | {:error, term()}

page_token()

@type page_token() :: String.t() | nil

Functions

build_paginated_query(table, filters, page_size)

@spec build_paginated_query(String.t(), list(), pos_integer()) :: {String.t(), list()}

Builds a CQL query string with LIMIT for pagination.

Returns {query, params} suitable for repo.query/3.

build_paginated_query(table, filters, page_token, page_size)

@spec build_paginated_query(String.t(), list(), page_token(), pos_integer()) ::
  {String.t(), list()}

Builds a CQL query string with LIMIT for pagination, with optional page token.

When page_token is non-nil, adds a token() > ? condition to the WHERE clause. Returns {query, params} suitable for repo.query/3.

decode_page_token(page_token)

@spec decode_page_token(String.t()) :: binary()

Decodes a page token string back to a paging state binary.

default_page_size()

@spec default_page_size() :: pos_integer()

Returns the default page size.

encode_page_token(paging_state)

@spec encode_page_token(binary()) :: String.t()

Encodes a paging state binary to an opaque page token string.

fetch_page(repo, table, filters, page_token, page_size \\ 50)

@spec fetch_page(module(), String.t(), list(), page_token(), pos_integer()) ::
  page_result()

Fetches a page of results using Xandra's native paging state.

Parameters

  • repo - The Ecto repo module
  • table - The table name
  • filters - Filter list for WHERE clause
  • page_token - Opaque page token from previous page (nil for first page)
  • page_size - Number of results per page (default: 50, max: 1000)

Returns

{:ok, records, next_page_token} where next_page_token is nil when there are no more pages.

max_page_size()

@spec max_page_size() :: pos_integer()

Returns the maximum allowed page size.