-
Notifications
You must be signed in to change notification settings - Fork 39
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
Emitting :key_deleted
when key is purged
#157
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,6 +9,9 @@ defmodule Gnat.Jetstream.API.KV do | |||||||||||||||||||||||||||||||||
@stream_prefix "KV_" | ||||||||||||||||||||||||||||||||||
@subject_prefix "$KV." | ||||||||||||||||||||||||||||||||||
@two_minutes_in_nanoseconds 1_200_000_000 | ||||||||||||||||||||||||||||||||||
@operation_header "kv-operation" | ||||||||||||||||||||||||||||||||||
@operation_del "DEL" | ||||||||||||||||||||||||||||||||||
@operation_purge "PURGE" | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@type bucket_options :: | ||||||||||||||||||||||||||||||||||
{:history, non_neg_integer()} | ||||||||||||||||||||||||||||||||||
|
@@ -248,10 +251,34 @@ defmodule Gnat.Jetstream.API.KV do | |||||||||||||||||||||||||||||||||
Gnat.Jetstream.API.KV.Watcher.stop(pid) | ||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@doc ~S""" | ||||||||||||||||||||||||||||||||||
Returns true if operation is `{"kv-operation", "DEL"}` or `{"kv-operation", "PURGE"}` | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
## Parameters | ||||||||||||||||||||||||||||||||||
- `headers` - a list of headers to test | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
## Examples | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
iex> is_delete_operation?([{"kv-operation", "DEL"}]) | ||||||||||||||||||||||||||||||||||
true | ||||||||||||||||||||||||||||||||||
iex> is_delete_operation?([{"kv-operation", "PURGE"}]) | ||||||||||||||||||||||||||||||||||
true | ||||||||||||||||||||||||||||||||||
iex> is_delete_operation?([{"kv-operation", "ADD"}]) | ||||||||||||||||||||||||||||||||||
false | ||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
@spec is_delete_operation?(headers :: Gnat.headers()) :: boolean() | ||||||||||||||||||||||||||||||||||
def is_delete_operation?(headers) do | ||||||||||||||||||||||||||||||||||
headers | ||||||||||||||||||||||||||||||||||
|> Enum.filter(fn {k, v} -> | ||||||||||||||||||||||||||||||||||
k == @operation_header and (v == @operation_del or v == @operation_purge) | ||||||||||||||||||||||||||||||||||
end) | ||||||||||||||||||||||||||||||||||
|> length() > 0 | ||||||||||||||||||||||||||||||||||
Comment on lines
+271
to
+275
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
In practice this probably isn't a huge issue as written since the size of headers is probably small, but this is equivalent. At worst it makes a full pass once, at best it short-circuits early
Comment on lines
+271
to
+275
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
In practice this probably isn't a huge issue as written since the size of headers is probably small, but this is equivalent. At worst it makes a full pass once, at best it short-circuits early |
||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
defp receive_keys(keys \\ %{}, bucket_name) do | ||||||||||||||||||||||||||||||||||
receive do | ||||||||||||||||||||||||||||||||||
{:msg, %{topic: key, body: body, headers: headers}} -> | ||||||||||||||||||||||||||||||||||
if {"kv-operation", "DEL"} in headers do | ||||||||||||||||||||||||||||||||||
if is_delete_operation?(headers) do | ||||||||||||||||||||||||||||||||||
receive_keys(keys, bucket_name) | ||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||
Map.put(keys, subject_to_key(key, bucket_name), body) |> receive_keys(bucket_name) | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an Elixir convention, using the
?
is correct for a function that returns a boolean, but theis_
prefix is reserved for guards. See https://hexdocs.pm/elixir/naming-conventions.html#trailing-question-mark-foo