-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prefix search functionality to the search store (#632)
* Add prefix search functionality to the search store ***Description:*** This commit adds the `prefix` function to the `Lexical.RemoteControl.Search.Store` module, allowing for searching entries by prefix. It also implements the `find_by_prefix` callback in the `Lexical.RemoteControl.Search.Store.Backends.Ets` module. ***Changes:*** - Added `prefix` function to `Lexical.RemoteControl.Search.Store` - Implemented `find_by_prefix` callback in `Lexical.RemoteControl.Search.Store.Backends.Ets` * Revert `to_subject_charlist` to `to_subject` * Add a comment for `to_prefix` * Refine the implementation logic for prefix search. Utilize List.pop_at directly. * Revert the `Schemas.V2` changes, use V3 to do the force migiration * Update apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/state.ex Co-authored-by: Steve Cohen <[email protected]> --------- Co-authored-by: Steve Cohen <[email protected]>
- Loading branch information
Showing
7 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
apps/remote_control/lib/lexical/remote_control/search/store/backends/ets/schemas/v3.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule Lexical.RemoteControl.Search.Store.Backends.Ets.Schemas.V3 do | ||
alias Lexical.RemoteControl.Search.Indexer.Entry | ||
alias Lexical.RemoteControl.Search.Store.Backends.Ets.Schema | ||
|
||
require Entry | ||
use Schema, version: 3 | ||
|
||
defkey :by_id, [:id, :type, :subtype] | ||
|
||
defkey :by_subject, [ | ||
:subject, | ||
:type, | ||
:subtype, | ||
:path | ||
] | ||
|
||
defkey :by_path, [:path] | ||
defkey :by_block_id, [:block_id, :path] | ||
defkey :structure, [:path] | ||
|
||
def migrate(entries) do | ||
migrated = | ||
entries | ||
|> Stream.filter(fn | ||
{query_by_subject(), %Entry{}} -> true | ||
_ -> false | ||
end) | ||
|> Stream.map(fn {_, entry} -> entry end) | ||
|> Schema.entries_to_rows(__MODULE__) | ||
|
||
{:ok, migrated} | ||
end | ||
|
||
def to_rows(%Entry{} = entry) when Entry.is_structure(entry) do | ||
structure_key = structure(path: entry.path) | ||
[{structure_key, entry.subject}] | ||
end | ||
|
||
def to_rows(%Entry{} = entry) do | ||
subject_key = | ||
by_subject( | ||
subject: to_subject(entry.subject), | ||
type: entry.type, | ||
subtype: entry.subtype, | ||
path: entry.path | ||
) | ||
|
||
id_key = | ||
by_id( | ||
id: entry.id, | ||
type: entry.type, | ||
subtype: entry.subtype | ||
) | ||
|
||
path_key = by_path(path: entry.path) | ||
block_key = by_block_id(path: entry.path, block_id: entry.block_id) | ||
|
||
[{id_key, entry}, {subject_key, id_key}, {path_key, id_key}, {block_key, id_key}] | ||
end | ||
|
||
# This case will handle any namespaced entries | ||
def to_rows(%{type: _, subtype: _, id: _} = entry) do | ||
map = Map.delete(entry, :__struct__) | ||
|
||
Entry | ||
|> struct(map) | ||
|> to_rows() | ||
end | ||
|
||
def table_options do | ||
[:named_table, :ordered_set, :compressed] | ||
end | ||
|
||
def to_subject(charlist) when is_list(charlist), do: charlist | ||
def to_subject(:_), do: :_ | ||
def to_subject(atom) when is_atom(atom), do: atom |> inspect() |> to_charlist() | ||
def to_subject(other), do: to_charlist(other) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters