]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
TokyoCabinet pseudo-environment
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 26 Jan 2011 17:36:55 +0000 (18:36 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 26 Jan 2011 17:39:44 +0000 (18:39 +0100)
Since TokyoCabinet does not provide a DB environment, trying to reopen
the same db multiple times (something that happens with subregistry
mostly, and possibly after rescans) causes the subsequent opens to fail
due to "threading errors".

Fix by implementing some sort of environmentalish database management
(only keeps track of open databases, returns existing ones if reopening
the same db multiple times, closes all of them on exit).

lib/rbot/registry/tc.rb

index c114a1be696ac94fe8b8b8f393b9e9e10e55a8cd..4477c1158b9ae03be7a7748c2f5ab79d7d142b8c 100644 (file)
@@ -146,19 +146,43 @@ module Irc
       return @db.send(method, *args, &block)
     end
 
+    # Since TokyoCabinet does not have the concept of an environment, we have to do the
+    # database management ourselves. In particular, we have to keep a list of open
+    # registries to be sure we to close all of them on exit
+    @@bot_registries={ }
+    def self.close_bot_registries
+      @@bot_registries.each { |name, reg| reg.close }
+      @@bot_registries.clear
+    end
+
     def DBTree.create_db(name)
       debug "DBTree: creating empty db #{name}"
+      if @@bot_registries.key? name
+        error "DBTree: creating assumingly allocated db #{name}?!"
+        return @@bot_registries[name]
+      end
       db = TokyoCabinet::CIBDB.new
       res = db.open(name, TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OCREAT | TokyoCabinet::CIBDB::OWRITER)
-       warning "DBTree: creating empty db #{name}: #{db.errmsg(db.ecode) unless res}"
+      if res
+        @@bot_registries[name] = db
+      else
+        error "DBTree: creating empty db #{name}: #{db.errmsg(db.ecode)}"
+      end
       return db
     end
 
     def DBTree.open_db(name)
       debug "DBTree: opening existing db #{name}"
+      if @@bot_registries.key? name
+        return @@bot_registries[name]
+      end
       db = TokyoCabinet::CIBDB.new
       res = db.open(name, TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OWRITER)
-       warning "DBTree:opening db #{name}: #{db.errmsg(db.ecode) unless res}"
+      if res
+        @@bot_registries[name] = db
+      else
+        error "DBTree: opening db #{name}: #{db.errmsg(db.ecode)}"
+      end
       return db
     end
 
@@ -171,7 +195,7 @@ module Irc
     end
 
     def DBTree.cleanup_env()
-      # no-op
+      DBTree.close_bot_registries
     end
 
   end