Skip to content
Permalink
Browse files
Add 'src/ken/' from commit '506606ff2b29f72fcc9e875f1b9a33c258ad2af1'
git-subtree-dir: src/ken
git-subtree-mainline: 8d9f80d
git-subtree-split: 506606f
  • Loading branch information
kocolosk committed Nov 22, 2019
2 parents 8d9f80d + 506606f commit 752e8c4b1102455382cd395ee3e58af7008792ba
Show file tree
Hide file tree
Showing 10 changed files with 886 additions and 0 deletions.
@@ -0,0 +1,12 @@
ken
===

Ken builds views and search indexes. Automatically.

#### Overview

When the couch\_db\_update event is triggered with an `updated` event, ken will spawn indexing jobs for view groups and search indexes (one job per view group shard or search index shard). If a `deleted` event is triggered, all jobs associated with the corresponding database shard will be removed.

#### Testing

Testing for ken expected to be executed from the top level `couchdb` repo as a part of `make check` run. The isolated ken test could be ran as `rebar eunit apps=ken verbose=1` from the `couchdb`'s root directory.
@@ -0,0 +1,26 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

HaveDreyfus = element(1, file:list_dir("../dreyfus")) == ok.
HaveHastings = element(1, file:list_dir("../hastings")) == ok.

CurrOpts = case lists:keyfind(erl_opts, 1, CONFIG) of
{erl_opts, Opts} -> Opts;
false -> []
end,

NewOpts =
if HaveDreyfus -> [{d, 'HAVE_DREYFUS'}]; true -> [] end ++
if HaveHastings -> [{d, 'HAVE_HASTINGS'}]; true -> [] end ++
[{i, "../"}] ++ CurrOpts.

lists:keystore(erl_opts, 1, CONFIG, {erl_opts, NewOpts}).
@@ -0,0 +1,38 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

HaveDreyfus = code:lib_dir(dreyfus) /= {error, bad_name}.
HaveHastings = code:lib_dir(hastings) /= {error, bad_name}.

BaseApplications = [
kernel,
stdlib,
couch_log,
couch_event,
couch,
config
].

Applications =
if HaveDreyfus -> [dreyfus]; true -> [] end ++
if HaveHastings -> [hastings]; true -> [] end ++
BaseApplications.

{application, ken,
[
{description, ""},
{vsn, git},
{registered, []},
{applications, Applications},
{mod, { ken_app, []}},
{env, []}
]}.
@@ -0,0 +1,29 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(ken).

-export([add/1]).
-export([remove/1]).
-export([add_all_shards/1]).

% Add a database shard to be indexed.
add(DbName) ->
ken_server:add(DbName).

% Remove all pending jobs for a database shard.
remove(DbName) ->
ken_server:remove(DbName).

% Add all shards for a database to be indexed.
add_all_shards(DbName) ->
ken_server:add_all_shards(DbName).
@@ -0,0 +1,28 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(ken_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
ken_sup:start_link().

stop(_State) ->
ok.
@@ -0,0 +1,56 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(ken_event_handler).
-behaviour(couch_event_listener).

-export([
start_link/0
]).

-export([
init/1,
terminate/2,
handle_event/3,
handle_cast/2,
handle_info/2
]).


start_link() ->
couch_event_listener:start_link(?MODULE, nil, [all_dbs]).

%% couch_event_listener callbacks

init(_) ->
{ok, nil}.

terminate(_Reason, _State) ->
ok.

handle_event(DbName, updated, State) ->
ken:add(DbName),
{ok, State};
handle_event(DbName, deleted, State) ->
ken:remove(DbName),
{ok, State};
handle_event(DbName, ddoc_updated, State) ->
ken:add_all_shards(DbName),
{ok, State};
handle_event(_DbName, _Event, State) ->
{ok, State}.

handle_cast(_Msg, State) ->
{ok, State}.

handle_info(_Msg, State) ->
{ok, State}.

0 comments on commit 752e8c4

Please sign in to comment.