changeset 615:a88ead1a1537

updated query to support new dot syntax, and hacked api0 to mirror dbdict.status and dbdict.priority to true columns
author James Bergstra <bergstrj@iro.umontreal.ca>
date Sat, 17 Jan 2009 17:37:55 -0500
parents 7261e7d6368d
children d0f7a6f87adc
files pylearn/dbdict/api0.py pylearn/dbdict/sql.py
diffstat 2 files changed, 39 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/dbdict/api0.py	Fri Jan 16 19:10:50 2009 -0500
+++ b/pylearn/dbdict/api0.py	Sat Jan 17 17:37:55 2009 -0500
@@ -68,7 +68,7 @@
         h_self._link_table = link_table
 
         #TODO: replace this crude algorithm (ticket #17)
-        if ['id', 'create', 'write', 'read'] != [c.name for c in dict_table.c]:
+        if ['id', 'create', 'write', 'read', 'status', 'priority'] != [c.name for c in dict_table.c]:
             raise ValueError(h_self.e_bad_table, dict_table)
         if ['id', 'name', 'ntype', 'fval', 'sval', 'bval'] != [c.name for c in pair_table.c]:
             raise ValueError(h_self.e_bad_table, pair_table)
@@ -243,6 +243,14 @@
             # helper routine by update() and __setitem__
             def _set_in_session(d_self, key, val, session):
                 """Modify an existing key or create a key to hold val"""
+                #FIRST SOME MIRRORING HACKS
+                if key == 'dbdict.status':
+                    ival = int(val)
+                    d_self.status = ival
+                if key == 'dbdict.priority':
+                    fval = float(val)
+                    d_self.priority = fval
+
                 if key in d_self._forbidden_keys:
                     raise KeyError(key)
                 created = None
@@ -278,25 +286,24 @@
             def __getitem__(q_self, item):
                 return q_self._query.__getitem__(item)
 
-            def filter_by(q_self, **kwargs):
+            def filter_eq(q_self, kw, arg):
                 """Return a Query object that restricts to dictionaries containing
                 the given kwargs"""
 
                 #Note: when we add new types to the key columns, add them here
                 q = q_self._query
                 T = h_self._Dict
-                for kw, arg in kwargs.items():
-                    if isinstance(arg, (str,unicode)):
-                        q = q.filter(T._attrs.any(name=kw, sval=arg))
+                if isinstance(arg, (str,unicode)):
+                    q = q.filter(T._attrs.any(name=kw, sval=arg))
+                else:
+                    try:
+                        f = float(arg)
+                    except (TypeError, ValueError):
+                        f = None
+                    if f is None:
+                        q = q.filter(T._attrs.any(name=kw, bval=repr(arg)))
                     else:
-                        try:
-                            f = float(arg)
-                        except (TypeError, ValueError):
-                            f = None
-                        if f is None:
-                            q = q.filter(T._attrs.any(name=kw, bval=repr(arg)))
-                        else:
-                            q = q.filter(T._attrs.any(name=kw, fval=f))
+                        q = q.filter(T._attrs.any(name=kw, fval=f))
 
                 return h_self._Query(q)
 
@@ -356,7 +363,7 @@
         h_self._Query = _Query
 
     def __iter__(h_self):
-        return h_self.query().__iter__()
+        return h_self.query.__iter__()
 
     def insert_kwargs(h_self, **dct):
         """
@@ -387,13 +394,13 @@
         if dct: rval.update(dct)
         return rval
 
-    def query(h_self, **kwargs): 
+    def __get_query(h_self): 
         """Construct an SqlAlchemy query, which can be subsequently filtered
         using the instance methods of DbQuery"""
 
         return h_self._Query(h_self._session.query(h_self._Dict)\
-                        .options(eagerload('_attrs')))\
-                        .filter_by(**kwargs)
+                        .options(eagerload('_attrs')))
+    query = property(__get_query)
 
     def createView(h_self, view):
 
@@ -465,7 +472,10 @@
             Column('id', Integer, primary_key=True),
             Column('create', DateTime),
             Column('write', DateTime),
-            Column('read', DateTime))
+            Column('read', DateTime),
+            Column('status', Integer),
+            Column('priority', Float(53))
+            )
 
     t_keyval = Table(table_prefix+keyval_suffix, metadata,
             Column('id', Integer, primary_key=True),
--- a/pylearn/dbdict/sql.py	Fri Jan 16 19:10:50 2009 -0500
+++ b/pylearn/dbdict/sql.py	Sat Jan 17 17:37:55 2009 -0500
@@ -12,8 +12,10 @@
 
 
 EXPERIMENT = 'dbdict.experiment'
+#using the dictionary to store these is too slow
 STATUS = 'dbdict.status'
 PRIORITY = 'dbdict.sql.priority'
+
 HOST = 'dbdict.sql.hostname'
 HOST_WORKDIR = 'dbdict.sql.host_workdir'
 PUSH_ERROR = 'dbdict.sql.push_error'
@@ -54,7 +56,7 @@
 def book_dct_postgres_serial(db, retry_max_sleep=10.0, verbose=0):
     """Find a trial in the lisa_db with status START.
 
-    A trial will be returned with dbdict_status=RUNNING.
+    A trial will be returned with status=RUNNING.
 
     Returns None if no such trial exists in DB.
 
@@ -78,8 +80,12 @@
     while (dct is None) and keep_trying:
         #build a query
         q = s.query(db._Dict)
-        q = q.options(eagerload('_attrs')) #hard-coded in api0
-        q = q.filter(db._Dict._attrs.any(name=STATUS, fval=START))
+        #q = q.options(eagerload('_attrs')) #hard-coded in api0
+
+        #N.B.
+        # use dedicated column to retrieve jobs, not the dictionary keyval pair
+        # This should be much faster.
+        q = q.filter(db._Dict.status==START)
 
         #try to reserve a dct
         try:
@@ -190,7 +196,8 @@
     try:
         return postgres_db(username, password, hostname, dbname, table_prefix=tablename)
     except:
-        print 'pw', password
+        print 'Error connecting with password', password
+        raise
 
 
 ###########