Skip to content

Commit c9b2453

Browse files
committed
Reduce I/O
1 parent 056aa33 commit c9b2453

File tree

1 file changed

+154
-103
lines changed

1 file changed

+154
-103
lines changed

ip2proxy.erl

Lines changed: 154 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
last_seen = "-",
1515
is_proxy = 0
1616
}).
17+
-define(IF(Cond), (case (Cond) of true -> (0); false -> (1) end)).
1718

1819
getpackageversion() ->
1920
case ets:info(mymeta) of
@@ -31,7 +32,7 @@ getpackageversion() ->
3132
end.
3233

3334
getmoduleversion() ->
34-
"2.1.1".
35+
"2.2.0".
3536

3637
getdatabaseversion() ->
3738
case ets:info(mymeta) of
@@ -58,12 +59,19 @@ readuint(S, StartPos, Len) ->
5859
binary:decode_unsigned(Data, little)
5960
end.
6061

62+
readuintrow(R, StartPos, Len) ->
63+
Data = binary:part(R, StartPos, Len),
64+
binary:decode_unsigned(Data, little).
65+
6166
readuint8(S, StartPos) ->
6267
readuint(S, StartPos, 1).
6368

6469
readuint32(S, StartPos) ->
6570
readuint(S, StartPos, 4).
6671

72+
readuint32row(R, StartPos) ->
73+
readuintrow(R, StartPos, 4).
74+
6775
readuint128(S, StartPos) ->
6876
readuint(S, StartPos, 16).
6977

@@ -133,28 +141,49 @@ open(InputFile) ->
133141
-1 % negative one means error
134142
end.
135143

136-
readcolcountry(S, Dbtype, Rowoffset, Col) ->
144+
% readcolcountry(S, Dbtype, Rowoffset, Col) ->
145+
% X = "NOT SUPPORTED",
146+
% case lists:nth(Dbtype, Col) of
147+
% 0 ->
148+
% {X, X};
149+
% Colpos ->
150+
% Coloffset = (Colpos - 1) bsl 2,
151+
% X0 = readuint32(S, Rowoffset + Coloffset),
152+
% X1 = readstr(S, X0),
153+
% X2 = readstr(S, X0 + 3),
154+
% {X1, X2}
155+
% end.
156+
157+
readcolcountryrow(S, R, Dbtype, Col) ->
137158
X = "NOT SUPPORTED",
138159
case lists:nth(Dbtype, Col) of
139160
0 ->
140161
{X, X};
141162
Colpos ->
142-
Coloffset = (Colpos - 1) bsl 2,
143-
X0 = readuint32(S, Rowoffset + Coloffset),
163+
Coloffset = (Colpos - 2) bsl 2,
164+
X0 = readuint32row(R, Coloffset),
144165
X1 = readstr(S, X0),
145166
X2 = readstr(S, X0 + 3),
146167
{X1, X2}
147168
end.
148169

149-
readcolstring(S, Dbtype, Rowoffset, Col) ->
170+
% readcolstring(S, Dbtype, Rowoffset, Col) ->
171+
% case lists:nth(Dbtype, Col) of
172+
% 0 ->
173+
% "NOT SUPPORTED";
174+
% Colpos ->
175+
% Coloffset = (Colpos - 1) bsl 2,
176+
% readstr(S, readuint32(S, Rowoffset + Coloffset))
177+
% end.
178+
179+
readcolstringrow(S, R, Dbtype, Col) ->
150180
case lists:nth(Dbtype, Col) of
151181
0 ->
152182
"NOT SUPPORTED";
153183
Colpos ->
154-
Coloffset = (Colpos - 1) bsl 2,
155-
readstr(S, readuint32(S, Rowoffset + Coloffset))
184+
Coloffset = (Colpos - 2) bsl 2,
185+
readstr(S, readuint32row(R, Coloffset))
156186
end.
157-
158187

159188
readrecord(S, Dbtype, Rowoffset, Mode) ->
160189
Country_position = [0, 2, 3, 3, 3, 3, 3, 3, 3],
@@ -181,102 +210,122 @@ readrecord(S, Dbtype, Rowoffset, Mode) ->
181210
As_field = 1024,
182211
Lastseen_field = 2048,
183212

184-
if
185-
(Mode band Proxytype_field /= 0) or (Mode band Isproxy_field /= 0) ->
186-
Proxy_type = readcolstring(S, Dbtype, Rowoffset, Proxytype_position);
187-
true ->
188-
Proxy_type = ""
189-
end,
190-
191-
if
192-
(Mode band Countryshort_field /= 0) or (Mode band Countrylong_field /= 0) or (Mode band Isproxy_field /= 0) ->
193-
{Country_short, Country_long} = readcolcountry(S, Dbtype, Rowoffset, Country_position);
194-
true ->
195-
{Country_short, Country_long} = {"", ""}
196-
end,
197-
198-
if
199-
Mode band Region_field /= 0 ->
200-
Region = readcolstring(S, Dbtype, Rowoffset, Region_position);
201-
true ->
202-
Region = ""
203-
end,
204-
205-
if
206-
Mode band City_field /= 0 ->
207-
City = readcolstring(S, Dbtype, Rowoffset, City_position);
208-
true ->
209-
City = ""
210-
end,
211-
212-
if
213-
Mode band Isp_field /= 0 ->
214-
Isp = readcolstring(S, Dbtype, Rowoffset, Isp_position);
215-
true ->
216-
Isp = ""
217-
end,
218-
219-
if
220-
Mode band Domain_field /= 0 ->
221-
Domain = readcolstring(S, Dbtype, Rowoffset, Domain_position);
222-
true ->
223-
Domain = ""
224-
end,
225-
226-
if
227-
Mode band Usagetype_field /= 0 ->
228-
Usage_type = readcolstring(S, Dbtype, Rowoffset, Usagetype_position);
229-
true ->
230-
Usage_type = ""
231-
end,
213+
Cols = ?IF(lists:nth(Dbtype, Country_position) == 0) + ?IF(lists:nth(Dbtype, Region_position) == 0) + ?IF(lists:nth(Dbtype, City_position) == 0) + ?IF(lists:nth(Dbtype, Isp_position) == 0) + ?IF(lists:nth(Dbtype, Proxytype_position) == 0) + ?IF(lists:nth(Dbtype, Domain_position) == 0) + ?IF(lists:nth(Dbtype, Usagetype_position) == 0) + ?IF(lists:nth(Dbtype, Asn_position) == 0) + ?IF(lists:nth(Dbtype, As_position) == 0) + ?IF(lists:nth(Dbtype, Lastseen_position) == 0),
214+
Rowlength = Cols bsl 2,
232215

233-
if
234-
Mode band Asn_field /= 0 ->
235-
Asn = readcolstring(S, Dbtype, Rowoffset, Asn_position);
236-
true ->
237-
Asn = ""
238-
end,
239-
240-
if
241-
Mode band As_field /= 0 ->
242-
As = readcolstring(S, Dbtype, Rowoffset, As_position);
243-
true ->
244-
As = ""
245-
end,
246-
247-
if
248-
Mode band Lastseen_field /= 0 ->
249-
Last_seen = readcolstring(S, Dbtype, Rowoffset, Lastseen_position);
250-
true ->
251-
Last_seen = ""
252-
end,
253-
254-
if
255-
(Country_short == "-") or (Proxy_type == "-") ->
256-
Is_proxy = 0;
257-
true ->
216+
case file:pread(S, Rowoffset - 1, Rowlength) of
217+
eof ->
218+
#ip2proxyrecord{};
219+
{ok, Data} ->
220+
R = Data,
221+
258222
if
259-
(Proxy_type == "DCH") or (Proxy_type == "SES") ->
260-
Is_proxy = 2;
223+
(Mode band Proxytype_field /= 0) or (Mode band Isproxy_field /= 0) ->
224+
% Proxy_type = readcolstring(S, Dbtype, Rowoffset, Proxytype_position);
225+
Proxy_type = readcolstringrow(S, R, Dbtype, Proxytype_position);
261226
true ->
262-
Is_proxy = 1
263-
end
264-
end,
265-
266-
#ip2proxyrecord{
267-
country_short = Country_short,
268-
country_long = Country_long,
269-
region = Region,
270-
city = City,
271-
isp = Isp,
272-
proxy_type = Proxy_type,
273-
domain = Domain,
274-
usage_type = Usage_type,
275-
asn = Asn,
276-
as = As,
277-
last_seen = Last_seen,
278-
is_proxy = Is_proxy
279-
}.
227+
Proxy_type = ""
228+
end,
229+
230+
if
231+
(Mode band Countryshort_field /= 0) or (Mode band Countrylong_field /= 0) or (Mode band Isproxy_field /= 0) ->
232+
% {Country_short, Country_long} = readcolcountry(S, Dbtype, Rowoffset, Country_position);
233+
{Country_short, Country_long} = readcolcountryrow(S, R, Dbtype, Country_position);
234+
true ->
235+
{Country_short, Country_long} = {"", ""}
236+
end,
237+
238+
if
239+
Mode band Region_field /= 0 ->
240+
% Region = readcolstring(S, Dbtype, Rowoffset, Region_position);
241+
Region = readcolstringrow(S, R, Dbtype, Region_position);
242+
true ->
243+
Region = ""
244+
end,
245+
246+
if
247+
Mode band City_field /= 0 ->
248+
% City = readcolstring(S, Dbtype, Rowoffset, City_position);
249+
City = readcolstringrow(S, R, Dbtype, City_position);
250+
true ->
251+
City = ""
252+
end,
253+
254+
if
255+
Mode band Isp_field /= 0 ->
256+
% Isp = readcolstring(S, Dbtype, Rowoffset, Isp_position);
257+
Isp = readcolstringrow(S, R, Dbtype, Isp_position);
258+
true ->
259+
Isp = ""
260+
end,
261+
262+
if
263+
Mode band Domain_field /= 0 ->
264+
% Domain = readcolstring(S, Dbtype, Rowoffset, Domain_position);
265+
Domain = readcolstringrow(S, R, Dbtype, Domain_position);
266+
true ->
267+
Domain = ""
268+
end,
269+
270+
if
271+
Mode band Usagetype_field /= 0 ->
272+
% Usage_type = readcolstring(S, Dbtype, Rowoffset, Usagetype_position);
273+
Usage_type = readcolstringrow(S, R, Dbtype, Usagetype_position);
274+
true ->
275+
Usage_type = ""
276+
end,
277+
278+
if
279+
Mode band Asn_field /= 0 ->
280+
% Asn = readcolstring(S, Dbtype, Rowoffset, Asn_position);
281+
Asn = readcolstringrow(S, R, Dbtype, Asn_position);
282+
true ->
283+
Asn = ""
284+
end,
285+
286+
if
287+
Mode band As_field /= 0 ->
288+
% As = readcolstring(S, Dbtype, Rowoffset, As_position);
289+
As = readcolstringrow(S, R, Dbtype, As_position);
290+
true ->
291+
As = ""
292+
end,
293+
294+
if
295+
Mode band Lastseen_field /= 0 ->
296+
% Last_seen = readcolstring(S, Dbtype, Rowoffset, Lastseen_position);
297+
Last_seen = readcolstringrow(S, R, Dbtype, Lastseen_position);
298+
true ->
299+
Last_seen = ""
300+
end,
301+
302+
if
303+
(Country_short == "-") or (Proxy_type == "-") ->
304+
Is_proxy = 0;
305+
true ->
306+
if
307+
(Proxy_type == "DCH") or (Proxy_type == "SES") ->
308+
Is_proxy = 2;
309+
true ->
310+
Is_proxy = 1
311+
end
312+
end,
313+
314+
#ip2proxyrecord{
315+
country_short = Country_short,
316+
country_long = Country_long,
317+
region = Region,
318+
city = City,
319+
isp = Isp,
320+
proxy_type = Proxy_type,
321+
domain = Domain,
322+
usage_type = Usage_type,
323+
asn = Asn,
324+
as = As,
325+
last_seen = Last_seen,
326+
is_proxy = Is_proxy
327+
}
328+
end.
280329

281330
searchtree(S, Ipnum, Dbtype, Low, High, BaseAddr, Colsize, Iptype, Mode) ->
282331
X = "INVALID IP ADDRESS",
@@ -300,9 +349,11 @@ searchtree(S, Ipnum, Dbtype, Low, High, BaseAddr, Colsize, Iptype, Mode) ->
300349
Ipnum >= Ipfrom andalso Ipnum < Ipto ->
301350
if
302351
Iptype == ipv4 ->
303-
readrecord(S, Dbtype + 1, Rowoffset, Mode);
352+
% readrecord(S, Dbtype + 1, Rowoffset, Mode);
353+
readrecord(S, Dbtype + 1, Rowoffset + 4, Mode);
304354
true ->
305-
readrecord(S, Dbtype + 1, Rowoffset + 12, Mode)
355+
% readrecord(S, Dbtype + 1, Rowoffset + 12, Mode)
356+
readrecord(S, Dbtype + 1, Rowoffset + 16, Mode)
306357
end;
307358
true ->
308359
if

0 commit comments

Comments
 (0)