Bedrock.KeySelector (bedrock v0.6.0)

View Source

A KeySelector describes a key in the database that can be resolved to an actual key at runtime.

KeySelectors leverage the lexicographically ordered nature of keys to find keys based on their positional relationships to other keys, without needing to know exact key values.

Examples

iex> KeySelector.first_greater_or_equal("user:")
%KeySelector{key: "user:", or_equal: true, offset: 0}

iex> KeySelector.first_greater_than("data") |> KeySelector.add(5)
%KeySelector{key: "data", or_equal: false, offset: 6}

Summary

Functions

Adds an offset to a KeySelector, moving the resolution point forward (positive) or backward (negative) by the specified number of keys.

Creates a KeySelector that resolves to the lexicographically smallest key greater than or equal to the given key.

Creates a KeySelector that resolves to the lexicographically smallest key strictly greater than the given key.

Creates a KeySelector that resolves to the lexicographically largest key less than or equal to the given key.

Creates a KeySelector that resolves to the lexicographically largest key strictly less than the given key.

Returns true if the KeySelector has a negative offset, meaning it will resolve to a key that comes before the reference position.

Returns true if the KeySelector has a positive offset, meaning it will resolve to a key that comes after the reference position.

Subtracts an offset from a KeySelector, moving the resolution point backward (positive) or forward (negative) by the specified number of keys.

Converts a KeySelector to a human-readable string representation.

Returns true if the KeySelector has a zero offset, meaning it resolves to a key based only on the base key and or_equal flag.

Types

t()

@type t() :: %Bedrock.KeySelector{
  key: binary(),
  offset: integer(),
  or_equal: boolean()
}

Functions

add(selector, offset)

@spec add(t(), integer()) :: t()

Adds an offset to a KeySelector, moving the resolution point forward (positive) or backward (negative) by the specified number of keys.

Examples

iex> KeySelector.first_greater_or_equal("a") |> KeySelector.add(3)
%KeySelector{key: "a", or_equal: true, offset: 3}

iex> KeySelector.first_greater_than("z") |> KeySelector.add(-2)
%KeySelector{key: "z", or_equal: false, offset: -1}

first_greater_or_equal(key)

@spec first_greater_or_equal(binary()) :: t()

Creates a KeySelector that resolves to the lexicographically smallest key greater than or equal to the given key.

first_greater_than(key)

@spec first_greater_than(binary()) :: t()

Creates a KeySelector that resolves to the lexicographically smallest key strictly greater than the given key.

last_less_or_equal(key)

@spec last_less_or_equal(binary()) :: t()

Creates a KeySelector that resolves to the lexicographically largest key less than or equal to the given key.

last_less_than(key)

@spec last_less_than(binary()) :: t()

Creates a KeySelector that resolves to the lexicographically largest key strictly less than the given key.

negative_offset?(key_selector)

@spec negative_offset?(t()) :: boolean()

Returns true if the KeySelector has a negative offset, meaning it will resolve to a key that comes before the reference position.

positive_offset?(key_selector)

@spec positive_offset?(t()) :: boolean()

Returns true if the KeySelector has a positive offset, meaning it will resolve to a key that comes after the reference position.

subtract(selector, offset)

@spec subtract(t(), integer()) :: t()

Subtracts an offset from a KeySelector, moving the resolution point backward (positive) or forward (negative) by the specified number of keys.

Examples

iex> KeySelector.first_greater_or_equal("m") |> KeySelector.subtract(2)
%KeySelector{key: "m", or_equal: true, offset: -2}

to_string(key_selector)

@spec to_string(t()) :: String.t()

Converts a KeySelector to a human-readable string representation.

Examples

iex> KeySelector.first_greater_or_equal("user:") |> KeySelector.to_string()
"first_greater_or_equal("user:")"

iex> KeySelector.first_greater_than("data") |> KeySelector.add(3) |> KeySelector.to_string()
"first_greater_than("data") + 3"

zero_offset?(key_selector)

@spec zero_offset?(t()) :: boolean()

Returns true if the KeySelector has a zero offset, meaning it resolves to a key based only on the base key and or_equal flag.