Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breaking change] improve the select/3 API for key ranges #5

Merged
merged 2 commits into from
Sep 11, 2019

Conversation

lucaong
Copy link
Owner

@lucaong lucaong commented Sep 11, 2019

The previous API had a few shortcomings:

  • Corner cases for nil min_key/max_key and min_key/max_key literally being tuples like {123, :included}

  • Misspelling :included or :excluded would silently fail or lead to unexpected results

  • The API was a bit cumbersome in general

See also the discussion here: #3

The new API is much clearer, and removes the shortcomings:

# Exclusive ranges:
## Before:
{:ok, result} = CubDB.select(db,
  min_key: :foo,
  max_key: {:bar, :excluded}
)
## Now:
{:ok, result} = CubDB.select(db,
  min_key: :foo,
  max_key: :bar,
  max_key_inclusive: false
)

# Open-ended ranges:
## Before
{:ok, result} = CubDB.select(db,
  min_key: :foo,
  max_key: nil
)
## Now
{:ok, result} = CubDB.select(db,
  min_key: :foo
)

# Setting min/max key to `nil`:
## Before:
{:ok, result} = CubDB.select(db,
  min_key: {nil, :included},
  max_key: :bar
)
# Now:
{:ok, result} = CubDB.select(db,
  min_key: nil,
  max_key: :bar
)

The previous API had a few shortcomings:

  - There were corner cases for `nil` min_key/max_key, or
  min_key/max_key literally being tuples like `{123, :included}`

  - It was not possible in these corner cases to detect error conditions

  - Mispelling `:included` or `:excluded` would silently fail or lead to
  unexpected results

  - The API was a bit cumbersome in general

The new API is much clearer and removes the shortcomings:

```elixir
{:ok, result} = CubDB.select(db,
  min_key: :foo,
  max_key: {:bar, :excluded}
)
{:ok, result} = CubDB.select(db,
  min_key: :foo,
  max_key: :bar,
  max_key_inclusive: false
)

{:ok, result} = CubDB.select(db,
  min_key: :foo,
  max_key: nil
)
{:ok, result} = CubDB.select(db,
  min_key: :foo
)

{:ok, result} = CubDB.select(db,
  min_key: {nil, :included},
  max_key: :bar
)
{:ok, result} = CubDB.select(db,
  min_key: nil,
  max_key: :bar
)
```
@lucaong lucaong merged commit 0cdb7e4 into master Sep 11, 2019
@lucaong lucaong deleted the better_select_key_ranges_api branch September 11, 2019 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant