railhead_handlersocket 0.0.1

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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = RailheadHandlersocket
2
+
3
+ == License
4
+
5
+ Copyright (c) 2011 Bence Nagy (nagybence@tipogral.hu), released under the MIT license.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'railhead_handlersocket'
@@ -0,0 +1,158 @@
1
+ require 'handlersocket'
2
+
3
+
4
+ class HandlerSocket
5
+
6
+ class HandlerSocketError < RuntimeError
7
+ end
8
+
9
+ def open_index_with_error(*args)
10
+ result = open_index(*args)
11
+ if result.nil?
12
+ raise HandlerSocketError, 'nil returned (open index)'
13
+ elsif result.size == 1
14
+ raise HandlerSocketError, 'empty array returned (open index)'
15
+ elsif result[0] != 0
16
+ raise HandlerSocketError, result[1]
17
+ else
18
+ result
19
+ end
20
+ end
21
+
22
+ def execute_single_with_error(*args)
23
+ result = execute_single(*args)
24
+ if result.nil?
25
+ raise HandlerSocketError, 'nil returned (execute single)'
26
+ elsif result.size == 1
27
+ raise HandlerSocketError, 'empty array returned (execute single)'
28
+ elsif result[0] != 0
29
+ raise HandlerSocketError, result[1]
30
+ else
31
+ result
32
+ end
33
+ end
34
+
35
+ def execute_multi_with_error(*args)
36
+ result = execute_multi(*args)
37
+ if result.nil?
38
+ raise HandlerSocketError, 'nil returned (execute multi)'
39
+ elsif not result[0].is_a?(Array)
40
+ raise HandlerSocketError, result[1]
41
+ else
42
+ result
43
+ end
44
+ end
45
+ end
46
+
47
+
48
+ class ActiveRecord::Base
49
+
50
+ def self.use_handlersocket(host = '127.0.0.1', port = 9998)
51
+ @@hs = HandlerSocket.new(host, port)
52
+ @@hs_db_name = connection.instance_variable_get(:@config)[:database]
53
+ @@hs_indexes = {}
54
+
55
+ class << self
56
+ alias_method_chain :find_one, :handlersocket
57
+ alias_method_chain :find_some, :handlersocket
58
+ alias_method_chain :find_every, :handlersocket
59
+ alias_method_chain :find_with_permalink, :handlersocket
60
+ end
61
+ end
62
+
63
+ def self.reconnect_handlersocket
64
+ @@hs.reconnect
65
+ @@hs_indexes = {}
66
+ @hs_reconnected = true
67
+ end
68
+
69
+ class << self
70
+
71
+ def open_index(index_name)
72
+ unless index = @@hs_indexes["#{@@hs_db_name}.#{table_name} #{index_name}"]
73
+ index = @@hs_indexes.size
74
+ @@hs.open_index_with_error(index, @@hs_db_name, table_name, index_name, column_names.join(','))
75
+ @@hs_indexes["#{@@hs_db_name}.#{table_name} #{index_name}"] = index
76
+ end
77
+ index
78
+ end
79
+
80
+ def instantiate_result(result)
81
+ if result.size > 1
82
+ instantiate(Hash[*column_names.zip(result[1..-1]).flatten])
83
+ else
84
+ nil
85
+ end
86
+ end
87
+
88
+ def get_through_handlersocket(ids, index_name = 'PRIMARY')
89
+ unless ids.nil?
90
+ @hs_reconnected = false
91
+ begin
92
+ index = open_index(index_name)
93
+ rescue HandlerSocket::HandlerSocketError => e
94
+ unless @hs_reconnected
95
+ logger.info "HS: reconnecting"
96
+ ActiveRecord::Base.reconnect_handlersocket
97
+ retry
98
+ else
99
+ logger.info "HS: reconnection failed"
100
+ raise e
101
+ end
102
+ end
103
+ begin
104
+ logger.debug "HS: #{@@hs_db_name}.#{table_name} #{index_name} #{ids.inspect}"
105
+ if ids.is_a?(Array)
106
+ @@hs.execute_multi_with_error(ids.map { |id| [index, '=', [id], 1, 0] }).map { |result| instantiate_result(result) }.compact
107
+ else
108
+ instantiate_result(@@hs.execute_single_with_error(index, '=', [ids], 1, 0))
109
+ end
110
+ rescue HandlerSocket::HandlerSocketError => e
111
+ logger.debug "HS: loading failed"
112
+ raise e
113
+ end
114
+ end
115
+ end
116
+
117
+ def find_one_with_handlersocket(ids, options = {})
118
+ get_through_handlersocket(ids) rescue find_one_without_handlersocket(ids, options)
119
+ end
120
+
121
+ def find_some_with_handlersocket(ids, options = {})
122
+ get_through_handlersocket(ids) rescue find_some_without_handlersocket(ids, options)
123
+ end
124
+
125
+ def find_every_with_handlersocket(options = {})
126
+ if (options.has_key?(:select) and options[:select].nil?) or (options[:conditions].is_a?(Hash) and options[:conditions].has_key?(:id))
127
+ if options[:conditions].is_a?(Array)
128
+ ids = options[:conditions].last
129
+ get_through_handlersocket(ids) rescue find_every_without_handlersocket(options)
130
+ elsif options[:conditions].is_a?(Hash)
131
+ ids = options[:conditions][:id]
132
+ ids.is_a?(Array) ? get_through_handlersocket(ids) : [get_through_handlersocket(ids)] rescue find_every_without_handlersocket(options)
133
+ else
134
+ find_every_without_handlersocket(options)
135
+ end
136
+ else
137
+ find_every_without_handlersocket(options)
138
+ end
139
+ end
140
+
141
+ def find_with_permalink_with_handlersocket(ids, options = {})
142
+ if ids.is_a?(String)
143
+ begin
144
+ get_through_handlersocket(ids, "index_#{table_name}_on_permalink")
145
+ rescue
146
+ begin
147
+ get_through_handlersocket(ids)
148
+ rescue
149
+ find_with_permalink_without_handlersocket(ids, options)
150
+ end
151
+ end
152
+ else
153
+ find_with_permalink_without_handlersocket(ids, options)
154
+ end
155
+ end
156
+ end
157
+ end
158
+
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "railhead_handlersocket"
3
+ s.version = "0.0.1"
4
+ s.date = "2011-05-07"
5
+ s.summary = "Hacks for ActiveRecord 2.x to working with HandlerSocket.B"
6
+ s.email = "nagybence@tipogral.hu"
7
+ s.homepage = "http://github.com/nagybence/railhead_cacheify"
8
+ s.description = "Hacks for ActiveRecord 2.x to working with HandlerSocket."
9
+ s.has_rdoc = true
10
+ s.authors = ["Bence Nagy"]
11
+ s.files = ["MIT-LICENSE",
12
+ "README.rdoc",
13
+ "init.rb",
14
+ "railhead_handlersocket.gemspec",
15
+ "lib/railhead_handlersocket.rb"]
16
+ s.rdoc_options = ["--main", "README.rdoc"]
17
+ s.extra_rdoc_files = ["README.rdoc"]
18
+ end
19
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railhead_handlersocket
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Bence Nagy
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-07 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Hacks for ActiveRecord 2.x to working with HandlerSocket.
23
+ email: nagybence@tipogral.hu
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - MIT-LICENSE
32
+ - README.rdoc
33
+ - init.rb
34
+ - railhead_handlersocket.gemspec
35
+ - lib/railhead_handlersocket.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/nagybence/railhead_cacheify
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --main
43
+ - README.rdoc
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.5.2
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Hacks for ActiveRecord 2.x to working with HandlerSocket.B
71
+ test_files: []
72
+