Skip to content

Commit

Permalink
fix: avoid returning results from other tables on AsyncStore.next/prev (
Browse files Browse the repository at this point in the history
  • Loading branch information
sborrazas committed Jul 27, 2022
1 parent d909dd7 commit 048757e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/ae_mdw/db/async_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ defmodule AeMdw.Db.AsyncStore do
@spec next(t(), table(), key() | nil) :: {:ok, key()} | :none
def next(%__MODULE__{tid: tid}, table, key) do
case EtsCache.next(tid, {table, key}) do
nil -> :none
{_table, next_key} -> {:ok, next_key}
{^table, next_key} -> {:ok, next_key}
_not_found_or_mismatch -> :none
end
end

@spec prev(t(), table(), key() | nil) :: {:ok, key()} | :none
def prev(%__MODULE__{tid: tid}, table, key) do
case EtsCache.prev(tid, {table, key}) do
nil -> :none
{_table, prev_key} -> {:ok, prev_key}
{^table, prev_key} -> {:ok, prev_key}
_not_found_or_mismatch -> :none
end
end

Expand Down
33 changes: 33 additions & 0 deletions test/ae_mdw/db/async_store_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule AeMdw.Db.AsyncStoreTest do
use ExUnit.Case

alias AeMdw.Db.AsyncStore
alias AeMdw.Db.Store

describe "next/2" do
test "it returns the next value" do
store = AsyncStore.instance()

store =
store
|> Store.put(:table, {:record, :key1, :val1})
|> Store.put(:table, {:record, :key2, :val2})

assert {:ok, :key1} = Store.next(store, :table, :key0)
assert {:ok, :key2} = Store.next(store, :table, :key1)
assert :none = Store.next(store, :table, :key2)
end

test "when other tables present, it doesn't return values from them" do
store = AsyncStore.instance()

store =
store
|> Store.put(:table1, {:record, :key1, :val1})
|> Store.put(:table1, {:record, :key2, :val2})
|> Store.put(:table2, {:record, :key3, :val3})

assert :none = Store.next(store, :table1, :key2)
end
end
end

0 comments on commit 048757e

Please sign in to comment.