Skip to content

Commit 7e03425

Browse files
add create and show fns for search schema cli updates
1 parent c972760 commit 7e03425

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

include/yokozuna.hrl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
-type ring_event() :: {ring_event, riak_core_ring:riak_core_ring()}.
9090
-type event() :: ring_event().
9191

92+
%% @doc
93+
-type schema_err() :: {error, string()}.
94+
9295
%% @doc The `component()' type represents components that may be
9396
%% enabled or disabled at runtime. Typically a component is
9497
%% disabled in a live, production cluster in order to isolate
@@ -105,7 +108,6 @@
105108
%% action to manually index the missing data or wait for AAE to
106109
%% take care of it.
107110
-type component() :: search | index.
108-
109111
%%%===================================================================
110112
%%% Macros
111113
%%%===================================================================

src/yz_console.erl

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
-module(yz_console).
2121
-include("yokozuna.hrl").
2222
-export([aae_status/1,
23-
switch_to_new_search/1]).
23+
switch_to_new_search/1,
24+
create_schema/1,
25+
show_schema/1,
26+
add_to_schema/1,
27+
remove_from_schema/1]).
2428

2529
%% @doc Print the Active Anti-Entropy status to stdout.
2630
-spec aae_status([]) -> ok.
@@ -51,3 +55,64 @@ switch_to_new_search([]) ->
5155
io:format(standard_error, "The following nodes could not be reached: ~s", [DownStr]),
5256
{error, {nodes_down, Down}}
5357
end.
58+
59+
%% @doc Creates (and overrides) schema for name and file path.
60+
-spec create_schema([string()|string()]) -> ok | schema_err().
61+
create_schema([Name, Path]) ->
62+
try
63+
RawSchema = read_schema(Path),
64+
FMTName = list_to_atom(Name),
65+
case yz_schema:store(list_to_binary(Name), RawSchema) of
66+
ok ->
67+
io:format("~p schema created~n", [FMTName]),
68+
ok;
69+
{error, _} ->
70+
io:format("Error creating schema ~p", [FMTName]),
71+
error
72+
end
73+
catch fileReadError:exitError ->
74+
exitError
75+
end.
76+
77+
%% @doc Shows solr schema for name passed in.
78+
-spec show_schema([string()]) -> ok | schema_err().
79+
show_schema([Name]) ->
80+
FMTName = list_to_atom(Name),
81+
case yz_schema:get(list_to_binary(Name)) of
82+
{ok, R} ->
83+
io:format("Schema ~p:~n~s", [FMTName, binary_to_list(R)]),
84+
ok;
85+
{error, notfound} ->
86+
io:format("Schema ~p doesn't exist~n", [FMTName]),
87+
error
88+
end.
89+
90+
%% @doc
91+
-spec add_to_schema([string()|string()]) -> ok | schema_err().
92+
add_to_schema([Name|Opts]) ->
93+
{Name, Opts}.
94+
95+
%% @doc
96+
-spec remove_from_schema([string()|string()]) -> ok | schema_err().
97+
remove_from_schema([Name, FieldName]) ->
98+
{Name, FieldName}.
99+
100+
%%%===================================================================
101+
%%% Private
102+
%%%===================================================================
103+
104+
%% @doc Reads and returns `RawSchema` from file path.
105+
-spec read_schema(string()) -> raw_schema() | schema_err().
106+
read_schema(Path) ->
107+
AbsPath = filename:absname(Path),
108+
case file:read_file(AbsPath) of
109+
{ok, RawSchema} ->
110+
RawSchema;
111+
{error, enoent} ->
112+
io:format("No such file or directory: ~s~n", [Path]),
113+
throw({fileReadError, enoent});
114+
{error, Reason} ->
115+
?ERROR("Error reading file ~s:~p", [Path, Reason]),
116+
io:format("Error reading file ~s, see log for details~n", [Path]),
117+
throw({fileReadError, Reason})
118+
end.

src/yz_schema.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
-compile(export_all).
2525

2626
-define(SCHEMA_VSN, "1.5").
27-
-type schema_err() :: {error, string()}.
2827

2928
%%%===================================================================
3029
%%% API

0 commit comments

Comments
 (0)