Skip to content

Commit

Permalink
refactor: delete duplicated logic and wrap DBLevel calls (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
leobz authored Oct 21, 2024
1 parent 46ce72b commit 7c75e16
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lib/lambda_ethereum_consensus/store/blob_db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ defmodule LambdaEthereumConsensus.Store.BlobDb do

with {:ok, it} <- Db.iterate(),
{:ok, @block_root_prefix <> _, _value} <-
Exleveldb.iterator_move(it, last_finalized_key),
Db.iterator_move(it, last_finalized_key),
{:ok, keys_to_remove} <- get_block_root_keys_to_remove(it),
:ok <- Exleveldb.iterator_close(it) do
:ok <- Db.iterator_close(it) do
total_removed =
keys_to_remove
|> Enum.reduce_while(0, fn
Expand All @@ -111,7 +111,7 @@ defmodule LambdaEthereumConsensus.Store.BlobDb do
@spec get_block_root_keys_to_remove(list(binary()), :eleveldb.itr_ref()) ::
{:ok, list(binary())}
defp get_block_root_keys_to_remove(keys_to_remove \\ [], iterator) do
case Exleveldb.iterator_move(iterator, :prev) do
case Db.iterator_move(iterator, :prev) do
{:ok, <<@block_root_prefix, _rest::binary>> = block_root_key, _root} ->
[block_root_key | keys_to_remove] |> get_block_root_keys_to_remove(iterator)

Expand Down
40 changes: 26 additions & 14 deletions lib/lambda_ethereum_consensus/store/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,49 @@ defmodule LambdaEthereumConsensus.Store.Db do

@spec put(binary, binary) :: :ok
def put(key, value) do
ref = GenServer.call(@registered_name, :get_ref)
Exleveldb.put(ref, key, value)
Exleveldb.put(ref(), key, value)
end

@spec delete(binary) :: :ok
def delete(key) do
ref = GenServer.call(@registered_name, :get_ref)
Exleveldb.delete(ref, key)
Exleveldb.delete(ref(), key)
end

@spec get(binary) :: {:ok, binary} | :not_found
def get(key) do
ref = GenServer.call(@registered_name, :get_ref)
Exleveldb.get(ref, key)
Exleveldb.get(ref(), key)
end

@spec size() :: non_neg_integer()
def size() do
ref = GenServer.call(@registered_name, :get_ref)
{:ok, size} = :eleveldb.status(ref, "leveldb.total-bytes")
{:ok, size} = :eleveldb.status(ref(), "leveldb.total-bytes")
String.to_integer(size)
end

@spec iterate() :: {:ok, :eleveldb.itr_ref()} | {:error, any()}
def iterate() do
ref = GenServer.call(@registered_name, :get_ref)
# TODO: wrap cursor to make it DB-agnostic
Exleveldb.iterator(ref, [])
Exleveldb.iterator(ref(), [])
end

@spec iterate_keys() :: {:ok, :eleveldb.itr_ref()} | {:error, any()}
def iterate_keys() do
ref = GenServer.call(@registered_name, :get_ref)
# TODO: wrap cursor to make it DB-agnostic
Exleveldb.iterator(ref, [], :keys_only)
Exleveldb.iterator(ref(), [], :keys_only)
end

@spec iterator_close(:eleveldb.itr_ref()) :: :ok
def iterator_close(iter_ref) do
Exleveldb.iterator_close(iter_ref)
end

@spec iterator_move(
:eleveldb.itr_ref(),
:first | :last | :next | :prefetch | :prefetch_stop | :prev | binary()
) ::
{:error, :invalid_iterator | :iterator_closed}
| {:ok, binary()}
| {:ok, binary(), binary()}
def iterator_move(iter_ref, action) do
Exleveldb.iterator_move(iter_ref, action)
end

@impl true
Expand All @@ -74,4 +82,8 @@ defmodule LambdaEthereumConsensus.Store.Db do
Application.fetch_env!(:lambda_ethereum_consensus, __MODULE__)
|> Keyword.fetch!(:dir)
end

defp ref() do
GenServer.call(__MODULE__, :get_ref)
end
end
14 changes: 7 additions & 7 deletions lib/lambda_ethereum_consensus/store/kv_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do

with {:ok, it} <- Db.iterate_keys(),
{:ok, encoded_start} <- do_encode_key(start_key),
{:ok, ^encoded_start} <- Exleveldb.iterator_move(it, encoded_start) do
{:ok, ^encoded_start} <- Db.iterator_move(it, encoded_start) do
res = iterate(it, starting_value, f, direction, encoded_start, include_first?)
Exleveldb.iterator_close(it)
Db.iterator_close(it)
{:ok, res}
else
# The iterator moved for the first time to a place where it wasn't expected.
Expand Down Expand Up @@ -109,7 +109,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do
def first_key() do
{:ok, it} = Db.iterate_keys()

case Exleveldb.iterator_move(it, @prefix) do
case Db.iterator_move(it, @prefix) do
{:ok, @prefix <> _k = full_key} -> do_decode_key(full_key)
{:ok, _other} -> :not_found
{:error, :invalid_iterator} -> :not_found
Expand All @@ -135,7 +135,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do
defp key_iterator(key) do
with {:ok, it} <- Db.iterate_keys(),
{:ok, encoded_start} <- do_encode_key(key),
{:ok, ^encoded_start} <- Exleveldb.iterator_move(it, encoded_start) do
{:ok, ^encoded_start} <- Db.iterator_move(it, encoded_start) do
{:first, it, key}
else
# The iterator moved for the first time to a place where it wasn't expected.
Expand All @@ -154,7 +154,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do
defp next_key({:first, it, key}, direction), do: {[key], {:next, it}}

defp move_iterator(it, direction) do
case Exleveldb.iterator_move(it, direction) do
case Db.iterator_move(it, direction) do
{:ok, @prefix <> _ = k} ->
{:ok, decoded_key} = do_decode_key(k)
{[decoded_key], {:next, it}}
Expand All @@ -168,7 +168,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do
defp next_key({:error, _}, _direction), do: nil

defp close(nil), do: :ok
defp close(it), do: :ok == Exleveldb.iterator_close(it)
defp close(it), do: :ok == Db.iterator_close(it)

defp iterate(it, acc, f, direction, _first_key, false) do
iterate(it, acc, f, direction)
Expand All @@ -189,7 +189,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do
end

defp accumulate(it, acc, f, direction) do
case Exleveldb.iterator_move(it, direction) do
case Db.iterator_move(it, direction) do
{:ok, @prefix <> _ = k} ->
{:ok, decoded_key} = do_decode_key(k)
{:cont, f.(decoded_key, acc)}
Expand Down

0 comments on commit 7c75e16

Please sign in to comment.