From a216adbfe894f01bcb5c77bbe72ee00e6f203edd Mon Sep 17 00:00:00 2001 From: Steve Cohen Date: Wed, 7 Feb 2024 11:07:16 -0800 Subject: [PATCH] wal as part of a supervision tree --- .../lib/lexical/remote_control/application.ex | 17 +++++++++++------ .../search/store/backends/ets.ex | 18 +++++++++++++++--- .../search/store/backends/ets/state.ex | 5 ++--- .../search/store/backends/ets/wal.ex | 4 ++++ .../code_intelligence/references_test.exs | 1 + .../dispatch/handlers/indexer_test.exs | 1 + .../search/store/backends/ets_test.exs | 1 + .../remote_control/search/store_test.exs | 1 + 8 files changed, 36 insertions(+), 12 deletions(-) diff --git a/apps/remote_control/lib/lexical/remote_control/application.ex b/apps/remote_control/lib/lexical/remote_control/application.ex index 7ac285ed4..8670795ec 100644 --- a/apps/remote_control/lib/lexical/remote_control/application.ex +++ b/apps/remote_control/lib/lexical/remote_control/application.ex @@ -26,6 +26,8 @@ defmodule Lexical.RemoteControl.Application do RemoteControl.Plugin.Runner.Coordinator, maybe_search_store() ] + |> Enum.reject(&is_nil/1) + |> List.flatten() else [] end @@ -39,16 +41,19 @@ defmodule Lexical.RemoteControl.Application do defp maybe_search_store do if Features.indexing_enabled?() do - {RemoteControl.Search.Store, - [ - &RemoteControl.Search.Indexer.create_index/1, - &RemoteControl.Search.Indexer.update_index/2 - ]} + [ + RemoteControl.Search.Store.Backends.Ets, + {RemoteControl.Search.Store, + [ + &RemoteControl.Search.Indexer.create_index/1, + &RemoteControl.Search.Indexer.update_index/2 + ]} + ] end end defp maybe_reindex do - if Features.indexing_enabled? do + if Features.indexing_enabled?() do {RemoteControl.Commands.Reindex, nil} end end diff --git a/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets.ex b/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets.ex index e1ab6ce76..ea0269e06 100644 --- a/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets.ex +++ b/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets.ex @@ -10,8 +10,8 @@ defmodule Lexical.RemoteControl.Search.Store.Backends.Ets do @behaviour Backend @impl Backend - def new(%Project{} = project) do - start_link(project) + def new(%Project{}) do + {:ok, Process.whereis(__MODULE__)} end @impl Backend @@ -85,7 +85,19 @@ defmodule Lexical.RemoteControl.Search.Store.Backends.Ets do end def start_link(%Project{} = project) do - GenServer.start_link(__MODULE__, [project]) + GenServer.start_link(__MODULE__, [project], name: __MODULE__) + end + + def start_link do + start_link(RemoteControl.get_project()) + end + + def child_spec([%Project{}] = init_args) do + %{id: __MODULE__, start: {__MODULE__, :start_link, init_args}} + end + + def child_spec(_) do + child_spec([RemoteControl.get_project()]) end @impl GenServer diff --git a/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/state.ex b/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/state.ex index 8b4e5b4f6..e1d86ac77 100644 --- a/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/state.ex +++ b/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/state.ex @@ -52,9 +52,8 @@ defmodule Lexical.RemoteControl.Search.Store.Backends.Ets.State do end def drop(%__MODULE__{leader?: true} = state) do - with_wal state.wal_state do - :ets.delete_all_objects(state.table_name) - end + Wal.truncate(state.wal_state) + :ets.delete_all_objects(state.table_name) end def insert(%__MODULE__{leader?: true} = state, entries) do diff --git a/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/wal.ex b/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/wal.ex index 39578c731..ef0928380 100644 --- a/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/wal.ex +++ b/apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/wal.ex @@ -113,6 +113,10 @@ defmodule Lexical.RemoteControl.Search.Store.Backends.Ets.Wal do end end + def truncate(%__MODULE__{} = wal) do + :disk_log.truncate(wal.update_log) + end + def destroy(%__MODULE__{} = wal) do close(wal) destroy(wal.project, wal.schema_version) diff --git a/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs b/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs index b5c630ce0..7199cbcf3 100644 --- a/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs +++ b/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs @@ -22,6 +22,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do start_supervised!(Document.Store) start_supervised!(RemoteControl.Dispatch) + start_supervised!(Backends.Ets) start_supervised!( {Search.Store, [project, fn _ -> {:ok, []} end, fn _, _ -> {:ok, [], []} end, Backends.Ets]} diff --git a/apps/remote_control/test/lexical/remote_control/dispatch/handlers/indexer_test.exs b/apps/remote_control/test/lexical/remote_control/dispatch/handlers/indexer_test.exs index d5a0de695..60eed4e7e 100644 --- a/apps/remote_control/test/lexical/remote_control/dispatch/handlers/indexer_test.exs +++ b/apps/remote_control/test/lexical/remote_control/dispatch/handlers/indexer_test.exs @@ -22,6 +22,7 @@ defmodule Lexical.RemoteControl.Dispatch.Handlers.IndexingTest do start_supervised!(RemoteControl.Dispatch) start_supervised!(Commands.Reindex) + start_supervised!(Search.Store.Backends.Ets) start_supervised!({Search.Store, [project, create_index, update_index]}) start_supervised!({Document.Store, derive: [analysis: &Lexical.Ast.analyze/1]}) diff --git a/apps/remote_control/test/lexical/remote_control/search/store/backends/ets_test.exs b/apps/remote_control/test/lexical/remote_control/search/store/backends/ets_test.exs index 9a92899a4..99b81762e 100644 --- a/apps/remote_control/test/lexical/remote_control/search/store/backends/ets_test.exs +++ b/apps/remote_control/test/lexical/remote_control/search/store/backends/ets_test.exs @@ -51,6 +51,7 @@ defmodule Lexical.RemoteControl.Search.Store.Backend.EtsTest do defp start_supervised_store(%Project{} = project, create_fn, update_fn, backend) do start_supervised!(Dispatch) + start_supervised!(Backends.Ets) start_supervised!({Store, [project, create_fn, update_fn, backend]}) assert_eventually alive?(), 1500 Store.enable() diff --git a/apps/remote_control/test/lexical/remote_control/search/store_test.exs b/apps/remote_control/test/lexical/remote_control/search/store_test.exs index c287e76c8..46c35c02b 100644 --- a/apps/remote_control/test/lexical/remote_control/search/store_test.exs +++ b/apps/remote_control/test/lexical/remote_control/search/store_test.exs @@ -344,6 +344,7 @@ defmodule Lexical.RemoteControl.Search.StoreTest do destroy_backend(backend, project) start_supervised!(Dispatch) + start_supervised!(backend) start_supervised!({Store, [project, &default_create/1, &default_update/2, backend]}) assert_eventually alive?()