tsukasaoishi-miyazakiresistance 0.0.11 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
@@ -36,6 +36,30 @@ MiyazakiResistance is a library like ActiveRecord to use Tokyo Tyrant.
|
|
36
36
|
Example.find(:all)
|
37
37
|
Example.find(:all, :conditions => ["name = ? age = ?", "tsukasa", 34], :order => "created_at DESC", :offset => 1, :limit => 1)
|
38
38
|
|
39
|
+
|
40
|
+
* You can write in config file
|
41
|
+
|
42
|
+
require 'miyazakiresistance'
|
43
|
+
class Example < MiyazakiResistance::Base
|
44
|
+
server_config Rails.env, "log/miyazakiresistance.yml"
|
45
|
+
set_timeout 60
|
46
|
+
set_column :name, :string
|
47
|
+
set_column :age, :integer
|
48
|
+
set_column :birthday, :date
|
49
|
+
set_column :created_at, :datetime
|
50
|
+
end
|
51
|
+
|
52
|
+
'in log/miyazakiresistance.log'
|
53
|
+
developemnt:
|
54
|
+
set_server:
|
55
|
+
- server: localhost
|
56
|
+
port: 1975
|
57
|
+
role: write
|
58
|
+
- server: localhost
|
59
|
+
port: 1978
|
60
|
+
role: readonly
|
61
|
+
log_file: log/miyazakiresistance.log
|
62
|
+
|
39
63
|
== REQUIREMENTS:
|
40
64
|
|
41
65
|
Tokyo Tyrant
|
@@ -34,12 +34,12 @@ module MiyazakiResistance
|
|
34
34
|
con = write_connection
|
35
35
|
self.id = kaeru_timeout{con.genuid.to_i} if new_record?
|
36
36
|
kaeru_timeout {con.put(self.id, raw_attributes)}
|
37
|
+
self
|
37
38
|
rescue TimeoutError
|
38
39
|
remove_pool(con)
|
39
40
|
retry
|
40
41
|
end
|
41
42
|
|
42
|
-
|
43
43
|
def update_attributes(args)
|
44
44
|
raise NewRecordError if new_record?
|
45
45
|
attributes = args
|
@@ -124,9 +124,7 @@ module MiyazakiResistance
|
|
124
124
|
end
|
125
125
|
|
126
126
|
def create(args)
|
127
|
-
|
128
|
-
inst.save
|
129
|
-
inst
|
127
|
+
self.new(args).save
|
130
128
|
end
|
131
129
|
end
|
132
130
|
|
@@ -10,11 +10,11 @@ module MiyazakiResistance
|
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
def logger
|
13
|
-
@@logger
|
13
|
+
class_variable_get("@@logger") || (logger = Logger.new("miyazakiresistance.log"))
|
14
14
|
end
|
15
15
|
|
16
16
|
def logger=(target)
|
17
|
-
@@logger
|
17
|
+
class_variable_set("@@logger", target)
|
18
18
|
end
|
19
19
|
|
20
20
|
%w|fatal error warn info debug|.each do|level|
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'yaml'
|
1
2
|
module MiyazakiResistance
|
2
3
|
module TokyoConnection
|
3
4
|
def self.included(base)
|
@@ -8,24 +9,45 @@ module MiyazakiResistance
|
|
8
9
|
module ClassMethods
|
9
10
|
attr_accessor :connection_pool
|
10
11
|
attr_accessor :all_columns
|
11
|
-
attr_accessor :all_indexes
|
12
12
|
attr_accessor :timeout_time
|
13
13
|
|
14
|
-
|
14
|
+
DEFAULT = {
|
15
|
+
:timeout => 60,
|
16
|
+
:config => "miyazakiresistance.yml",
|
17
|
+
:port => 1978,
|
18
|
+
:role => :readonly
|
19
|
+
}
|
20
|
+
|
21
|
+
def server_config(env, file = DEFAULT[:config])
|
22
|
+
env = env.to_s
|
23
|
+
conf = YAML.load_file(file)
|
24
|
+
if (config = conf[env]).nil?
|
25
|
+
logger_fatal "specified environment(#{env}) is not found in conig file(#{file})"
|
26
|
+
return
|
27
|
+
end
|
15
28
|
|
16
|
-
|
17
|
-
logger_debug "set_server host : #{host} port : #{port} target : #{target}"
|
29
|
+
class_variable_set("@@logger", Logger.new(config["log_file"])) if config["log_file"]
|
18
30
|
|
31
|
+
config["set_server"].each do |work|
|
32
|
+
set_server work["server"], work["port"], work["role"]
|
33
|
+
end
|
34
|
+
rescue Errno::ENOENT => e
|
35
|
+
logger_fatal "config file is not found : #{file}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_server(host, port = DEFAULT[:port], role = DEFAULT[:role])
|
19
39
|
self.connection_pool ||= {:read => [], :write => nil, :standby => nil}
|
20
40
|
rdb = TokyoTyrant::RDBTBL.new
|
41
|
+
logger_info "set server host : #{host} port : #{port} role : #{role}"
|
42
|
+
|
21
43
|
unless rdb.open(host.to_s, port)
|
22
|
-
logger_fatal "TokyoTyrantConnectError host : #{host} port : #{port}
|
44
|
+
logger_fatal "TokyoTyrantConnectError host : #{host} port : #{port} role : #{role}"
|
23
45
|
raise TokyoTyrantConnectError
|
24
46
|
end
|
25
47
|
|
26
48
|
self.connection_pool[:read] << rdb
|
27
|
-
self.connection_pool[:write] = rdb if
|
28
|
-
self.connection_pool[:standby] = rdb if
|
49
|
+
self.connection_pool[:write] = rdb if role.to_sym == :write
|
50
|
+
self.connection_pool[:standby] = rdb if role.to_sym == :standby
|
29
51
|
end
|
30
52
|
|
31
53
|
def set_timeout(seconds)
|
@@ -33,7 +55,6 @@ module MiyazakiResistance
|
|
33
55
|
end
|
34
56
|
|
35
57
|
def set_column(name, type, index = :no_index)
|
36
|
-
self.all_indexes ||= []
|
37
58
|
self.all_columns ||= {}
|
38
59
|
name = name.to_s
|
39
60
|
self.__send__(:attr_accessor, name)
|
@@ -68,7 +89,7 @@ module MiyazakiResistance
|
|
68
89
|
def kaeru_timeout(&block)
|
69
90
|
ret = nil
|
70
91
|
thread = Thread.new{ret = yield}
|
71
|
-
raise TimeoutError, "tokyo tyrant server response error" unless thread.join(self.timeout_time ||
|
92
|
+
raise TimeoutError, "tokyo tyrant server response error" unless thread.join(self.timeout_time || DEFAULT[:timeout])
|
72
93
|
ret
|
73
94
|
end
|
74
95
|
|
data/lib/miyazakiresistance.rb
CHANGED