china_ad_ip 0.0.3 → 0.1.0

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: 1c9308d7c16f1fd0d16c4454a9a77c654dc5bdcd
4
- data.tar.gz: 482b0e42b7abe1634dc5fc93ee6a8334fd6884ce
3
+ metadata.gz: 6f93ae8ff8e2025522fb7b4ab01ca88284b6b78b
4
+ data.tar.gz: 39e883203a646deb706077d5035a3aa8e0507ef6
5
5
  SHA512:
6
- metadata.gz: 9a9da6df919e5d79c814d7355a2582ab9ad9554031e8701884da023504ba71cbd8488fc1a767e935bfe9a57eacd6adf488bef2c32d6f33b531845985bdc29ebb
7
- data.tar.gz: 161cdfc0ff74af14cbdac61f4a7169d0fe56ea007a7913882fd328c2905f8aa9bfbfda366439addac526f6b418bd9d546dc6bbaa8f7f5e0faefbb3c92e14a301
6
+ metadata.gz: 537a6704a6deeb63a2ae4fed3fa911991f0caea259553dfaac00948ca9765b8acb4f9c73f56b2ec048bc6cb1eff83b2d2a0c9babd68fcec86ab6054f2ebe4078
7
+ data.tar.gz: 76811430031222e2f64c086b76bf747cdcb03268009634ff23357acda96ac8b0728392e1f50df6ae981240446c0f04a9f858cd88cdc909c19d55595b3d4fd592
data/.gitignore CHANGED
@@ -16,7 +16,6 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  *.bundle
19
- *.so
20
19
  *.o
21
20
  *.a
22
21
  mkmf.log
data/china_ad_ip.gemspec CHANGED
@@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.6"
22
22
  spec.add_development_dependency "rake"
23
- # spec.add_development_dependency "ipaddress"
23
+ spec.add_development_dependency "ffi"
24
24
  end
data/lib/.DS_Store ADDED
Binary file
data/lib/china_ad_ip.rb CHANGED
@@ -1,12 +1,34 @@
1
1
  require "china_ad_ip/version"
2
2
  require 'china_ad_ip/ipv4'
3
3
  require 'csv'
4
+ require 'ffi'
4
5
 
5
6
  module ChinaAdIp
6
7
  NAME = "ChinaAdIp"
7
8
  GEM = "china_ad_ip"
8
9
  AUTHORS = ["suezhen <sz3001@gmail.com>"]
9
10
 
11
+ extend FFI::Library
12
+ ffi_lib "libbinarysearch.so"
13
+ attach_function :binarysearch, [:pointer,:long,:long], :long
14
+
15
+
16
+ module LibC
17
+ extend FFI::Library
18
+ ffi_lib FFI::Library::LIBC
19
+
20
+ # memory allocators
21
+ attach_function :malloc, [:size_t], :pointer
22
+ attach_function :calloc, [:size_t], :pointer
23
+ attach_function :valloc, [:size_t], :pointer
24
+ attach_function :realloc, [:pointer, :size_t], :pointer
25
+ attach_function :free, [:pointer], :void
26
+
27
+ # memory movers
28
+ attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer
29
+ attach_function :bcopy, [:pointer, :pointer, :size_t], :void
30
+
31
+ end # module LibC
10
32
 
11
33
  def self.ntoa(uint)
12
34
  unless(uint.is_a? Numeric and uint <= 0xffffffff and uint >= 0)
@@ -64,9 +86,7 @@ module ChinaAdIp
64
86
  ip_addr = ChinaAdIp::IPv4.new(addr)
65
87
 
66
88
  if $ip_sections && $locations
67
-
68
- index = dichotomy(ip_addr.to_i,$ip_sections.collect{|ip_section| ip_section[0]}.to_a)
69
-
89
+ index = ChinaAdIp.binarysearch(self.arr_pointer,ip_addr.to_i,ip_arr.length)
70
90
  $locations[$ip_sections[index][2]]
71
91
  else
72
92
  raise(::ArgumentError, "not exists csv data")
@@ -75,16 +95,13 @@ module ChinaAdIp
75
95
  end
76
96
 
77
97
 
78
- def self.dichotomy(search,array)
79
- index = 0 , start_index = 0 , end_index = array.length
80
- while(true)
81
- index = start_index + ((end_index - start_index) / 2)
82
- start_index = index if array[index] < search
83
- end_index = index if array[index] > search
84
- break if array[index] == search || ((start_index == index) && (start_index+1 == end_index))
85
- end
86
- index
98
+ def self.ip_arr
99
+ @@ip_arr ||= $ip_sections.collect{|ip_section| ip_section[0]}.to_a
87
100
  end
88
101
 
102
+ def self.arr_pointer
103
+ buffer = LibC.malloc(ip_arr.first.size * ip_arr.size)
104
+ buffer.write_array_of_long ip_arr
105
+ end
89
106
 
90
107
  end
@@ -1,3 +1,3 @@
1
1
  module ChinaAdIp
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: china_ad_ip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - suezhen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-09 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ffi
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: 'locate ip '
42
56
  email:
43
57
  - sz3001@gmail.com
@@ -51,9 +65,11 @@ files:
51
65
  - README.md
52
66
  - Rakefile
53
67
  - china_ad_ip.gemspec
68
+ - lib/.DS_Store
54
69
  - lib/china_ad_ip.rb
55
70
  - lib/china_ad_ip/ipv4.rb
56
71
  - lib/china_ad_ip/version.rb
72
+ - lib/libbinarysearch.so
57
73
  homepage: https://github.com/suzhen/china_ad_ip
58
74
  licenses:
59
75
  - MIT