| Class | Irc::DBTree |
| In: |
lib/rbot/registry/bdb.rb
lib/rbot/registry/tc.rb |
| Parent: | Object |
# File lib/rbot/registry/bdb.rb, line 179
179: def DBTree.cleanup_env()
180: begin
181: debug "DBTree: checking transactions ..."
182: has_active_txn = @@env.txn_stat["st_nactive"] > 0
183: if has_active_txn
184: warning "DBTree: not all transactions completed!"
185: end
186: DBTree.cleanup_logs
187: debug "DBTree: closing environment #{@@env}"
188: path = @@env.home
189: @@env.close
190: @@env = nil
191: if has_active_txn
192: debug "DBTree: keeping file because of incomplete transactions"
193: else
194: debug "DBTree: cleaning up environment in #{path}"
195: BDB::Env.remove("#{path}")
196: end
197: rescue Exception => e
198: error "failed to clean up environment: #{e.pretty_inspect}"
199: end
200: end
# File lib/rbot/registry/bdb.rb, line 144
144: def DBTree.cleanup_logs()
145: begin
146: debug "DBTree: checkpointing ..."
147: @@env.checkpoint
148: rescue Exception => e
149: debug "Failed: #{e.pretty_inspect}"
150: end
151: begin
152: debug "DBTree: flushing log ..."
153: @@env.log_flush
154: logs = @@env.log_archive(BDB::ARCH_ABS)
155: debug "DBTree: deleting archivable logs: #{logs.join(', ')}."
156: logs.each { |log|
157: File.delete(log)
158: }
159: rescue Exception => e
160: debug "Failed: #{e.pretty_inspect}"
161: end
162: end
# File lib/rbot/registry/bdb.rb, line 134
134: def DBTree.create_db(name)
135: debug "DBTree: creating empty db #{name}"
136: return @@env.open_db(BDB::CIBtree, name, nil, BDB::CREATE | BDB::EXCL, 0600)
137: end
# File lib/rbot/registry/tc.rb, line 146
146: def DBTree.create_db(name)
147: debug "DBTree: creating empty db #{name}"
148: db = TokyoCabinet::CIBDB.new
149: res = db.open(name, TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OCREAT | TokyoCabinet::CIBDB::OWRITER)
150: warning "DBTree: creating empty db #{name}: #{db.errmsg(db.ecode) unless res}"
151: return db
152: end
| absfilename: | use key as an actual filename, don‘t prepend the bot‘s config path and don‘t append ".db" |
# File lib/rbot/registry/tc.rb, line 107
107: def initialize(bot, key, absfilename=false)
108: @bot = bot
109: @key = key
110:
111: relfilename = @bot.path key
112: relfilename << '.tdb'
113:
114: if absfilename && File.exist?(key)
115: # db already exists, use it
116: @db = DBTree.open_db(key)
117: elsif absfilename
118: # create empty db
119: @db = DBTree.create_db(key)
120: elsif File.exist? relfilename
121: # db already exists, use it
122: @db = DBTree.open_db relfilename
123: else
124: # create empty db
125: @db = DBTree.create_db relfilename
126: end
127: oldbasename = (absfilename ? key : relfilename).gsub(/\.tdb$/, ".db")
128: if File.exists? oldbasename and defined? BDB
129: # upgrading
130: warning "Upgrading old database #{oldbasename}..."
131: oldb = ::BDB::Btree.open(oldbasename, nil, "r", 0600)
132: oldb.each_key do |k|
133: @db.outlist k
134: @db.putlist k, (oldb.duplicates(k, false))
135: end
136: oldb.close
137: File.rename oldbasename, oldbasename+".bak"
138: end
139: @db
140: end
| absfilename: | use key as an actual filename, don‘t prepend the bot‘s config path and don‘t append ".db" |
# File lib/rbot/registry/bdb.rb, line 98
98: def initialize(bot, key, absfilename=false)
99: @bot = bot
100: @key = key
101: if @@env.nil?
102: begin
103: @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
104: debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
105: rescue => e
106: debug "DBTree: failed to open environment: #{e.pretty_inspect}. Retrying ..."
107: @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
108: end
109: #@@env = BDB::Env.open(@bot.botclass, BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
110: end
111:
112: relfilename = @bot.path key
113: relfilename << '.db'
114:
115: if absfilename && File.exist?(key)
116: # db already exists, use it
117: @db = DBTree.open_db(key)
118: elsif absfilename
119: # create empty db
120: @db = DBTree.create_db(key)
121: elsif File.exist? relfilename
122: # db already exists, use it
123: @db = DBTree.open_db relfilename
124: else
125: # create empty db
126: @db = DBTree.create_db relfilename
127: end
128: end
# File lib/rbot/registry/tc.rb, line 154
154: def DBTree.open_db(name)
155: debug "DBTree: opening existing db #{name}"
156: db = TokyoCabinet::CIBDB.new
157: res = db.open(name, TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OWRITER)
158: warning "DBTree:opening db #{name}: #{db.errmsg(db.ecode) unless res}"
159: return db
160: end
# File lib/rbot/registry/bdb.rb, line 139
139: def DBTree.open_db(name)
140: debug "DBTree: opening existing db #{name}"
141: return @@env.open_db(BDB::CIBtree, name, nil, "r+", 0600)
142: end
# File lib/rbot/registry/bdb.rb, line 164
164: def DBTree.stats()
165: begin
166: debug "General stats:"
167: debug @@env.stat
168: debug "Lock stats:"
169: debug @@env.lock_stat
170: debug "Log stats:"
171: debug @@env.log_stat
172: debug "Txn stats:"
173: debug @@env.txn_stat
174: rescue
175: debug "Couldn't dump stats"
176: end
177: end
# File lib/rbot/registry/bdb.rb, line 130
130: def method_missing(method, *args, &block)
131: return @db.send(method, *args, &block)
132: end