Skip to content

Commit

Permalink
[RTI-7894] Move the project into rebar3 and use all the goodies that …
Browse files Browse the repository at this point in the history
…come with it (#3)

* Move the project into rebar3 and use all the goodies that come with it

* Some formatting improvements
  • Loading branch information
Brujo Benavides authored Apr 22, 2020
1 parent a553c5d commit 762ffcc
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 277 deletions.
6 changes: 5 additions & 1 deletion .buildkite/pipelines/geodata2-pr-builder/start.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
- label: 'Test Build'
command:
- if [ ! -z "$MERGE_BRANCH" ]; then git merge $MERGE_BRANCH -m "merging $MERGE_BRANCH"; fi
- make test
- curl https://rebar3.s3.amazonaws.com/rebar3 -o ./rebar3
- chmod +x rebar3
- aws s3 sync s3://adroll-misc/rtb/geodata2/ . --exclude "*" --include "*plt" --no-follow-symlinks
- ./rebar3 test
- aws s3 sync . s3://adroll-misc/rtb/geodata2/ --exclude "*" --include "*plt" --no-follow-symlinks
agents:
queue: 'rtb'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
_build/
*~
/.eunit
/_test
Expand Down
32 changes: 32 additions & 0 deletions elvis.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
elvis,
[
{config,
[#{dirs => ["src"],
filter => "*.erl",
ruleset => erl_files,
rules => [
{elvis_style, dont_repeat_yourself, #{min_complexity => 40}},
{elvis_style, line_length, #{limit => 150}},
{elvis_style, invalid_dynamic_call, #{ignore => [geodata2]}}
]
},
#{dirs => ["test"],
filter => "*.erl",
ruleset => erl_files,
rules => [
{elvis_style, dont_repeat_yourself, #{min_complexity => 40}},
%% Variables in eunit macros are called, for instance, __V
{elvis_style, variable_naming_convention, #{regex => "^_?_?([A-Z][0-9a-zA-Z]*)_?$"}}
]
},
#{dirs => ["."],
filter => "elvis.config",
ruleset => elvis_config
}
]
}
]
}
].
21 changes: 16 additions & 5 deletions include/geodata2.hrl
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
%%-define(BINARY_COPY(Val), Val) %% - faster, less memory while you don't reload files. If you do reload you can potentially prevent old binaries from getting garbage collected
-define(BINARY_COPY(Val), binary:copy(Val)). %% bit slower, more memory, but allows garbage collecting old large binaries with ease

-define(BINARY_COPY(Val),
binary:copy(Val)). %% bit slower, more memory, but allows garbage collecting old large binaries with ease
-define(IPV6, 6).
-define(IPV4, 4).

-define(GEODATA2_STATE_TID, geodata2_state).

-record(meta, {descr, database_type, languages, timestamp, ip_version, record_size, node_count, vsn, whole, remdr, tree_size, data_start, v4_start}).

-record(meta,
{descr,
database_type,
languages,
timestamp,
ip_version,
record_size,
node_count,
vsn,
whole,
remdr,
tree_size,
data_start,
v4_start}).
-record(geocity, {country, city, city_geoid, long, lat}).

-type geocity() :: #geocity{}.
44 changes: 42 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
% -*- mode: erlang -*-
{clean_files, ["ebin/*.beam", "ebin/egeoip.app", "doc/", "_test"]}.
{erl_opts, [debug_info]}.
{erl_opts, [
warn_unused_vars,
warn_export_all,
warn_shadow_vars,
warn_unused_import,
warn_unused_function,
warn_bif_clash,
warn_unused_record,
warn_deprecated_function,
warn_obsolete_guard,
strict_validation,
warn_export_vars,
warn_exported_vars,
warnings_as_errors
]}.

{cover_enabled, true}.
{cover_opts, [verbose]}.
{cover_print_enabled, true}.

{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
{dialyzer_opts, [{plt, "_test/dialyzer_plt"}, src]}.

{dialyzer, [
{warnings, [unknown, no_return, error_handling]},
{plt_apps, top_level_deps},
{plt_extra_apps, []},
{plt_location, local},
{base_plt_apps, [erts, stdlib, kernel]},
{base_plt_location, global}
]}.

{xref_checks,[
undefined_function_calls,
locals_not_used,
deprecated_function_calls,
deprecated_functions
]}.

{alias, [{test, [format, lint, xref, dialyzer, eunit, cover]}]}.

{format, [
{files, ["src/*.erl", "test/*.erl", "include/*.hrl"]}
]}.

{plugins, [rebar3_lint, rebar3_format]}.
1 change: 1 addition & 0 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[].
86 changes: 42 additions & 44 deletions src/geodata2.erl
Original file line number Diff line number Diff line change
@@ -1,60 +1,64 @@
-module(geodata2).

-behavior(gen_server).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-export([init/1, handle_call/3, handle_cast/2]).
%% API
-export([lookup/1, start/0, start_link/1, stop/0, get_env/2, id/1]).

-include("geodata2.hrl").

-record(state, {}).

-spec lookup(any()) -> {ok, Result::list()} | not_found | {error, Reason :: term()}.
-type state() :: #state{}.

-spec lookup(any()) -> {ok, Result :: list()} | not_found | {error, Reason :: term()}.
lookup(IP) ->
[{data, Data}] = ets:lookup(?GEODATA2_STATE_TID, data),
[{meta, Meta}] = ets:lookup(?GEODATA2_STATE_TID, meta),
case geodata2_ip:make_ip(IP) of
{ok, Bits, IPV} ->
geodata2_format:lookup(Meta, Data, Bits, IPV);
{error, Reason} ->
{error, Reason}
{ok, Bits, IPV} ->
geodata2_format:lookup(Meta, Data, Bits, IPV);
{error, Reason} ->
{error, Reason}
end.

start() ->
application:start(geodata2).
application:start(geodata2).

start_link(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [], []).

stop() ->
application:stop(geodata2).
application:stop(geodata2).

new(File) ->
case filelib:is_file(File) of
true ->
{ok, RawData} = file:read_file(File),
Data = case is_compressed(File) of
true ->
zlib:gunzip(RawData);
false ->
RawData
end,
{ok, Meta} = geodata2_format:meta(Data),
ets:new(?GEODATA2_STATE_TID, [set, protected, named_table, {read_concurrency, true}]),
ets:insert(?GEODATA2_STATE_TID, {data, Data}),
ets:insert(?GEODATA2_STATE_TID, {meta, Meta}),
{ok, #state{}};
_ ->
{stop, {geodata2_dbfile_not_found, File}}
true ->
{ok, RawData} = file:read_file(File),
Data = case is_compressed(File) of
true ->
zlib:gunzip(RawData);
false ->
RawData
end,
{ok, Meta} = geodata2_format:meta(Data),
ets:new(?GEODATA2_STATE_TID, [set, protected, named_table, {read_concurrency, true}]),
ets:insert(?GEODATA2_STATE_TID, {data, Data}),
ets:insert(?GEODATA2_STATE_TID, {meta, Meta}),
{ok, #state{}};
_ ->
{stop, {geodata2_dbfile_not_found, File}}
end.

-spec init(_) -> {ok, state()} | {stop, tuple()}.
init(_Args) ->
case get_env(geodata2, dbfile) of
{ok, File} ->
new(File);
_ ->
{stop, {geodata2_dbfile_unspecified}}
{ok, File} ->
new(File);
_ ->
{stop, {geodata2_dbfile_unspecified}}
end.

handle_call(_What, _From, State) ->
Expand All @@ -63,33 +67,27 @@ handle_call(_What, _From, State) ->
handle_cast(_Request, State) ->
{noreply, State}.

handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

get_env(App, Key) ->
{ConfigModule, ConfigFun} = case application:get_env(geodata2, config_interp) of
{ok, {Cm, Cf}} -> {Cm, Cf};
_ -> {?MODULE, id}
{ok, {Cm, Cf}} ->
{Cm, Cf};
_ ->
{?MODULE, id}
end,
case application:get_env(App, Key) of
{ok, Value} ->
{ok, ConfigModule:ConfigFun(Value)};
Other ->
Other
{ok, Value} ->
{ok, ConfigModule:ConfigFun(Value)};
Other ->
Other
end.

%% this is used to return app env values as-is when config_interp is not set:
id(X) -> X.
id(X) ->
X.

is_compressed(Filename) ->
<<".gz">> == iolist_to_binary(filename:extension(Filename)).
5 changes: 3 additions & 2 deletions src/geodata2_app.erl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
-module(geodata2_app).

-behaviour(application).

-export([start/2]).
-export([stop/1]).

start(_Type, _Args) ->
geodata2_sup:start_link().
geodata2_sup:start_link().

stop(_State) ->
ok.
ok.
Loading

0 comments on commit 762ffcc

Please sign in to comment.