Skip to content

Commit be0fb0b

Browse files
committed
Reduced file I/O
1 parent 3b1f8ce commit be0fb0b

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2021 IP2Location ( [email protected] )
1+
Copyright (c) 2022 IP2Location ( [email protected] )
22

33
Permission is hereby granted, free of charge, to any person obtaining
44
a copy of this software and associated documentation files (the

ip2proxy_ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Gem::Specification.new do |s|
44
s.name = "ip2proxy_ruby"
5-
s.version = "3.2.1"
5+
s.version = "3.3.0"
66
s.authors = ["ip2location"]
77
s.email = ["[email protected]"]
88

lib/ip2proxy_ruby.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class Ip2proxy
1212
attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr, :databaseyear, :databasemonth, :databaseday, :last_err_msg
1313

14-
VERSION = '3.2.1'
14+
VERSION = '3.3.0'
1515
FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
1616
INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
1717
INVALID_BIN_DATABASE = 'Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.'
@@ -101,8 +101,7 @@ def get_record(ip)
101101
ipnum = realipno - 1
102102
end
103103
end
104-
low = read32(indexpos)
105-
high = read32(indexpos + 4)
104+
low, high = read32x2(indexpos)
106105
return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
107106
else
108107
return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
@@ -419,11 +418,12 @@ def bsearch(low, high, ipnum, base_addr, col_length)
419418
end
420419

421420
def get_from_to(mid, base_addr, col_length)
422-
from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
421+
from_base = (base_addr + mid * (col_length + (v4 ? 0 : 12)))
422+
data_length = col_length + (v4 ? 4 : (12 + 16))
423423
file.seek(from_base)
424-
ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
425-
file.seek(from_base + col_length + (v4 ? 0 : 12))
426-
ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
424+
data_read = file.read(data_length)
425+
ip_from = v4 ? data_read[0..3].unpack('V').first : readipv6(data_read[0..15].unpack('V*'))
426+
ip_to = v4 ? data_read[(data_length - 4)..(data_length - 1)].unpack('V').first : readipv6(data_read[(data_length - 16)..(data_length - 1)].unpack('V*'))
427427
[ip_from, ip_to]
428428
end
429429

@@ -459,17 +459,19 @@ def validateip(ip)
459459
[ipv, ipnum]
460460
end
461461

462-
def read32(indexp)
462+
def read32x2(indexp)
463463
file.seek(indexp - 1)
464-
return file.read(4).unpack('V').first
464+
data_read = file.read(8)
465+
data1 = data_read[0..3].unpack('V').first
466+
data2 = data_read[4..7].unpack('V').first
467+
return [data1, data2]
465468
end
466469

467-
def readipv6(filer)
468-
parts = filer.read(16).unpack('V*')
470+
def readipv6(parts)
469471
return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
470472
end
471473

472-
private :get_record, :bsearch, :get_from_to, :read32, :readipv6
474+
private :get_record, :bsearch, :get_from_to, :read32x2, :readipv6
473475

474476
end
475477

spec/ip2proxy_ruby_database_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
it "work correctly with get_module_version" do
107107
i2p = Ip2proxy.new.open(File.dirname(__FILE__) + "/assets/PX11.SAMPLE.BIN")
108108
record = i2p.get_module_version()
109-
expect(record).to eq '3.2.1'
109+
expect(record).to eq '3.3.0'
110110
end
111111

112112
it "work correctly with get_package_version" do

0 commit comments

Comments
 (0)