defmodule Ibex.Contracts.Storage do use GenServer require Logger @initial_state %{contracts: %{}} def start_link(_) do Logger.info("Starting Inbound as a supervised process...") GenServer.start_link(__MODULE__, @initial_state, name: __MODULE__) end def init(state) do {:ok, table_name} = :dets.open_file(:disk_storage, [type: :set]) state = Map.put(state, :dets_table, table_name) {:ok, state} end def handle_info({:add_contract, contract_description}, state) do symbol = contract_description.contract.symbol case :dets.lookup(state[:dets_table], symbol) do [{^symbol, value}] -> Logger.info("Appending contract to symbol #{symbol}") value = Map.put(value, contract_description.contract.con_id, contract_description) :dets.insert(state[:dets_table], {symbol, value}) [] -> Logger.info("First contract for symbol #{contract_description.contract.symbol}") :dets.insert(state[:dets_table], {contract_description.contract.symbol, %{}}) end {:noreply, state} end def handle_call({:get_table_name}, _from, state) do {:reply, state[:dets_table], state} end end