Class: Mongorilla::Collection
- Inherits:
-
Object
- Object
- Mongorilla::Collection
- Defined in:
- lib/mongorilla/collection.rb
Constant Summary collapse
- @@master =
nil
- @@slaves =
nil
- @@slave_index =
0
- @@config =
nil
- @@logger =
nil
Class Method Summary collapse
- .build(path = File.expand_path("../config.yml",__FILE__), logger = nil) ⇒ Object
- .config ⇒ Object
- .load_config(path) ⇒ Object
- .master ⇒ Object
- .output_log(method, contents) ⇒ Object
- .slaves ⇒ Object
Instance Method Summary collapse
- #count(cond = {}, opt = {}) ⇒ Object
- #find(cond = {}, opt = {}) ⇒ Object
- #find_one(cond = {}, opt = {}) ⇒ Object
-
#initialize(collection_name) ⇒ Collection
constructor
A new instance of Collection.
- #insert(data, opt = {}) ⇒ Object
- #r_col ⇒ Object
- #remove(cond = {}, opt = {}) ⇒ Object
- #rescue_connection_failure(max_retries = ) ⇒ Object
- #update(cond, data, opt) ⇒ Object
- #w_col ⇒ Object
Constructor Details
#initialize(collection_name) ⇒ Collection
Returns a new instance of Collection.
61 62 63 |
# File 'lib/mongorilla/collection.rb', line 61 def initialize(collection_name) @name = collection_name end |
Class Method Details
.build(path = File.expand_path("../config.yml",__FILE__), logger = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mongorilla/collection.rb', line 31 def self.build(path=File.("../config.yml",__FILE__),logger=nil) load_config(path) @@config["max_retries"] ||= 10 @@config["meantime"] ||= 0.5 @@logger = logger if @@config["hosts"] @@master = Mongo::ReplSetConnection.new(*@@config["hosts"]).db(@@config["database"]) elsif @@config["slaves"] @@master = Mongo::Connection.new(@@config["host"],@@config["port"]).db(@@config["database"]) @@slaves = @@config["slaves"].map{|s| Mongo::Connection.new(s["host"],s["port"]).db(@@config["database"])} else host = @@config["host"] ? @@config["host"] : "localhost" port = @@config["port"] ? @@config["port"].to_i : 27017 @@master = Mongo::Connection.new(host,port).db(@@config["database"]) end end |
.config ⇒ Object
19 20 21 |
# File 'lib/mongorilla/collection.rb', line 19 def self.config @@config end |
.load_config(path) ⇒ Object
23 24 25 |
# File 'lib/mongorilla/collection.rb', line 23 def self.load_config(path) @@config = YAML.load(File.read(path)) end |
.master ⇒ Object
11 12 13 |
# File 'lib/mongorilla/collection.rb', line 11 def self.master @@master end |
.output_log(method, contents) ⇒ Object
27 28 29 |
# File 'lib/mongorilla/collection.rb', line 27 def self.output_log(method,contents) @@logger.send(method,contents) if @@logger end |
.slaves ⇒ Object
15 16 17 |
# File 'lib/mongorilla/collection.rb', line 15 def self.slaves @@slaves end |
Instance Method Details
#count(cond = {}, opt = {}) ⇒ Object
75 76 77 78 |
# File 'lib/mongorilla/collection.rb', line 75 def count(cond={},opt={}) @@logger.info("count #{@name} cond:#{cond.inspect} opt:#{opt.inspect}") if @@logger find(cond,opt).count end |
#find(cond = {}, opt = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mongorilla/collection.rb', line 80 def find(cond={},opt={}) if opt[:master] || opt["master"] opt.delete(:master) opt.delete("master") if @@config["hosts"] opt[:read] = :primary end rescue_connection_failure do @@logger.info("find(master) #{@name} cond:#{cond.inspect} opt:#{opt.inspect}") if @@logger w_col.find(cond,opt) end else if @@config["hosts"] && @@config["read_secondary"] opt[:read] = :secondary end begin rescue_connection_failure do @@logger.info("find(secondary) #{@name} cond:#{cond.inspect} opt:#{opt.inspect}") if @@logger r_col.find(cond,opt) end rescue @@logger.info("find(master) #{@name} cond:#{cond.inspect} opt:#{opt.inspect}") if @@logger w_col.find(cond,opt) end end end |
#find_one(cond = {}, opt = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/mongorilla/collection.rb', line 65 def find_one(cond={},opt={}) opt[:limit] = 1 if cond.is_a?(String) || cond.is_a?(BSON::ObjectId) cond = BSON::ObjectId(cond) if cond.is_a?(String) cond = {:_id => cond} end ret = find(cond,opt) ret.first end |
#insert(data, opt = {}) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/mongorilla/collection.rb', line 107 def insert(data,opt={}) rescue_connection_failure do @@logger.info("insert #{@name} data:#{data.inspect} opt:#{opt.inspect}") if @@logger w_col.insert(data,opt) end end |
#r_col ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/mongorilla/collection.rb', line 48 def r_col if @@slaves @@slave_index += 1 @@slaves[@@slave_index % @@slaves.length][@name] else @@master[@name] end end |
#remove(cond = {}, opt = {}) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mongorilla/collection.rb', line 121 def remove(cond={},opt={}) if cond.is_a? String cond = {:_id => BSON::ObjectId(cond)} elsif cond.is_a? BSON::ObjectId cond = {:_id => cond} end rescue_connection_failure do @@logger.info("remove #{@name} cond:#{cond.inspect} opt:#{opt.inspect}") if @@logger w_col.remove(cond,opt) end end |
#rescue_connection_failure(max_retries = ) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mongorilla/collection.rb', line 133 def rescue_connection_failure(max_retries=@@config["max_retries"]) retries = 0 begin yield rescue Mongo::ConnectionFailure => ex retries += 1 raise ex if retries > max_retries sleep(@@config["meantime"]) retry end end |
#update(cond, data, opt) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/mongorilla/collection.rb', line 114 def update(cond,data,opt) rescue_connection_failure do @@logger.info("update #{@name} cond:#{cond.inspect} data:#{data.inspect} opt:#{opt.inspect}") if @@logger w_col.update(cond,data,opt) end end |
#w_col ⇒ Object
57 58 59 |
# File 'lib/mongorilla/collection.rb', line 57 def w_col @@master[@name] end |