ip2cityisp 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45cb7493b2a23fe9c395f54052523cfe5cb83f55
4
- data.tar.gz: 1cdc500256b41de11ff32ded3231c71095d36d51
3
+ metadata.gz: 3344f17152f528b22d350cc53215abee21c00351
4
+ data.tar.gz: 8b23d93db20e84c7c14b582a6321133e4b56564c
5
5
  SHA512:
6
- metadata.gz: e05112f49a911907eae0cb98eeeb227da04bcc180525a38491e5eaefb017036acbedb38ac6424d5a66607c6125d63d8e49390c50aaf6252f3e4b78338d464fe1
7
- data.tar.gz: c74bb311d6d7ce86a062b1c268048768d6781cf11a7864418e349dbcabc0e3953b59009d5bae77719acaa2bd90dd12f949a14b89635d43228d846e54b2e769fd
6
+ metadata.gz: 16ee18179c69f63d8cc393ea9fcf684f89c2b358bc8ab0b6f2c39a4c18bfdda97e41f1f08d3bc24b0240e90eae464c42eb64d550ea0fea2676b3bdb91348d4cd
7
+ data.tar.gz: 77ab73e25f602edaf6c0750ada1d71cf7dfaef67731b95ab8428e1e6114b0a103df1e23d079a1334c228ef9a0958b54b49fd6e917b468ea4dc148964c2e84b10
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'bindata'
4
+ require 'thread'
4
5
  module Ip2cityisp
5
6
  class Header < BinData::Record
6
7
  SIZE = 8
@@ -21,11 +22,14 @@ module Ip2cityisp
21
22
  class Database
22
23
  attr_reader :area, :isp
23
24
  def initialize(file = 'ipinfo.data')
25
+ p "enable multi threads test...."
26
+ @mutex = Mutex.new
24
27
  if file.respond_to?(:to_str)
25
28
  @file = File.new(file.to_str)
26
29
  else
27
30
  @file = file
28
- @file.seek(0)
31
+ atomic_seek(0)
32
+ #@file.seek(0)
29
33
  end
30
34
  @header = Ip2cityisp::Header.read(@file)
31
35
  $start = Ip2cityisp::Header::SIZE
@@ -33,13 +37,26 @@ module Ip2cityisp
33
37
  $off1 = @header.total_nums*12 + 20
34
38
  $version = @header.index_version
35
39
  #get end off2
36
- @file.seek($high*12+8)
40
+ atomic_seek($high*12+8)
41
+ #@file.seek($high*12+8)
37
42
  index = Ip2cityisp::Index.read(@file)
38
43
  $off2 = $off1 + index.country_offset
39
44
  $create_time= index.isp_offset
40
- @file.seek(0)
45
+ #@file.seek(0)
46
+ atomic_seek(0)
47
+ end
48
+ def atomic_seek( offset)
49
+ if @mutex
50
+ @mutex.synchronize { atomic_seek_unguarded(offset) }
51
+ else
52
+ atomic_seek_unguarded( offset)
53
+ end
54
+ end
55
+ def atomic_seek_unguarded( offset)
56
+ io = @file
57
+ io.seek(offset)
58
+ #io.read(length)
41
59
  end
42
-
43
60
  def version
44
61
  $version + ":CREATE TIME:" + Time.at($create_time).to_date.to_s
45
62
  end
@@ -61,7 +78,8 @@ module Ip2cityisp
61
78
  while(low <= high)
62
79
  middle = (low + high) >> 1
63
80
  #p middle
64
- @file.seek(start + middle * Ip2cityisp::Index::SIZE)
81
+ #@file.seek(start + middle * Ip2cityisp::Index::SIZE)
82
+ atomic_seek(start + middle * Ip2cityisp::Index::SIZE)
65
83
  index = Ip2cityisp::Index.read(@file)
66
84
  ip_start = index.ip_start
67
85
  country_offset = index.country_offset
@@ -75,7 +93,8 @@ module Ip2cityisp
75
93
  if country_offset >= 0
76
94
  country_off = $off1 + country_offset
77
95
  if country_off < @file.size
78
- @file.seek(country_off)
96
+ atomic_seek(country_off)
97
+ #@file.seek(country_off)
79
98
  vlen = @file.read(1).unpack('C')[0]
80
99
  info["area"] = @file.read(vlen).force_encoding("UTF-8")
81
100
  end
@@ -84,7 +103,8 @@ module Ip2cityisp
84
103
  if isp_offset >= 0
85
104
  isp_off = $off2 + isp_offset
86
105
  if isp_off < @file.size
87
- @file.seek(isp_off)
106
+ atomic_seek(isp_off)
107
+ #@file.seek(isp_off)
88
108
  vlen = @file.read(1).unpack('C')[0]
89
109
  info["isp"] = @file.read(vlen).force_encoding("UTF-8")
90
110
  end
@@ -1,3 +1,3 @@
1
1
  module Ip2cityisp
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/tt.rb CHANGED
@@ -3,7 +3,15 @@ require 'bundler/setup'
3
3
  require 'ip2cityisp'
4
4
  ip = ARGV[0]
5
5
  @db = Ip2cityisp::Database.new('/tmp/ipinfo.data')
6
- r = @db.query('8.8.8.8')
6
+ r = @db.query('123.123.21.12')
7
+ rip = r["area"]+r["isp"]
8
+ p rip
9
+ p @db.version
10
+ r = @db.query('13.123.21.12')
11
+ rip = r["area"]+r["isp"]
12
+ p rip
13
+ p @db.version
14
+ r = @db.query('3.123.21.12')
7
15
  rip = r["area"]+r["isp"]
8
16
  p rip
9
17
  p @db.version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip2cityisp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - geda
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2014-09-09 00:00:00 Z
12
+ date: 2014-10-20 00:00:00 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -60,8 +60,6 @@ files:
60
60
  - lib/ip2cityisp.rb
61
61
  - lib/ip2cityisp/database.rb
62
62
  - lib/ip2cityisp/version.rb
63
- - test.rb
64
- - test2.rb
65
63
  - tt.rb
66
64
  homepage: ""
67
65
  licenses:
data/test.rb DELETED
File without changes
data/test2.rb DELETED
File without changes