Packages
xandra
0.4.1
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.18.0-rc.9
0.18.0-rc.8
0.18.0-rc.7
0.18.0-rc.6
0.18.0-rc.5
0.18.0-rc.4
0.18.0-rc.3
0.18.0-rc.2
0.18.0-rc.1
0.17.0
0.16.0
0.15.0
0.15.0-rc.1
0.14.0
0.13.1
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.0
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
Fast, simple, and robust Cassandra driver for Elixir.
Current section
Files
Jump to
Current section
Files
lib/xandra/page.ex
defmodule Xandra.Page do
@moduledoc ~S"""
A struct that represents a page of rows.
This struct represents a page of rows that have been returned by the
Cassandra server in response to a query such as `SELECT`, but have not yet
been parsed into Elixir values.
This struct implements the `Enumerable` protocol and is therefore a stream. It
is through this protocol that a `Xandra.Page` struct can be parsed into Elixir
values. The simplest way of getting a list of single rows out of a
`Xandra.Page` struct is to use something like `Enum.to_list/1`. Each element
emitted when streaming out of a `Xandra.Page` struct is a map of string column
names to their corresponding value.
## Examples
statement = "SELECT name, age FROM users"
%Xandra.Page{} = page = Xandra.execute!(conn, statement, _params = [])
Enum.each(page, fn %{"name" => name, "age" => age} ->
IO.puts "Read user with name #{name} (age #{age}) out of the database"
end)
"""
defstruct [:content, :columns, :paging_state]
@opaque t :: %__MODULE__{}
@doc """
Tells whether more pages are available to fetch after the given one.
This function can be used to check whether more pages are available to fetch
after the given page. This is useful when implementing manual paging. See also
the documentation for `Xandra.execute/4`.
"""
@spec more_pages_available?(t) :: boolean
def more_pages_available?(%__MODULE__{paging_state: paging_state}) do
paging_state != nil
end
defimpl Enumerable do
def reduce(%{content: content, columns: columns}, acc, fun) do
reduce(content, columns, acc, fun)
end
defp reduce(_content, _columns, {:halt, acc}, _fun) do
{:halted, acc}
end
defp reduce(content, columns, {:suspend, acc}, fun) do
{:suspended, acc, &reduce(content, columns, &1, fun)}
end
defp reduce([values | rest], columns, {:cont, acc}, fun) do
row = zip(values, columns, []) |> :maps.from_list
reduce(rest, columns, fun.(row, acc), fun)
end
defp reduce([], _columns, {:cont, acc}, _fun) do
{:done, acc}
end
defp zip([], [], acc), do: acc
defp zip([value | values], [{_, _, name, _} | columns], acc) do
zip(values, columns, [{name, value} | acc])
end
def member?(_page, _term) do
{:error, __MODULE__}
end
def count(_page) do
{:error, __MODULE__}
end
end
defimpl Inspect do
import Inspect.Algebra
def inspect(page, options) do
properties = [
rows: Enum.to_list(page),
more_pages_available: @for.more_pages_available?(page),
]
concat(["#Xandra.Page<", to_doc(properties, options), ">"])
end
end
end