zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

ets_table_helper.ex (1222B)


      1 defmodule Credo.Service.ETSTableHelper do
      2   @moduledoc false
      3 
      4   defmacro __using__(_opts \\ []) do
      5     quote do
      6       use GenServer
      7 
      8       @timeout 60_000
      9 
     10       alias Credo.Service.ETSTableHelper
     11 
     12       @table_name __MODULE__
     13 
     14       def start_link(opts \\ []) do
     15         {:ok, _pid} = GenServer.start_link(__MODULE__, opts, name: __MODULE__)
     16       end
     17 
     18       def get(source_file) do
     19         hash = source_file.hash
     20 
     21         case :ets.lookup(@table_name, hash) do
     22           [{^hash, value}] ->
     23             {:ok, value}
     24 
     25           [] ->
     26             :notfound
     27         end
     28       end
     29 
     30       def put(source_file, value) do
     31         GenServer.call(__MODULE__, {:put, source_file.hash, value}, @timeout)
     32       end
     33 
     34       # callbacks
     35 
     36       def init(opts), do: ETSTableHelper.init(@table_name, opts)
     37 
     38       def handle_call(msg, from, current_state),
     39         do: ETSTableHelper.handle_call(@table_name, msg, from, current_state)
     40     end
     41   end
     42 
     43   def init(table_name, _) do
     44     ets = :ets.new(table_name, [:named_table, read_concurrency: true])
     45 
     46     {:ok, ets}
     47   end
     48 
     49   def handle_call(table_name, {:put, hash, value}, _from, current_state) do
     50     :ets.insert(table_name, {hash, value})
     51 
     52     {:reply, value, current_state}
     53   end
     54 end