<commit_before>defmodule Geolix.Server.Worker do
  @moduledoc """
  Worker module reading a database and looking up IP information.
  """

  alias Geolix.Adapter.MMDB2
  alias Geolix.Database.Loader

  use GenServer


  @behaviour :poolboy_worker

  def start_link(default \\ %{}) do
    GenServer.start_link(__MODULE__, default)
  end

  def handle_call({ :lookup, ip, opts }, _, state) do
    case opts[:where] do
      nil    -> { :reply, lookup_all(ip, opts),    state }
      _where -> { :reply, lookup_single(ip, opts), state }
    end
  end


  defp lookup_all(ip, opts) do
    databases = GenServer.call(Loader, :registered)

    lookup_all(ip, opts, databases)
  end

  defp lookup_all(_,  _,    []),       do: %{}
  defp lookup_all(ip, opts, databases) do
    databases
    |> Enum.map(fn (database) ->
	 task_opts = Keyword.put(opts, :where, database)

         { database, Task.async(fn -> lookup_single(ip, task_opts) end) }
       end)
    |> Enum.map(fn ({ database, task }) -> { database, Task.await(task) } end)
    |> Enum.into(%{})
  end

  defp lookup_single(ip, opts), do: MMDB2.lookup(ip, opts)
end<commit_msg>checks for registered database in worker<commit_after>defmodule Geolix.Server.Worker do
  @moduledoc """
  Worker module reading a database and looking up IP information.
  """

  alias Geolix.Adapter.MMDB2
  alias Geolix.Database.Loader

  use GenServer


  @behaviour :poolboy_worker

  def start_link(default \\ %{}) do
    GenServer.start_link(__MODULE__, default)
  end

  def handle_call({ :lookup, ip, opts }, _, state) do
    case opts[:where] do
      nil    -> { :reply, lookup_all(ip, opts),    state }
      _where -> { :reply, lookup_single(ip, opts), state }
    end
  end


  defp lookup_all(ip, opts) do
    databases = GenServer.call(Loader, :registered)

    lookup_all(ip, opts, databases)
  end

  defp lookup_all(_,  _,    []),       do: %{}
  defp lookup_all(ip, opts, databases) do
    databases
    |> Enum.map(fn (database) ->
	 task_opts = Keyword.put(opts, :where, database)

         { database, Task.async(fn -> lookup_single(ip, task_opts) end) }
       end)
    |> Enum.map(fn ({ database, task }) -> { database, Task.await(task) } end)
    |> Enum.into(%{})
  end

  defp lookup_single(ip, opts) do
    database = GenServer.call(Loader, { :get_database, opts[:where] })

    case database do
      nil   -> nil
      _info -> MMDB2.lookup(ip, opts)
    end
  end
end
{'fraction_non_alphanumeric': 0.1308641975308642, 'fraction_numerical': 0.0016460905349794238, 'mean_word_length': 3.0049423393739705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5802455', 'n_tokens_mistral': 853, 'n_tokens_neox': 810, 'n_words': 210}