2626 add_to_schema /1 ,
2727 remove_from_schema /1 ]).
2828
29+ -type field_data () :: {dynamicfield | field , list (), list ()}.
30+
31+ -define (LIST_TO_ATOM (L ), list_to_atom (L )).
32+ -define (LIST_TO_BINARY (L ), list_to_binary (L )).
33+ -define (FIELD_DEFAULTS , [{type , " text_general" },
34+ {indexed , " true" },
35+ {stored , " false" },
36+ {multiValued , " true" }]).
37+
2938% % @doc Print the Active Anti-Entropy status to stdout.
3039-spec aae_status ([]) -> ok .
3140aae_status ([]) ->
@@ -45,62 +54,109 @@ aae_status([]) ->
4554% % back without restarting the cluster.
4655-spec switch_to_new_search ([]) -> ok | {error , {nodes_down , [node ()]}}.
4756switch_to_new_search ([]) ->
48- {_Good , Down } = riak_core_util :rpc_every_member_ann (yokozuna , switch_to_yokozuna , [], 5000 ),
57+ {_Good , Down } = riak_core_util :rpc_every_member_ann (
58+ yokozuna , switch_to_yokozuna , [], 5000 ),
4959 case Down of
5060 [] ->
5161 ok ;
5262 _ ->
5363 Down2 = [atom_to_list (Node ) || Node <- Down ],
5464 DownStr = string :join (Down2 , " " ),
55- io : format ( standard_error , " The following nodes could not be reached: ~s " , [DownStr ]),
65+ ? ERROR ( " The following nodes could not be reached: ~s " , [DownStr ]),
5666 {error , {nodes_down , Down }}
5767 end .
5868
5969% % @doc Creates (and overrides) schema for name and file path.
60- -spec create_schema ([string ()|string ()]) -> ok | schema_err ().
70+ % % riak-admin search schema create <schema name> <path to file>
71+ -spec create_schema ([string ()|string ()]) -> ok | error .
6172create_schema ([Name , Path ]) ->
6273 try
6374 RawSchema = read_schema (Path ),
64- FMTName = list_to_atom (Name ),
65- case yz_schema :store (list_to_binary (Name ), RawSchema ) of
75+ FMTName = ? LIST_TO_ATOM (Name ),
76+ case yz_schema :store (? LIST_TO_BINARY (Name ), RawSchema ) of
6677 ok ->
6778 io :format (" ~p schema created~n " , [FMTName ]),
6879 ok ;
6980 {error , _ } ->
70- io :format (" Error creating schema ~p " , [FMTName ]),
81+ io :format (" Error creating schema ~p~n " , [FMTName ]),
7182 error
7283 end
73- catch fileReadError : exitError ->
74- exitError
84+ catch { fileReadError , _ } ->
85+ error
7586 end .
7687
7788% % @doc Shows solr schema for name passed in.
78- -spec show_schema ([string ()]) -> ok | schema_err ().
89+ % % riak-admin search schema show <name of schema>
90+ -spec show_schema ([string ()]) -> ok | error .
7991show_schema ([Name ]) ->
80- FMTName = list_to_atom (Name ),
81- case yz_schema :get (list_to_binary (Name )) of
92+ FMTName = ? LIST_TO_ATOM (Name ),
93+ case yz_schema :get (? LIST_TO_BINARY (Name )) of
8294 {ok , R } ->
83- io :format (" Schema ~p :~n~s " , [FMTName , binary_to_list (R )]),
95+ io :format (" Schema ~p :~n~s~n " , [FMTName , binary_to_list (R )]),
8496 ok ;
8597 {error , notfound } ->
86- io :format (" Schema ~p doesn't exist~n " , [FMTName ]),
98+ io :format (" Schema ~p not found~n " , [FMTName ]),
99+ error
100+ end .
101+
102+ % % @doc Adds field of <fieldname> to schema contents.
103+ % % riak-admin search schema <name> add field|dynamicfield <fieldname> [<option>=<value> [...]]
104+ -spec add_to_schema ([string ()|string ()]) -> ok | error .
105+ add_to_schema ([Name ,FieldType ,FieldName |Options ]) ->
106+ try
107+ ParsedOptions = [{? LIST_TO_ATOM (K ), V } || {K , V } <- parse_options (Options )],
108+ Field = make_field (? LIST_TO_ATOM (FieldType ), FieldName , ParsedOptions ),
109+ % % TODO: Wrap *update_schema* in a Case to check for ok|error
110+ update_schema (add , Name , Field ),
111+ ok
112+ catch {error , {invalid_option , Option }} ->
113+ io :format (" Invalid Option: ~p~n " , [? LIST_TO_ATOM (Option )]),
87114 error
88115 end .
89116
90- % % @doc
91- -spec add_to_schema ([string ()|string ()]) -> ok | schema_err ().
92- add_to_schema ([Name |Opts ]) ->
93- {Name , Opts }.
94117
95- % % @doc
96- -spec remove_from_schema ([string ()|string ()]) -> ok | schema_err ().
118+ % % @doc Removes field of <fieldname> from schema contents.
119+ % % riak-admin search schema <name> remove <fieldname>
120+ -spec remove_from_schema ([string ()|string ()]) -> ok | error .
97121remove_from_schema ([Name , FieldName ]) ->
98- {Name , FieldName }.
122+ % % TODO: Wrap Update in a Case to check for ok|error
123+ update_schema (remove , Name , FieldName ),
124+ ok .
99125
100126% %%===================================================================
101127% %% Private
102128% %%===================================================================
103129
130+ % % @doc Create field tagged tuple.
131+ -spec make_field (atom (), string (), list ()) -> field_data ().
132+ make_field (dynamicfield , FieldName , Options ) ->
133+ {dynamicfield , [{name , FieldName }|merge (Options , ? FIELD_DEFAULTS )], []};
134+ make_field (field , FieldName , Options ) ->
135+ {field , [{name , FieldName }|merge (Options , ? FIELD_DEFAULTS )], []}.
136+
137+ % % @doc Update schema with change(s).
138+ -spec update_schema (add | remove , string (), field_data () | string ()) ->
139+ ok | schema_err ().
140+ update_schema (add , Name , Field ) ->
141+ {Name , Field };
142+ update_schema (remove , Name , FieldName ) ->
143+ {Name , FieldName }.
144+
145+ % % TODO: Use Riak-Cli or place *parse_options* in a one place for all consoles
146+ -spec parse_options (list (string ())) -> list ({string (), string ()}).
147+ parse_options (Options ) ->
148+ parse_options (Options , []).
149+
150+ parse_options ([], Acc ) ->
151+ Acc ;
152+ parse_options ([H |T ], Acc ) ->
153+ case re :split (H , " =" , [{parts , 2 }, {return , list }]) of
154+ [Key , Value ] when is_list (Key ), is_list (Value ) ->
155+ parse_options (T , [{string :to_lower (Key ), Value }|Acc ]);
156+ _Other ->
157+ throw ({error , {invalid_option , H }})
158+ end .
159+
104160% % @doc Reads and returns `RawSchema` from file path.
105161-spec read_schema (string ()) -> raw_schema () | schema_err ().
106162read_schema (Path ) ->
@@ -116,3 +172,9 @@ read_schema(Path) ->
116172 io :format (" Error reading file ~s , see log for details~n " , [Path ]),
117173 throw ({fileReadError , Reason })
118174 end .
175+
176+ % % @doc Merge field defaults.
177+ -spec merge ([{atom (), any ()}], [{atom (), any ()}]) -> [{atom (), any ()}].
178+ merge (Overriding , Other ) ->
179+ lists :ukeymerge (1 , lists :ukeysort (1 , Overriding ),
180+ lists :ukeysort (1 , Other )).
0 commit comments