Bedrock.KeyRange (bedrock v0.6.0)

View Source

Utilities for working with key ranges.

A key range is represented as a tuple {start_key, end_key} where:

  • start_key is inclusive
  • end_key is exclusive

Note: The atom :end is accepted in the public API for ergonomics and is automatically converted to the end-of-keyspace sentinel <<0xFF, 0xFF>> by Bedrock.key_range/2. Internally, all ranges use the binary sentinel.

Summary

Functions

Check if a key range contains a specific key.

Returns a range tuple {start, end} for all keys that start with the given prefix.

Check if two key ranges overlap.

Types

t()

@type t() :: {Bedrock.Key.t(), Bedrock.Key.t()}

Functions

contains?(arg, key)

@spec contains?(t(), Bedrock.Key.t()) :: boolean()

Check if a key range contains a specific key.

Returns true if the key is within the range.

Examples

iex> KeyRange.contains?({"a", "c"}, "b")
true

iex> KeyRange.contains?({"a", "c"}, "c")
false

iex> KeyRange.contains?({"a", Bedrock.end_of_keyspace()}, "z")
true

from_prefix(prefix)

@spec from_prefix(binary()) :: {binary(), binary()}

Returns a range tuple {start, end} for all keys that start with the given prefix.

The end key is computed using strinc/1 to create a tight upper bound, ensuring the range includes only keys with the exact prefix.

Examples

iex> Bedrock.Key.from_prefix("user")
{"user", "uses"}

iex> Bedrock.Key.from_prefix("prefix/")
{"prefix/", "prefix0"}

Errors

Raises an ArgumentError if the prefix contains only 0xFF bytes.

iex> Bedrock.Key.from_prefix(<<0xFF>>)
** (ArgumentError) Key must contain at least one byte not equal to 0xFF

overlap?(arg1, arg2)

@spec overlap?(t(), t()) :: boolean()

Check if two key ranges overlap.

Returns true if the ranges have any keys in common.

Examples

iex> KeyRange.overlaps?({"a", "c"}, {"b", "d"})
true

iex> KeyRange.overlaps?({"a", "b"}, {"c", "d"})
false

iex> KeyRange.overlaps?({"a", "c"}, {"b", Bedrock.end_of_keyspace()})
true