- 论坛徽章:
- 30
|
- -module(location).
- -include_lib("include/location.hrl").
- -include_lib("stdlib/include/qlc.hrl").
- -compile(export_all).
- read_txt() ->
- {ok, M} = file:open("dbip-city.csv", [binary, read]),
- read_line(M).
- read_line(M) ->
- case file:read_line(M) of
- {ok, L} -> i(L), read_line(M);
- {eof} -> ok, file:close(M)
- end.
- i(S) ->
- Slash = binary:replace(S, <<"\"">>, <<>>, [global]),
- T = erlang:binary_to_list(Slash),
- R = string:tokens(T, "\n"),
- spawn(fun () -> insert_data(R) end).
- insert_data(L) ->
- case string:tokens(lists:flatten(L), ",") of
- [Ip, Mask, Country, Area0, Area1, Area2] ->
- Row = #location{ip = Ip, mask = Mask, country = Country,
- area = Area0, city = lists:concat([Area1, Area2])};
- [Ip, Mask, Country, Area, City] ->
- Row = #location{ip = Ip, mask = Mask, country = Country,
- area = Area, city = City};
- [Ip, Mask, Country, Area] ->
- Row = #location{ip = Ip, mask = Mask, country = Country,
- area = Area};
- [Ip, Mask, Country] ->
- Row = #location{ip = Ip, mask = Mask, country = Country}
- end,
- db_dirty_write(Row).
- init_table() ->
- mnesia:start(),
- mnesia:delete_table(location),
- mnesia:create_table(location,
- [{attributes, record_info(fields, location)},
- {disc_only_copies, [node()]}]),
- mnesia:change_config(extra_db_nodes, [b]),
- mnesia:add_table_copy(location, nodes(),
- disc_only_copies),
- read_txt().
- address(Ip) ->
- MatchHead = #location{ip = '$4', mask = '$5',
- country = '$1', area = '$2', city = '$3'},
- Guard = [{'>=', '$4', Ip}, {'=<', '$5', Ip}],
- Result = ['$4', '$5', '$1', '$2', '$3'],
- MatchSpec = [{MatchHead, Guard, Result}],
- SelFun = fun () ->
- mnesia:select(location, MatchSpec, 10, read)
- end,
- mnesia:activity(transaction, SelFun, [], mnesia_frag).
- query(Ip) ->
- Query = fun () ->
- Q = qlc:q([E
- || E <- mnesia:table(location),
- E#location.ip =< Ip]),
- Sort = qlc:e(qlc:keysort(2, Q, [{order, descending}])),
- QC = qlc:cursor(Sort),
- qlc:next_answers(QC, 1)
- end,
- {atomic, Results} = mnesia:transaction(Query),
- Results.
- q(Ip) ->
- Query = fun () ->
- qlc:eval(qlc:q([[E#location.ip, E#location.mask,
- E#location.country, E#location.area,
- E#location.city]
- || E <- mnesia:table(location),
- (Ip =< E#location.mask) and
- (E#location.ip =< Ip)]))
- end,
- {atomic, Results} = mnesia:transaction(Query),
- Results.
- q1() -> mnesia:dirty_read({location, "195.101.114.0"}).
- q2() ->
- mnesia:match_object({location, "195.101.114.0"}).
- db() ->
- mnesia:create_table(location,
- [{frag_properties,
- [{n_fragments, 20}, {n_disc_copies, 1},
- {node_pool, [node()]}]},
- %{index, [ip, mask, country,area]},
- {attributes, record_info(fields, location)}]).
- db_dirty_write(Row) ->
- AddFun = fun () -> mnesia:write(location, Row, write)
- end,
- ok = mnesia:activity(async_dirty, AddFun, [],
- mnesia_frag).
复制代码 |
|