fix: `nil` in `OrderedCache` (#10647)

* fix: `nil` in `OrderedCache`

* Update CHANGELOG

---------

Co-authored-by: Viktor Baranov <baranov.viktor.27@gmail.com>
pull/10656/head
Maxim Filonov 3 months ago committed by Viktor Baranov
parent 6bf68baee8
commit 7eac1ed967
  1. 1
      CHANGELOG.md
  2. 6
      apps/explorer/lib/explorer/chain.ex
  3. 2
      apps/explorer/lib/explorer/chain/address.ex
  4. 21
      apps/explorer/lib/explorer/chain/ordered_cache.ex

@ -31,6 +31,7 @@
### 🐛 Bug Fixes
- nil in OrderedCache ([#10647](https://github.com/blockscout/blockscout/pull/10647))
- Fix for metadata detection at ipfs protocol([#10646](https://github.com/blockscout/blockscout/pull/10646))
- Change default shrink internal_transactions table migration params ([#10644](https://github.com/blockscout/blockscout/pull/10644))
- Fix bug in update_replaced_transactions query ([#10634](https://github.com/blockscout/blockscout/issues/10634))

@ -1638,7 +1638,7 @@ defmodule Explorer.Chain do
end
defp block_from_cache(block_type, paging_options, necessity_by_association, options) do
case Blocks.take_enough(paging_options.page_size) do
case Blocks.atomic_take_enough(paging_options.page_size) do
nil ->
elements = fetch_blocks(block_type, paging_options, necessity_by_association, options)
@ -1652,7 +1652,7 @@ defmodule Explorer.Chain do
end
def uncles_from_cache(block_type, paging_options, necessity_by_association, options) do
case Uncles.take_enough(paging_options.page_size) do
case Uncles.atomic_take_enough(paging_options.page_size) do
nil ->
elements = fetch_blocks(block_type, paging_options, necessity_by_association, options)
@ -2620,7 +2620,7 @@ defmodule Explorer.Chain do
if is_nil(paging_options.key) or paging_options.page_number == 1 do
paging_options.page_size
|> Kernel.+(1)
|> Transactions.take_enough()
|> Transactions.atomic_take_enough()
|> case do
nil ->
transactions = fetch_recent_collated_transactions_for_rap(paging_options, necessity_by_association)

@ -328,7 +328,7 @@ defmodule Explorer.Chain.Address do
if is_nil(paging_options.key) do
paging_options.page_size
|> Accounts.take_enough()
|> Accounts.atomic_take_enough()
|> case do
nil ->
get_addresses(options)

@ -112,6 +112,11 @@ defmodule Explorer.Chain.OrderedCache do
"""
@callback take_enough(integer()) :: [element] | nil
@doc """
Behaves like `take_enough/1`, but addresses [#10445](https://github.com/blockscout/blockscout/issues/10445).
"""
@callback atomic_take_enough(integer()) :: [element] | nil
@doc """
Adds an element, or a list of elements, to the cache.
When the cache is full, only the most prevailing elements will be stored, based
@ -204,6 +209,22 @@ defmodule Explorer.Chain.OrderedCache do
end
end
@impl OrderedCache
def atomic_take_enough(amount) do
items =
cache_name()
|> ConCache.ets()
|> :ets.tab2list()
if amount <= Enum.count(items) - 1 do
items
|> Enum.reject(fn {key, _value} -> key == ids_list_key() end)
|> Enum.sort(&prevails?/2)
|> Enum.take(amount)
|> Enum.map(fn {_key, value} -> value end)
end
end
### Updating function
def remove_deleted_from_index({:delete, _cache_pid, id}) do

Loading…
Cancel
Save