Fix how we cache the dbs; helper to decode json; logging tweaks twisty
authorMark Hammond <mhammond@skippinet.com.au>
Wed, 11 Mar 2009 15:01:44 +1100
branchtwisty
changeset 71 3e37c73cb0001578c8390b4fa7168dd6b44d7251
parent 70 557ef1b824258f490a54d08545b90b9d88cf746b
child 72 c384433be638842c633c018a90aa57bf20933751
push id1
push userroot
push dateWed, 08 Apr 2009 01:46:05 +0000
Fix how we cache the dbs; helper to decode json; logging tweaks
server/python/junius/model.py
--- a/server/python/junius/model.py
+++ b/server/python/junius/model.py
@@ -16,52 +16,69 @@ config = get_config()
 
 class _NotSpecified:
     pass
 
 logger = logging.getLogger('model')
 
 DBs = {}
 
+
+def _raw_to_rows(raw):
+    # {'rows': [], 'total_rows': 0} -> the actual rows.
+    ret = raw['rows']
+    assert len(ret)==raw['total_rows'], raw
+    return ret
+
+
 class CouchDB(paisley.CouchDB):
     def postob(self, uri, ob):
         # This seems to not use keep-alives etc where using twisted.web
         # directly doesn't?
         body = json.dumps(ob, allow_nan=False,
                           ensure_ascii=False).encode('utf-8')
         return self.post(uri, body)
 
+    def openView(self, *args, **kwargs):
+        # The base class of this returns the raw json object - eg:
+        # {'rows': [], 'total_rows': 0}
+        base_ret = super(CouchDB, self).openView(*args, **kwargs)
+        return base_ret.addCallback(_raw_to_rows)
+
 
 def get_db(couchname="local", dbname=_NotSpecified):
-    try:
-        return DBs[couchname]
-    except KeyError:
-        pass
     dbinfo = config.couches[couchname]
     if dbname is _NotSpecified:
         dbname = dbinfo['name']
+    key = couchname, dbname
+    try:
+        return DBs[key]
+    except KeyError:
+        pass
     logger.info("Connecting to couchdb at %s", dbinfo)
     db = CouchDB(dbinfo['host'], dbinfo['port'], dbname)
-    DBs[couchname] = db
+    DBs[key] = db
     return db
 
 
 class WildField(schema.Field):
     '''
     Allows us to have dictionaries without schemas.
     '''
     def _to_python(self, value):
         return value
-    
+
     def _to_json(self, value):
         return value
 
+
 class RaindropDocument(schema.Document):
     type = schema.TextField()
 
+
 class Account(RaindropDocument):
   '''
   Accounts correspond to instances of protocols to send/receive messages.
   Although they may correlate with the various identities of the user, they
   are not the same.  Just because you have a facebook account does not mean
   you get an account instance; you would always want the info on the facebook
   account in the identity list for the user, but it doesn't get to be an
   account until we are capable of doing something with it.  (In the specific
@@ -76,16 +93,17 @@ class Account(RaindropDocument):
   password = schema.TextField()
   ssl = schema.BooleanField(default=False)
 
   #: Have we ever successfully connected to this account?
   verified = schema.BooleanField(default=False)
 
   folderStatuses = WildField(default={})
 
+
 class Contact(RaindropDocument):
     name = schema.TextField()
     identities = schema.ListField(schema.DictField(schema.Schema.build(
         kind = schema.TextField(),
         value = schema.TextField()
     )))
 
 
@@ -350,17 +368,17 @@ def fab_db(update_views=False):
     couch_name = 'local'
     db = get_db(couch_name, None)
     dbinfo = config.couches[couch_name]
 
     def _create_failed(failure, *args, **kw):
         failure.trap(twisted.web.error.Error)
         if failure.value.status != '412': # precondition failed.
             failure.raiseException()
-        logger.info("DB already exists!")
+        logger.info("couch database %(name)r already exists", dbinfo)
 
     def _created_ok(d):
         logger.info("created new database")
         schema_src = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                   "../../../schema"))
 
         docs = [d for d in generate_designs_from_filesystem(schema_src)]
         logger.info("Found %d documents in '%s'", len(docs), schema_src)