anchor.diff:              end;
           _ -> error
       end.
  +
  +save(IP, ISP, Country, Province, City, County) ->
  +    ISP_id      = ask_geoman(isp, ISP),
  +    Country_id  = ask_geoman(country, Country),
  +    Province_id = ask_geoman(province, Province),
  +    City_id     = ask_geoman(city, City),
  +    County_id   = ask_geoman(county, County),
  +    {ok, {A, B, C, D}} = inet:parse_ipv4_address(IP),
  +    ok.
  +
  +http_response(Code, Json) ->
  +    Status = case Code of
  +        200 -> "HTTP/1.1 200 OK\r\nServer: Erlang\r\n";
  +        400 -> "HTTP/1.1 BAD REQUEST\r\nServer: Erlang\r\n"
  +    end,
  +    {{Year, Month, Mday}, {Hour, Min, Sec}} = erlang:universaltime(),
  +    Wday = calendar:day_of_the_week({Year, Month, Mday}),
  +    Args = [lists:nth(Wday, weekdays()), Mday, lists:nth(Month, months()),
  +            Year, Hour, Min, Sec],
  +    Date = io_lib:format("Date: ~s, ~b ~s ~b ~b:~b:~b GMT\r\n", Args),
  +    lists:concat([Status, Date,
  +                  "Content-Type: application/json\r\n",
  +                  "Transfer-Encoding: chunked\r\n",
  +                  "Connection: close\r\n\r\n",
  +                  Json]).
  +
  +id_to_name(ISP_id, Country_id, Province_id, City_id, County_id) ->
  +    [{_, ISP}]      = ets:lookup(?GEO_TABLE, {isp,      ISP_id}),
  +    [{_, Country}]  = ets:lookup(?GEO_TABLE, {country,  Country_id}),
  +    [{_, Province}] = ets:lookup(?GEO_TABLE, {province, Province_id}),
  +    [{_, City}]     = ets:lookup(?GEO_TABLE, {city,     City_id}),
  +    [{_, County}]   = ets:lookup(?GEO_TABLE, {county,   County_id}),
  +    [ISP, Country, Province, City, County].
  +
  +format(Args) when is_list(Args) ->
  +    % [IP, ISP, Country, Province, City, County] = Args
  +    io_lib:format(normal_ip(), Args).
  +
  +normal_ip() ->
  +    "{\"ip\":\"~s\", \"isp\":\"~s\", \"country\":\"~s\"," ++
  +    "\"province\":\"~s\", \"city\":\"~s\", \"county\":\"~s\"}".
  +
  +special_ip(IP) ->
  +    Json = "{\"province\":\"https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml\"," ++
  +           "\"city\":\"\"," ++ 
  +           "\"ip\":\"" ++ IP ++ "\"," ++
  +           "\"isp\":\"\"," ++
  +           "\"county\":\"\"," ++
  +           "\"country\":\"IANA Special-Purpose Address\"}",
  +    {200, Json}.
  +
  +multicast_ip(IP) ->
  +    Json = "{\"province\":\"https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml\"," ++
  +           "\"city\":\"\"," ++ 
  +           "\"ip\":\"" ++ IP ++ "\"," ++
  +           "\"isp\":\"\"," ++
  +           "\"county\":\"\"," ++
  +           "\"country\":\"IANA Multicast Address\"}",
  +    {200, Json}.
  +
  +invalid_ip() ->
  +    {400, "{\"message\":\"Invalid IP\"}"}.
  +
  +time_suffix() ->
  +    {{Y, M, D}, {H, Mi, S}} = erlang:localtime(),
  +    Sfx1 = integer_to_list(Y * 10000 + M * 100 + D),
  +    Sfx2 = integer_to_list(H * 10000 + Mi * 100 + S),
  +    Pad = lists:duplicate(6 - length(Sfx2), $0), 
  +    lists:concat(["-", Sfx1, "-", Pad, Sfx2]).
  +
  +months() ->
  +    ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  +     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].
  +
  +weekdays() ->
  +    ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].
  +
  
positive.code:     {A, B, C, D} = inet:parse_ipv4_address(IP),
      Key = (A bsl 16) + (B bsl 8) + C,
      case ets:lookup(?IPS_TABLE, Key) of
          [{Key, Value}] ->
              L = 255 - D,
              <<Flag:2, ISP:10, Country:8, Province:12, City:16,
                County:16, _:L, X:1, _:D>> = Value,
              case X of
                  1 ->
                      Geo = id_to_name(ISP, Country, Province, City, County),
                      {200, format([IP | Geo])};
                  0 -> ok
              end;
          [] ->
              case taobao_api(IP) of
                  ok -> lookup_ets(IP);
                  error -> invalid_ip()
              end
      end.
  
  id_to_name(ISP_id, Country_id, Province_id, City_id, County_id) ->
      [{_, ISP}]      = ets:lookup(?GEO_TABLE, {isp,      ISP_id}),
      [{_, Country}]  = ets:lookup(?GEO_TABLE, {country,  Country_id}),
      [{_, Province}] = ets:lookup(?GEO_TABLE, {province, Province_id}),
      [{_, City}]     = ets:lookup(?GEO_TABLE, {city,     City_id}),
      [{_, County}]   = ets:lookup(?GEO_TABLE, {county,   County_id}),
      [ISP, Country, Province, City, County].
  
  format(Args) when is_list(Args) ->
      % [IP, ISP, Country, Province, City, County] = Args
      io_lib:format(normal_ip(), Args).
  
  normal_ip() ->
      "{\"ip\":\"~s\", \"isp\":\"~s\", \"country\":\"~s\"," ++
      "\"province\":\"~s\", \"city\":\"~s\", \"county\":\"~s\"}".
  
  special_ip(IP) ->
      Json = "{\"province\":\"https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml\"," ++
             "\"city\":\"\"," ++ 
             "\"ip\":\"" ++ IP ++ "\"," ++
             "\"isp\":\"\"," ++
             "\"county\":\"\"," ++
             "\"country\":\"IANA Special-Purpose Address\"}",
      {200, Json}.
  
  multicast_ip(IP) ->
      Json = "{\"province\":\"https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml\"," ++
             "\"city\":\"\"," ++ 
             "\"ip\":\"" ++ IP ++ "\"," ++
             "\"isp\":\"\"," ++
             "\"county\":\"\"," ++
             "\"country\":\"IANA Multicast Address\"}",
      {200, Json}.
  
  invalid_ip() ->
      {400, "{\"message\":\"Invalid IP\"}"}.
  
  months() ->
      ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].
  
  weekdays() ->
      ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].
  
  http_response(Code, Json) ->
      Status = case Code of
          200 -> "HTTP/1.1 200 OK\r\nServer: Erlang\r\n";
          400 -> "HTTP/1.1 BAD REQUEST\r\nServer: Erlang\r\n"
      end,
      {{Year, Month, Mday}, {Hour, Min, Sec}} = calendar:universal_time(),
      Wday = calendar:day_of_the_week({Year, Month, Mday}),
      Args = [lists:nth(Wday, weekdays()), Mday, lists:nth(Month, months()),
              Year, Hour, Min, Sec],
      Date = io_lib:format("Date: ~s, ~b ~s ~b ~b:~b:~b GMT\r\n", Args),
      lists:concat([Status, Date,
                    "Content-Type: application/json\r\n",
                    "Transfer-Encoding: chunked\r\n",
                    "Connection: close\r\n\r\n",
                    Json]).
  
  
  save(IP, ISP, Country, Province, City, County) ->
      {ok, {A, B, C, D}} = inet:parse_ipv4strict_address(IP),
      Key = (A bsl 16) + (B bsl 8) + C,
      ok.
  
  taobao_api(IP) ->
      URL = "http://ip.taobao.com/service/getIpInfo.php?ip=" ++ IP,
  
negative.code: -define(GEO_TABLE, mt_geo).
  -define(GEO_MANAGER, geoman).
  -define(SNAPSHOT_INTV, 86400000).
  -define(HIGH_WATER_MARK, 1000).
  
  init(careful) ->
      ets:new(?IPS_TABLE, [set, public, named_table]),
      ets:new(?GEO_TABLE, [set, public, named_table]),
      ets:insert(?IPS_TABLE, {count, 0}),
      ets:insert(?GEO_TABLE, {{isp,      count}, 0}),
      ets:insert(?GEO_TABLE, {{country,  count}, 0}),
      ets:insert(?GEO_TABLE, {{province, count}, 0}),
      ets:insert(?GEO_TABLE, {{city,     count}, 0}),
  